blob: 986fa2162c20385ef39d2ddde320e636d2c5c702 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080044import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -080099import com.android.launcher3.PagedView.PageSwitchListener;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700103import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000115import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
Winson Chunga6945242014-01-08 14:04:34 -0800117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118/**
119 * Default launcher application.
120 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100121public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700122 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppalf5d29132014-02-28 13:33:27 -0800123 View.OnTouchListener, PageSwitchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800124 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400128 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700129 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700131 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700132
Dan Sandlerd5024042014-01-09 15:01:33 -0500133 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800160 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chung2672ff92012-05-04 16:22:30 -0700162 // The Intent extra that defines whether to ignore the launch animation
163 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400164 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
167 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700168 // Type: int
169 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
172 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
174 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: boolean
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
180 // Type: long
181 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: int
183 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
184 // Type: int
185 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
186 // Type: parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: int[]
191 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohenb54a5982014-01-08 15:21:04 -0800193
194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700199 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100200 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700201
Adam Cohen39a06042013-07-19 14:30:12 -0700202 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700203 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700204
Winson Chunga6945242014-01-08 14:04:34 -0800205 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
206
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700208 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700209 private State mState = State.WORKSPACE;
210 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800214 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE = 400;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800218 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000220 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
221 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
222
Winson Chunga2413752012-04-03 14:22:34 -0700223 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700224 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
225 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700226 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700227
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800228 private final BroadcastReceiver mCloseSystemDialogsReceiver
229 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800230 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private LayoutInflater mInflater;
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800236 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragLayer mDragLayer;
238 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700239 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800240 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 private AppWidgetManager mAppWidgetManager;
243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Michael Jurkac9d95c52011-08-29 14:03:34 -0700245 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private FolderInfo mFolderInfo;
252
Winson Chung3d503fb2011-07-13 17:25:49 -0700253 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800254 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700255
Michael Jurka838a4ca2011-02-07 13:33:06 -0800256 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700257
Winson Chungc51db6a2011-10-05 11:44:49 -0700258 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700259 private AppsCustomizeTabHost mAppsCustomizeTabHost;
260 private AppsCustomizePagedView mAppsCustomizeContent;
261 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800262 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
266 // scroll issues (because the workspace may not have been measured yet) and extra work.
267 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
268 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
270 private SpannableStringBuilder mDefaultKeySsb = null;
271
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 private boolean mWorkspaceLoading = true;
273
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800274 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private boolean mRestoring;
276 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700277 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
281
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // Keep track of whether the user has left launcher
283 private static boolean sPausedFromUserAction = false;
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedInstanceState;
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800288 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mUserPresent = true;
290 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700291 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700294 private static LocaleConfiguration sLocaleConfiguration = null;
295
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700296 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
302 private final int mAdvanceInterval = 20000;
303 private final int mAdvanceStagger = 250;
304 private long mAutoAdvanceSentTime;
305 private long mAutoAdvanceTimeLeft = -1;
306 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
307 new HashMap<View, AppWidgetProviderInfo>();
308
Winson Chung400438b2011-01-16 17:53:48 -0800309 // Determines how long to wait after a rotation before restoring the screen orientation to
310 // match the sensor state.
311 private final int mRestoreScreenOrientationDelay = 500;
312
Michael Jurka4ef207b2010-11-29 17:05:45 -0800313 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700314 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
315 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
316 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800317
Winson Chungd64a6662013-09-30 11:06:59 -0700318 private Intent mAppMarketIntent = null;
319 private static final boolean DISABLE_MARKET_BUTTON = true;
320
Craig Mautner360310b2012-10-26 15:13:08 -0700321 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700322
Adam Cohen1462de32012-07-24 22:34:36 -0700323 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700324 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700325
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700326 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700327 static Date sDateStamp = new Date();
328 static DateFormat sDateFormat =
329 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
330 static long sRunStart = System.currentTimeMillis();
331 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332
Winson Chung46353de2012-02-16 14:05:10 -0800333 // We only want to get the SharedPreferences once since it does an FS stat each time we get
334 // it from the context.
335 private SharedPreferences mSharedPrefs;
336
Adam Cohene25af792013-06-06 23:08:25 -0700337 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
338
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 // Holds the page that we need to animate to, and the icon views that we need to animate up
340 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private ImageView mFolderIconImageView;
342 private Bitmap mFolderIconBitmap;
343 private Canvas mFolderIconCanvas;
344 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345
Michael Jurkaddd62e92011-02-16 17:49:14 -0800346 private BubbleTextView mWaitingForResume;
347
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 private Runnable mBuildLayersRunnable = new Runnable() {
349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 private static ArrayList<PendingAddArguments> sPendingAddList
357 = new ArrayList<PendingAddArguments>();
358
Michael Jurka7267fa52013-09-26 15:29:57 -0700359 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800360
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 private static class PendingAddArguments {
362 int requestCode;
363 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700364 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700365 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int cellX;
367 int cellY;
368 }
369
Daniel Sandlerff02d492013-08-05 02:12:05 -0400370 private Stats mStats;
371
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800372 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800373 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700374 }
375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 @Override
377 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700378 if (DEBUG_STRICT_MODE) {
379 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
380 .detectDiskReads()
381 .detectDiskWrites()
382 .detectNetwork() // or .detectAll() for all detectable problems
383 .penaltyLog()
384 .build());
385 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
386 .detectLeakedSqlLiteObjects()
387 .detectLeakedClosableObjects()
388 .penaltyLog()
389 .penaltyDeath()
390 .build());
391 }
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400394
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400395 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400396 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700397
398 // Determine the dynamic grid properties
399 Point smallestSize = new Point();
400 Point largestSize = new Point();
401 Point realSize = new Point();
402 Display display = getWindowManager().getDefaultDisplay();
403 display.getCurrentSizeRange(smallestSize, largestSize);
404 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700405 DisplayMetrics dm = new DisplayMetrics();
406 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700407
Winson Chung5f8afe62013-08-12 16:19:28 -0700408 // Lazy-initialize the dynamic grid
409 DeviceProfile grid = app.initDynamicGrid(this,
410 Math.min(smallestSize.x, smallestSize.y),
411 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700412 realSize.x, realSize.y,
413 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414
415 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400416 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700417 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700420 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400421 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800422 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Daniel Sandlerff02d492013-08-05 02:12:05 -0400425 mStats = new Stats(this);
426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200438 android.os.Debug.startMethodTracing(
439 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441
Adam Cohen53805212013-10-01 10:39:23 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100447 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800449 registerContentObservers();
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
456 if (PROFILE_STARTUP) {
457 android.os.Debug.stopMethodTracing();
458 }
459
460 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500464 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800468 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
477 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Adam Cohenf9426d52012-06-04 17:26:21 -0700479 updateGlobalIcons();
480
481 // On large interfaces, we want the screen to auto-rotate based on the current orientation
482 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700483
Winson Chunge43a1e72014-01-15 10:33:02 -0800484 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
485 // on the device, then we always show the first run cling experience (or if there is no
486 // launcher2). Otherwise, we prompt the user upon started for migration
Adam Cohenb54a5982014-01-08 15:21:04 -0800487 showFirstRunActivity();
Winson Chunge43a1e72014-01-15 10:33:02 -0800488 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
489 if (mModel.canMigrateFromOldLauncherDb(this)) {
490 mLauncherClings.showMigrationCling();
491 } else {
492 mLauncherClings.showFirstRunCling();
493 }
Winson Chunga6945242014-01-08 14:04:34 -0800494 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800495 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800496 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700497 }
498
Winson Chung4a2afa32012-07-19 14:53:05 -0700499 protected void onUserLeaveHint() {
500 super.onUserLeaveHint();
501 sPausedFromUserAction = true;
502 }
503
Winson Chung98ca0f72013-07-29 12:58:51 -0700504 /** To be overriden by subclasses to hint to Launcher that we have custom content */
505 protected boolean hasCustomContentToLeft() {
506 return false;
507 }
508
Dave Hawkeya8881582013-09-17 15:55:33 -0600509 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500510 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600511 * {@link #addToCustomContentPage}. This will only be invoked if
512 * {@link #hasCustomContentToLeft()} is {@code true}.
513 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500514 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600515 }
516
517 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800518 * To be overridden by subclasses to indicate that there is an activity to launch
519 * before showing the standard launcher experience.
520 */
521 protected boolean hasFirstRunActivity() {
522 return false;
523 }
524
525 /**
526 * To be overridden by subclasses to launch any first run activity
527 */
528 protected Intent getFirstRunActivity() {
529 return null;
530 }
531
532 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600533 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
534 * ensure the custom content page is added or removed if necessary.
535 */
536 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600537 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600538 // Not bound yet, wait for bindScreens to be called.
539 return;
540 }
541
542 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
543 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500544 mWorkspace.createCustomContentContainer();
545 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600546 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
547 mWorkspace.removeCustomContentPage();
548 }
549 }
550
Adam Cohenf9426d52012-06-04 17:26:21 -0700551 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700552 boolean searchVisible = false;
553 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800554 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700555 int coi = getCurrentOrientationIndexForGlobalIcons();
556 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
557 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700558 if (!DISABLE_MARKET_BUTTON) {
559 updateAppMarketIcon();
560 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700561 searchVisible = updateGlobalSearchIcon();
562 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700563 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700564 if (sGlobalSearchIcon[coi] != null) {
565 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700566 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700567 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700568 if (sVoiceSearchIcon[coi] != null) {
569 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700570 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700571 }
Winson Chungd64a6662013-09-30 11:06:59 -0700572 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700573 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800574 }
Winson Chungadf0c182012-08-23 14:59:07 -0700575 if (mSearchDropTargetBar != null) {
576 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
Romain Guycbb89e42009-06-08 15:52:54 -0700579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 if (sLocaleConfiguration == null) {
582 new AsyncTask<Void, Void, LocaleConfiguration>() {
583 @Override
584 protected LocaleConfiguration doInBackground(Void... unused) {
585 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
586 readConfiguration(Launcher.this, localeConfiguration);
587 return localeConfiguration;
588 }
589
590 @Override
591 protected void onPostExecute(LocaleConfiguration result) {
592 sLocaleConfiguration = result;
593 checkForLocaleChange(); // recursive, but now with a locale configuration
594 }
595 }.execute();
596 return;
597 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final Configuration configuration = getResources().getConfiguration();
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final String locale = configuration.locale.toString();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final int mcc = configuration.mcc;
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mnc = configuration.mnc;
609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 sLocaleConfiguration.locale = locale;
614 sLocaleConfiguration.mcc = mcc;
615 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700616
Joe Onorato0589f0f2010-02-08 13:44:00 -0800617 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618
619 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100620 new AsyncTask<Void, Void, Void>() {
621 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100623 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 }
Michael Jurka43467462013-11-14 12:03:58 +0100625 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700626 }
627 }
628
629 private static class LocaleConfiguration {
630 public String locale;
631 public int mcc = -1;
632 public int mnc = -1;
633 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700634
Romain Guy98d01652009-06-30 16:21:04 -0700635 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
636 DataInputStream in = null;
637 try {
638 in = new DataInputStream(context.openFileInput(PREFERENCES));
639 configuration.locale = in.readUTF();
640 configuration.mcc = in.readInt();
641 configuration.mnc = in.readInt();
642 } catch (FileNotFoundException e) {
643 // Ignore
644 } catch (IOException e) {
645 // Ignore
646 } finally {
647 if (in != null) {
648 try {
649 in.close();
650 } catch (IOException e) {
651 // Ignore
652 }
653 }
654 }
655 }
656
657 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
658 DataOutputStream out = null;
659 try {
660 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
661 out.writeUTF(configuration.locale);
662 out.writeInt(configuration.mcc);
663 out.writeInt(configuration.mnc);
664 out.flush();
665 } catch (FileNotFoundException e) {
666 // Ignore
667 } catch (IOException e) {
668 //noinspection ResultOfMethodCallIgnored
669 context.getFileStreamPath(PREFERENCES).delete();
670 } finally {
671 if (out != null) {
672 try {
673 out.close();
674 } catch (IOException e) {
675 // Ignore
676 }
677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
679 }
680
Anton Hanssona2f665f2013-09-26 12:18:32 +0100681 public Stats getStats() {
682 return mStats;
683 }
684
Bjorn Bringertc459e522013-06-07 19:36:01 +0100685 public LayoutInflater getInflater() {
686 return mInflater;
687 }
688
Winson Chung36a62fe2012-05-06 18:04:42 -0700689 boolean isDraggingEnabled() {
690 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
691 // that is subsequently removed from the workspace in startBinding().
692 return !mModel.isLoadingWorkspace();
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 static int getScreen() {
696 synchronized (sLock) {
697 return sScreen;
698 }
699 }
700
701 static void setScreen(int screen) {
702 synchronized (sLock) {
703 sScreen = screen;
704 }
705 }
706
Winson Chung557d6ed2011-07-08 15:34:52 -0700707 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000708 * Copied from View -- the View version of the method isn't called
709 * anywhere else in our process and only exists for API level 17+,
710 * so it's ok to keep our own version with no API requirement.
711 */
712 public static int generateViewId() {
713 for (;;) {
714 final int result = sNextGeneratedId.get();
715 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
716 int newValue = result + 1;
717 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
718 if (sNextGeneratedId.compareAndSet(result, newValue)) {
719 return result;
720 }
721 }
722 }
723
724 public int getViewIdForItem(ItemInfo info) {
725 // This cast is safe given the > 2B range for int.
726 int itemId = (int) info.id;
727 if (mItemIdToViewId.containsKey(itemId)) {
728 return mItemIdToViewId.get(itemId);
729 }
730 int viewId = generateViewId();
731 mItemIdToViewId.put(itemId, viewId);
732 return viewId;
733 }
734
735 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700736 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
737 * a configuration step, this allows the proper animations to run after other transitions.
738 */
739 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700740 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 switch (args.requestCode) {
742 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 break;
746 case REQUEST_PICK_SHORTCUT:
747 processShortcut(args.intent);
748 break;
749 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700751 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700752 result = true;
753 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800754 case REQUEST_CREATE_APPWIDGET:
755 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700756 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700757 result = true;
758 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 }
Michael Jurka27614d22012-04-02 06:40:22 -0700760 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
761 // if you turned the screen off and then back while in All Apps, Launcher would not
762 // return to the workspace. Clearing mAddInfo.container here fixes this issue
763 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700764 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 protected void onActivityResult(
769 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700770 // Reset the startActivity waiting flag
771 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800772 int pendingAddWidgetId = mPendingAddWidgetId;
773 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700774
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 Runnable exitSpringLoaded = new Runnable() {
776 @Override
777 public void run() {
778 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
779 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
780 }
781 };
782
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
786 if (resultCode == RESULT_CANCELED) {
787 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
789 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
792 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 }
794 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200795 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
796 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
797 mWorkspace.exitOverviewMode(false);
798 }
799 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700800 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
803 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700804
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 // We have special handling for widgets
806 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800807 final int appWidgetId;
808 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
809 : -1;
810 if (widgetId < 0) {
811 appWidgetId = pendingAddWidgetId;
812 } else {
813 appWidgetId = widgetId;
814 }
815
Adam Cohenad4e15c2013-10-17 16:21:35 -0700816 final int result;
817 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800818 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700819 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
820 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700821 result = RESULT_CANCELED;
822 completeTwoStageWidgetDrop(result, appWidgetId);
823 onComplete = new Runnable() {
824 @Override
825 public void run() {
826 exitSpringLoadedDragModeDelayed(false, 0, null);
827 }
828 };
Winson Chung5aaab772012-04-27 15:24:02 -0700829 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700831 final CellLayout dropLayout =
832 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
833 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700834 onComplete = new Runnable() {
835 @Override
836 public void run() {
837 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700838 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700839 }
840 };
Winson Chung5aaab772012-04-27 15:24:02 -0700841 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700842 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
843 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 return;
845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 // The pattern used here is that a user PICKs a specific application,
848 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
851 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700852 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800853 final PendingAddArguments args = new PendingAddArguments();
854 args.requestCode = requestCode;
855 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 args.cellX = mPendingAddInfo.cellX;
859 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800860 if (isWorkspaceLocked()) {
861 sPendingAddList.add(args);
862 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700865 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700867 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 }
873
874 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700875 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 Runnable onCompleteRunnable = null;
878 int animationType = 0;
879
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700880 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 if (resultCode == RESULT_OK) {
882 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
883 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700884 mPendingAddWidgetInfo);
885 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 onCompleteRunnable = new Runnable() {
887 @Override
888 public void run() {
889 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700891 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
892 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 }
894 };
895 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800896 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 if (mDragLayer.getAnimatedView() != null) {
900 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
901 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
902 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700903 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700904 // The animated view may be null in the case of a rotation during widget configuration
905 onCompleteRunnable.run();
906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908
909 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700910 protected void onStop() {
911 super.onStop();
912 FirstFrameAnimatorHelper.setIsVisible(false);
913 }
914
915 @Override
916 protected void onStart() {
917 super.onStart();
918 FirstFrameAnimatorHelper.setIsVisible(true);
919 }
920
921 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200923 long startTime = 0;
924 if (DEBUG_RESUME_TIME) {
925 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400926 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700929
Winson Chung4a2afa32012-07-19 14:53:05 -0700930 // Restore the previous launcher state
931 if (mOnResumeState == State.WORKSPACE) {
932 showWorkspace(false);
933 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800934 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700935 }
936 mOnResumeState = State.NONE;
937
Craig Mautner360310b2012-10-26 15:13:08 -0700938 // Background was set to gradient in onPause(), restore to black if in all apps.
939 setWorkspaceBackground(mState == State.WORKSPACE);
940
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800941 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700942 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700943 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500945 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700947 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700949 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200950 // We might have postponed some bind calls until onResume (see waitUntilResume) --
951 // execute them here
952 long startTimeCallbacks = 0;
953 if (DEBUG_RESUME_TIME) {
954 startTimeCallbacks = System.currentTimeMillis();
955 }
956
957 if (mAppsCustomizeContent != null) {
958 mAppsCustomizeContent.setBulkBind(true);
959 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700960 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
961 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200962 }
963 if (mAppsCustomizeContent != null) {
964 mAppsCustomizeContent.setBulkBind(false);
965 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700966 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200967 if (DEBUG_RESUME_TIME) {
968 Log.d(TAG, "Time spent processing callbacks in onResume: " +
969 (System.currentTimeMillis() - startTimeCallbacks));
970 }
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Michael Jurka54554252013-08-01 12:52:23 +0200972 if (mOnResumeCallbacks.size() > 0) {
973 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
974 mOnResumeCallbacks.get(i).run();
975 }
976 mOnResumeCallbacks.clear();
977 }
Winson Chunge4e50662012-01-23 14:45:13 -0800978
979 // Reset the pressed state of icons that were locked in the press state while activities
980 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800981 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800982 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800983 mWaitingForResume.setStayPressed(false);
984 }
Winson Chunge4e50662012-01-23 14:45:13 -0800985 if (mAppsCustomizeContent != null) {
986 // Resets the previous all apps icon press state
987 mAppsCustomizeContent.resetDrawableState();
988 }
Winson Chung82963d52013-10-09 11:20:57 -0700989
Adam Cohen06dff352012-06-01 17:17:08 -0700990 // It is possible that widgets can receive updates while launcher is not in the foreground.
991 // Consequently, the widgets will be inflated in the orientation of the foreground activity
992 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
993 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700994 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700995
Winson Chung780fe592013-09-26 14:48:44 -0700996 // Process any items that were added while Launcher was away.
997 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
998
Winson Chung5841efa2013-09-30 18:06:44 -0700999 // Update the voice search button proxy
1000 updateVoiceButtonProxyVisible(false);
1001
Adam Cohenf9426d52012-06-04 17:26:21 -07001002 // Again, as with the above scenario, it's possible that one or more of the global icons
1003 // were updated in the wrong orientation.
1004 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001005 if (DEBUG_RESUME_TIME) {
1006 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1007 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001008
1009 if (mWorkspace.getCustomContentCallbacks() != null) {
1010 // If we are resuming and the custom content is the current page, we call onShow().
1011 // It is also poassible that onShow will instead be called slightly after first layout
1012 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1013 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001014 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001015 }
1016 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001017 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001018 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001022 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001023 // Ensure that items added to Launcher are queued until Launcher returns
1024 InstallShortcutReceiver.enableInstallQueue();
1025
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001027 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001028 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001029 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001030
1031 // We call onHide() aggressively. The custom content callbacks should be able to
1032 // debounce excess onHide calls.
1033 if (mWorkspace.getCustomContentCallbacks() != null) {
1034 mWorkspace.getCustomContentCallbacks().onHide();
1035 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
Adam Cohenbffe7452013-07-22 18:21:45 -07001038 QSBScroller mQsbScroller = new QSBScroller() {
1039 int scrollY = 0;
1040
1041 @Override
1042 public void setScrollY(int scroll) {
1043 scrollY = scroll;
1044
1045 if (mWorkspace.isOnOrMovingToCustomContent()) {
1046 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001047 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001048 }
1049 }
1050 };
1051
1052 public void resetQSBScroll() {
1053 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001054 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001055 }
1056
1057 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001058 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1059 // by a onResume or by scrolling otherwise.
1060 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001061
1062 // Custom content is completely hidden
1063 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001064
1065 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1066 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 }
1068
Jorim Jaggid017f882014-01-14 17:08:48 -08001069 protected boolean hasSettings() {
1070 return false;
1071 }
1072
Adam Cohenbffe7452013-07-22 18:21:45 -07001073 public interface QSBScroller {
1074 public void setScrollY(int scrollY);
1075 }
1076
Winson Chung98ca0f72013-07-29 12:58:51 -07001077 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001078 CustomContentCallbacks callbacks, String description) {
1079 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001081 }
1082
Adam Cohenedb40762013-07-18 16:45:45 -07001083 // The custom content needs to offset its content to account for the QSB
1084 public int getTopOffsetForCustomContent() {
1085 return mWorkspace.getPaddingTop();
1086 }
1087
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001088 @Override
1089 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 // Flag the loader to stop early before switching
1091 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001092 if (mAppsCustomizeContent != null) {
1093 mAppsCustomizeContent.surrender();
1094 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001095 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001096 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001098 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001099 @Override
1100 public void onWindowFocusChanged(boolean hasFocus) {
1101 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001102 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001103 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 private boolean acceptFilter() {
1106 final InputMethodManager inputManager = (InputMethodManager)
1107 getSystemService(Context.INPUT_METHOD_SERVICE);
1108 return !inputManager.isFullscreenMode();
1109 }
1110
1111 @Override
1112 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001113 final int uniChar = event.getUnicodeChar();
1114 final boolean handled = super.onKeyDown(keyCode, event);
1115 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1116 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1118 keyCode, event);
1119 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001120 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001121 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001122 // showSearchDialog()
1123 // If there are multiple keystrokes before the search dialog takes focus,
1124 // onSearchRequested() will be called for every keystroke,
1125 // but it is idempotent, so it's fine.
1126 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128 }
1129
Joe Onorato8a9625e2010-01-28 15:55:35 -08001130 // Eat the long press event so the keyboard doesn't come up.
1131 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1132 return true;
1133 }
1134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 return handled;
1136 }
1137
Karl Rosaen138a0412009-04-23 19:00:21 -07001138 private String getTypedText() {
1139 return mDefaultKeySsb.toString();
1140 }
1141
1142 private void clearTypedText() {
1143 mDefaultKeySsb.clear();
1144 mDefaultKeySsb.clearSpans();
1145 Selection.setSelection(mDefaultKeySsb, 0);
1146 }
1147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001149 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1150 * State
1151 */
1152 private static State intToState(int stateOrdinal) {
1153 State state = State.WORKSPACE;
1154 final State[] stateValues = State.values();
1155 for (int i = 0; i < stateValues.length; i++) {
1156 if (stateValues[i].ordinal() == stateOrdinal) {
1157 state = stateValues[i];
1158 break;
1159 }
1160 }
1161 return state;
1162 }
1163
1164 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 * Restores the previous state, if it exists.
1166 *
1167 * @param savedState The previous state.
1168 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001169 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 private void restoreState(Bundle savedState) {
1171 if (savedState == null) {
1172 return;
1173 }
1174
Michael Jurka883f55b2010-10-21 15:47:14 -07001175 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001176 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001177 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001178 }
1179
Adam Cohen21cd0022013-10-09 18:57:02 -07001180 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1181 PagedView.INVALID_RESTORE_PAGE);
1182 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001183 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185
Winson Chung3d503fb2011-07-13 17:25:49 -07001186 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001187 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1190 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001191 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001192 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1193 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001194 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1195 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1196 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001197 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001198 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 mRestoring = true;
1200 }
1201
1202 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1203 if (renameFolder) {
1204 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001205 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 mRestoring = true;
1207 }
Winson Chunga12a2502010-12-20 14:41:35 -08001208
Winson Chung785d2eb2011-04-14 16:08:02 -07001209 // Restore the AppsCustomize tab
1210 if (mAppsCustomizeTabHost != null) {
1211 String curTab = savedState.getString("apps_customize_currentTab");
1212 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001213 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001215 mAppsCustomizeContent.loadAssociatedPages(
1216 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001217 }
1218
Winson Chung5afbf7b2011-07-25 11:53:08 -07001219 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1220 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001221 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001222 mItemIdToViewId = (HashMap<Integer, Integer>)
1223 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
1226 /**
1227 * Finds all the views we need and configure them properly.
1228 */
1229 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001230 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001231
Craig Mautner360310b2012-10-26 15:13:08 -07001232 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001233 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1234 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001235 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001236 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001237
John Spurlock77e1f472013-09-11 10:09:51 -04001238 mLauncherView.setSystemUiVisibility(
1239 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001240 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
Winson Chung4c98d922011-05-31 16:50:48 -07001242 // Setup the drag layer
1243 mDragLayer.setup(this, dragController);
1244
Winson Chung3d503fb2011-07-13 17:25:49 -07001245 // Setup the hotseat
1246 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1247 if (mHotseat != null) {
1248 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001249 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001250 }
1251
Jorim Jaggid017f882014-01-14 17:08:48 -08001252 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001253 View widgetButton = findViewById(R.id.widget_button);
1254 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001255 @Override
1256 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001257 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001258 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001259 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 }
1261 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001262 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1263
1264 View wallpaperButton = findViewById(R.id.wallpaper_button);
1265 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001266 @Override
1267 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001268 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001269 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001270 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001271 }
1272 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001273 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1274
1275 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001276 if (hasSettings()) {
1277 settingsButton.setOnClickListener(new OnClickListener() {
1278 @Override
1279 public void onClick(View arg0) {
1280 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001281 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001282 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001283 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001284 });
1285 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1286 } else {
1287 settingsButton.setVisibility(View.GONE);
1288 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1289 lp.gravity = Gravity.END | Gravity.TOP;
1290 widgetButton.requestLayout();
1291 }
1292
Adam Cohendbdff6b2013-09-18 19:09:15 -07001293 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001294
Winson Chung4c98d922011-05-31 16:50:48 -07001295 // Setup the workspace
1296 mWorkspace.setHapticFeedbackEnabled(false);
1297 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001298 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001299 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001300
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001302 mSearchDropTargetBar = (SearchDropTargetBar)
1303 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001304
Winson Chungf0ea4d32011-06-06 14:27:16 -07001305 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001306 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001307 mAppsCustomizeContent = (AppsCustomizePagedView)
1308 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1309 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001312 dragController.setDragScoller(mWorkspace);
1313 dragController.setScrollView(mDragLayer);
1314 dragController.setMoveTarget(mWorkspace);
1315 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001316 if (mSearchDropTargetBar != null) {
1317 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001318 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001319
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001320 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001321 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001322 mWeightWatcher = new WeightWatcher(this);
1323 mWeightWatcher.setAlpha(0.5f);
1324 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001325 new FrameLayout.LayoutParams(
1326 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001327 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001328 Gravity.BOTTOM)
1329 );
Adam Cohen39a06042013-07-19 14:30:12 -07001330
1331 boolean show = shouldShowWeightWatcher();
1332 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
1336 /**
1337 * Creates a view representing a shortcut.
1338 *
1339 * @param info The data structure describing the shortcut.
1340 *
1341 * @return A View inflated from R.layout.application.
1342 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001343 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001345 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
1348 /**
1349 * Creates a view representing a shortcut inflated from the specified resource.
1350 *
1351 * @param layoutResId The id of the XML layout used to create the shortcut.
1352 * @param parent The group the shortcut belongs to.
1353 * @param info The data structure describing the shortcut.
1354 *
1355 * @return A View inflated from layoutResId.
1356 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001357 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001358 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1359 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 return favorite;
1362 }
1363
1364 /**
1365 * Add an application shortcut to the workspace.
1366 *
1367 * @param data The intent describing the application.
1368 * @param cellInfo The position on screen where to create the shortcut.
1369 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001370 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001372 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001373
Adam Cohenfbba09b2011-07-18 21:43:05 -07001374 // First we check if we already know the exact location where we want to add this item.
1375 if (cellX >= 0 && cellY >= 0) {
1376 cellXY[0] = cellX;
1377 cellXY[1] = cellY;
1378 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001379 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001380 return;
1381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001383 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001384
Romain Guy73b979d2009-06-09 12:57:21 -07001385 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001386 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001388 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001389 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001390 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001391 } else {
1392 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394 }
Romain Guycbb89e42009-06-08 15:52:54 -07001395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 /**
1397 * Add a shortcut to the workspace.
1398 *
1399 * @param data The intent describing the shortcut.
1400 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001403 int cellY) {
1404 int[] cellXY = mTmpAddItemCellCoordinates;
1405 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001406 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001407
Michael Jurkad3ef3062010-11-23 16:23:58 -08001408 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001409
Adam Cohen558baaf2011-08-15 15:22:57 -07001410 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001411 if (info == null) {
1412 return;
1413 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001414 final View view = createShortcut(info);
1415
Adam Cohenfbba09b2011-07-18 21:43:05 -07001416 // First we check if we already know the exact location where we want to add this item.
1417 if (cellX >= 0 && cellY >= 0) {
1418 cellXY[0] = cellX;
1419 cellXY[1] = cellY;
1420 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001421
1422 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001423 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001424 true, null,null)) {
1425 return;
1426 }
1427 DragObject dragObject = new DragObject();
1428 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001429 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1430 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001431 return;
1432 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001433 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001434 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001436 foundCellSpan = (result != null);
1437 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001438 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001439 }
1440
1441 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001442 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001443 return;
1444 }
1445
Adam Cohendcd297f2013-06-18 13:13:40 -07001446 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
1448 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001449 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001450 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452 }
1453
Adam Cohen2f093b62012-04-30 18:59:53 -07001454 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1455 int minHeight) {
1456 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001457 // We want to account for the extra amount of padding that we are adding to the widget
1458 // to ensure that it gets the full amount of space that it has requested
1459 int requiredWidth = minWidth + padding.left + padding.right;
1460 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001461 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001462 }
1463
Adam Cohen2f093b62012-04-30 18:59:53 -07001464 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1465 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1469 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001470 }
1471
Adam Cohen2f093b62012-04-30 18:59:53 -07001472 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1473 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001474 }
1475
Adam Cohen2f093b62012-04-30 18:59:53 -07001476 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1477 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001478 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001482 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001484 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001485 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001487 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001488 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1489 if (appWidgetInfo == null) {
1490 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1491 }
Romain Guycbb89e42009-06-08 15:52:54 -07001492
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001493 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001495
Adam Cohen2f093b62012-04-30 18:59:53 -07001496 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1497 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001500 // We have saved the position to which the widget was dragged-- this really only matters
1501 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 int[] cellXY = mTmpAddItemCellCoordinates;
1503 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001504 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1507 cellXY[0] = mPendingAddInfo.cellX;
1508 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001509 spanXY[0] = mPendingAddInfo.spanX;
1510 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 foundCellSpan = true;
1512 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001513 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001515 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1516 spanXY[1], cellXY, finalSpan);
1517 spanXY[0] = finalSpan[0];
1518 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001519 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001521 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001522 }
1523
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001525 if (appWidgetId != -1) {
1526 // Deleting an app widget ID is a void call but writes to disk before returning
1527 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001528 new AsyncTask<Void, Void, Void>() {
1529 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001530 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001531 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001532 }
Michael Jurka43467462013-11-14 12:03:58 +01001533 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001534 }
Winson Chung93eef082012-03-23 15:59:27 -07001535 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001536 return;
1537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001539 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001540 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1541 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 launcherInfo.spanX = spanXY[0];
1543 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001544 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1545 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 if (hostView == null) {
1552 // Perform actual inflation because we're live
1553 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1554 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1555 } else {
1556 // The AppWidgetHostView has already been inflated and instantiated
1557 launcherInfo.hostView = hostView;
1558 }
Romain Guycbb89e42009-06-08 15:52:54 -07001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001561 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001562 launcherInfo.notifyWidgetSizeChanged(this);
1563
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001566
1567 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001569 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Adam Cohended9f8d2010-11-03 13:25:16 -07001572 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1573 @Override
1574 public void onReceive(Context context, Intent intent) {
1575 final String action = intent.getAction();
1576 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1577 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001578 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001580
Winson Chungc100e8e2011-08-09 16:02:43 -07001581 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001582 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001583 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001584 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001585 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001586 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1587 mUserPresent = true;
1588 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1590 mModel.resetLoadedState(false, true);
1591 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1592 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1593 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1594 mModel.resetLoadedState(false, true);
1595 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1596 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1597 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 }
1599 }
1600 };
1601
1602 @Override
1603 public void onAttachedToWindow() {
1604 super.onAttachedToWindow();
1605
1606 // Listen for broadcasts related to user-presence
1607 final IntentFilter filter = new IntentFilter();
1608 filter.addAction(Intent.ACTION_SCREEN_OFF);
1609 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001610 if (ENABLE_DEBUG_INTENTS) {
1611 filter.addAction(DebugIntents.DELETE_DATABASE);
1612 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001615 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001616 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 mVisible = true;
1618 }
1619
1620 @Override
1621 public void onDetachedFromWindow() {
1622 super.onDetachedFromWindow();
1623 mVisible = false;
1624
Adam Cohend113e0c2010-11-11 10:48:05 -08001625 if (mAttached) {
1626 unregisterReceiver(mReceiver);
1627 mAttached = false;
1628 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 updateRunning();
1630 }
1631
1632 public void onWindowVisibilityChanged(int visibility) {
1633 mVisible = visibility == View.VISIBLE;
1634 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001635 // The following code used to be in onResume, but it turns out onResume is called when
1636 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1637 // is a more appropriate event to handle
1638 if (mVisible) {
1639 mAppsCustomizeTabHost.onWindowVisible();
1640 if (!mWorkspaceLoading) {
1641 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001642 // We want to let Launcher draw itself at least once before we force it to build
1643 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001644 // apps is nice and speedy.
1645 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001646 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001647 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001648 if (mStarted) return;
1649 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001650 // We delay the layer building a bit in order to give
1651 // other message processing a time to run. In particular
1652 // this avoids a delay in hiding the IME if it was
1653 // currently shown, because doing that may involve
1654 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001655 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001656 final ViewTreeObserver.OnDrawListener listener = this;
1657 mWorkspace.post(new Runnable() {
1658 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001659 if (mWorkspace != null &&
1660 mWorkspace.getViewTreeObserver() != null) {
1661 mWorkspace.getViewTreeObserver().
1662 removeOnDrawListener(listener);
1663 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001664 }
1665 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001666 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001667 }
1668 });
1669 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001670 // When Launcher comes back to foreground, a different Activity might be responsible for
1671 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001672 if (!DISABLE_MARKET_BUTTON) {
1673 updateAppMarketIcon();
1674 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 clearTypedText();
1676 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001677 }
1678
1679 private void sendAdvanceMessage(long delay) {
1680 mHandler.removeMessages(ADVANCE_MSG);
1681 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1682 mHandler.sendMessageDelayed(msg, delay);
1683 mAutoAdvanceSentTime = System.currentTimeMillis();
1684 }
1685
1686 private void updateRunning() {
1687 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1688 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1689 mAutoAdvanceRunning = autoAdvanceRunning;
1690 if (autoAdvanceRunning) {
1691 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1692 sendAdvanceMessage(delay);
1693 } else {
1694 if (!mWidgetsToAdvance.isEmpty()) {
1695 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1696 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1697 }
1698 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001699 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 }
1701 }
1702 }
1703
1704 private final Handler mHandler = new Handler() {
1705 @Override
1706 public void handleMessage(Message msg) {
1707 if (msg.what == ADVANCE_MSG) {
1708 int i = 0;
1709 for (View key: mWidgetsToAdvance.keySet()) {
1710 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1711 final int delay = mAdvanceStagger * i;
1712 if (v instanceof Advanceable) {
1713 postDelayed(new Runnable() {
1714 public void run() {
1715 ((Advanceable) v).advance();
1716 }
1717 }, delay);
1718 }
1719 i++;
1720 }
1721 sendAdvanceMessage(mAdvanceInterval);
1722 }
1723 }
1724 };
1725
1726 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001727 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1729 if (v instanceof Advanceable) {
1730 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001731 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 updateRunning();
1733 }
1734 }
1735
1736 void removeWidgetToAutoAdvance(View hostView) {
1737 if (mWidgetsToAdvance.containsKey(hostView)) {
1738 mWidgetsToAdvance.remove(hostView);
1739 updateRunning();
1740 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 }
1742
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 launcherInfo.hostView = null;
1746 }
1747
Winson Chung93eef082012-03-23 15:59:27 -07001748 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1749 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1750 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
1752
Winson Chunga6945242014-01-08 14:04:34 -08001753 public DragLayer getDragLayer() {
1754 return mDragLayer;
1755 }
1756
1757 public Workspace getWorkspace() {
1758 return mWorkspace;
1759 }
1760
1761 public Hotseat getHotseat() {
1762 return mHotseat;
1763 }
1764
Jorim Jaggid017f882014-01-14 17:08:48 -08001765 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001766 return mOverviewPanel;
1767 }
1768
1769 public SearchDropTargetBar getSearchBar() {
1770 return mSearchDropTargetBar;
1771 }
1772
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001773 public LauncherAppWidgetHost getAppWidgetHost() {
1774 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Romain Guycbb89e42009-06-08 15:52:54 -07001776
Winson Chunga9abd0e2010-10-27 17:18:37 -07001777 public LauncherModel getModel() {
1778 return mModel;
1779 }
1780
Winson Chunga6945242014-01-08 14:04:34 -08001781 public LauncherClings getLauncherClings() {
1782 return mLauncherClings;
1783 }
1784
1785 protected SharedPreferences getSharedPrefs() {
1786 return mSharedPrefs;
1787 }
1788
Bjorn Bringertc459e522013-06-07 19:36:01 +01001789 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001790 getWindow().closeAllPanels();
1791
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001792 // Whatever we were doing is hereby canceled.
1793 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001794 }
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 @Override
1797 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001798 long startTime = 0;
1799 if (DEBUG_RESUME_TIME) {
1800 startTime = System.currentTimeMillis();
1801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 super.onNewIntent(intent);
1803
1804 // Close the menu
1805 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001806 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001807 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001808
Adam Cohened307df2013-10-02 09:37:31 -07001809 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1810 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1811 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001812
Adam Cohen6fecd412013-10-02 17:41:50 -07001813 if (mWorkspace == null) {
1814 // Can be cases where mWorkspace is null, this prevents a NPE
1815 return;
1816 }
1817 Folder openFolder = mWorkspace.getOpenFolder();
1818 // In all these cases, only animate if we're already on home
1819 mWorkspace.exitWidgetResizeMode();
1820 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001821 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001822 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001823 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001824
Adam Cohen6fecd412013-10-02 17:41:50 -07001825 closeFolder();
1826 exitSpringLoadedDragMode();
1827
1828 // If we are already on home, then just animate back to the workspace,
1829 // otherwise, just wait until onResume to set the state back to Workspace
1830 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001831 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 } else {
1833 mOnResumeState = State.WORKSPACE;
1834 }
1835
1836 final View v = getWindow().peekDecorView();
1837 if (v != null && v.getWindowToken() != null) {
1838 InputMethodManager imm = (InputMethodManager)getSystemService(
1839 INPUT_METHOD_SERVICE);
1840 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1841 }
1842
1843 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001844 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001845 mAppsCustomizeTabHost.reset();
1846 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001847
1848 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
Adam Cohened307df2013-10-02 09:37:31 -07001850
Michael Jurka447bf842013-05-15 14:52:15 +02001851 if (DEBUG_RESUME_TIME) {
1852 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855
Adam Coppa120b8e2013-11-12 16:26:04 +00001856 /**
1857 * Override point for subclasses to prevent movement to the default screen when the home
1858 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1859 */
1860 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1861 return true;
1862 }
1863
1864 /**
1865 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1866 */
1867 protected void onHomeIntent() {
1868 // Do nothing
1869 }
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001872 public void onRestoreInstanceState(Bundle state) {
1873 super.onRestoreInstanceState(state);
1874 for (int page: mSynchronouslyBoundPages) {
1875 mWorkspace.restoreInstanceStateForChild(page);
1876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
1878
1879 @Override
1880 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001881 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001882 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1883 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001884 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001885 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886
Michael Jurka883f55b2010-10-21 15:47:14 -07001887 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001888 // We close any open folder since it will not be re-opened, and we need to make sure
1889 // this state is reflected.
1890 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891
Adam Cohendcd297f2013-06-18 13:13:40 -07001892 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001893 mWaitingForResult) {
1894 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001895 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1897 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001898 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1899 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1900 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001901 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903
1904 if (mFolderInfo != null && mWaitingForResult) {
1905 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1906 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1907 }
Michael Jurka946ad472010-07-09 18:05:18 -07001908
Winson Chung785d2eb2011-04-14 16:08:02 -07001909 // Save the current AppsCustomize tab
1910 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001911 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1912 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001913 if (currentTabTag != null) {
1914 outState.putString("apps_customize_currentTab", currentTabTag);
1915 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001916 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1917 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001918 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001919 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
1921
1922 @Override
1923 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001925
Winson Chunge7a03942011-08-05 15:05:12 -07001926 // Remove all pending runnables
1927 mHandler.removeMessages(ADVANCE_MSG);
1928 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001929 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001930
Winson Chungcd2b0142011-06-08 16:02:26 -07001931 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001932 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001933 mModel.stopLoader();
1934 app.setLauncher(null);
1935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001937 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001939 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001941 mAppWidgetHost = null;
1942
1943 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
1945 TextKeyListener.getInstance().release();
1946
Winson Chung81b52252012-08-27 15:34:29 -07001947 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1948 // to prevent leaking Launcher activities on orientation change.
1949 if (mModel != null) {
1950 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1951 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001952
1953 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001954 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001955
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001956 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001957 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001958 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001959 mWorkspace = null;
1960 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001961
Michael Jurka2ecf9952012-06-18 12:52:28 -07001962 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
Adam Cohena9cf38f2011-05-02 15:36:58 -07001965 public DragController getDragController() {
1966 return mDragController;
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
1970 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001971 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 super.startActivityForResult(intent, requestCode);
1973 }
1974
Winson Chung70d72102011-08-12 11:24:35 -07001975 /**
1976 * Indicates that we want global search for this activity by setting the globalSearch
1977 * argument for {@link #startSearch} to true.
1978 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001980 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001982
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001983 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001984
Karl Rosaen138a0412009-04-23 19:00:21 -07001985 if (initialQuery == null) {
1986 // Use any text typed in the launcher as the initial query
1987 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 if (appSearchData == null) {
1990 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001991 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Winson Chungadf0c182012-08-23 14:59:07 -07001993 Rect sourceBounds = new Rect();
1994 if (mSearchDropTargetBar != null) {
1995 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1996 }
Romain Guycbb89e42009-06-08 15:52:54 -07001997
Bjorn Bringertc459e522013-06-07 19:36:01 +01001998 startSearch(initialQuery, selectInitialQuery,
1999 appSearchData, sourceBounds);
2000 }
2001
2002 public void startSearch(String initialQuery,
2003 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002004 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002005 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002006 }
2007
2008 /**
2009 * Starts the global search activity. This code is a copied from SearchManager
2010 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002011 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002012 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002013 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002014 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2015 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2016 if (globalSearchActivity == null) {
2017 Log.w(TAG, "No global search activity found.");
2018 return;
2019 }
2020 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2021 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2022 intent.setComponent(globalSearchActivity);
2023 // Make sure that we have a Bundle to put source in
2024 if (appSearchData == null) {
2025 appSearchData = new Bundle();
2026 } else {
2027 appSearchData = new Bundle(appSearchData);
2028 }
2029 // Set source to package name of app that starts global search, if not set already.
2030 if (!appSearchData.containsKey("source")) {
2031 appSearchData.putString("source", getPackageName());
2032 }
2033 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2034 if (!TextUtils.isEmpty(initialQuery)) {
2035 intent.putExtra(SearchManager.QUERY, initialQuery);
2036 }
2037 if (selectInitialQuery) {
2038 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2039 }
2040 intent.setSourceBounds(sourceBounds);
2041 try {
2042 startActivity(intent);
2043 } catch (ActivityNotFoundException ex) {
2044 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Yura4f93ec62014-02-04 14:15:21 +00002048 public boolean isOnCustomContent() {
2049 return mWorkspace.isOnOrMovingToCustomContent();
2050 }
2051
Winson Chung70d72102011-08-12 11:24:35 -07002052 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002053 public boolean onPrepareOptionsMenu(Menu menu) {
2054 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002055 if (!isOnCustomContent()) {
2056 // 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 }
Winson Chunge0298742014-01-17 12:03:00 -08002066 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002067 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002070 @Override
2071 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002072 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002073 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002074 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002075 }
2076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 public boolean isWorkspaceLocked() {
2078 return mWorkspaceLoading || mWaitingForResult;
2079 }
2080
Adam Cohen517a7f52014-03-01 12:12:59 -08002081 public boolean isWorkspaceLoading() {
2082 return mWorkspaceLoading;
2083 }
2084
Michael Jurka0280c3b2010-09-17 15:00:07 -07002085 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002087 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002088 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2089 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002090 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002091 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002092 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002093
Adam Cohenad4e15c2013-10-17 16:21:35 -07002094 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2095 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2096 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2097 }
2098
2099 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2100 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2101 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002102 if (appWidgetInfo.configure != null) {
2103 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002104 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002105
Bjorn Bringert7984c942009-12-09 15:38:25 +00002106 // Launch over to configure widget, if needed
2107 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002108 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002109 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002110 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002112 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002113 Runnable onComplete = new Runnable() {
2114 @Override
2115 public void run() {
2116 // Exit spring loaded mode if necessary after adding the widget
2117 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2118 null);
2119 }
2120 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002121 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002122 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002123 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125 }
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002127 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002128 // Close any folders that may be open.
2129 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002130 mWorkspace.moveToCustomContentScreen(animate);
2131 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002132 /**
2133 * Process a shortcut drop.
2134 *
2135 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002136 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002137 * @param cell The cell it should be added to, optional
2138 * @param position The location on the screen where it was dropped, optional
2139 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002140 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002141 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002142 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002145 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002146
2147 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002148 mPendingAddInfo.cellX = cell[0];
2149 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002150 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002151
2152 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2153 createShortcutIntent.setComponent(componentName);
2154 processShortcut(createShortcutIntent);
2155 }
2156
Adam Cohenfbba09b2011-07-18 21:43:05 -07002157 /**
2158 * Process a widget drop.
2159 *
2160 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002161 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002162 * @param cell The cell it should be added to, optional
2163 * @param position The location on the screen where it was dropped, optional
2164 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002166 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002167 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002169 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002171 mPendingAddInfo.minSpanX = info.minSpanX;
2172 mPendingAddInfo.minSpanY = info.minSpanY;
2173
Adam Cohenfbba09b2011-07-18 21:43:05 -07002174 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002175 mPendingAddInfo.cellX = cell[0];
2176 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002177 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002178 if (span != null) {
2179 mPendingAddInfo.spanX = span[0];
2180 mPendingAddInfo.spanY = span[1];
2181 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002182
Adam Cohened66b2b2012-01-23 17:28:51 -08002183 AppWidgetHostView hostView = info.boundWidget;
2184 int appWidgetId;
2185 if (hostView != null) {
2186 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002187 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002188 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002189 // In this case, we either need to start an activity to get permission to bind
2190 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002191 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002192 Bundle options = info.bindOptions;
2193
2194 boolean success = false;
2195 if (options != null) {
2196 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2197 info.componentName, options);
2198 } else {
2199 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2200 info.componentName);
2201 }
2202 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002203 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002204 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002205 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002206 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2207 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2208 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002209 // TODO: we need to make sure that this accounts for the options bundle.
2210 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002211 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2212 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002213 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002214 }
2215
Joe Onoratodeb98af2010-02-19 14:59:39 -08002216 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002217 // Handle case where user selected "Applications"
2218 String applicationName = getResources().getString(R.string.group_applications);
2219 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002221 if (applicationName != null && applicationName.equals(shortcutName)) {
2222 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2223 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002224
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002225 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2226 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002227 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002228 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002229 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002230 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233
Winson Chung24ab2f12010-09-16 14:10:47 -07002234 void processWallpaper(Intent intent) {
2235 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2236 }
2237
Adam Cohendcd297f2013-06-18 13:13:40 -07002238 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002240 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 folderInfo.title = getText(R.string.folder_name);
2242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002246 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247
2248 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 FolderIcon newFolder =
2250 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002253 // Force measure the new folder icon
2254 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2255 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002256 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 }
Romain Guycbb89e42009-06-08 15:52:54 -07002258
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002260 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 }
2262
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002263 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002264 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002265 }
2266
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002267 /**
2268 * Registers various content observers. The current implementation registers
2269 * only a favorites observer to keep track of the favorites applications.
2270 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002271 private void registerContentObservers() {
2272 ContentResolver resolver = getContentResolver();
2273 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2274 true, mWidgetObserver);
2275 }
2276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 @Override
2278 public boolean dispatchKeyEvent(KeyEvent event) {
2279 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2280 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002282 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002283 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002284 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002285 dumpState();
2286 return true;
2287 }
2288 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002289 }
2290 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2291 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002292 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 return true;
2294 }
2295 }
2296
2297 return super.dispatchKeyEvent(event);
2298 }
2299
Joe Onorato88ec0992009-11-19 13:16:06 -08002300 @Override
2301 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002302 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002303 if (mAppsCustomizeContent.getContentType() ==
2304 AppsCustomizePagedView.ContentType.Applications) {
2305 showWorkspace(true);
2306 } else {
2307 showOverviewMode(true);
2308 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002309 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002310 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002311 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002312 Folder openFolder = mWorkspace.getOpenFolder();
2313 if (openFolder.isEditingName()) {
2314 openFolder.dismissEditingName();
2315 } else {
2316 closeFolder();
2317 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002318 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002319 mWorkspace.exitWidgetResizeMode();
2320
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002321 // Back button is a no-op here, but give at least some feedback for the button press
2322 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002323 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002324 }
2325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002327 * Re-listen when widgets are reset.
2328 */
2329 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002330 if (mAppWidgetHost != null) {
2331 mAppWidgetHost.startListening();
2332 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002333 }
2334
2335 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 * Launches the intent referred by the clicked shortcut.
2337 *
2338 * @param v The view representing the clicked shortcut.
2339 */
2340 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002341 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2342 // view has detached (it's possible for this to happen if the view is removed mid touch).
2343 if (v.getWindowToken() == null) {
2344 return;
2345 }
2346
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002347 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002348 return;
2349 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002350
Adam Cohen1697b792013-09-17 19:08:21 -07002351 if (v instanceof Workspace) {
2352 if (mWorkspace.isInOverviewMode()) {
2353 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002354 }
2355 return;
2356 }
2357
2358 if (v instanceof CellLayout) {
2359 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002360 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002361 }
2362 }
2363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002365 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002366 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002368 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002369 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002370 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002371 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002372 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 }
2374 }
2375
Michael Jurka0e260592010-06-30 17:07:39 -07002376 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002377 return false;
2378 }
2379
Michael Jurkaaf442092010-06-10 17:01:57 -07002380 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002381 * Event handler for the search button
2382 *
2383 * @param v The view that was clicked.
2384 */
2385 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002386 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2387
Amith Yamasania135ba82011-08-09 17:42:01 -07002388 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002389 }
2390
2391 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002392 * Event handler for the voice button
2393 *
2394 * @param v The view that was clicked.
2395 */
2396 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002397 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002398
Bjorn Bringertc459e522013-06-07 19:36:01 +01002399 startVoice();
2400 }
2401
2402 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002403 try {
2404 final SearchManager searchManager =
2405 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2406 ComponentName activityName = searchManager.getGlobalSearchActivity();
2407 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002408 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002409 if (activityName != null) {
2410 intent.setPackage(activityName.getPackageName());
2411 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002412 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002413 } catch (ActivityNotFoundException e) {
2414 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002415 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002416 startActivitySafely(null, intent, "onClickVoiceButton");
2417 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002418 }
2419
2420 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002421 * Event handler for the "grid" button that appears on the home screen, which
2422 * enters all apps mode.
2423 *
2424 * @param v The view that was clicked.
2425 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002426 protected void onClickAllAppsButton(View v) {
2427 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2428 if (isAllAppsVisible()) {
2429 showWorkspace(true);
2430 } else {
2431 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2432 }
2433 }
2434
2435 /**
2436 * Event handler for an app shortcut click.
2437 *
2438 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2439 */
2440 protected void onClickAppShortcut(View v) {
2441 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2442 Object tag = v.getTag();
2443 if (!(tag instanceof ShortcutInfo)) {
2444 throw new IllegalArgumentException("Input must be a Shortcut");
2445 }
2446
2447 // Open shortcut
2448 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2449 final Intent intent = shortcut.intent;
2450
2451 // Check for special shortcuts
2452 if (intent.getComponent() != null) {
2453 final String shortcutClass = intent.getComponent().getClassName();
2454
2455 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2456 MemoryDumpActivity.startDump(this);
2457 return;
2458 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2459 toggleShowWeightWatcher();
2460 return;
2461 }
2462 }
2463
2464 // Start activities
2465 int[] pos = new int[2];
2466 v.getLocationOnScreen(pos);
2467 intent.setSourceBounds(new Rect(pos[0], pos[1],
2468 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2469
2470 boolean success = startActivitySafely(v, intent, tag);
2471
2472 mStats.recordLaunch(intent, shortcut);
2473
2474 if (success && v instanceof BubbleTextView) {
2475 mWaitingForResume = (BubbleTextView) v;
2476 mWaitingForResume.setStayPressed(true);
2477 }
2478 }
2479
2480 /**
2481 * Event handler for a folder icon click.
2482 *
2483 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2484 */
2485 protected void onClickFolderIcon(View v) {
2486 if (LOGD) Log.d(TAG, "onClickFolder");
2487 if (!(v instanceof FolderIcon)){
2488 throw new IllegalArgumentException("Input must be a FolderIcon");
2489 }
2490
2491 FolderIcon folderIcon = (FolderIcon) v;
2492 final FolderInfo info = folderIcon.getFolderInfo();
2493 Folder openFolder = mWorkspace.getFolderForTag(info);
2494
2495 // If the folder info reports that the associated folder is open, then verify that
2496 // it is actually opened. There have been a few instances where this gets out of sync.
2497 if (info.opened && openFolder == null) {
2498 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2499 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2500 info.opened = false;
2501 }
2502
2503 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2504 // Close any open folder
2505 closeFolder();
2506 // Open the requested folder
2507 openFolder(folderIcon);
2508 } else {
2509 // Find the open folder...
2510 int folderScreen;
2511 if (openFolder != null) {
2512 folderScreen = mWorkspace.getPageForView(openFolder);
2513 // .. and close it
2514 closeFolder(openFolder);
2515 if (folderScreen != mWorkspace.getCurrentPage()) {
2516 // Close any folder open on the current screen
2517 closeFolder();
2518 // Pull the folder onto this screen
2519 openFolder(folderIcon);
2520 }
2521 }
2522 }
Michael Jurka5130e402011-10-13 04:55:35 -07002523 }
2524
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002525 /**
2526 * Event handler for the (Add) Widgets button that appears after a long press
2527 * on the home screen.
2528 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002529 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002531 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2532 }
2533
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002534 /**
2535 * Event handler for the wallpaper picker button that appears after a long press
2536 * on the home screen.
2537 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002538 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002539 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2540 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2541 pickWallpaper.setComponent(getWallpaperPickerComponent());
2542 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2543 }
2544
2545 /**
2546 * Event handler for a click on the settings button that appears after a long press
2547 * on the home screen.
2548 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002549 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2551 }
2552
Michael Jurka5130e402011-10-13 04:55:35 -07002553 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002554 // Provide the same haptic feedback that the system offers for virtual keys.
2555 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002556 }
2557
Adam Cohen61f560d2013-09-30 15:58:20 -07002558 public void performHapticFeedbackOnTouchDown(View v) {
2559 // Provide the same haptic feedback that the system offers for virtual keys.
2560 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2561 }
2562
2563 public View.OnTouchListener getHapticFeedbackTouchListener() {
2564 if (mHapticFeedbackTouchListener == null) {
2565 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2566 @Override
2567 public boolean onTouch(View v, MotionEvent event) {
2568 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2569 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2570 }
2571 return false;
2572 }
2573 };
2574 }
2575 return mHapticFeedbackTouchListener;
2576 }
2577
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002578 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002579 if (!DISABLE_MARKET_BUTTON) {
2580 if (mAppMarketIntent != null) {
2581 startActivitySafely(v, mAppMarketIntent, "app market");
2582 } else {
2583 Log.e(TAG, "Invalid app market intent.");
2584 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002585 }
2586 }
2587
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002588 /**
2589 * Called when the user stops interacting with the launcher.
2590 * This implies that the user is now on the homescreen and is not doing housekeeping.
2591 */
2592 protected void onInteractionEnd() {}
2593
2594 /**
2595 * Called when the user starts interacting with the launcher.
2596 * The possible interactions are:
2597 * - open all apps
2598 * - reorder an app shortcut, or a widget
2599 * - open the overview mode.
2600 * This is a good time to stop doing things that only make sense
2601 * when the user is on the homescreen and not doing housekeeping.
2602 */
2603 protected void onInteractionBegin() {}
2604
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002605 void startApplicationDetailsActivity(ComponentName componentName) {
2606 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002607 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2608 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002609 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2610 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002611 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002612 }
2613
Michael Jurka1e2f4652013-07-08 18:03:46 -07002614 // returns true if the activity was started
2615 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002616 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002617 // System applications cannot be installed. For now, show a toast explaining that.
2618 // We may give them the option of disabling apps this way.
2619 int messageId = R.string.uninstall_system_app_text;
2620 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002621 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002622 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002623 String packageName = componentName.getPackageName();
2624 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002625 Intent intent = new Intent(
2626 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002627 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2628 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002629 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002630 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002631 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002632 }
2633
Michael Jurka86a720e2012-05-09 11:23:23 -07002634 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002636
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002638 // Only launch using the new animation if the shortcut has not opted out (this is a
2639 // private contract between launcher and may be ignored in the future).
2640 boolean useLaunchAnimation = (v != null) &&
2641 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2642 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002643 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2644 v.getMeasuredWidth(), v.getMeasuredHeight());
2645
2646 startActivity(intent, opts.toBundle());
2647 } else {
2648 startActivity(intent);
2649 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002650 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 } catch (SecurityException e) {
2652 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002653 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002655 "or use the exported attribute for this activity. "
2656 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002658 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002660
Michael Jurka86a720e2012-05-09 11:23:23 -07002661 boolean startActivitySafely(View v, Intent intent, Object tag) {
2662 boolean success = false;
2663 try {
2664 success = startActivity(v, intent, tag);
2665 } catch (ActivityNotFoundException e) {
2666 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2667 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2668 }
2669 return success;
2670 }
2671
Adam Cohen268c4752012-06-06 17:47:33 -07002672 /**
2673 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2674 * in the DragLayer in the exact absolute location of the original FolderIcon.
2675 */
2676 private void copyFolderIconToImage(FolderIcon fi) {
2677 final int width = fi.getMeasuredWidth();
2678 final int height = fi.getMeasuredHeight();
2679
2680 // Lazy load ImageView, Bitmap and Canvas
2681 if (mFolderIconImageView == null) {
2682 mFolderIconImageView = new ImageView(this);
2683 }
2684 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2685 mFolderIconBitmap.getHeight() != height) {
2686 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2687 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2688 }
2689
2690 DragLayer.LayoutParams lp;
2691 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2692 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2693 } else {
2694 lp = new DragLayer.LayoutParams(width, height);
2695 }
2696
Adam Cohen307fe232012-08-16 17:55:58 -07002697 // The layout from which the folder is being opened may be scaled, adjust the starting
2698 // view size by this scale factor.
2699 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002700 lp.customPosition = true;
2701 lp.x = mRectForFolderAnimation.left;
2702 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002703 lp.width = (int) (scale * width);
2704 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002705
2706 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2707 fi.draw(mFolderIconCanvas);
2708 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002709 if (fi.getFolder() != null) {
2710 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2711 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002712 }
Adam Cohen268c4752012-06-06 17:47:33 -07002713 // Just in case this image view is still in the drag layer from a previous animation,
2714 // we remove it and re-add it.
2715 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2716 mDragLayer.removeView(mFolderIconImageView);
2717 }
2718 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002719 if (fi.getFolder() != null) {
2720 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002721 }
Adam Cohen268c4752012-06-06 17:47:33 -07002722 }
2723
Adam Cohen2801caf2011-05-13 20:57:39 -07002724 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002725 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002726 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2727 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2728 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2729
Adam Cohenc51934b2011-07-26 21:07:43 -07002730 FolderInfo info = (FolderInfo) fi.getTag();
2731 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2732 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002733 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2734 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002735 }
2736
Adam Cohen268c4752012-06-06 17:47:33 -07002737 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2738 copyFolderIconToImage(fi);
2739 fi.setVisibility(View.INVISIBLE);
2740
Michael Jurka2ecf9952012-06-18 12:52:28 -07002741 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002742 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002743 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2744 oa.start();
2745 }
2746
Adam Cohen268c4752012-06-06 17:47:33 -07002747 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002748 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002749 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2750 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2751 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2752
Adam Cohen268c4752012-06-06 17:47:33 -07002753 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002754
Adam Cohen268c4752012-06-06 17:47:33 -07002755 // We remove and re-draw the FolderIcon in-case it has changed
2756 mDragLayer.removeView(mFolderIconImageView);
2757 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002758 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002759 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002760 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002761 oa.addListener(new AnimatorListenerAdapter() {
2762 @Override
2763 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002764 if (cl != null) {
2765 cl.clearFolderLeaveBehind();
2766 // Remove the ImageView copy of the FolderIcon and make the original visible.
2767 mDragLayer.removeView(mFolderIconImageView);
2768 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002769 }
2770 }
2771 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002772 oa.start();
2773 }
2774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002776 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777 * is animated relative to the specified View. If the View is null, no animation
2778 * is played.
2779 *
2780 * @param folderInfo The FolderInfo describing the folder to open.
2781 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002782 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002783 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002784 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785
Adam Cohena9cf38f2011-05-02 15:36:58 -07002786 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002787
Adam Cohen4554ee12011-08-03 16:13:21 -07002788 // Just verify that the folder hasn't already been added to the DragLayer.
2789 // There was a one-off crash where the folder had a parent already.
2790 if (folder.getParent() == null) {
2791 mDragLayer.addView(folder);
2792 mDragController.addDropTarget((DropTarget) folder);
2793 } else {
2794 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2795 folder.getParent() + ").");
2796 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002797 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002798 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002799
2800 // Notify the accessibility manager that this folder "window" has appeared and occluded
2801 // the workspace items
2802 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2803 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002804 }
2805
2806 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002807 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002808 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002809 if (folder.isEditingName()) {
2810 folder.dismissEditingName();
2811 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002812 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002813
2814 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002815 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002816 }
2817 }
2818
2819 void closeFolder(Folder folder) {
2820 folder.getInfo().opened = false;
2821
2822 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2823 if (parent != null) {
2824 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2825 shrinkAndFadeInFolderIcon(fi);
2826 }
2827 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002828
2829 // Notify the accessibility manager that this folder "window" has disappeard and no
2830 // longer occludeds the workspace items
2831 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 }
2833
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002835 if (!isDraggingEnabled()) return false;
2836 if (isWorkspaceLocked()) return false;
2837 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838
Adam Cohen1697b792013-09-17 19:08:21 -07002839 if (v instanceof Workspace) {
2840 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002841 if (mWorkspace.enterOverviewMode()) {
2842 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2843 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2844 return true;
2845 } else {
2846 return false;
2847 }
Adam Cohen1697b792013-09-17 19:08:21 -07002848 }
Adam Cohen1697b792013-09-17 19:08:21 -07002849 }
2850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002852 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 }
2854
Michael Jurka0280c3b2010-09-17 15:00:07 -07002855 resetAddInfo();
2856 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002858 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 return true;
2860 }
2861
Winson Chung3d503fb2011-07-13 17:25:49 -07002862 // The hotseat touch handling does not go through Workspace, and we always allow long press
2863 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002864 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002865 final boolean inHotseat = isHotseatLayout(v);
2866 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002867 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002868 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002869 // User long pressed on empty space
2870 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2871 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2872 // Disabling reordering until we sort out some issues.
2873 if (mWorkspace.isInOverviewMode()) {
2874 mWorkspace.startReordering(v);
2875 } else {
2876 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002879 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2880 mHotseat.getOrderInHotseat(
2881 longClickCellInfo.cellX,
2882 longClickCellInfo.cellY));
2883 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002885 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
2887 }
2888 }
2889 return true;
2890 }
2891
Winson Chung3d503fb2011-07-13 17:25:49 -07002892 boolean isHotseatLayout(View layout) {
2893 return mHotseat != null && layout != null &&
2894 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2895 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002896
Winson Chung3d503fb2011-07-13 17:25:49 -07002897 /**
2898 * Returns the CellLayout of the specified container at the specified screen.
2899 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002900 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002901 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2902 if (mHotseat != null) {
2903 return mHotseat.getLayout();
2904 } else {
2905 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002906 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002907 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002908 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002909 }
2910 }
2911
Daniel Sandler843e8602010-06-07 14:59:01 -04002912 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002913 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002914 }
2915
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002916 /**
2917 * Helper method for the cameraZoomIn/cameraZoomOut animations
2918 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002919 * @param scaleFactor The scale factor used for the zoom
2920 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002921 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002922 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002923 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002924 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002925
Craig Mautner360310b2012-10-26 15:13:08 -07002926 private void setWorkspaceBackground(boolean workspace) {
2927 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002928 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002929 }
2930
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002931 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002932 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2933 int curflags = getWindow().getAttributes().flags
2934 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2935 if (wpflags != curflags) {
2936 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2937 }
Craig Mautner360310b2012-10-26 15:13:08 -07002938 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002939 }
2940
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002941 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2942 if (v instanceof LauncherTransitionable) {
2943 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2944 }
2945 }
2946
Michael Jurkabed61d22012-02-14 22:51:29 -08002947 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2948 if (v instanceof LauncherTransitionable) {
2949 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2950 }
Winson Chung70442722012-02-10 15:43:22 -08002951
2952 // Update the workspace transition step as well
2953 dispatchOnLauncherTransitionStep(v, 0f);
2954 }
2955
2956 private void dispatchOnLauncherTransitionStep(View v, float t) {
2957 if (v instanceof LauncherTransitionable) {
2958 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2959 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002960 }
2961
2962 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2963 if (v instanceof LauncherTransitionable) {
2964 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2965 }
Winson Chung70442722012-02-10 15:43:22 -08002966
2967 // Update the workspace transition step as well
2968 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002969 }
2970
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002971 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002972 * Things to test when changing the following seven functions.
2973 * - Home from workspace
2974 * - from center screen
2975 * - from other screens
2976 * - Home from all apps
2977 * - from center screen
2978 * - from other screens
2979 * - Back from all apps
2980 * - from center screen
2981 * - from other screens
2982 * - Launch app from workspace and quit
2983 * - with back
2984 * - with home
2985 * - Launch app from all apps and quit
2986 * - with back
2987 * - with home
2988 * - Go to a screen that's not the default, then all
2989 * apps, and launch and app, and go back
2990 * - with back
2991 * -with home
2992 * - On workspace, long press power and go back
2993 * - with back
2994 * - with home
2995 * - On all apps, long press power and go back
2996 * - with back
2997 * - with home
2998 * - On workspace, power off
2999 * - On all apps, power off
3000 * - Launch an app and turn off the screen while in that app
3001 * - Go back with home key
3002 * - Go back with back key TODO: make this not go to workspace
3003 * - From all apps
3004 * - From workspace
3005 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3006 * - From all apps
3007 * - From the center workspace
3008 * - From another workspace
3009 */
3010
3011 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003012 * Zoom the camera out from the workspace to reveal 'toView'.
3013 * Assumes that the view to show is anchored at either the very top or very bottom
3014 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003015 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003016 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003017 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3018 showAppsCustomizeHelper(animated, springLoaded, contentType);
3019 }
3020 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3021 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003022 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003023 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003024 mStateAnimation.cancel();
3025 mStateAnimation = null;
3026 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003027 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003028
Winson Chungf0ea4d32011-06-06 14:27:16 -07003029 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3030 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3031 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003032 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003033 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003034 final int startDelay =
3035 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003036
Michael Jurkab3e22d92011-10-31 15:58:33 -07003037 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003038
Michael Jurkad74c9842011-07-10 12:44:21 -07003039 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003040 Animator workspaceAnim =
3041 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003042 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003043 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3044 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003045 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3046 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003047
3048 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003049 toView.setScaleX(scale);
3050 toView.setScaleY(scale);
3051 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3052 scaleAnim.
3053 scaleX(1f).scaleY(1f).
3054 setDuration(duration).
3055 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003056
Winson Chungf0ea4d32011-06-06 14:27:16 -07003057 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003058 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003059 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003060 .ofFloat(toView, "alpha", 0f, 1f)
3061 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003062 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003063 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3064 @Override
3065 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003066 if (animation == null) {
3067 throw new RuntimeException("animation is null");
3068 }
Winson Chung70442722012-02-10 15:43:22 -08003069 float t = (Float) animation.getAnimatedValue();
3070 dispatchOnLauncherTransitionStep(fromView, t);
3071 dispatchOnLauncherTransitionStep(toView, t);
3072 }
3073 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003074
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003075 // toView should appear right at the end of the workspace shrink
3076 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003077 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003078 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003079 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003080
3081 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003082 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003083 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003084 // Prepare the position
3085 toView.setTranslationX(0.0f);
3086 toView.setTranslationY(0.0f);
3087 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003088 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003089 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003090 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003091 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003092 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3093 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003094
Winson Chungc7d2b602012-05-16 17:02:20 -07003095 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003096 if (mSearchDropTargetBar != null) {
3097 mSearchDropTargetBar.hideSearchBar(false);
3098 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003099 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003100 });
3101
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003102 if (workspaceAnim != null) {
3103 mStateAnimation.play(workspaceAnim);
3104 }
Michael Jurka1899a362011-11-03 13:50:45 -07003105
3106 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003107
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003108 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3109 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003110
3111 // If any of the objects being animated haven't been measured/laid out
3112 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003113 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003114 (mWorkspace.getMeasuredWidth() == 0) ||
3115 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003116 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003117 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003118
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003119 final AnimatorSet stateAnimation = mStateAnimation;
3120 final Runnable startAnimRunnable = new Runnable() {
3121 public void run() {
3122 // Check that mStateAnimation hasn't changed while
3123 // we waited for a layout/draw pass
3124 if (mStateAnimation != stateAnimation)
3125 return;
3126 setPivotsForZoom(toView, scale);
3127 dispatchOnLauncherTransitionStart(fromView, animated, false);
3128 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003129 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003130 }
3131 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003132 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003133 final ViewTreeObserver observer = toView.getViewTreeObserver();
3134 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3135 public void onGlobalLayout() {
3136 startAnimRunnable.run();
3137 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3138 }
3139 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003140 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003141 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003142 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003143 } else {
3144 toView.setTranslationX(0.0f);
3145 toView.setTranslationY(0.0f);
3146 toView.setScaleX(1.0f);
3147 toView.setScaleY(1.0f);
3148 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003149 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003150
Daniel Sandlere4f98912013-06-25 15:13:26 -04003151 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003152 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003153 if (mSearchDropTargetBar != null) {
3154 mSearchDropTargetBar.hideSearchBar(false);
3155 }
Michael Jurkaabded662011-03-04 12:06:57 -08003156 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003157 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003158 dispatchOnLauncherTransitionStart(fromView, animated, false);
3159 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003160 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003161 dispatchOnLauncherTransitionStart(toView, animated, false);
3162 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003163 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003164 }
3165
3166 /**
3167 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003168 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003169 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003170 */
Adam Cohened307df2013-10-02 09:37:31 -07003171 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003172 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003173
Michael Jurkab3e22d92011-10-31 15:58:33 -07003174 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003175 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003176 mStateAnimation.cancel();
3177 mStateAnimation = null;
3178 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003179 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003180
Winson Chungf0ea4d32011-06-06 14:27:16 -07003181 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003182 final int fadeOutDuration =
3183 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003184 final float scaleFactor = (float)
3185 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3186 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003187 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003188 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003189 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003190 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3191 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003192 toState, animated, stagger, -1);
3193 } else if (toState == Workspace.State.SPRING_LOADED ||
3194 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003195 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003196 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003197 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003198
Michael Jurkab3e22d92011-10-31 15:58:33 -07003199 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003200 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003201 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003202 final LauncherViewPropertyAnimator scaleAnim =
3203 new LauncherViewPropertyAnimator(fromView);
3204 scaleAnim.
3205 scaleX(scaleFactor).scaleY(scaleFactor).
3206 setDuration(duration).
3207 setInterpolator(new Workspace.ZoomInInterpolator());
3208
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003209 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003210 .ofFloat(fromView, "alpha", 1f, 0f)
3211 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003212 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003213 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3214 @Override
3215 public void onAnimationUpdate(ValueAnimator animation) {
3216 float t = 1f - (Float) animation.getAnimatedValue();
3217 dispatchOnLauncherTransitionStep(fromView, t);
3218 dispatchOnLauncherTransitionStep(toView, t);
3219 }
3220 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003221
Michael Jurka2ecf9952012-06-18 12:52:28 -07003222 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003223
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003224 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3225 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003226 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003227
3228 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003229 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003230 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003231 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003232 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3233 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003234 if (onCompleteRunnable != null) {
3235 onCompleteRunnable.run();
3236 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003237 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003238 }
3239 });
3240
Winson Chungf0ea4d32011-06-06 14:27:16 -07003241 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003242 if (workspaceAnim != null) {
3243 mStateAnimation.play(workspaceAnim);
3244 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003245 dispatchOnLauncherTransitionStart(fromView, animated, true);
3246 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003247 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003248 } else {
3249 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003250 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003251 dispatchOnLauncherTransitionStart(fromView, animated, true);
3252 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003253 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003254 dispatchOnLauncherTransitionStart(toView, animated, true);
3255 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003256 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003257 }
3258
Michael Jurkae326f182011-11-21 14:05:46 -08003259 @Override
3260 public void onTrimMemory(int level) {
3261 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003262 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003263 mAppsCustomizeTabHost.onTrimMemory();
3264 }
3265 }
3266
Adam Cohened307df2013-10-02 09:37:31 -07003267 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003268 showWorkspace(animated, null);
3269 }
3270
Adam Cohened307df2013-10-02 09:37:31 -07003271 protected void showWorkspace() {
3272 showWorkspace(true);
3273 }
3274
Adam Cohened66b2b2012-01-23 17:28:51 -08003275 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003276 if (mWorkspace.isInOverviewMode()) {
3277 mWorkspace.exitOverviewMode(animated);
3278 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003279 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003280 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003281 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003282 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003283
Winson Chungc7d2b602012-05-16 17:02:20 -07003284 // Show the search bar (only animate if we were showing the drop target bar in spring
3285 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003286 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003287 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003288 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003289
Michael Jurkab3e22d92011-10-31 15:58:33 -07003290 // Set focus to the AppsCustomize button
3291 if (mAllAppsButton != null) {
3292 mAllAppsButton.requestFocus();
3293 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003294 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003295
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003296 // Change the state *after* we've called all the transition code
3297 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003298
Winson Chung5fb63472011-02-02 17:03:37 -08003299 // Resume the auto-advance of widgets
3300 mUserPresent = true;
3301 updateRunning();
3302
alanv1d4fde62012-10-17 13:15:47 -07003303 // Send an accessibility event to announce the context change
3304 getWindow().getDecorView()
3305 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003306
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003307 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003308 }
3309
Adam Cohened307df2013-10-02 09:37:31 -07003310 void showOverviewMode(boolean animated) {
3311 mWorkspace.setVisibility(View.VISIBLE);
3312 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3313 mState = State.WORKSPACE;
3314 onWorkspaceShown(animated);
3315 }
3316
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003317 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003318 }
3319
Winson Chung82963d52013-10-09 11:20:57 -07003320 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3321 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322 if (mState != State.WORKSPACE) return;
3323
Winson Chung82963d52013-10-09 11:20:57 -07003324 if (resetPageToZero) {
3325 mAppsCustomizeTabHost.reset();
3326 }
Winson Chungc58497e2013-09-03 17:48:37 -07003327 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003328 mAppsCustomizeTabHost.requestFocus();
3329
Michael Jurkab3e22d92011-10-31 15:58:33 -07003330 // Change the state *after* we've called all the transition code
3331 mState = State.APPS_CUSTOMIZE;
3332
3333 // Pause the auto-advance of widgets until we are out of AllApps
3334 mUserPresent = false;
3335 updateRunning();
3336 closeFolder();
3337
3338 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003339 getWindow().getDecorView()
3340 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341 }
3342
Adam Cohen7777d962011-08-18 18:58:38 -07003343 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003344 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003345 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003346 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003347 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003348 }
Adam Cohen7777d962011-08-18 18:58:38 -07003349
Adam Cohenad4e15c2013-10-17 16:21:35 -07003350 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003351 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003352 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3353
Winson Chunge7a03942011-08-05 15:05:12 -07003354 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003355 @Override
3356 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003357 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003358 // Before we show workspace, hide all apps again because
3359 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3360 // clean up our state transition functions
3361 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003362 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003363 } else {
3364 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003365 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003366 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003367 }, delay);
3368
Winson Chung557d6ed2011-07-08 15:34:52 -07003369 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370
Michael Jurkad3ef3062010-11-23 16:23:58 -08003371 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003372 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373 final boolean animated = true;
3374 final boolean springLoaded = true;
3375 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003376 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003377 }
3378 // Otherwise, we are not in spring loaded mode, so don't do anything.
3379 }
3380
Michael Jurkab3e22d92011-10-31 15:58:33 -07003381 void lockAllApps() {
3382 // TODO
3383 }
3384
3385 void unlockAllApps() {
3386 // TODO
3387 }
3388
Winson Chungf0ea4d32011-06-06 14:27:16 -07003389 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003390 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003391 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003392 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003393 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003394 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003395 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003396 int duration = 0;
3397 if (mSearchDropTargetBar != null) {
3398 duration = mSearchDropTargetBar.getTransitionInDuration();
3399 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003400 mHotseat.animate().alpha(1f).setDuration(duration);
3401 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003402 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003403 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003404 }
3405 }
3406 }
3407
3408 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003409 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003410 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003411 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003412 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003413 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003414 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003415 int duration = 0;
3416 if (mSearchDropTargetBar != null) {
3417 duration = mSearchDropTargetBar.getTransitionOutDuration();
3418 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003419 mHotseat.animate().alpha(0f).setDuration(duration);
3420 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003422 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003423 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003424 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003425 }
3426
Patrick Dubroy5f445422011-02-18 14:35:21 -08003427 /**
3428 * Add an item from all apps or customize onto the given workspace screen.
3429 * If layout is null, add to the current screen.
3430 */
3431 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003432 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003433 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003434 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003435 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003436
Winson Chungdff8ebb2011-09-08 17:25:31 -07003437 /** Maps the current orientation to an index for referencing orientation correct global icons */
3438 private int getCurrentOrientationIndexForGlobalIcons() {
3439 // default - 0, landscape - 1
3440 switch (getResources().getConfiguration().orientation) {
3441 case Configuration.ORIENTATION_LANDSCAPE:
3442 return 1;
3443 default:
3444 return 0;
3445 }
3446 }
3447
Michael Jurkaae65ee32012-04-30 11:45:54 -07003448 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003449 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003450 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003451 // Look for the toolbar icon specified in the activity meta-data
3452 Bundle metaData = packageManager.getActivityInfo(
3453 activityName, PackageManager.GET_META_DATA).metaData;
3454 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003455 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003456 if (iconResId != 0) {
3457 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003458 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003459 }
3460 }
3461 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003462 // This can happen if the activity defines an invalid drawable
3463 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3464 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003465 } catch (Resources.NotFoundException nfe) {
3466 // This can happen if the activity defines an invalid drawable
3467 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3468 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003469 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003470 return null;
3471 }
3472
3473 // if successful in getting icon, return it; otherwise, set button to use default drawable
3474 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003475 int buttonId, ComponentName activityName, int fallbackDrawableId,
3476 String toolbarResourceName) {
3477 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003478 Resources r = getResources();
3479 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3480 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003481
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003482 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003483 // If we were unable to find the icon via the meta-data, use a generic one
3484 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003485 toolbarIcon = r.getDrawable(fallbackDrawableId);
3486 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003487 if (button != null) {
3488 button.setCompoundDrawables(toolbarIcon, null, null, null);
3489 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003490 return null;
3491 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003492 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003493 if (button != null) {
3494 button.setCompoundDrawables(toolbarIcon, null, null, null);
3495 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003496 return toolbarIcon.getConstantState();
3497 }
3498 }
3499
3500 // if successful in getting icon, return it; otherwise, set button to use default drawable
3501 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003502 int buttonId, ComponentName activityName, int fallbackDrawableId,
3503 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003504 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003505 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003506
Michael Jurka19e0fc52011-07-22 18:00:21 -07003507 if (button != null) {
3508 // If we were unable to find the icon via the meta-data, use a
3509 // generic one
3510 if (toolbarIcon == null) {
3511 button.setImageResource(fallbackDrawableId);
3512 } else {
3513 button.setImageDrawable(toolbarIcon);
3514 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003515 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003516
3517 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3518
Michael Jurka0423dcf2010-10-05 14:56:18 -07003519 }
3520
Winson Chung1b884092012-06-08 17:13:02 -07003521 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003522 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003523 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003524 }
3525
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003526 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003527 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003528 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003529 }
3530
Winson Chungbb185bd2011-11-21 12:31:42 -08003531 private void invalidatePressedFocusedStates(View container, View button) {
3532 if (container instanceof HolographicLinearLayout) {
3533 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3534 layout.invalidatePressedFocusedStates();
3535 } else if (button instanceof HolographicImageView) {
3536 HolographicImageView view = (HolographicImageView) button;
3537 view.invalidatePressedFocusedStates();
3538 }
3539 }
3540
Cristina Stancu476493b2013-08-07 17:20:14 +01003541 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003542 if (mQsb == null) {
3543 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3544 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003545 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003546 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003547 }
3548
3549 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003550 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003551 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003552 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003553 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003554
Winson Chung1cad91e2011-05-25 17:41:01 -07003555 final SearchManager searchManager =
3556 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3557 ComponentName activityName = searchManager.getGlobalSearchActivity();
3558 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003559 int coi = getCurrentOrientationIndexForGlobalIcons();
3560 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003561 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3562 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3563 if (sGlobalSearchIcon[coi] == null) {
3564 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3565 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3566 TOOLBAR_ICON_METADATA_NAME);
3567 }
3568
Winson Chungc51db6a2011-10-05 11:44:49 -07003569 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3570 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003571 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003572 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003573 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003574 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003575 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3576 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003577 if (searchButton != null) searchButton.setVisibility(View.GONE);
3578 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003579 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003580 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003581 }
3582 }
3583
Cristina Stancu476493b2013-08-07 17:20:14 +01003584 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003585 final View searchButtonContainer = findViewById(R.id.search_button_container);
3586 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003587 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003588 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003589 }
3590
Cristina Stancu476493b2013-08-07 17:20:14 +01003591 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003592 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003593 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003594
Winson Chungc51db6a2011-10-05 11:44:49 -07003595 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003596 final SearchManager searchManager =
3597 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3598 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3599
3600 ComponentName activityName = null;
3601 if (globalSearchActivity != null) {
3602 // Check if the global search activity handles voice search
3603 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3604 intent.setPackage(globalSearchActivity.getPackageName());
3605 activityName = intent.resolveActivity(getPackageManager());
3606 }
3607
3608 if (activityName == null) {
3609 // Fallback: check if an activity other than the global search activity
3610 // resolves this
3611 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3612 activityName = intent.resolveActivity(getPackageManager());
3613 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003614 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003615 int coi = getCurrentOrientationIndexForGlobalIcons();
3616 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003617 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3618 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3619 if (sVoiceSearchIcon[coi] == null) {
3620 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3621 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3622 TOOLBAR_ICON_METADATA_NAME);
3623 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003624 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003625 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003626 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003627 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003628 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003629 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003630 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003631 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003632 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003633 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003634 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003635 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003636
Cristina Stancu476493b2013-08-07 17:20:14 +01003637 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003638 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3639 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003640 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003641 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003642 }
3643
Winson Chung5841efa2013-09-30 18:06:44 -07003644 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003645 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3646 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003647 boolean visible = !forceDisableVoiceButtonProxy &&
3648 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003649 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003650 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003651 }
3652 }
Winson Chung5841efa2013-09-30 18:06:44 -07003653
3654 /**
3655 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3656 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3657 */
3658 public void disableVoiceButtonProxy(boolean disabled) {
3659 updateVoiceButtonProxyVisible(disabled);
3660 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003661 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003662 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003663 */
3664 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003665 if (!DISABLE_MARKET_BUTTON) {
3666 final View marketButton = findViewById(R.id.market_button);
3667 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3668 // Find the app market activity by resolving an intent.
3669 // (If multiple app markets are installed, it will return the ResolverActivity.)
3670 ComponentName activityName = intent.resolveActivity(getPackageManager());
3671 if (activityName != null) {
3672 int coi = getCurrentOrientationIndexForGlobalIcons();
3673 mAppMarketIntent = intent;
3674 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3675 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3676 TOOLBAR_ICON_METADATA_NAME);
3677 marketButton.setVisibility(View.VISIBLE);
3678 } else {
3679 // We should hide and disable the view so that we don't try and restore the visibility
3680 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3681 marketButton.setVisibility(View.GONE);
3682 marketButton.setEnabled(false);
3683 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003684 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003685 }
3686
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003687 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003688 if (!DISABLE_MARKET_BUTTON) {
3689 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3690 Resources r = getResources();
3691 Drawable marketIconDrawable = d.newDrawable(r);
3692 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3693 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3694 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003695
Winson Chungd64a6662013-09-30 11:06:59 -07003696 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3697 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003698 }
3699
Michael Jurkad7c28052012-04-27 15:43:36 -07003700 @Override
3701 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003702 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003703 final List<CharSequence> text = event.getText();
3704 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003705 // Populate event with a fake title based on the current state.
3706 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003707 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003708 } else {
3709 text.add(getString(R.string.all_apps_home_button_label));
3710 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003711 return result;
3712 }
3713
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003714 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003715 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003716 */
3717 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3718 @Override
3719 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003720 closeSystemDialogs();
3721 }
3722 }
3723
3724 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003725 * Receives notifications whenever the appwidgets are reset.
3726 */
3727 private class AppWidgetResetObserver extends ContentObserver {
3728 public AppWidgetResetObserver() {
3729 super(new Handler());
3730 }
3731
3732 @Override
3733 public void onChange(boolean selfChange) {
3734 onAppWidgetReset();
3735 }
3736 }
3737
3738 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003739 * If the activity is currently paused, signal that we need to run the passed Runnable
3740 * in onResume.
3741 *
3742 * This needs to be called from incoming places where resources might have been loaded
3743 * while we are paused. That is becaues the Configuration might be wrong
3744 * when we're not running, and if it comes back to what it was when we
3745 * were paused, we are not restarted.
3746 *
3747 * Implementation of the method from LauncherModel.Callbacks.
3748 *
3749 * @return true if we are currently paused. The caller might be able to
3750 * skip some work in that case since we will come back again.
3751 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003752 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003753 if (mPaused) {
3754 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003755 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003756 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003757 }
3758 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003759 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003760 return true;
3761 } else {
3762 return false;
3763 }
3764 }
3765
Michael Jurkac402cd92013-05-20 15:49:32 +02003766 private boolean waitUntilResume(Runnable run) {
3767 return waitUntilResume(run, false);
3768 }
3769
Michael Jurka1e2f4652013-07-08 18:03:46 -07003770 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003771 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003772 }
3773
Michael Jurka7607c2f2013-04-03 14:33:19 -07003774 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003775 * If the activity is currently paused, signal that we need to re-run the loader
3776 * in onResume.
3777 *
3778 * This needs to be called from incoming places where resources might have been loaded
3779 * while we are paused. That is becaues the Configuration might be wrong
3780 * when we're not running, and if it comes back to what it was when we
3781 * were paused, we are not restarted.
3782 *
3783 * Implementation of the method from LauncherModel.Callbacks.
3784 *
3785 * @return true if we are currently paused. The caller might be able to
3786 * skip some work in that case since we will come back again.
3787 */
3788 public boolean setLoadOnResume() {
3789 if (mPaused) {
3790 Log.i(TAG, "setLoadOnResume");
3791 mOnResumeNeedsLoad = true;
3792 return true;
3793 } else {
3794 return false;
3795 }
3796 }
3797
3798 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003800 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003802 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003803 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003804 } else {
3805 return SCREEN_COUNT / 2;
3806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003808
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 /**
3810 * Refreshes the shortcuts shown on the workspace.
3811 *
3812 * Implementation of the method from LauncherModel.Callbacks.
3813 */
3814 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003815 mWorkspaceLoading = true;
3816
Michael Jurka7607c2f2013-04-03 14:33:19 -07003817 // If we're starting binding all over again, clear any bind calls we'd postponed in
3818 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3819 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003820 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003821
Winson Chungd64d1762013-08-20 14:37:16 -07003822 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003823 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003824 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003825
Michael Jurka05bf6442011-11-30 20:28:53 -08003826 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003827 if (mHotseat != null) {
3828 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003829 }
3830 }
3831
Adam Cohendcd297f2013-06-18 13:13:40 -07003832 @Override
3833 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003834 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003835
Adam Cohen5084cba2013-09-03 12:01:16 -07003836 // If there are no screens, we need to have an empty screen
3837 if (orderedScreenIds.size() == 0) {
3838 mWorkspace.addExtraEmptyScreen();
3839 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003840
3841 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003842 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003843 if (hasCustomContentToLeft()) {
3844 mWorkspace.createCustomContentContainer();
3845 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003846 }
Winson Chung64359a52013-07-08 17:17:08 -07003847 }
3848
3849 @Override
3850 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003851 // Log to disk
3852 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3853 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3854 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003855 int count = orderedScreenIds.size();
3856 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003857 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003858 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003859 }
3860
Adam Cohen39a06042013-07-19 14:30:12 -07003861 private boolean shouldShowWeightWatcher() {
3862 String spKey = LauncherAppState.getSharedPreferencesKey();
3863 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003864 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003865
3866 return show;
3867 }
3868
3869 private void toggleShowWeightWatcher() {
3870 String spKey = LauncherAppState.getSharedPreferencesKey();
3871 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3872 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3873
3874 show = !show;
3875
3876 SharedPreferences.Editor editor = sp.edit();
3877 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3878 editor.commit();
3879
3880 if (mWeightWatcher != null) {
3881 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3882 }
3883 }
3884
Winson Chungd64d1762013-08-20 14:37:16 -07003885 public void bindAppsAdded(final ArrayList<Long> newScreens,
3886 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003887 final ArrayList<ItemInfo> addAnimated,
3888 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003889 Runnable r = new Runnable() {
3890 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003891 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003892 }
3893 };
3894 if (waitUntilResume(r)) {
3895 return;
3896 }
3897
Winson Chungd64d1762013-08-20 14:37:16 -07003898 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003899 if (newScreens != null) {
3900 bindAddScreens(newScreens);
3901 }
Winson Chungd64d1762013-08-20 14:37:16 -07003902
3903 // We add the items without animation on non-visible pages, and with
3904 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003905 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003906 bindItems(addNotAnimated, 0,
3907 addNotAnimated.size(), false);
3908 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003909 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003910 bindItems(addAnimated, 0,
3911 addAnimated.size(), true);
3912 }
Winson Chungc58497e2013-09-03 17:48:37 -07003913
Winson Chung87412982013-10-03 18:34:14 -07003914 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003915 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003916
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003917 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003918 addedApps != null && mAppsCustomizeContent != null) {
3919 mAppsCustomizeContent.addApps(addedApps);
3920 }
Winson Chungd64d1762013-08-20 14:37:16 -07003921 }
3922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003923 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 * Bind the items start-end from the list.
3925 *
3926 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003927 */
Winson Chung64359a52013-07-08 17:17:08 -07003928 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3929 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003930 Runnable r = new Runnable() {
3931 public void run() {
3932 bindItems(shortcuts, start, end, forceAnimateIcons);
3933 }
3934 };
3935 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003936 return;
3937 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003938
Winson Chungf0c6ae02012-03-21 16:10:31 -07003939 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003940 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3941 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003942 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003943 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003944 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003945 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003947
3948 // Short circuit if we are loading dock items for a configuration which has no dock
3949 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3950 mHotseat == null) {
3951 continue;
3952 }
3953
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 switch (item.itemType) {
3955 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3956 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003957 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003958 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003959
Winson Chung64359a52013-07-08 17:17:08 -07003960 /*
3961 * TODO: FIX collision case
3962 */
Winson Chungce376632013-07-11 16:12:41 -07003963 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3964 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3965 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3966 throw new RuntimeException("OCCUPIED");
3967 }
Winson Chung64359a52013-07-08 17:17:08 -07003968 }
3969
Adam Cohendcd297f2013-06-18 13:13:40 -07003970 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3971 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003972 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003973 // Animate all the applications up now
3974 shortcut.setAlpha(0f);
3975 shortcut.setScaleX(0f);
3976 shortcut.setScaleY(0f);
3977 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003978 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003979 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003980 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003981 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003982 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003983 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003984 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003985 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3986 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003988 default:
3989 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003991 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003992
Winson Chung997a9232013-07-24 15:33:46 -07003993 if (animateIcons) {
3994 // Animate to the correct page
3995 if (newShortcutsScreenId > -1) {
3996 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003997 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003998 final Runnable startBounceAnimRunnable = new Runnable() {
3999 public void run() {
4000 anim.playTogether(bounceAnims);
4001 anim.start();
4002 }
4003 };
Winson Chung997a9232013-07-24 15:33:46 -07004004 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004005 // We post the animation slightly delayed to prevent slowdowns
4006 // when we are loading right after we return to launcher.
4007 mWorkspace.postDelayed(new Runnable() {
4008 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004009 if (mWorkspace != null) {
4010 mWorkspace.snapToPage(newScreenIndex);
4011 mWorkspace.postDelayed(startBounceAnimRunnable,
4012 NEW_APPS_ANIMATION_DELAY);
4013 }
Winson Chung94d67682013-09-25 16:29:40 -07004014 }
4015 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004016 } else {
4017 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004018 }
4019 }
Winson Chung64359a52013-07-08 17:17:08 -07004020 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004021 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004022 }
4023
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 /**
4025 * Implementation of the method from LauncherModel.Callbacks.
4026 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004027 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004028 Runnable r = new Runnable() {
4029 public void run() {
4030 bindFolders(folders);
4031 }
4032 };
4033 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004034 return;
4035 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004036 sFolders.clear();
4037 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004039
4040 /**
4041 * Add the views for a widget to the workspace.
4042 *
4043 * Implementation of the method from LauncherModel.Callbacks.
4044 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004045 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004046 Runnable r = new Runnable() {
4047 public void run() {
4048 bindAppWidget(item);
4049 }
4050 };
4051 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004052 return;
4053 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004054
Daniel Sandler843e8602010-06-07 14:59:01 -04004055 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4056 if (DEBUG_WIDGETS) {
4057 Log.d(TAG, "bindAppWidget: " + item);
4058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 final Workspace workspace = mWorkspace;
4060
4061 final int appWidgetId = item.appWidgetId;
4062 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004063 if (DEBUG_WIDGETS) {
4064 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4065 }
4066
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4068
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004070 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071
Adam Cohendcd297f2013-06-18 13:13:40 -07004072 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004074 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4075
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076 workspace.requestLayout();
4077
Daniel Sandler843e8602010-06-07 14:59:01 -04004078 if (DEBUG_WIDGETS) {
4079 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4080 + (SystemClock.uptimeMillis()-start) + "ms");
4081 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 }
4083
Adam Cohen1462de32012-07-24 22:34:36 -07004084 public void onPageBoundSynchronously(int page) {
4085 mSynchronouslyBoundPages.add(page);
4086 }
4087
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 /**
4089 * Callback saying that there aren't any more items to bind.
4090 *
4091 * Implementation of the method from LauncherModel.Callbacks.
4092 */
Adam Cohene25af792013-06-06 23:08:25 -07004093 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004094 Runnable r = new Runnable() {
4095 public void run() {
4096 finishBindingItems(upgradePath);
4097 }
4098 };
4099 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004100 return;
4101 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 if (mSavedState != null) {
4103 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004104 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004105 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 mSavedState = null;
4107 }
4108
Adam Cohen1462de32012-07-24 22:34:36 -07004109 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004111 // If we received the result of any pending adds while the loader was running (e.g. the
4112 // widget configuration forced an orientation change), process them now.
4113 for (int i = 0; i < sPendingAddList.size(); i++) {
4114 completeAdd(sPendingAddList.get(i));
4115 }
4116 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117
Winson Chungc51db6a2011-10-05 11:44:49 -07004118 // Update the market app icon as necessary (the other icons will be managed in response to
4119 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004120 if (!DISABLE_MARKET_BUTTON) {
4121 updateAppMarketIcon();
4122 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004123
Winson Chungf0c6ae02012-03-21 16:10:31 -07004124 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004125 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004126 mWorkspace.getUniqueComponents(true, null);
4127 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004128 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004129 }
4130
Winson Chunga0b7e862013-09-05 16:03:15 -07004131 public boolean isAllAppsButtonRank(int rank) {
4132 if (mHotseat != null) {
4133 return mHotseat.isAllAppsButtonRank(rank);
4134 }
4135 return false;
4136 }
4137
Winson Chunga2413752012-04-03 14:22:34 -07004138 private boolean canRunNewAppsAnimation() {
4139 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4140 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4141 }
4142
Winson Chungc9168342013-06-26 14:54:55 -07004143 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4144 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4145 PropertyValuesHolder.ofFloat("alpha", 1f),
4146 PropertyValuesHolder.ofFloat("scaleX", 1f),
4147 PropertyValuesHolder.ofFloat("scaleY", 1f));
4148 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4149 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4150 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4151 return bounceAnim;
4152 }
4153
Adam Cohen27772732013-11-11 14:00:56 +00004154 public boolean useVerticalBarLayout() {
4155 return LauncherAppState.getInstance().getDynamicGrid().
4156 getDeviceProfile().isVerticalBarLayout();
4157 }
4158
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004159 protected Rect getSearchBarBounds() {
4160 return LauncherAppState.getInstance().getDynamicGrid().
4161 getDeviceProfile().getSearchBarBounds();
4162 }
4163
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004164 @Override
4165 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004166 boolean searchVisible = updateGlobalSearchIcon();
4167 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004168 if (mSearchDropTargetBar != null) {
4169 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4170 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004171 }
4172
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004173 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004174 * Add the icons for all apps.
4175 *
4176 * Implementation of the method from LauncherModel.Callbacks.
4177 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004178 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004179 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004180 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4181 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4182 getHotseat().addAllAppsFolder(mIconCache, apps,
4183 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4184 }
4185 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004186 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004187 if (mAppsCustomizeContent != null) {
4188 mAppsCustomizeContent.onPackagesUpdated(
4189 LauncherModel.getSortedWidgetsAndShortcuts(this));
4190 }
Winson Chungc58497e2013-09-03 17:48:37 -07004191 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004192 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004193 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004194 mAppsCustomizeContent.onPackagesUpdated(
4195 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004196 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004198 }
4199
4200 /**
4201 * A package was updated.
4202 *
4203 * Implementation of the method from LauncherModel.Callbacks.
4204 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004205 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004206 Runnable r = new Runnable() {
4207 public void run() {
4208 bindAppsUpdated(apps);
4209 }
4210 };
4211 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004212 return;
4213 }
4214
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004215 if (mWorkspace != null) {
4216 mWorkspace.updateShortcuts(apps);
4217 }
Winson Chungc58497e2013-09-03 17:48:37 -07004218
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004219 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004220 mAppsCustomizeContent != null) {
4221 mAppsCustomizeContent.updateApps(apps);
4222 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004223 }
4224
4225 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004226 * A package was uninstalled. We take both the super set of packageNames
4227 * in addition to specific applications to remove, the reason being that
4228 * this can be called when a package is updated as well. In that scenario,
4229 * we only remove specific components from the workspace, where as
4230 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004231 *
4232 * Implementation of the method from LauncherModel.Callbacks.
4233 */
Winson Chung83892cc2013-05-01 16:53:33 -07004234 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004235 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004236 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004237 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004238 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004239 }
Winson Chungd64d1762013-08-20 14:37:16 -07004240 };
4241 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004242 return;
4243 }
4244
Winson Chungdf95eb12013-10-16 14:57:07 -07004245 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004246 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004247 }
4248 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004249 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004250 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004251
Winson Chunga1820962011-10-03 16:31:06 -07004252 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004253 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004254
Winson Chungdf95eb12013-10-16 14:57:07 -07004255 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004256 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004257 mAppsCustomizeContent != null) {
4258 mAppsCustomizeContent.removeApps(appInfos);
4259 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 }
Joe Onoratobe386092009-11-17 17:32:16 -08004261
4262 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004263 * A number of packages were updated.
4264 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004265 private ArrayList<Object> mWidgetsAndShortcuts;
4266 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004267 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004268 bindPackagesUpdated(mWidgetsAndShortcuts);
4269 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004270 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004271 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004272 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4273 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4274 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004275 return;
4276 }
4277
Winson Chung64359a52013-07-08 17:17:08 -07004278 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004279 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004280 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004281 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004282 }
4283
Winson Chung400438b2011-01-16 17:53:48 -08004284 private int mapConfigurationOriActivityInfoOri(int configOri) {
4285 final Display d = getWindowManager().getDefaultDisplay();
4286 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4287 switch (d.getRotation()) {
4288 case Surface.ROTATION_0:
4289 case Surface.ROTATION_180:
4290 // We are currently in the same basic orientation as the natural orientation
4291 naturalOri = configOri;
4292 break;
4293 case Surface.ROTATION_90:
4294 case Surface.ROTATION_270:
4295 // We are currently in the other basic orientation to the natural orientation
4296 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4297 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4298 break;
4299 }
4300
4301 int[] oriMap = {
4302 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4303 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4304 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4305 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4306 };
4307 // Since the map starts at portrait, we need to offset if this device's natural orientation
4308 // is landscape.
4309 int indexOffset = 0;
4310 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4311 indexOffset = 1;
4312 }
4313 return oriMap[(d.getRotation() + indexOffset) % 4];
4314 }
Adam Cohen446e9402011-09-15 18:21:21 -07004315
Winson Chung4b919f82012-05-01 10:44:08 -07004316 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004317 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004318 getResources().getBoolean(R.bool.allow_rotation);
4319 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004320 }
Winson Chung4b919f82012-05-01 10:44:08 -07004321 public void lockScreenOrientation() {
4322 if (isRotationEnabled()) {
4323 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4324 .getConfiguration().orientation));
4325 }
4326 }
4327 public void unlockScreenOrientation(boolean immediate) {
4328 if (isRotationEnabled()) {
4329 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004330 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004331 } else {
4332 mHandler.postDelayed(new Runnable() {
4333 public void run() {
4334 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4335 }
4336 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004337 }
Winson Chung4b919f82012-05-01 10:44:08 -07004338 }
Winson Chung400438b2011-01-16 17:53:48 -08004339 }
4340
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004341 /**
4342 * Called when the SearchBar hint should be changed.
4343 *
4344 * @param hint the hint to be displayed in the search bar.
4345 */
4346 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004347 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004348 }
4349
Winson Chunge43a1e72014-01-15 10:33:02 -08004350 protected boolean isLauncherPreinstalled() {
4351 PackageManager pm = getPackageManager();
4352 try {
4353 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4354 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4355 return true;
4356 } else {
4357 return false;
4358 }
4359 } catch (NameNotFoundException e) {
4360 e.printStackTrace();
4361 return false;
4362 }
4363 }
4364
Winson Chunge6eabff2013-09-24 11:01:23 -07004365 protected String getFirstRunClingSearchBarHint() {
4366 return "";
4367 }
4368 protected String getFirstRunCustomContentHint() {
4369 return "";
4370 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004371 protected int getFirstRunFocusedHotseatAppDrawableId() {
4372 return -1;
4373 }
4374 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4375 return null;
4376 }
4377 protected int getFirstRunFocusedHotseatAppRank() {
4378 return -1;
4379 }
4380 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4381 return "";
4382 }
4383 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4384 return "";
4385 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004386
Winson Chungaf40f202013-09-18 18:26:31 -07004387 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004388 mLauncherClings.dismissFirstRunCling(v);
4389 }
4390 public void dismissMigrationClingCopyApps(View v) {
4391 mLauncherClings.dismissMigrationClingCopyApps(v);
4392 }
4393 public void dismissMigrationClingUseDefault(View v) {
4394 mLauncherClings.dismissMigrationClingUseDefault(v);
4395 }
4396 public void dismissMigrationWorkspaceCling(View v) {
4397 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004398 }
Winson Chung82f55532011-08-09 14:14:23 -07004399 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004400 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004401 }
4402 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004403 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004404 }
4405
Winson Chunga6945242014-01-08 14:04:34 -08004406 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004407 return !ActivityManager.isRunningInTestHarness() &&
4408 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004409 }
4410
4411 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004412 if (shouldRunFirstRunActivity() &&
4413 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004414 Intent firstRunIntent = getFirstRunActivity();
4415 if (firstRunIntent != null) {
4416 startActivity(firstRunIntent);
4417 markFirstRunActivityShown();
4418 }
4419 }
4420 }
4421
4422 private void markFirstRunActivityShown() {
4423 SharedPreferences.Editor editor = mSharedPrefs.edit();
4424 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4425 editor.apply();
4426 }
4427
4428 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004429 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4430 if (mHotseat != null) mHotseat.setAlpha(1f);
4431 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4432 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004433 }
4434
4435 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004436 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4437 if (mHotseat != null) mHotseat.setAlpha(0f);
4438 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4439 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004440 }
4441
4442
Mathew Inwood72fbec12013-11-19 15:45:07 +00004443 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4444 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4445 if (ri == null) {
4446 return null;
4447 }
4448 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4449 }
4450
4451 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4452 Bitmap icon) {
4453 return new ShortcutInfo(shortcutIntent, caption, icon);
4454 }
4455
4456 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4457 dragView.setTag(dragInfo);
4458 mWorkspace.onDragStartedWithItem(dragView);
4459 mWorkspace.beginDragShared(dragView, source);
4460 }
4461
Anjali Koppalf5d29132014-02-28 13:33:27 -08004462 @Override
4463 public void onPageSwitch(View newPage, int newPageIndex) {
4464 }
4465
Winson Chung80baf5a2010-08-09 16:03:15 -07004466 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004467 * Prints out out state for debugging.
4468 */
4469 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004470 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004471 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004472 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4473 Log.d(TAG, "mRestoring=" + mRestoring);
4474 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4475 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004476 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004477 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004478
Winson Chung785d2eb2011-04-14 16:08:02 -07004479 if (mAppsCustomizeContent != null) {
4480 mAppsCustomizeContent.dumpState();
4481 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004482 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004483 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004484
4485 @Override
4486 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4487 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004488 synchronized (sDumpLogs) {
4489 writer.println(" ");
4490 writer.println("Debug logs: ");
4491 for (int i = 0; i < sDumpLogs.size(); i++) {
4492 writer.println(" " + sDumpLogs.get(i));
4493 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004494 }
4495 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004496
4497 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004498 if (DEBUG_DUMP_LOG) {
4499 synchronized (sDumpLogs) {
4500 Log.d(TAG, "");
4501 Log.d(TAG, "*********************");
4502 Log.d(TAG, "Launcher debug logs: ");
4503 for (int i = 0; i < sDumpLogs.size(); i++) {
4504 Log.d(TAG, " " + sDumpLogs.get(i));
4505 }
4506 Log.d(TAG, "*********************");
4507 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004508 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004509 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004510 }
4511
4512 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004513 addDumpLog(tag, log, null, debugLog);
4514 }
4515
4516 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004517 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004518 if (e != null) {
4519 Log.d(tag, log, e);
4520 } else {
4521 Log.d(tag, log);
4522 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004523 }
Winson Chungede41292013-09-19 16:27:36 -07004524 if (DEBUG_DUMP_LOG) {
4525 sDateStamp.setTime(System.currentTimeMillis());
4526 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004527 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4528 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004529 }
Winson Chungede41292013-09-19 16:27:36 -07004530 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004531 }
4532
Winson Chungede41292013-09-19 16:27:36 -07004533 public void dumpLogsToLocalData() {
4534 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004535 new AsyncTask<Void, Void, Void>() {
4536 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004537 boolean success = false;
4538 sDateStamp.setTime(sRunStart);
4539 String FILENAME = sDateStamp.getMonth() + "-"
4540 + sDateStamp.getDay() + "_"
4541 + sDateStamp.getHours() + "-"
4542 + sDateStamp.getMinutes() + "_"
4543 + sDateStamp.getSeconds() + ".txt";
4544
4545 FileOutputStream fos = null;
4546 File outFile = null;
4547 try {
4548 outFile = new File(getFilesDir(), FILENAME);
4549 outFile.createNewFile();
4550 fos = new FileOutputStream(outFile);
4551 } catch (Exception e) {
4552 e.printStackTrace();
4553 }
4554 if (fos != null) {
4555 PrintWriter writer = new PrintWriter(fos);
4556
4557 writer.println(" ");
4558 writer.println("Debug logs: ");
4559 synchronized (sDumpLogs) {
4560 for (int i = 0; i < sDumpLogs.size(); i++) {
4561 writer.println(" " + sDumpLogs.get(i));
4562 }
4563 }
4564 writer.close();
4565 }
4566 try {
4567 if (fos != null) {
4568 fos.close();
4569 success = true;
4570 }
4571 } catch (IOException e) {
4572 e.printStackTrace();
4573 }
Michael Jurka43467462013-11-14 12:03:58 +01004574 return null;
Winson Chungede41292013-09-19 16:27:36 -07004575 }
Michael Jurka43467462013-11-14 12:03:58 +01004576 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004577 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004579}
Michael Jurka2763be32011-02-24 11:19:57 -08004580
Michael Jurkaabded662011-03-04 12:06:57 -08004581interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004582 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004583 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004584 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004585 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004586 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004587}
Dan Sandlerd5024042014-01-09 15:01:33 -05004588
4589interface DebugIntents {
4590 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4591 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004592}