blob: 218b787e8a7b61f5d61364fb534c53e48e210187 [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()) {
1014 mWorkspace.getCustomContentCallbacks().onShow();
1015 }
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 {
1058 // Custom content is completely shown
1059 public void onShow();
1060
1061 // Custom content is completely hidden
1062 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001063
1064 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1065 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001066 }
1067
Jorim Jaggid017f882014-01-14 17:08:48 -08001068 protected boolean hasSettings() {
1069 return false;
1070 }
1071
Adam Cohenbffe7452013-07-22 18:21:45 -07001072 public interface QSBScroller {
1073 public void setScrollY(int scrollY);
1074 }
1075
Winson Chung98ca0f72013-07-29 12:58:51 -07001076 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001077 CustomContentCallbacks callbacks, String description) {
1078 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001079 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001080 }
1081
Adam Cohenedb40762013-07-18 16:45:45 -07001082 // The custom content needs to offset its content to account for the QSB
1083 public int getTopOffsetForCustomContent() {
1084 return mWorkspace.getPaddingTop();
1085 }
1086
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001087 @Override
1088 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089 // Flag the loader to stop early before switching
1090 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 if (mAppsCustomizeContent != null) {
1092 mAppsCustomizeContent.surrender();
1093 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001094 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001095 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001098 @Override
1099 public void onWindowFocusChanged(boolean hasFocus) {
1100 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001101 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001102 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 private boolean acceptFilter() {
1105 final InputMethodManager inputManager = (InputMethodManager)
1106 getSystemService(Context.INPUT_METHOD_SERVICE);
1107 return !inputManager.isFullscreenMode();
1108 }
1109
1110 @Override
1111 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001112 final int uniChar = event.getUnicodeChar();
1113 final boolean handled = super.onKeyDown(keyCode, event);
1114 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1115 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1117 keyCode, event);
1118 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001119 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001120 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001121 // showSearchDialog()
1122 // If there are multiple keystrokes before the search dialog takes focus,
1123 // onSearchRequested() will be called for every keystroke,
1124 // but it is idempotent, so it's fine.
1125 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127 }
1128
Joe Onorato8a9625e2010-01-28 15:55:35 -08001129 // Eat the long press event so the keyboard doesn't come up.
1130 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1131 return true;
1132 }
1133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 return handled;
1135 }
1136
Karl Rosaen138a0412009-04-23 19:00:21 -07001137 private String getTypedText() {
1138 return mDefaultKeySsb.toString();
1139 }
1140
1141 private void clearTypedText() {
1142 mDefaultKeySsb.clear();
1143 mDefaultKeySsb.clearSpans();
1144 Selection.setSelection(mDefaultKeySsb, 0);
1145 }
1146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001148 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1149 * State
1150 */
1151 private static State intToState(int stateOrdinal) {
1152 State state = State.WORKSPACE;
1153 final State[] stateValues = State.values();
1154 for (int i = 0; i < stateValues.length; i++) {
1155 if (stateValues[i].ordinal() == stateOrdinal) {
1156 state = stateValues[i];
1157 break;
1158 }
1159 }
1160 return state;
1161 }
1162
1163 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 * Restores the previous state, if it exists.
1165 *
1166 * @param savedState The previous state.
1167 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001168 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 private void restoreState(Bundle savedState) {
1170 if (savedState == null) {
1171 return;
1172 }
1173
Michael Jurka883f55b2010-10-21 15:47:14 -07001174 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001175 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001176 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001177 }
1178
Adam Cohen21cd0022013-10-09 18:57:02 -07001179 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1180 PagedView.INVALID_RESTORE_PAGE);
1181 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001182 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 }
1184
Winson Chung3d503fb2011-07-13 17:25:49 -07001185 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001186 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187
Winson Chung3d503fb2011-07-13 17:25:49 -07001188 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1189 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001190 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001191 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1192 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001193 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1194 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1195 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001196 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001197 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 mRestoring = true;
1199 }
1200
1201 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1202 if (renameFolder) {
1203 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001204 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 mRestoring = true;
1206 }
Winson Chunga12a2502010-12-20 14:41:35 -08001207
Winson Chung785d2eb2011-04-14 16:08:02 -07001208 // Restore the AppsCustomize tab
1209 if (mAppsCustomizeTabHost != null) {
1210 String curTab = savedState.getString("apps_customize_currentTab");
1211 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001212 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001213 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001214 mAppsCustomizeContent.loadAssociatedPages(
1215 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001216 }
1217
Winson Chung5afbf7b2011-07-25 11:53:08 -07001218 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1219 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001221 mItemIdToViewId = (HashMap<Integer, Integer>)
1222 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224
1225 /**
1226 * Finds all the views we need and configure them properly.
1227 */
1228 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001229 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001230
Craig Mautner360310b2012-10-26 15:13:08 -07001231 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001232 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1233 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001234 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001235 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001236
John Spurlock77e1f472013-09-11 10:09:51 -04001237 mLauncherView.setSystemUiVisibility(
1238 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001239 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
Winson Chung4c98d922011-05-31 16:50:48 -07001241 // Setup the drag layer
1242 mDragLayer.setup(this, dragController);
1243
Winson Chung3d503fb2011-07-13 17:25:49 -07001244 // Setup the hotseat
1245 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1246 if (mHotseat != null) {
1247 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001248 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001249 }
1250
Jorim Jaggid017f882014-01-14 17:08:48 -08001251 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001252 View widgetButton = findViewById(R.id.widget_button);
1253 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001254 @Override
1255 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001256 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001257 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001258 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001259 }
1260 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001261 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1262
1263 View wallpaperButton = findViewById(R.id.wallpaper_button);
1264 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001265 @Override
1266 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001267 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001268 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001269 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001270 }
1271 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001272 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1273
1274 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001275 if (hasSettings()) {
1276 settingsButton.setOnClickListener(new OnClickListener() {
1277 @Override
1278 public void onClick(View arg0) {
1279 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001280 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001281 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001282 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001283 });
1284 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1285 } else {
1286 settingsButton.setVisibility(View.GONE);
1287 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1288 lp.gravity = Gravity.END | Gravity.TOP;
1289 widgetButton.requestLayout();
1290 }
1291
Adam Cohendbdff6b2013-09-18 19:09:15 -07001292 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001293
Winson Chung4c98d922011-05-31 16:50:48 -07001294 // Setup the workspace
1295 mWorkspace.setHapticFeedbackEnabled(false);
1296 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001297 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001298 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001299
Winson Chungf0ea4d32011-06-06 14:27:16 -07001300 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001301 mSearchDropTargetBar = (SearchDropTargetBar)
1302 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001303
Winson Chungf0ea4d32011-06-06 14:27:16 -07001304 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001305 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001306 mAppsCustomizeContent = (AppsCustomizePagedView)
1307 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1308 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001309
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001311 dragController.setDragScoller(mWorkspace);
1312 dragController.setScrollView(mDragLayer);
1313 dragController.setMoveTarget(mWorkspace);
1314 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001315 if (mSearchDropTargetBar != null) {
1316 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001317 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001318
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001319 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001320 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001321 mWeightWatcher = new WeightWatcher(this);
1322 mWeightWatcher.setAlpha(0.5f);
1323 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001324 new FrameLayout.LayoutParams(
1325 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001326 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001327 Gravity.BOTTOM)
1328 );
Adam Cohen39a06042013-07-19 14:30:12 -07001329
1330 boolean show = shouldShowWeightWatcher();
1331 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 /**
1336 * Creates a view representing a shortcut.
1337 *
1338 * @param info The data structure describing the shortcut.
1339 *
1340 * @return A View inflated from R.layout.application.
1341 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001342 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001344 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 /**
1348 * Creates a view representing a shortcut inflated from the specified resource.
1349 *
1350 * @param layoutResId The id of the XML layout used to create the shortcut.
1351 * @param parent The group the shortcut belongs to.
1352 * @param info The data structure describing the shortcut.
1353 *
1354 * @return A View inflated from layoutResId.
1355 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001356 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001357 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1358 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 return favorite;
1361 }
1362
1363 /**
1364 * Add an application shortcut to the workspace.
1365 *
1366 * @param data The intent describing the application.
1367 * @param cellInfo The position on screen where to create the shortcut.
1368 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001369 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001371 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001372
Adam Cohenfbba09b2011-07-18 21:43:05 -07001373 // First we check if we already know the exact location where we want to add this item.
1374 if (cellX >= 0 && cellY >= 0) {
1375 cellXY[0] = cellX;
1376 cellXY[1] = cellY;
1377 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001378 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001379 return;
1380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001382 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001383
Romain Guy73b979d2009-06-09 12:57:21 -07001384 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001385 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001387 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001388 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001389 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001390 } else {
1391 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393 }
Romain Guycbb89e42009-06-08 15:52:54 -07001394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 /**
1396 * Add a shortcut to the workspace.
1397 *
1398 * @param data The intent describing the shortcut.
1399 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001401 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 int cellY) {
1403 int[] cellXY = mTmpAddItemCellCoordinates;
1404 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001405 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001406
Michael Jurkad3ef3062010-11-23 16:23:58 -08001407 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001408
Adam Cohen558baaf2011-08-15 15:22:57 -07001409 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001410 if (info == null) {
1411 return;
1412 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001413 final View view = createShortcut(info);
1414
Adam Cohenfbba09b2011-07-18 21:43:05 -07001415 // First we check if we already know the exact location where we want to add this item.
1416 if (cellX >= 0 && cellY >= 0) {
1417 cellXY[0] = cellX;
1418 cellXY[1] = cellY;
1419 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001420
1421 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001422 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001423 true, null,null)) {
1424 return;
1425 }
1426 DragObject dragObject = new DragObject();
1427 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001428 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1429 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001430 return;
1431 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001432 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001433 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001434 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001435 foundCellSpan = (result != null);
1436 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001438 }
1439
1440 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001441 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001442 return;
1443 }
1444
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446
1447 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001448 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001449 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451 }
1452
Adam Cohen2f093b62012-04-30 18:59:53 -07001453 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1454 int minHeight) {
1455 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001456 // We want to account for the extra amount of padding that we are adding to the widget
1457 // to ensure that it gets the full amount of space that it has requested
1458 int requiredWidth = minWidth + padding.left + padding.right;
1459 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001460 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001461 }
1462
Adam Cohen2f093b62012-04-30 18:59:53 -07001463 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1464 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001465 }
1466
Adam Cohen2f093b62012-04-30 18:59:53 -07001467 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1468 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001469 }
1470
Adam Cohen2f093b62012-04-30 18:59:53 -07001471 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1472 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001473 }
1474
Adam Cohen2f093b62012-04-30 18:59:53 -07001475 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1476 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001477 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001478 }
1479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001481 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001483 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001484 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001486 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001487 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1488 if (appWidgetInfo == null) {
1489 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1490 }
Romain Guycbb89e42009-06-08 15:52:54 -07001491
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001492 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001493 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001494
Adam Cohen2f093b62012-04-30 18:59:53 -07001495 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1496 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001499 // We have saved the position to which the widget was dragged-- this really only matters
1500 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001501 int[] cellXY = mTmpAddItemCellCoordinates;
1502 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001503 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1506 cellXY[0] = mPendingAddInfo.cellX;
1507 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001508 spanXY[0] = mPendingAddInfo.spanX;
1509 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001510 foundCellSpan = true;
1511 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001514 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1515 spanXY[1], cellXY, finalSpan);
1516 spanXY[0] = finalSpan[0];
1517 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001520 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001521 }
1522
Michael Jurka0280c3b2010-09-17 15:00:07 -07001523 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001524 if (appWidgetId != -1) {
1525 // Deleting an app widget ID is a void call but writes to disk before returning
1526 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001527 new AsyncTask<Void, Void, Void>() {
1528 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001529 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001530 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001531 }
Michael Jurka43467462013-11-14 12:03:58 +01001532 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001533 }
Winson Chung93eef082012-03-23 15:59:27 -07001534 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001535 return;
1536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001538 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001539 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1540 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 launcherInfo.spanX = spanXY[0];
1542 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001543 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1544 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
1549 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001550 if (hostView == null) {
1551 // Perform actual inflation because we're live
1552 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1553 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1554 } else {
1555 // The AppWidgetHostView has already been inflated and instantiated
1556 launcherInfo.hostView = hostView;
1557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001560 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001561 launcherInfo.notifyWidgetSizeChanged(this);
1562
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001565
1566 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001568 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Adam Cohended9f8d2010-11-03 13:25:16 -07001571 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1572 @Override
1573 public void onReceive(Context context, Intent intent) {
1574 final String action = intent.getAction();
1575 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1576 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001577 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001578 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001579
Winson Chungc100e8e2011-08-09 16:02:43 -07001580 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001581 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001582 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001583 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1586 mUserPresent = true;
1587 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001588 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1589 mModel.resetLoadedState(false, true);
1590 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1591 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1592 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1593 mModel.resetLoadedState(false, true);
1594 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1595 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1596 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 }
1598 }
1599 };
1600
1601 @Override
1602 public void onAttachedToWindow() {
1603 super.onAttachedToWindow();
1604
1605 // Listen for broadcasts related to user-presence
1606 final IntentFilter filter = new IntentFilter();
1607 filter.addAction(Intent.ACTION_SCREEN_OFF);
1608 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001609 if (ENABLE_DEBUG_INTENTS) {
1610 filter.addAction(DebugIntents.DELETE_DATABASE);
1611 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1612 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001614 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001615 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 mVisible = true;
1617 }
1618
1619 @Override
1620 public void onDetachedFromWindow() {
1621 super.onDetachedFromWindow();
1622 mVisible = false;
1623
Adam Cohend113e0c2010-11-11 10:48:05 -08001624 if (mAttached) {
1625 unregisterReceiver(mReceiver);
1626 mAttached = false;
1627 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 updateRunning();
1629 }
1630
1631 public void onWindowVisibilityChanged(int visibility) {
1632 mVisible = visibility == View.VISIBLE;
1633 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001634 // The following code used to be in onResume, but it turns out onResume is called when
1635 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1636 // is a more appropriate event to handle
1637 if (mVisible) {
1638 mAppsCustomizeTabHost.onWindowVisible();
1639 if (!mWorkspaceLoading) {
1640 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001641 // We want to let Launcher draw itself at least once before we force it to build
1642 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001643 // apps is nice and speedy.
1644 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001645 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001646 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001647 if (mStarted) return;
1648 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001649 // We delay the layer building a bit in order to give
1650 // other message processing a time to run. In particular
1651 // this avoids a delay in hiding the IME if it was
1652 // currently shown, because doing that may involve
1653 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001654 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001655 final ViewTreeObserver.OnDrawListener listener = this;
1656 mWorkspace.post(new Runnable() {
1657 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001658 if (mWorkspace != null &&
1659 mWorkspace.getViewTreeObserver() != null) {
1660 mWorkspace.getViewTreeObserver().
1661 removeOnDrawListener(listener);
1662 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001663 }
1664 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001665 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001666 }
1667 });
1668 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001669 // When Launcher comes back to foreground, a different Activity might be responsible for
1670 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001671 if (!DISABLE_MARKET_BUTTON) {
1672 updateAppMarketIcon();
1673 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001674 clearTypedText();
1675 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001676 }
1677
1678 private void sendAdvanceMessage(long delay) {
1679 mHandler.removeMessages(ADVANCE_MSG);
1680 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1681 mHandler.sendMessageDelayed(msg, delay);
1682 mAutoAdvanceSentTime = System.currentTimeMillis();
1683 }
1684
1685 private void updateRunning() {
1686 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1687 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1688 mAutoAdvanceRunning = autoAdvanceRunning;
1689 if (autoAdvanceRunning) {
1690 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1691 sendAdvanceMessage(delay);
1692 } else {
1693 if (!mWidgetsToAdvance.isEmpty()) {
1694 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1695 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1696 }
1697 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001698 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 }
1700 }
1701 }
1702
1703 private final Handler mHandler = new Handler() {
1704 @Override
1705 public void handleMessage(Message msg) {
1706 if (msg.what == ADVANCE_MSG) {
1707 int i = 0;
1708 for (View key: mWidgetsToAdvance.keySet()) {
1709 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1710 final int delay = mAdvanceStagger * i;
1711 if (v instanceof Advanceable) {
1712 postDelayed(new Runnable() {
1713 public void run() {
1714 ((Advanceable) v).advance();
1715 }
1716 }, delay);
1717 }
1718 i++;
1719 }
1720 sendAdvanceMessage(mAdvanceInterval);
1721 }
1722 }
1723 };
1724
1725 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001726 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1728 if (v instanceof Advanceable) {
1729 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001730 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 updateRunning();
1732 }
1733 }
1734
1735 void removeWidgetToAutoAdvance(View hostView) {
1736 if (mWidgetsToAdvance.containsKey(hostView)) {
1737 mWidgetsToAdvance.remove(hostView);
1738 updateRunning();
1739 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001740 }
1741
Joe Onorato9c1289c2009-08-17 11:03:03 -04001742 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 launcherInfo.hostView = null;
1745 }
1746
Winson Chung93eef082012-03-23 15:59:27 -07001747 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1748 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1749 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
1751
Winson Chunga6945242014-01-08 14:04:34 -08001752 public DragLayer getDragLayer() {
1753 return mDragLayer;
1754 }
1755
1756 public Workspace getWorkspace() {
1757 return mWorkspace;
1758 }
1759
1760 public Hotseat getHotseat() {
1761 return mHotseat;
1762 }
1763
Jorim Jaggid017f882014-01-14 17:08:48 -08001764 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001765 return mOverviewPanel;
1766 }
1767
1768 public SearchDropTargetBar getSearchBar() {
1769 return mSearchDropTargetBar;
1770 }
1771
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001772 public LauncherAppWidgetHost getAppWidgetHost() {
1773 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
Romain Guycbb89e42009-06-08 15:52:54 -07001775
Winson Chunga9abd0e2010-10-27 17:18:37 -07001776 public LauncherModel getModel() {
1777 return mModel;
1778 }
1779
Winson Chunga6945242014-01-08 14:04:34 -08001780 public LauncherClings getLauncherClings() {
1781 return mLauncherClings;
1782 }
1783
1784 protected SharedPreferences getSharedPrefs() {
1785 return mSharedPrefs;
1786 }
1787
Bjorn Bringertc459e522013-06-07 19:36:01 +01001788 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001789 getWindow().closeAllPanels();
1790
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001791 // Whatever we were doing is hereby canceled.
1792 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001793 }
1794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 @Override
1796 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001797 long startTime = 0;
1798 if (DEBUG_RESUME_TIME) {
1799 startTime = System.currentTimeMillis();
1800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 super.onNewIntent(intent);
1802
1803 // Close the menu
1804 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001805 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001806 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001807
Adam Cohened307df2013-10-02 09:37:31 -07001808 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1809 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1810 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001811
Adam Cohen6fecd412013-10-02 17:41:50 -07001812 if (mWorkspace == null) {
1813 // Can be cases where mWorkspace is null, this prevents a NPE
1814 return;
1815 }
1816 Folder openFolder = mWorkspace.getOpenFolder();
1817 // In all these cases, only animate if we're already on home
1818 mWorkspace.exitWidgetResizeMode();
1819 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001820 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001821 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001822 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001823
Adam Cohen6fecd412013-10-02 17:41:50 -07001824 closeFolder();
1825 exitSpringLoadedDragMode();
1826
1827 // If we are already on home, then just animate back to the workspace,
1828 // otherwise, just wait until onResume to set the state back to Workspace
1829 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001830 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001831 } else {
1832 mOnResumeState = State.WORKSPACE;
1833 }
1834
1835 final View v = getWindow().peekDecorView();
1836 if (v != null && v.getWindowToken() != null) {
1837 InputMethodManager imm = (InputMethodManager)getSystemService(
1838 INPUT_METHOD_SERVICE);
1839 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1840 }
1841
1842 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001843 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001844 mAppsCustomizeTabHost.reset();
1845 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001846
1847 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Adam Cohened307df2013-10-02 09:37:31 -07001849
Michael Jurka447bf842013-05-15 14:52:15 +02001850 if (DEBUG_RESUME_TIME) {
1851 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
1854
Adam Coppa120b8e2013-11-12 16:26:04 +00001855 /**
1856 * Override point for subclasses to prevent movement to the default screen when the home
1857 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1858 */
1859 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1860 return true;
1861 }
1862
1863 /**
1864 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1865 */
1866 protected void onHomeIntent() {
1867 // Do nothing
1868 }
1869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001871 public void onRestoreInstanceState(Bundle state) {
1872 super.onRestoreInstanceState(state);
1873 for (int page: mSynchronouslyBoundPages) {
1874 mWorkspace.restoreInstanceStateForChild(page);
1875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 }
1877
1878 @Override
1879 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001880 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001881 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1882 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001883 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001884 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885
Michael Jurka883f55b2010-10-21 15:47:14 -07001886 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001887 // We close any open folder since it will not be re-opened, and we need to make sure
1888 // this state is reflected.
1889 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 mWaitingForResult) {
1893 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001894 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001895 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1896 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001897 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1898 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1899 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001900 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902
1903 if (mFolderInfo != null && mWaitingForResult) {
1904 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1905 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1906 }
Michael Jurka946ad472010-07-09 18:05:18 -07001907
Winson Chung785d2eb2011-04-14 16:08:02 -07001908 // Save the current AppsCustomize tab
1909 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001910 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1911 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001912 if (currentTabTag != null) {
1913 outState.putString("apps_customize_currentTab", currentTabTag);
1914 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001915 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1916 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001917 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001918 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920
1921 @Override
1922 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001924
Winson Chunge7a03942011-08-05 15:05:12 -07001925 // Remove all pending runnables
1926 mHandler.removeMessages(ADVANCE_MSG);
1927 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001928 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001929
Winson Chungcd2b0142011-06-08 16:02:26 -07001930 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001931 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001932 mModel.stopLoader();
1933 app.setLauncher(null);
1934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001936 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001938 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001940 mAppWidgetHost = null;
1941
1942 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943
1944 TextKeyListener.getInstance().release();
1945
Winson Chung81b52252012-08-27 15:34:29 -07001946 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1947 // to prevent leaking Launcher activities on orientation change.
1948 if (mModel != null) {
1949 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1950 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001951
1952 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001953 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001954
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001955 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001956 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001957 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001958 mWorkspace = null;
1959 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001960
Michael Jurka2ecf9952012-06-18 12:52:28 -07001961 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
Adam Cohena9cf38f2011-05-02 15:36:58 -07001964 public DragController getDragController() {
1965 return mDragController;
1966 }
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 @Override
1969 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001970 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 super.startActivityForResult(intent, requestCode);
1972 }
1973
Winson Chung70d72102011-08-12 11:24:35 -07001974 /**
1975 * Indicates that we want global search for this activity by setting the globalSearch
1976 * argument for {@link #startSearch} to true.
1977 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001979 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001981
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001982 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001983
Karl Rosaen138a0412009-04-23 19:00:21 -07001984 if (initialQuery == null) {
1985 // Use any text typed in the launcher as the initial query
1986 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 if (appSearchData == null) {
1989 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001990 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
Winson Chungadf0c182012-08-23 14:59:07 -07001992 Rect sourceBounds = new Rect();
1993 if (mSearchDropTargetBar != null) {
1994 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1995 }
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Bjorn Bringertc459e522013-06-07 19:36:01 +01001997 startSearch(initialQuery, selectInitialQuery,
1998 appSearchData, sourceBounds);
1999 }
2000
2001 public void startSearch(String initialQuery,
2002 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002003 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002004 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002005 }
2006
2007 /**
2008 * Starts the global search activity. This code is a copied from SearchManager
2009 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002010 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002011 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002012 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002013 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2014 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2015 if (globalSearchActivity == null) {
2016 Log.w(TAG, "No global search activity found.");
2017 return;
2018 }
2019 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2020 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2021 intent.setComponent(globalSearchActivity);
2022 // Make sure that we have a Bundle to put source in
2023 if (appSearchData == null) {
2024 appSearchData = new Bundle();
2025 } else {
2026 appSearchData = new Bundle(appSearchData);
2027 }
2028 // Set source to package name of app that starts global search, if not set already.
2029 if (!appSearchData.containsKey("source")) {
2030 appSearchData.putString("source", getPackageName());
2031 }
2032 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2033 if (!TextUtils.isEmpty(initialQuery)) {
2034 intent.putExtra(SearchManager.QUERY, initialQuery);
2035 }
2036 if (selectInitialQuery) {
2037 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2038 }
2039 intent.setSourceBounds(sourceBounds);
2040 try {
2041 startActivity(intent);
2042 } catch (ActivityNotFoundException ex) {
2043 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
Yura4f93ec62014-02-04 14:15:21 +00002047 public boolean isOnCustomContent() {
2048 return mWorkspace.isOnOrMovingToCustomContent();
2049 }
2050
Winson Chung70d72102011-08-12 11:24:35 -07002051 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002052 public boolean onPrepareOptionsMenu(Menu menu) {
2053 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002054 if (!isOnCustomContent()) {
2055 // Close any open folders
2056 closeFolder();
2057 // Stop resizing any widgets
2058 mWorkspace.exitWidgetResizeMode();
2059 if (!mWorkspace.isInOverviewMode()) {
2060 // Show the overview mode
2061 showOverviewMode(true);
2062 } else {
2063 showWorkspace(true);
2064 }
Winson Chunge0298742014-01-17 12:03:00 -08002065 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002066 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002069 @Override
2070 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002071 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002072 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002073 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002074 }
2075
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 public boolean isWorkspaceLocked() {
2077 return mWorkspaceLoading || mWaitingForResult;
2078 }
2079
Adam Cohen517a7f52014-03-01 12:12:59 -08002080 public boolean isWorkspaceLoading() {
2081 return mWorkspaceLoading;
2082 }
2083
Michael Jurka0280c3b2010-09-17 15:00:07 -07002084 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002085 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002086 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002087 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2088 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002089 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002092
Adam Cohenad4e15c2013-10-17 16:21:35 -07002093 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2094 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2095 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2096 }
2097
2098 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2099 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2100 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002101 if (appWidgetInfo.configure != null) {
2102 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002103 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002104
Bjorn Bringert7984c942009-12-09 15:38:25 +00002105 // Launch over to configure widget, if needed
2106 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002107 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002108 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002109 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002111 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002112 Runnable onComplete = new Runnable() {
2113 @Override
2114 public void run() {
2115 // Exit spring loaded mode if necessary after adding the widget
2116 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2117 null);
2118 }
2119 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002120 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002121 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002122 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124 }
Romain Guycbb89e42009-06-08 15:52:54 -07002125
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002126 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002127 // Close any folders that may be open.
2128 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002129 mWorkspace.moveToCustomContentScreen(animate);
2130 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002131 /**
2132 * Process a shortcut drop.
2133 *
2134 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002136 * @param cell The cell it should be added to, optional
2137 * @param position The location on the screen where it was dropped, optional
2138 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002141 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002142 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002143 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002144 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002145
2146 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002147 mPendingAddInfo.cellX = cell[0];
2148 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002149 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002150
2151 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2152 createShortcutIntent.setComponent(componentName);
2153 processShortcut(createShortcutIntent);
2154 }
2155
Adam Cohenfbba09b2011-07-18 21:43:05 -07002156 /**
2157 * Process a widget drop.
2158 *
2159 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002160 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002161 * @param cell The cell it should be added to, optional
2162 * @param position The location on the screen where it was dropped, optional
2163 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002164 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002165 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002166 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002167 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002168 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002169 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002170 mPendingAddInfo.minSpanX = info.minSpanX;
2171 mPendingAddInfo.minSpanY = info.minSpanY;
2172
Adam Cohenfbba09b2011-07-18 21:43:05 -07002173 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002174 mPendingAddInfo.cellX = cell[0];
2175 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002176 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002177 if (span != null) {
2178 mPendingAddInfo.spanX = span[0];
2179 mPendingAddInfo.spanY = span[1];
2180 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002181
Adam Cohened66b2b2012-01-23 17:28:51 -08002182 AppWidgetHostView hostView = info.boundWidget;
2183 int appWidgetId;
2184 if (hostView != null) {
2185 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002186 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002187 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002188 // In this case, we either need to start an activity to get permission to bind
2189 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002190 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002191 Bundle options = info.bindOptions;
2192
2193 boolean success = false;
2194 if (options != null) {
2195 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2196 info.componentName, options);
2197 } else {
2198 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2199 info.componentName);
2200 }
2201 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002202 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002203 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002204 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002205 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2206 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2207 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002208 // TODO: we need to make sure that this accounts for the options bundle.
2209 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002210 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2211 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002212 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002213 }
2214
Joe Onoratodeb98af2010-02-19 14:59:39 -08002215 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002216 // Handle case where user selected "Applications"
2217 String applicationName = getResources().getString(R.string.group_applications);
2218 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002219
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002220 if (applicationName != null && applicationName.equals(shortcutName)) {
2221 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2222 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002223
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002224 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2225 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002226 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002227 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002228 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002229 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 }
2232
Winson Chung24ab2f12010-09-16 14:10:47 -07002233 void processWallpaper(Intent intent) {
2234 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2235 }
2236
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002239 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 folderInfo.title = getText(R.string.folder_name);
2241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002245 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246
2247 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 FolderIcon newFolder =
2249 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002252 // Force measure the new folder icon
2253 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2254 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002255 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 }
Romain Guycbb89e42009-06-08 15:52:54 -07002257
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002259 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 }
2261
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002262 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002263 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002264 }
2265
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002266 /**
2267 * Registers various content observers. The current implementation registers
2268 * only a favorites observer to keep track of the favorites applications.
2269 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002270 private void registerContentObservers() {
2271 ContentResolver resolver = getContentResolver();
2272 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2273 true, mWidgetObserver);
2274 }
2275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 @Override
2277 public boolean dispatchKeyEvent(KeyEvent event) {
2278 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2279 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002281 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002282 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002283 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002284 dumpState();
2285 return true;
2286 }
2287 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002288 }
2289 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2290 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002291 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 return true;
2293 }
2294 }
2295
2296 return super.dispatchKeyEvent(event);
2297 }
2298
Joe Onorato88ec0992009-11-19 13:16:06 -08002299 @Override
2300 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002301 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002302 if (mAppsCustomizeContent.getContentType() ==
2303 AppsCustomizePagedView.ContentType.Applications) {
2304 showWorkspace(true);
2305 } else {
2306 showOverviewMode(true);
2307 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002308 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002309 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002310 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002311 Folder openFolder = mWorkspace.getOpenFolder();
2312 if (openFolder.isEditingName()) {
2313 openFolder.dismissEditingName();
2314 } else {
2315 closeFolder();
2316 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002317 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002318 mWorkspace.exitWidgetResizeMode();
2319
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002320 // Back button is a no-op here, but give at least some feedback for the button press
2321 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002322 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002323 }
2324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002326 * Re-listen when widgets are reset.
2327 */
2328 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002329 if (mAppWidgetHost != null) {
2330 mAppWidgetHost.startListening();
2331 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002332 }
2333
2334 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 * Launches the intent referred by the clicked shortcut.
2336 *
2337 * @param v The view representing the clicked shortcut.
2338 */
2339 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002340 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2341 // view has detached (it's possible for this to happen if the view is removed mid touch).
2342 if (v.getWindowToken() == null) {
2343 return;
2344 }
2345
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002346 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002347 return;
2348 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002349
Adam Cohen1697b792013-09-17 19:08:21 -07002350 if (v instanceof Workspace) {
2351 if (mWorkspace.isInOverviewMode()) {
2352 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002353 }
2354 return;
2355 }
2356
2357 if (v instanceof CellLayout) {
2358 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002359 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002360 }
2361 }
2362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002364 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002365 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002367 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002368 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002369 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002370 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002371 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 }
2373 }
2374
Michael Jurka0e260592010-06-30 17:07:39 -07002375 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002376 return false;
2377 }
2378
Michael Jurkaaf442092010-06-10 17:01:57 -07002379 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002380 * Event handler for the search button
2381 *
2382 * @param v The view that was clicked.
2383 */
2384 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002385 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2386
Amith Yamasania135ba82011-08-09 17:42:01 -07002387 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002388 }
2389
2390 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002391 * Event handler for the voice button
2392 *
2393 * @param v The view that was clicked.
2394 */
2395 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002396 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002397
Bjorn Bringertc459e522013-06-07 19:36:01 +01002398 startVoice();
2399 }
2400
2401 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002402 try {
2403 final SearchManager searchManager =
2404 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2405 ComponentName activityName = searchManager.getGlobalSearchActivity();
2406 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002407 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002408 if (activityName != null) {
2409 intent.setPackage(activityName.getPackageName());
2410 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002411 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002412 } catch (ActivityNotFoundException e) {
2413 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002414 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002415 startActivitySafely(null, intent, "onClickVoiceButton");
2416 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002417 }
2418
2419 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002420 * Event handler for the "grid" button that appears on the home screen, which
2421 * enters all apps mode.
2422 *
2423 * @param v The view that was clicked.
2424 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002425 protected void onClickAllAppsButton(View v) {
2426 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2427 if (isAllAppsVisible()) {
2428 showWorkspace(true);
2429 } else {
2430 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2431 }
2432 }
2433
2434 /**
2435 * Event handler for an app shortcut click.
2436 *
2437 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2438 */
2439 protected void onClickAppShortcut(View v) {
2440 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2441 Object tag = v.getTag();
2442 if (!(tag instanceof ShortcutInfo)) {
2443 throw new IllegalArgumentException("Input must be a Shortcut");
2444 }
2445
2446 // Open shortcut
2447 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2448 final Intent intent = shortcut.intent;
2449
2450 // Check for special shortcuts
2451 if (intent.getComponent() != null) {
2452 final String shortcutClass = intent.getComponent().getClassName();
2453
2454 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2455 MemoryDumpActivity.startDump(this);
2456 return;
2457 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2458 toggleShowWeightWatcher();
2459 return;
2460 }
2461 }
2462
2463 // Start activities
2464 int[] pos = new int[2];
2465 v.getLocationOnScreen(pos);
2466 intent.setSourceBounds(new Rect(pos[0], pos[1],
2467 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2468
2469 boolean success = startActivitySafely(v, intent, tag);
2470
2471 mStats.recordLaunch(intent, shortcut);
2472
2473 if (success && v instanceof BubbleTextView) {
2474 mWaitingForResume = (BubbleTextView) v;
2475 mWaitingForResume.setStayPressed(true);
2476 }
2477 }
2478
2479 /**
2480 * Event handler for a folder icon click.
2481 *
2482 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2483 */
2484 protected void onClickFolderIcon(View v) {
2485 if (LOGD) Log.d(TAG, "onClickFolder");
2486 if (!(v instanceof FolderIcon)){
2487 throw new IllegalArgumentException("Input must be a FolderIcon");
2488 }
2489
2490 FolderIcon folderIcon = (FolderIcon) v;
2491 final FolderInfo info = folderIcon.getFolderInfo();
2492 Folder openFolder = mWorkspace.getFolderForTag(info);
2493
2494 // If the folder info reports that the associated folder is open, then verify that
2495 // it is actually opened. There have been a few instances where this gets out of sync.
2496 if (info.opened && openFolder == null) {
2497 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2498 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2499 info.opened = false;
2500 }
2501
2502 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2503 // Close any open folder
2504 closeFolder();
2505 // Open the requested folder
2506 openFolder(folderIcon);
2507 } else {
2508 // Find the open folder...
2509 int folderScreen;
2510 if (openFolder != null) {
2511 folderScreen = mWorkspace.getPageForView(openFolder);
2512 // .. and close it
2513 closeFolder(openFolder);
2514 if (folderScreen != mWorkspace.getCurrentPage()) {
2515 // Close any folder open on the current screen
2516 closeFolder();
2517 // Pull the folder onto this screen
2518 openFolder(folderIcon);
2519 }
2520 }
2521 }
Michael Jurka5130e402011-10-13 04:55:35 -07002522 }
2523
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002524 /**
2525 * Event handler for the (Add) Widgets button that appears after a long press
2526 * on the home screen.
2527 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002528 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002529 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002530 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2531 }
2532
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 /**
2534 * Event handler for the wallpaper picker button that appears after a long press
2535 * on the home screen.
2536 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002537 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2539 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2540 pickWallpaper.setComponent(getWallpaperPickerComponent());
2541 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2542 }
2543
2544 /**
2545 * Event handler for a click on the settings button that appears after a long press
2546 * on the home screen.
2547 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002548 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002549 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2550 }
2551
Michael Jurka5130e402011-10-13 04:55:35 -07002552 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002553 // Provide the same haptic feedback that the system offers for virtual keys.
2554 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002555 }
2556
Adam Cohen61f560d2013-09-30 15:58:20 -07002557 public void performHapticFeedbackOnTouchDown(View v) {
2558 // Provide the same haptic feedback that the system offers for virtual keys.
2559 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2560 }
2561
2562 public View.OnTouchListener getHapticFeedbackTouchListener() {
2563 if (mHapticFeedbackTouchListener == null) {
2564 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2565 @Override
2566 public boolean onTouch(View v, MotionEvent event) {
2567 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2568 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2569 }
2570 return false;
2571 }
2572 };
2573 }
2574 return mHapticFeedbackTouchListener;
2575 }
2576
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002577 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002578 if (!DISABLE_MARKET_BUTTON) {
2579 if (mAppMarketIntent != null) {
2580 startActivitySafely(v, mAppMarketIntent, "app market");
2581 } else {
2582 Log.e(TAG, "Invalid app market intent.");
2583 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002584 }
2585 }
2586
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002587 /**
2588 * Called when the user stops interacting with the launcher.
2589 * This implies that the user is now on the homescreen and is not doing housekeeping.
2590 */
2591 protected void onInteractionEnd() {}
2592
2593 /**
2594 * Called when the user starts interacting with the launcher.
2595 * The possible interactions are:
2596 * - open all apps
2597 * - reorder an app shortcut, or a widget
2598 * - open the overview mode.
2599 * This is a good time to stop doing things that only make sense
2600 * when the user is on the homescreen and not doing housekeeping.
2601 */
2602 protected void onInteractionBegin() {}
2603
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002604 void startApplicationDetailsActivity(ComponentName componentName) {
2605 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002606 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2607 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002608 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2609 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002610 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002611 }
2612
Michael Jurka1e2f4652013-07-08 18:03:46 -07002613 // returns true if the activity was started
2614 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002615 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002616 // System applications cannot be installed. For now, show a toast explaining that.
2617 // We may give them the option of disabling apps this way.
2618 int messageId = R.string.uninstall_system_app_text;
2619 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002620 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002621 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002622 String packageName = componentName.getPackageName();
2623 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002624 Intent intent = new Intent(
2625 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002626 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2627 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002628 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002629 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002630 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002631 }
2632
Michael Jurka86a720e2012-05-09 11:23:23 -07002633 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002637 // Only launch using the new animation if the shortcut has not opted out (this is a
2638 // private contract between launcher and may be ignored in the future).
2639 boolean useLaunchAnimation = (v != null) &&
2640 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2641 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002642 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2643 v.getMeasuredWidth(), v.getMeasuredHeight());
2644
2645 startActivity(intent, opts.toBundle());
2646 } else {
2647 startActivity(intent);
2648 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002649 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 } catch (SecurityException e) {
2651 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002652 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002654 "or use the exported attribute for this activity. "
2655 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002657 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002659
Michael Jurka86a720e2012-05-09 11:23:23 -07002660 boolean startActivitySafely(View v, Intent intent, Object tag) {
2661 boolean success = false;
2662 try {
2663 success = startActivity(v, intent, tag);
2664 } catch (ActivityNotFoundException e) {
2665 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2666 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2667 }
2668 return success;
2669 }
2670
Adam Cohen268c4752012-06-06 17:47:33 -07002671 /**
2672 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2673 * in the DragLayer in the exact absolute location of the original FolderIcon.
2674 */
2675 private void copyFolderIconToImage(FolderIcon fi) {
2676 final int width = fi.getMeasuredWidth();
2677 final int height = fi.getMeasuredHeight();
2678
2679 // Lazy load ImageView, Bitmap and Canvas
2680 if (mFolderIconImageView == null) {
2681 mFolderIconImageView = new ImageView(this);
2682 }
2683 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2684 mFolderIconBitmap.getHeight() != height) {
2685 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2686 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2687 }
2688
2689 DragLayer.LayoutParams lp;
2690 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2691 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2692 } else {
2693 lp = new DragLayer.LayoutParams(width, height);
2694 }
2695
Adam Cohen307fe232012-08-16 17:55:58 -07002696 // The layout from which the folder is being opened may be scaled, adjust the starting
2697 // view size by this scale factor.
2698 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002699 lp.customPosition = true;
2700 lp.x = mRectForFolderAnimation.left;
2701 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002702 lp.width = (int) (scale * width);
2703 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002704
2705 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2706 fi.draw(mFolderIconCanvas);
2707 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002708 if (fi.getFolder() != null) {
2709 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2710 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002711 }
Adam Cohen268c4752012-06-06 17:47:33 -07002712 // Just in case this image view is still in the drag layer from a previous animation,
2713 // we remove it and re-add it.
2714 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2715 mDragLayer.removeView(mFolderIconImageView);
2716 }
2717 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002718 if (fi.getFolder() != null) {
2719 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002720 }
Adam Cohen268c4752012-06-06 17:47:33 -07002721 }
2722
Adam Cohen2801caf2011-05-13 20:57:39 -07002723 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002724 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002725 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2726 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2727 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2728
Adam Cohenc51934b2011-07-26 21:07:43 -07002729 FolderInfo info = (FolderInfo) fi.getTag();
2730 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2731 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002732 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2733 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002734 }
2735
Adam Cohen268c4752012-06-06 17:47:33 -07002736 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2737 copyFolderIconToImage(fi);
2738 fi.setVisibility(View.INVISIBLE);
2739
Michael Jurka2ecf9952012-06-18 12:52:28 -07002740 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002741 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002742 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2743 oa.start();
2744 }
2745
Adam Cohen268c4752012-06-06 17:47:33 -07002746 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002747 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002748 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2749 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2750 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2751
Adam Cohen268c4752012-06-06 17:47:33 -07002752 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002753
Adam Cohen268c4752012-06-06 17:47:33 -07002754 // We remove and re-draw the FolderIcon in-case it has changed
2755 mDragLayer.removeView(mFolderIconImageView);
2756 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002757 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002758 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002759 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002760 oa.addListener(new AnimatorListenerAdapter() {
2761 @Override
2762 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002763 if (cl != null) {
2764 cl.clearFolderLeaveBehind();
2765 // Remove the ImageView copy of the FolderIcon and make the original visible.
2766 mDragLayer.removeView(mFolderIconImageView);
2767 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002768 }
2769 }
2770 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002771 oa.start();
2772 }
2773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002775 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 * is animated relative to the specified View. If the View is null, no animation
2777 * is played.
2778 *
2779 * @param folderInfo The FolderInfo describing the folder to open.
2780 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002781 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002782 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002783 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784
Adam Cohena9cf38f2011-05-02 15:36:58 -07002785 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786
Adam Cohen4554ee12011-08-03 16:13:21 -07002787 // Just verify that the folder hasn't already been added to the DragLayer.
2788 // There was a one-off crash where the folder had a parent already.
2789 if (folder.getParent() == null) {
2790 mDragLayer.addView(folder);
2791 mDragController.addDropTarget((DropTarget) folder);
2792 } else {
2793 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2794 folder.getParent() + ").");
2795 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002796 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002797 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002798
2799 // Notify the accessibility manager that this folder "window" has appeared and occluded
2800 // the workspace items
2801 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2802 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002803 }
2804
2805 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002806 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002807 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002808 if (folder.isEditingName()) {
2809 folder.dismissEditingName();
2810 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002811 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002812
2813 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002814 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002815 }
2816 }
2817
2818 void closeFolder(Folder folder) {
2819 folder.getInfo().opened = false;
2820
2821 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2822 if (parent != null) {
2823 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2824 shrinkAndFadeInFolderIcon(fi);
2825 }
2826 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002827
2828 // Notify the accessibility manager that this folder "window" has disappeard and no
2829 // longer occludeds the workspace items
2830 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831 }
2832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002834 if (!isDraggingEnabled()) return false;
2835 if (isWorkspaceLocked()) return false;
2836 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837
Adam Cohen1697b792013-09-17 19:08:21 -07002838 if (v instanceof Workspace) {
2839 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002840 if (mWorkspace.enterOverviewMode()) {
2841 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2842 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2843 return true;
2844 } else {
2845 return false;
2846 }
Adam Cohen1697b792013-09-17 19:08:21 -07002847 }
Adam Cohen1697b792013-09-17 19:08:21 -07002848 }
2849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002851 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 }
2853
Michael Jurka0280c3b2010-09-17 15:00:07 -07002854 resetAddInfo();
2855 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002857 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 return true;
2859 }
2860
Winson Chung3d503fb2011-07-13 17:25:49 -07002861 // The hotseat touch handling does not go through Workspace, and we always allow long press
2862 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002863 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002864 final boolean inHotseat = isHotseatLayout(v);
2865 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002866 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002867 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002868 // User long pressed on empty space
2869 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2870 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2871 // Disabling reordering until we sort out some issues.
2872 if (mWorkspace.isInOverviewMode()) {
2873 mWorkspace.startReordering(v);
2874 } else {
2875 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002878 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2879 mHotseat.getOrderInHotseat(
2880 longClickCellInfo.cellX,
2881 longClickCellInfo.cellY));
2882 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002884 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885 }
2886 }
2887 }
2888 return true;
2889 }
2890
Winson Chung3d503fb2011-07-13 17:25:49 -07002891 boolean isHotseatLayout(View layout) {
2892 return mHotseat != null && layout != null &&
2893 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2894 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002895
Winson Chung3d503fb2011-07-13 17:25:49 -07002896 /**
2897 * Returns the CellLayout of the specified container at the specified screen.
2898 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002899 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002900 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2901 if (mHotseat != null) {
2902 return mHotseat.getLayout();
2903 } else {
2904 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002905 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002906 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002907 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002908 }
2909 }
2910
Daniel Sandler843e8602010-06-07 14:59:01 -04002911 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002912 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002913 }
2914
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002915 /**
2916 * Helper method for the cameraZoomIn/cameraZoomOut animations
2917 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002918 * @param scaleFactor The scale factor used for the zoom
2919 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002920 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002921 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002922 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002923 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002924
Craig Mautner360310b2012-10-26 15:13:08 -07002925 private void setWorkspaceBackground(boolean workspace) {
2926 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002927 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002928 }
2929
Jorim Jaggib45b65e2014-02-27 20:33:20 +01002930 void updateWallpaperVisibility(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002931 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2932 int curflags = getWindow().getAttributes().flags
2933 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2934 if (wpflags != curflags) {
2935 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2936 }
Craig Mautner360310b2012-10-26 15:13:08 -07002937 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002938 }
2939
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002940 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2941 if (v instanceof LauncherTransitionable) {
2942 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2943 }
2944 }
2945
Michael Jurkabed61d22012-02-14 22:51:29 -08002946 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2947 if (v instanceof LauncherTransitionable) {
2948 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2949 }
Winson Chung70442722012-02-10 15:43:22 -08002950
2951 // Update the workspace transition step as well
2952 dispatchOnLauncherTransitionStep(v, 0f);
2953 }
2954
2955 private void dispatchOnLauncherTransitionStep(View v, float t) {
2956 if (v instanceof LauncherTransitionable) {
2957 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2958 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002959 }
2960
2961 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2962 if (v instanceof LauncherTransitionable) {
2963 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2964 }
Winson Chung70442722012-02-10 15:43:22 -08002965
2966 // Update the workspace transition step as well
2967 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002968 }
2969
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002970 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002971 * Things to test when changing the following seven functions.
2972 * - Home from workspace
2973 * - from center screen
2974 * - from other screens
2975 * - Home from all apps
2976 * - from center screen
2977 * - from other screens
2978 * - Back from all apps
2979 * - from center screen
2980 * - from other screens
2981 * - Launch app from workspace and quit
2982 * - with back
2983 * - with home
2984 * - Launch app from all apps and quit
2985 * - with back
2986 * - with home
2987 * - Go to a screen that's not the default, then all
2988 * apps, and launch and app, and go back
2989 * - with back
2990 * -with home
2991 * - On workspace, long press power and go back
2992 * - with back
2993 * - with home
2994 * - On all apps, long press power and go back
2995 * - with back
2996 * - with home
2997 * - On workspace, power off
2998 * - On all apps, power off
2999 * - Launch an app and turn off the screen while in that app
3000 * - Go back with home key
3001 * - Go back with back key TODO: make this not go to workspace
3002 * - From all apps
3003 * - From workspace
3004 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3005 * - From all apps
3006 * - From the center workspace
3007 * - From another workspace
3008 */
3009
3010 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003011 * Zoom the camera out from the workspace to reveal 'toView'.
3012 * Assumes that the view to show is anchored at either the very top or very bottom
3013 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003014 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003015 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003016 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3017 showAppsCustomizeHelper(animated, springLoaded, contentType);
3018 }
3019 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3020 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003021 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003022 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003023 mStateAnimation.cancel();
3024 mStateAnimation = null;
3025 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003026 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003027
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3029 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3030 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003031 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003032 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003033 final int startDelay =
3034 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003035
Michael Jurkab3e22d92011-10-31 15:58:33 -07003036 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003037
Michael Jurkad74c9842011-07-10 12:44:21 -07003038 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003039 Animator workspaceAnim =
3040 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003041 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003042 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3043 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003044 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3045 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003046
3047 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003048 toView.setScaleX(scale);
3049 toView.setScaleY(scale);
3050 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3051 scaleAnim.
3052 scaleX(1f).scaleY(1f).
3053 setDuration(duration).
3054 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003055
Winson Chungf0ea4d32011-06-06 14:27:16 -07003056 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003057 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003058 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003059 .ofFloat(toView, "alpha", 0f, 1f)
3060 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003061 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003062 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3063 @Override
3064 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003065 if (animation == null) {
3066 throw new RuntimeException("animation is null");
3067 }
Winson Chung70442722012-02-10 15:43:22 -08003068 float t = (Float) animation.getAnimatedValue();
3069 dispatchOnLauncherTransitionStep(fromView, t);
3070 dispatchOnLauncherTransitionStep(toView, t);
3071 }
3072 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003073
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003074 // toView should appear right at the end of the workspace shrink
3075 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003076 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003077 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003078 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003079
3080 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003081 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003082 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003083 // Prepare the position
3084 toView.setTranslationX(0.0f);
3085 toView.setTranslationY(0.0f);
3086 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003087 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003088 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003089 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003090 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003091 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3092 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003093
Winson Chungc7d2b602012-05-16 17:02:20 -07003094 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003095 if (mSearchDropTargetBar != null) {
3096 mSearchDropTargetBar.hideSearchBar(false);
3097 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003098 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003099 });
3100
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003101 if (workspaceAnim != null) {
3102 mStateAnimation.play(workspaceAnim);
3103 }
Michael Jurka1899a362011-11-03 13:50:45 -07003104
3105 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003106
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003107 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3108 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003109
3110 // If any of the objects being animated haven't been measured/laid out
3111 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003112 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003113 (mWorkspace.getMeasuredWidth() == 0) ||
3114 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003115 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003116 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003117
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003118 final AnimatorSet stateAnimation = mStateAnimation;
3119 final Runnable startAnimRunnable = new Runnable() {
3120 public void run() {
3121 // Check that mStateAnimation hasn't changed while
3122 // we waited for a layout/draw pass
3123 if (mStateAnimation != stateAnimation)
3124 return;
3125 setPivotsForZoom(toView, scale);
3126 dispatchOnLauncherTransitionStart(fromView, animated, false);
3127 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003128 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003129 }
3130 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003131 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003132 final ViewTreeObserver observer = toView.getViewTreeObserver();
3133 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3134 public void onGlobalLayout() {
3135 startAnimRunnable.run();
3136 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3137 }
3138 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003139 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003140 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003141 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003142 } else {
3143 toView.setTranslationX(0.0f);
3144 toView.setTranslationY(0.0f);
3145 toView.setScaleX(1.0f);
3146 toView.setScaleY(1.0f);
3147 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003149
Daniel Sandlere4f98912013-06-25 15:13:26 -04003150 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003151 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003152 if (mSearchDropTargetBar != null) {
3153 mSearchDropTargetBar.hideSearchBar(false);
3154 }
Michael Jurkaabded662011-03-04 12:06:57 -08003155 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003156 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003157 dispatchOnLauncherTransitionStart(fromView, animated, false);
3158 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003159 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003160 dispatchOnLauncherTransitionStart(toView, animated, false);
3161 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003162 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003163 }
3164
3165 /**
3166 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003167 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003168 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003169 */
Adam Cohened307df2013-10-02 09:37:31 -07003170 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003171 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003172
Michael Jurkab3e22d92011-10-31 15:58:33 -07003173 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003174 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003175 mStateAnimation.cancel();
3176 mStateAnimation = null;
3177 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003178 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003179
Winson Chungf0ea4d32011-06-06 14:27:16 -07003180 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003181 final int fadeOutDuration =
3182 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003183 final float scaleFactor = (float)
3184 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3185 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003186 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003187 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003188 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003189 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3190 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003191 toState, animated, stagger, -1);
3192 } else if (toState == Workspace.State.SPRING_LOADED ||
3193 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003194 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003195 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003196 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003197
Michael Jurkab3e22d92011-10-31 15:58:33 -07003198 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003199 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003200 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003201 final LauncherViewPropertyAnimator scaleAnim =
3202 new LauncherViewPropertyAnimator(fromView);
3203 scaleAnim.
3204 scaleX(scaleFactor).scaleY(scaleFactor).
3205 setDuration(duration).
3206 setInterpolator(new Workspace.ZoomInInterpolator());
3207
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003208 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003209 .ofFloat(fromView, "alpha", 1f, 0f)
3210 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003211 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003212 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3213 @Override
3214 public void onAnimationUpdate(ValueAnimator animation) {
3215 float t = 1f - (Float) animation.getAnimatedValue();
3216 dispatchOnLauncherTransitionStep(fromView, t);
3217 dispatchOnLauncherTransitionStep(toView, t);
3218 }
3219 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003220
Michael Jurka2ecf9952012-06-18 12:52:28 -07003221 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003222
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003223 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3224 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003225 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003226
3227 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003228 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003229 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003230 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003231 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3232 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003233 if (onCompleteRunnable != null) {
3234 onCompleteRunnable.run();
3235 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003236 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003237 }
3238 });
3239
Winson Chungf0ea4d32011-06-06 14:27:16 -07003240 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003241 if (workspaceAnim != null) {
3242 mStateAnimation.play(workspaceAnim);
3243 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003244 dispatchOnLauncherTransitionStart(fromView, animated, true);
3245 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003246 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003247 } else {
3248 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003249 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003250 dispatchOnLauncherTransitionStart(fromView, animated, true);
3251 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003252 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003253 dispatchOnLauncherTransitionStart(toView, animated, true);
3254 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003255 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003256 }
3257
Michael Jurkae326f182011-11-21 14:05:46 -08003258 @Override
3259 public void onTrimMemory(int level) {
3260 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003261 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003262 mAppsCustomizeTabHost.onTrimMemory();
3263 }
3264 }
3265
Adam Cohened307df2013-10-02 09:37:31 -07003266 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003267 showWorkspace(animated, null);
3268 }
3269
Adam Cohened307df2013-10-02 09:37:31 -07003270 protected void showWorkspace() {
3271 showWorkspace(true);
3272 }
3273
Adam Cohened66b2b2012-01-23 17:28:51 -08003274 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003275 if (mWorkspace.isInOverviewMode()) {
3276 mWorkspace.exitOverviewMode(animated);
3277 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003278 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003279 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003280 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003281 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003282
Winson Chungc7d2b602012-05-16 17:02:20 -07003283 // Show the search bar (only animate if we were showing the drop target bar in spring
3284 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003285 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003286 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003287 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003288
Michael Jurkab3e22d92011-10-31 15:58:33 -07003289 // Set focus to the AppsCustomize button
3290 if (mAllAppsButton != null) {
3291 mAllAppsButton.requestFocus();
3292 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003293 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003294
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003295 // Change the state *after* we've called all the transition code
3296 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003297
Winson Chung5fb63472011-02-02 17:03:37 -08003298 // Resume the auto-advance of widgets
3299 mUserPresent = true;
3300 updateRunning();
3301
alanv1d4fde62012-10-17 13:15:47 -07003302 // Send an accessibility event to announce the context change
3303 getWindow().getDecorView()
3304 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003305
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003306 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003307 }
3308
Adam Cohened307df2013-10-02 09:37:31 -07003309 void showOverviewMode(boolean animated) {
3310 mWorkspace.setVisibility(View.VISIBLE);
3311 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3312 mState = State.WORKSPACE;
3313 onWorkspaceShown(animated);
3314 }
3315
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003316 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003317 }
3318
Winson Chung82963d52013-10-09 11:20:57 -07003319 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3320 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003321 if (mState != State.WORKSPACE) return;
3322
Winson Chung82963d52013-10-09 11:20:57 -07003323 if (resetPageToZero) {
3324 mAppsCustomizeTabHost.reset();
3325 }
Winson Chungc58497e2013-09-03 17:48:37 -07003326 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003327 mAppsCustomizeTabHost.requestFocus();
3328
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329 // Change the state *after* we've called all the transition code
3330 mState = State.APPS_CUSTOMIZE;
3331
3332 // Pause the auto-advance of widgets until we are out of AllApps
3333 mUserPresent = false;
3334 updateRunning();
3335 closeFolder();
3336
3337 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003338 getWindow().getDecorView()
3339 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003340 }
3341
Adam Cohen7777d962011-08-18 18:58:38 -07003342 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003343 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003344 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003345 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003346 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003347 }
Adam Cohen7777d962011-08-18 18:58:38 -07003348
Adam Cohenad4e15c2013-10-17 16:21:35 -07003349 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003350 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003351 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3352
Winson Chunge7a03942011-08-05 15:05:12 -07003353 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003354 @Override
3355 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003356 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003357 // Before we show workspace, hide all apps again because
3358 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3359 // clean up our state transition functions
3360 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003361 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003362 } else {
3363 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003364 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003365 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003366 }, delay);
3367
Winson Chung557d6ed2011-07-08 15:34:52 -07003368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Michael Jurkad3ef3062010-11-23 16:23:58 -08003370 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003371 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372 final boolean animated = true;
3373 final boolean springLoaded = true;
3374 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003375 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003376 }
3377 // Otherwise, we are not in spring loaded mode, so don't do anything.
3378 }
3379
Michael Jurkab3e22d92011-10-31 15:58:33 -07003380 void lockAllApps() {
3381 // TODO
3382 }
3383
3384 void unlockAllApps() {
3385 // TODO
3386 }
3387
Winson Chungf0ea4d32011-06-06 14:27:16 -07003388 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003389 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003390 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003391 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003392 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003393 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003394 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003395 int duration = 0;
3396 if (mSearchDropTargetBar != null) {
3397 duration = mSearchDropTargetBar.getTransitionInDuration();
3398 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003399 mHotseat.animate().alpha(1f).setDuration(duration);
3400 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003401 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003402 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003403 }
3404 }
3405 }
3406
3407 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003408 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003409 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003410 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003411 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003412 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003413 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003414 int duration = 0;
3415 if (mSearchDropTargetBar != null) {
3416 duration = mSearchDropTargetBar.getTransitionOutDuration();
3417 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003418 mHotseat.animate().alpha(0f).setDuration(duration);
3419 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003420 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003421 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003422 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003423 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003424 }
3425
Patrick Dubroy5f445422011-02-18 14:35:21 -08003426 /**
3427 * Add an item from all apps or customize onto the given workspace screen.
3428 * If layout is null, add to the current screen.
3429 */
3430 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003431 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003432 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003433 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003434 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003435
Winson Chungdff8ebb2011-09-08 17:25:31 -07003436 /** Maps the current orientation to an index for referencing orientation correct global icons */
3437 private int getCurrentOrientationIndexForGlobalIcons() {
3438 // default - 0, landscape - 1
3439 switch (getResources().getConfiguration().orientation) {
3440 case Configuration.ORIENTATION_LANDSCAPE:
3441 return 1;
3442 default:
3443 return 0;
3444 }
3445 }
3446
Michael Jurkaae65ee32012-04-30 11:45:54 -07003447 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003448 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003449 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003450 // Look for the toolbar icon specified in the activity meta-data
3451 Bundle metaData = packageManager.getActivityInfo(
3452 activityName, PackageManager.GET_META_DATA).metaData;
3453 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003454 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003455 if (iconResId != 0) {
3456 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003457 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003458 }
3459 }
3460 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003461 // This can happen if the activity defines an invalid drawable
3462 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3463 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003464 } catch (Resources.NotFoundException nfe) {
3465 // This can happen if the activity defines an invalid drawable
3466 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3467 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003468 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003469 return null;
3470 }
3471
3472 // if successful in getting icon, return it; otherwise, set button to use default drawable
3473 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003474 int buttonId, ComponentName activityName, int fallbackDrawableId,
3475 String toolbarResourceName) {
3476 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003477 Resources r = getResources();
3478 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3479 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003480
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003481 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003482 // If we were unable to find the icon via the meta-data, use a generic one
3483 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003484 toolbarIcon = r.getDrawable(fallbackDrawableId);
3485 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003486 if (button != null) {
3487 button.setCompoundDrawables(toolbarIcon, null, null, null);
3488 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003489 return null;
3490 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003491 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003492 if (button != null) {
3493 button.setCompoundDrawables(toolbarIcon, null, null, null);
3494 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003495 return toolbarIcon.getConstantState();
3496 }
3497 }
3498
3499 // if successful in getting icon, return it; otherwise, set button to use default drawable
3500 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003501 int buttonId, ComponentName activityName, int fallbackDrawableId,
3502 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003503 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003504 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003505
Michael Jurka19e0fc52011-07-22 18:00:21 -07003506 if (button != null) {
3507 // If we were unable to find the icon via the meta-data, use a
3508 // generic one
3509 if (toolbarIcon == null) {
3510 button.setImageResource(fallbackDrawableId);
3511 } else {
3512 button.setImageDrawable(toolbarIcon);
3513 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003514 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003515
3516 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3517
Michael Jurka0423dcf2010-10-05 14:56:18 -07003518 }
3519
Winson Chung1b884092012-06-08 17:13:02 -07003520 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003521 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003522 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003523 }
3524
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003525 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003526 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003527 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003528 }
3529
Winson Chungbb185bd2011-11-21 12:31:42 -08003530 private void invalidatePressedFocusedStates(View container, View button) {
3531 if (container instanceof HolographicLinearLayout) {
3532 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3533 layout.invalidatePressedFocusedStates();
3534 } else if (button instanceof HolographicImageView) {
3535 HolographicImageView view = (HolographicImageView) button;
3536 view.invalidatePressedFocusedStates();
3537 }
3538 }
3539
Cristina Stancu476493b2013-08-07 17:20:14 +01003540 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003541 if (mQsb == null) {
3542 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3543 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003544 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003545 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003546 }
3547
3548 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003549 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003550 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003551 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003552 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003553
Winson Chung1cad91e2011-05-25 17:41:01 -07003554 final SearchManager searchManager =
3555 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3556 ComponentName activityName = searchManager.getGlobalSearchActivity();
3557 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003558 int coi = getCurrentOrientationIndexForGlobalIcons();
3559 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003560 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3561 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3562 if (sGlobalSearchIcon[coi] == null) {
3563 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3564 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3565 TOOLBAR_ICON_METADATA_NAME);
3566 }
3567
Winson Chungc51db6a2011-10-05 11:44:49 -07003568 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3569 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003570 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003571 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003572 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003573 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003574 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3575 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003576 if (searchButton != null) searchButton.setVisibility(View.GONE);
3577 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003578 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003579 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003580 }
3581 }
3582
Cristina Stancu476493b2013-08-07 17:20:14 +01003583 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003584 final View searchButtonContainer = findViewById(R.id.search_button_container);
3585 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003586 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003587 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003588 }
3589
Cristina Stancu476493b2013-08-07 17:20:14 +01003590 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003591 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003592 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003593
Winson Chungc51db6a2011-10-05 11:44:49 -07003594 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003595 final SearchManager searchManager =
3596 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3597 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3598
3599 ComponentName activityName = null;
3600 if (globalSearchActivity != null) {
3601 // Check if the global search activity handles voice search
3602 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3603 intent.setPackage(globalSearchActivity.getPackageName());
3604 activityName = intent.resolveActivity(getPackageManager());
3605 }
3606
3607 if (activityName == null) {
3608 // Fallback: check if an activity other than the global search activity
3609 // resolves this
3610 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3611 activityName = intent.resolveActivity(getPackageManager());
3612 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003613 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003614 int coi = getCurrentOrientationIndexForGlobalIcons();
3615 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003616 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3617 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3618 if (sVoiceSearchIcon[coi] == null) {
3619 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3620 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3621 TOOLBAR_ICON_METADATA_NAME);
3622 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003623 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003624 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003625 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003626 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003627 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003628 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003629 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003630 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003631 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003632 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003633 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003634 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003635
Cristina Stancu476493b2013-08-07 17:20:14 +01003636 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003637 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3638 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003639 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003640 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003641 }
3642
Winson Chung5841efa2013-09-30 18:06:44 -07003643 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003644 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3645 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003646 boolean visible = !forceDisableVoiceButtonProxy &&
3647 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003648 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003649 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003650 }
3651 }
Winson Chung5841efa2013-09-30 18:06:44 -07003652
3653 /**
3654 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3655 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3656 */
3657 public void disableVoiceButtonProxy(boolean disabled) {
3658 updateVoiceButtonProxyVisible(disabled);
3659 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003660 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003661 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003662 */
3663 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003664 if (!DISABLE_MARKET_BUTTON) {
3665 final View marketButton = findViewById(R.id.market_button);
3666 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3667 // Find the app market activity by resolving an intent.
3668 // (If multiple app markets are installed, it will return the ResolverActivity.)
3669 ComponentName activityName = intent.resolveActivity(getPackageManager());
3670 if (activityName != null) {
3671 int coi = getCurrentOrientationIndexForGlobalIcons();
3672 mAppMarketIntent = intent;
3673 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3674 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3675 TOOLBAR_ICON_METADATA_NAME);
3676 marketButton.setVisibility(View.VISIBLE);
3677 } else {
3678 // We should hide and disable the view so that we don't try and restore the visibility
3679 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3680 marketButton.setVisibility(View.GONE);
3681 marketButton.setEnabled(false);
3682 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003683 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003684 }
3685
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003686 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003687 if (!DISABLE_MARKET_BUTTON) {
3688 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3689 Resources r = getResources();
3690 Drawable marketIconDrawable = d.newDrawable(r);
3691 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3692 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3693 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003694
Winson Chungd64a6662013-09-30 11:06:59 -07003695 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3696 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003697 }
3698
Michael Jurkad7c28052012-04-27 15:43:36 -07003699 @Override
3700 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003701 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003702 final List<CharSequence> text = event.getText();
3703 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003704 // Populate event with a fake title based on the current state.
3705 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003706 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003707 } else {
3708 text.add(getString(R.string.all_apps_home_button_label));
3709 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003710 return result;
3711 }
3712
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003713 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003714 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003715 */
3716 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3717 @Override
3718 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003719 closeSystemDialogs();
3720 }
3721 }
3722
3723 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003724 * Receives notifications whenever the appwidgets are reset.
3725 */
3726 private class AppWidgetResetObserver extends ContentObserver {
3727 public AppWidgetResetObserver() {
3728 super(new Handler());
3729 }
3730
3731 @Override
3732 public void onChange(boolean selfChange) {
3733 onAppWidgetReset();
3734 }
3735 }
3736
3737 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003738 * If the activity is currently paused, signal that we need to run the passed Runnable
3739 * in onResume.
3740 *
3741 * This needs to be called from incoming places where resources might have been loaded
3742 * while we are paused. That is becaues the Configuration might be wrong
3743 * when we're not running, and if it comes back to what it was when we
3744 * were paused, we are not restarted.
3745 *
3746 * Implementation of the method from LauncherModel.Callbacks.
3747 *
3748 * @return true if we are currently paused. The caller might be able to
3749 * skip some work in that case since we will come back again.
3750 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003751 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003752 if (mPaused) {
3753 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003754 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003755 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003756 }
3757 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003758 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003759 return true;
3760 } else {
3761 return false;
3762 }
3763 }
3764
Michael Jurkac402cd92013-05-20 15:49:32 +02003765 private boolean waitUntilResume(Runnable run) {
3766 return waitUntilResume(run, false);
3767 }
3768
Michael Jurka1e2f4652013-07-08 18:03:46 -07003769 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003770 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003771 }
3772
Michael Jurka7607c2f2013-04-03 14:33:19 -07003773 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003774 * If the activity is currently paused, signal that we need to re-run the loader
3775 * in onResume.
3776 *
3777 * This needs to be called from incoming places where resources might have been loaded
3778 * while we are paused. That is becaues the Configuration might be wrong
3779 * when we're not running, and if it comes back to what it was when we
3780 * were paused, we are not restarted.
3781 *
3782 * Implementation of the method from LauncherModel.Callbacks.
3783 *
3784 * @return true if we are currently paused. The caller might be able to
3785 * skip some work in that case since we will come back again.
3786 */
3787 public boolean setLoadOnResume() {
3788 if (mPaused) {
3789 Log.i(TAG, "setLoadOnResume");
3790 mOnResumeNeedsLoad = true;
3791 return true;
3792 } else {
3793 return false;
3794 }
3795 }
3796
3797 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003798 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003799 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003801 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003802 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003803 } else {
3804 return SCREEN_COUNT / 2;
3805 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003807
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 /**
3809 * Refreshes the shortcuts shown on the workspace.
3810 *
3811 * Implementation of the method from LauncherModel.Callbacks.
3812 */
3813 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003814 mWorkspaceLoading = true;
3815
Michael Jurka7607c2f2013-04-03 14:33:19 -07003816 // If we're starting binding all over again, clear any bind calls we'd postponed in
3817 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3818 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003819 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003820
Winson Chungd64d1762013-08-20 14:37:16 -07003821 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003822 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003823 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003824
Michael Jurka05bf6442011-11-30 20:28:53 -08003825 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003826 if (mHotseat != null) {
3827 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003828 }
3829 }
3830
Adam Cohendcd297f2013-06-18 13:13:40 -07003831 @Override
3832 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003833 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003834
Adam Cohen5084cba2013-09-03 12:01:16 -07003835 // If there are no screens, we need to have an empty screen
3836 if (orderedScreenIds.size() == 0) {
3837 mWorkspace.addExtraEmptyScreen();
3838 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003839
3840 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003841 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003842 if (hasCustomContentToLeft()) {
3843 mWorkspace.createCustomContentContainer();
3844 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003845 }
Winson Chung64359a52013-07-08 17:17:08 -07003846 }
3847
3848 @Override
3849 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003850 // Log to disk
3851 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3852 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3853 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003854 int count = orderedScreenIds.size();
3855 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003856 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003857 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003858 }
3859
Adam Cohen39a06042013-07-19 14:30:12 -07003860 private boolean shouldShowWeightWatcher() {
3861 String spKey = LauncherAppState.getSharedPreferencesKey();
3862 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003863 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003864
3865 return show;
3866 }
3867
3868 private void toggleShowWeightWatcher() {
3869 String spKey = LauncherAppState.getSharedPreferencesKey();
3870 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3871 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3872
3873 show = !show;
3874
3875 SharedPreferences.Editor editor = sp.edit();
3876 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3877 editor.commit();
3878
3879 if (mWeightWatcher != null) {
3880 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3881 }
3882 }
3883
Winson Chungd64d1762013-08-20 14:37:16 -07003884 public void bindAppsAdded(final ArrayList<Long> newScreens,
3885 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003886 final ArrayList<ItemInfo> addAnimated,
3887 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003888 Runnable r = new Runnable() {
3889 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003890 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003891 }
3892 };
3893 if (waitUntilResume(r)) {
3894 return;
3895 }
3896
Winson Chungd64d1762013-08-20 14:37:16 -07003897 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003898 if (newScreens != null) {
3899 bindAddScreens(newScreens);
3900 }
Winson Chungd64d1762013-08-20 14:37:16 -07003901
3902 // We add the items without animation on non-visible pages, and with
3903 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003904 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003905 bindItems(addNotAnimated, 0,
3906 addNotAnimated.size(), false);
3907 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003908 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003909 bindItems(addAnimated, 0,
3910 addAnimated.size(), true);
3911 }
Winson Chungc58497e2013-09-03 17:48:37 -07003912
Winson Chung87412982013-10-03 18:34:14 -07003913 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003914 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003915
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003916 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003917 addedApps != null && mAppsCustomizeContent != null) {
3918 mAppsCustomizeContent.addApps(addedApps);
3919 }
Winson Chungd64d1762013-08-20 14:37:16 -07003920 }
3921
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003922 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 * Bind the items start-end from the list.
3924 *
3925 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 */
Winson Chung64359a52013-07-08 17:17:08 -07003927 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3928 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003929 Runnable r = new Runnable() {
3930 public void run() {
3931 bindItems(shortcuts, start, end, forceAnimateIcons);
3932 }
3933 };
3934 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003935 return;
3936 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003937
Winson Chungf0c6ae02012-03-21 16:10:31 -07003938 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003939 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3940 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003941 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003942 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003943 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003944 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003946
3947 // Short circuit if we are loading dock items for a configuration which has no dock
3948 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3949 mHotseat == null) {
3950 continue;
3951 }
3952
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 switch (item.itemType) {
3954 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3955 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003956 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003957 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003958
Winson Chung64359a52013-07-08 17:17:08 -07003959 /*
3960 * TODO: FIX collision case
3961 */
Winson Chungce376632013-07-11 16:12:41 -07003962 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3963 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3964 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3965 throw new RuntimeException("OCCUPIED");
3966 }
Winson Chung64359a52013-07-08 17:17:08 -07003967 }
3968
Adam Cohendcd297f2013-06-18 13:13:40 -07003969 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3970 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003971 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003972 // Animate all the applications up now
3973 shortcut.setAlpha(0f);
3974 shortcut.setScaleX(0f);
3975 shortcut.setScaleY(0f);
3976 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003977 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003980 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003981 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003982 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003983 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003984 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3985 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003987 default:
3988 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003990 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003991
Winson Chung997a9232013-07-24 15:33:46 -07003992 if (animateIcons) {
3993 // Animate to the correct page
3994 if (newShortcutsScreenId > -1) {
3995 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003996 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003997 final Runnable startBounceAnimRunnable = new Runnable() {
3998 public void run() {
3999 anim.playTogether(bounceAnims);
4000 anim.start();
4001 }
4002 };
Winson Chung997a9232013-07-24 15:33:46 -07004003 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004004 // We post the animation slightly delayed to prevent slowdowns
4005 // when we are loading right after we return to launcher.
4006 mWorkspace.postDelayed(new Runnable() {
4007 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004008 if (mWorkspace != null) {
4009 mWorkspace.snapToPage(newScreenIndex);
4010 mWorkspace.postDelayed(startBounceAnimRunnable,
4011 NEW_APPS_ANIMATION_DELAY);
4012 }
Winson Chung94d67682013-09-25 16:29:40 -07004013 }
4014 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004015 } else {
4016 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004017 }
4018 }
Winson Chung64359a52013-07-08 17:17:08 -07004019 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004020 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004021 }
4022
Joe Onorato9c1289c2009-08-17 11:03:03 -04004023 /**
4024 * Implementation of the method from LauncherModel.Callbacks.
4025 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004026 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004027 Runnable r = new Runnable() {
4028 public void run() {
4029 bindFolders(folders);
4030 }
4031 };
4032 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004033 return;
4034 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004035 sFolders.clear();
4036 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004038
4039 /**
4040 * Add the views for a widget to the workspace.
4041 *
4042 * Implementation of the method from LauncherModel.Callbacks.
4043 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004044 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004045 Runnable r = new Runnable() {
4046 public void run() {
4047 bindAppWidget(item);
4048 }
4049 };
4050 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004051 return;
4052 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004053
Daniel Sandler843e8602010-06-07 14:59:01 -04004054 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4055 if (DEBUG_WIDGETS) {
4056 Log.d(TAG, "bindAppWidget: " + item);
4057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 final Workspace workspace = mWorkspace;
4059
4060 final int appWidgetId = item.appWidgetId;
4061 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004062 if (DEBUG_WIDGETS) {
4063 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4064 }
4065
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4067
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004069 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070
Adam Cohendcd297f2013-06-18 13:13:40 -07004071 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004073 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4074
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 workspace.requestLayout();
4076
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 if (DEBUG_WIDGETS) {
4078 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4079 + (SystemClock.uptimeMillis()-start) + "ms");
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
Adam Cohen1462de32012-07-24 22:34:36 -07004083 public void onPageBoundSynchronously(int page) {
4084 mSynchronouslyBoundPages.add(page);
4085 }
4086
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 /**
4088 * Callback saying that there aren't any more items to bind.
4089 *
4090 * Implementation of the method from LauncherModel.Callbacks.
4091 */
Adam Cohene25af792013-06-06 23:08:25 -07004092 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004093 Runnable r = new Runnable() {
4094 public void run() {
4095 finishBindingItems(upgradePath);
4096 }
4097 };
4098 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004099 return;
4100 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101 if (mSavedState != null) {
4102 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004103 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004104 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004105 mSavedState = null;
4106 }
4107
Adam Cohen1462de32012-07-24 22:34:36 -07004108 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004109
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004110 // If we received the result of any pending adds while the loader was running (e.g. the
4111 // widget configuration forced an orientation change), process them now.
4112 for (int i = 0; i < sPendingAddList.size(); i++) {
4113 completeAdd(sPendingAddList.get(i));
4114 }
4115 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116
Winson Chungc51db6a2011-10-05 11:44:49 -07004117 // Update the market app icon as necessary (the other icons will be managed in response to
4118 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004119 if (!DISABLE_MARKET_BUTTON) {
4120 updateAppMarketIcon();
4121 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004122
Winson Chungf0c6ae02012-03-21 16:10:31 -07004123 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004124 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004125 mWorkspace.getUniqueComponents(true, null);
4126 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004127 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004128 }
4129
Winson Chunga0b7e862013-09-05 16:03:15 -07004130 public boolean isAllAppsButtonRank(int rank) {
4131 if (mHotseat != null) {
4132 return mHotseat.isAllAppsButtonRank(rank);
4133 }
4134 return false;
4135 }
4136
Winson Chunga2413752012-04-03 14:22:34 -07004137 private boolean canRunNewAppsAnimation() {
4138 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4139 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4140 }
4141
Winson Chungc9168342013-06-26 14:54:55 -07004142 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4143 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4144 PropertyValuesHolder.ofFloat("alpha", 1f),
4145 PropertyValuesHolder.ofFloat("scaleX", 1f),
4146 PropertyValuesHolder.ofFloat("scaleY", 1f));
4147 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4148 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4149 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4150 return bounceAnim;
4151 }
4152
Adam Cohen27772732013-11-11 14:00:56 +00004153 public boolean useVerticalBarLayout() {
4154 return LauncherAppState.getInstance().getDynamicGrid().
4155 getDeviceProfile().isVerticalBarLayout();
4156 }
4157
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004158 protected Rect getSearchBarBounds() {
4159 return LauncherAppState.getInstance().getDynamicGrid().
4160 getDeviceProfile().getSearchBarBounds();
4161 }
4162
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004163 @Override
4164 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004165 boolean searchVisible = updateGlobalSearchIcon();
4166 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004167 if (mSearchDropTargetBar != null) {
4168 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4169 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004170 }
4171
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004172 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004173 * Add the icons for all apps.
4174 *
4175 * Implementation of the method from LauncherModel.Callbacks.
4176 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004177 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004178 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004179 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4180 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4181 getHotseat().addAllAppsFolder(mIconCache, apps,
4182 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4183 }
4184 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004185 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004186 if (mAppsCustomizeContent != null) {
4187 mAppsCustomizeContent.onPackagesUpdated(
4188 LauncherModel.getSortedWidgetsAndShortcuts(this));
4189 }
Winson Chungc58497e2013-09-03 17:48:37 -07004190 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004191 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004192 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004193 mAppsCustomizeContent.onPackagesUpdated(
4194 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004195 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004197 }
4198
4199 /**
4200 * A package was updated.
4201 *
4202 * Implementation of the method from LauncherModel.Callbacks.
4203 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004204 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004205 Runnable r = new Runnable() {
4206 public void run() {
4207 bindAppsUpdated(apps);
4208 }
4209 };
4210 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004211 return;
4212 }
4213
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004214 if (mWorkspace != null) {
4215 mWorkspace.updateShortcuts(apps);
4216 }
Winson Chungc58497e2013-09-03 17:48:37 -07004217
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004218 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004219 mAppsCustomizeContent != null) {
4220 mAppsCustomizeContent.updateApps(apps);
4221 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 }
4223
4224 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004225 * A package was uninstalled. We take both the super set of packageNames
4226 * in addition to specific applications to remove, the reason being that
4227 * this can be called when a package is updated as well. In that scenario,
4228 * we only remove specific components from the workspace, where as
4229 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004230 *
4231 * Implementation of the method from LauncherModel.Callbacks.
4232 */
Winson Chung83892cc2013-05-01 16:53:33 -07004233 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004234 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004235 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004236 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004237 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004238 }
Winson Chungd64d1762013-08-20 14:37:16 -07004239 };
4240 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004241 return;
4242 }
4243
Winson Chungdf95eb12013-10-16 14:57:07 -07004244 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004245 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004246 }
4247 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004248 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004249 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004250
Winson Chunga1820962011-10-03 16:31:06 -07004251 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004252 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004253
Winson Chungdf95eb12013-10-16 14:57:07 -07004254 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004255 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004256 mAppsCustomizeContent != null) {
4257 mAppsCustomizeContent.removeApps(appInfos);
4258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 }
Joe Onoratobe386092009-11-17 17:32:16 -08004260
4261 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004262 * A number of packages were updated.
4263 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004264 private ArrayList<Object> mWidgetsAndShortcuts;
4265 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004266 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004267 bindPackagesUpdated(mWidgetsAndShortcuts);
4268 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004269 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004270 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004271 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4272 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4273 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004274 return;
4275 }
4276
Winson Chung64359a52013-07-08 17:17:08 -07004277 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004278 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004279 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004280 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004281 }
4282
Winson Chung400438b2011-01-16 17:53:48 -08004283 private int mapConfigurationOriActivityInfoOri(int configOri) {
4284 final Display d = getWindowManager().getDefaultDisplay();
4285 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4286 switch (d.getRotation()) {
4287 case Surface.ROTATION_0:
4288 case Surface.ROTATION_180:
4289 // We are currently in the same basic orientation as the natural orientation
4290 naturalOri = configOri;
4291 break;
4292 case Surface.ROTATION_90:
4293 case Surface.ROTATION_270:
4294 // We are currently in the other basic orientation to the natural orientation
4295 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4296 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4297 break;
4298 }
4299
4300 int[] oriMap = {
4301 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4302 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4303 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4304 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4305 };
4306 // Since the map starts at portrait, we need to offset if this device's natural orientation
4307 // is landscape.
4308 int indexOffset = 0;
4309 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4310 indexOffset = 1;
4311 }
4312 return oriMap[(d.getRotation() + indexOffset) % 4];
4313 }
Adam Cohen446e9402011-09-15 18:21:21 -07004314
Winson Chung4b919f82012-05-01 10:44:08 -07004315 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004316 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004317 getResources().getBoolean(R.bool.allow_rotation);
4318 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004319 }
Winson Chung4b919f82012-05-01 10:44:08 -07004320 public void lockScreenOrientation() {
4321 if (isRotationEnabled()) {
4322 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4323 .getConfiguration().orientation));
4324 }
4325 }
4326 public void unlockScreenOrientation(boolean immediate) {
4327 if (isRotationEnabled()) {
4328 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004329 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004330 } else {
4331 mHandler.postDelayed(new Runnable() {
4332 public void run() {
4333 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4334 }
4335 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004336 }
Winson Chung4b919f82012-05-01 10:44:08 -07004337 }
Winson Chung400438b2011-01-16 17:53:48 -08004338 }
4339
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004340 /**
4341 * Called when the SearchBar hint should be changed.
4342 *
4343 * @param hint the hint to be displayed in the search bar.
4344 */
4345 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004346 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004347 }
4348
Winson Chunge43a1e72014-01-15 10:33:02 -08004349 protected boolean isLauncherPreinstalled() {
4350 PackageManager pm = getPackageManager();
4351 try {
4352 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4353 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4354 return true;
4355 } else {
4356 return false;
4357 }
4358 } catch (NameNotFoundException e) {
4359 e.printStackTrace();
4360 return false;
4361 }
4362 }
4363
Winson Chunge6eabff2013-09-24 11:01:23 -07004364 protected String getFirstRunClingSearchBarHint() {
4365 return "";
4366 }
4367 protected String getFirstRunCustomContentHint() {
4368 return "";
4369 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004370 protected int getFirstRunFocusedHotseatAppDrawableId() {
4371 return -1;
4372 }
4373 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4374 return null;
4375 }
4376 protected int getFirstRunFocusedHotseatAppRank() {
4377 return -1;
4378 }
4379 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4380 return "";
4381 }
4382 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4383 return "";
4384 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004385
Winson Chungaf40f202013-09-18 18:26:31 -07004386 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004387 mLauncherClings.dismissFirstRunCling(v);
4388 }
4389 public void dismissMigrationClingCopyApps(View v) {
4390 mLauncherClings.dismissMigrationClingCopyApps(v);
4391 }
4392 public void dismissMigrationClingUseDefault(View v) {
4393 mLauncherClings.dismissMigrationClingUseDefault(v);
4394 }
4395 public void dismissMigrationWorkspaceCling(View v) {
4396 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004397 }
Winson Chung82f55532011-08-09 14:14:23 -07004398 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004399 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004400 }
4401 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004402 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004403 }
4404
Winson Chunga6945242014-01-08 14:04:34 -08004405 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004406 return !ActivityManager.isRunningInTestHarness() &&
4407 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004408 }
4409
4410 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004411 if (shouldRunFirstRunActivity() &&
4412 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004413 Intent firstRunIntent = getFirstRunActivity();
4414 if (firstRunIntent != null) {
4415 startActivity(firstRunIntent);
4416 markFirstRunActivityShown();
4417 }
4418 }
4419 }
4420
4421 private void markFirstRunActivityShown() {
4422 SharedPreferences.Editor editor = mSharedPrefs.edit();
4423 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4424 editor.apply();
4425 }
4426
4427 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004428 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4429 if (mHotseat != null) mHotseat.setAlpha(1f);
4430 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4431 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004432 }
4433
4434 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004435 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4436 if (mHotseat != null) mHotseat.setAlpha(0f);
4437 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4438 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004439 }
4440
4441
Mathew Inwood72fbec12013-11-19 15:45:07 +00004442 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4443 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4444 if (ri == null) {
4445 return null;
4446 }
4447 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4448 }
4449
4450 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4451 Bitmap icon) {
4452 return new ShortcutInfo(shortcutIntent, caption, icon);
4453 }
4454
4455 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4456 dragView.setTag(dragInfo);
4457 mWorkspace.onDragStartedWithItem(dragView);
4458 mWorkspace.beginDragShared(dragView, source);
4459 }
4460
Anjali Koppalf5d29132014-02-28 13:33:27 -08004461 @Override
4462 public void onPageSwitch(View newPage, int newPageIndex) {
4463 }
4464
Winson Chung80baf5a2010-08-09 16:03:15 -07004465 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004466 * Prints out out state for debugging.
4467 */
4468 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004469 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004470 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004471 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4472 Log.d(TAG, "mRestoring=" + mRestoring);
4473 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4474 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004475 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004476 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004477
Winson Chung785d2eb2011-04-14 16:08:02 -07004478 if (mAppsCustomizeContent != null) {
4479 mAppsCustomizeContent.dumpState();
4480 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004481 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004482 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004483
4484 @Override
4485 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4486 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004487 synchronized (sDumpLogs) {
4488 writer.println(" ");
4489 writer.println("Debug logs: ");
4490 for (int i = 0; i < sDumpLogs.size(); i++) {
4491 writer.println(" " + sDumpLogs.get(i));
4492 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004493 }
4494 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004495
4496 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004497 if (DEBUG_DUMP_LOG) {
4498 synchronized (sDumpLogs) {
4499 Log.d(TAG, "");
4500 Log.d(TAG, "*********************");
4501 Log.d(TAG, "Launcher debug logs: ");
4502 for (int i = 0; i < sDumpLogs.size(); i++) {
4503 Log.d(TAG, " " + sDumpLogs.get(i));
4504 }
4505 Log.d(TAG, "*********************");
4506 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004507 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004508 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004509 }
4510
4511 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004512 addDumpLog(tag, log, null, debugLog);
4513 }
4514
4515 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004516 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004517 if (e != null) {
4518 Log.d(tag, log, e);
4519 } else {
4520 Log.d(tag, log);
4521 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004522 }
Winson Chungede41292013-09-19 16:27:36 -07004523 if (DEBUG_DUMP_LOG) {
4524 sDateStamp.setTime(System.currentTimeMillis());
4525 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004526 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4527 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004528 }
Winson Chungede41292013-09-19 16:27:36 -07004529 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004530 }
4531
Winson Chungede41292013-09-19 16:27:36 -07004532 public void dumpLogsToLocalData() {
4533 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004534 new AsyncTask<Void, Void, Void>() {
4535 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004536 boolean success = false;
4537 sDateStamp.setTime(sRunStart);
4538 String FILENAME = sDateStamp.getMonth() + "-"
4539 + sDateStamp.getDay() + "_"
4540 + sDateStamp.getHours() + "-"
4541 + sDateStamp.getMinutes() + "_"
4542 + sDateStamp.getSeconds() + ".txt";
4543
4544 FileOutputStream fos = null;
4545 File outFile = null;
4546 try {
4547 outFile = new File(getFilesDir(), FILENAME);
4548 outFile.createNewFile();
4549 fos = new FileOutputStream(outFile);
4550 } catch (Exception e) {
4551 e.printStackTrace();
4552 }
4553 if (fos != null) {
4554 PrintWriter writer = new PrintWriter(fos);
4555
4556 writer.println(" ");
4557 writer.println("Debug logs: ");
4558 synchronized (sDumpLogs) {
4559 for (int i = 0; i < sDumpLogs.size(); i++) {
4560 writer.println(" " + sDumpLogs.get(i));
4561 }
4562 }
4563 writer.close();
4564 }
4565 try {
4566 if (fos != null) {
4567 fos.close();
4568 success = true;
4569 }
4570 } catch (IOException e) {
4571 e.printStackTrace();
4572 }
Michael Jurka43467462013-11-14 12:03:58 +01004573 return null;
Winson Chungede41292013-09-19 16:27:36 -07004574 }
Michael Jurka43467462013-11-14 12:03:58 +01004575 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004576 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004578}
Michael Jurka2763be32011-02-24 11:19:57 -08004579
Michael Jurkaabded662011-03-04 12:06:57 -08004580interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004581 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004582 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004583 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004584 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004585 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004586}
Dan Sandlerd5024042014-01-09 15:01:33 -05004587
4588interface DebugIntents {
4589 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4590 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004591}