blob: 19dbe6afdc04da9d60531190068d5924f71ec4c9 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080044import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -080099import com.android.launcher3.PagedView.PageSwitchListener;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700103import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000115import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
Winson Chunga6945242014-01-08 14:04:34 -0800117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118/**
119 * Default launcher application.
120 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100121public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700122 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppalf5d29132014-02-28 13:33:27 -0800123 View.OnTouchListener, PageSwitchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800124 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400128 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700129 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700131 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700132
Dan Sandlerd5024042014-01-09 15:01:33 -0500133 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800160 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chung2672ff92012-05-04 16:22:30 -0700162 // The Intent extra that defines whether to ignore the launch animation
163 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400164 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
167 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700168 // Type: int
169 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
172 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
174 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: boolean
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
180 // Type: long
181 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: int
183 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
184 // Type: int
185 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
186 // Type: parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: int[]
191 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohenb54a5982014-01-08 15:21:04 -0800193
194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700199 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100200 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700201
Adam Cohen39a06042013-07-19 14:30:12 -0700202 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700203 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700204
Winson Chunga6945242014-01-08 14:04:34 -0800205 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
206
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700208 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700209 private State mState = State.WORKSPACE;
210 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800214 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE = 400;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800218 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000220 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
221 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
222
Winson Chunga2413752012-04-03 14:22:34 -0700223 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700224 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
225 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700226 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700227
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800228 private final BroadcastReceiver mCloseSystemDialogsReceiver
229 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800230 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private LayoutInflater mInflater;
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800236 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragLayer mDragLayer;
238 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700239 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800240 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 private AppWidgetManager mAppWidgetManager;
243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Michael Jurkac9d95c52011-08-29 14:03:34 -0700245 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private FolderInfo mFolderInfo;
252
Winson Chung3d503fb2011-07-13 17:25:49 -0700253 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800254 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700255
Michael Jurka838a4ca2011-02-07 13:33:06 -0800256 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700257
Winson Chungc51db6a2011-10-05 11:44:49 -0700258 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700259 private AppsCustomizeTabHost mAppsCustomizeTabHost;
260 private AppsCustomizePagedView mAppsCustomizeContent;
261 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800262 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
266 // scroll issues (because the workspace may not have been measured yet) and extra work.
267 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
268 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
270 private SpannableStringBuilder mDefaultKeySsb = null;
271
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 private boolean mWorkspaceLoading = true;
273
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800274 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private boolean mRestoring;
276 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700277 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
281
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // Keep track of whether the user has left launcher
283 private static boolean sPausedFromUserAction = false;
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedInstanceState;
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800288 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mUserPresent = true;
290 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700291 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700294 private static LocaleConfiguration sLocaleConfiguration = null;
295
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700296 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
302 private final int mAdvanceInterval = 20000;
303 private final int mAdvanceStagger = 250;
304 private long mAutoAdvanceSentTime;
305 private long mAutoAdvanceTimeLeft = -1;
306 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
307 new HashMap<View, AppWidgetProviderInfo>();
308
Winson Chung400438b2011-01-16 17:53:48 -0800309 // Determines how long to wait after a rotation before restoring the screen orientation to
310 // match the sensor state.
311 private final int mRestoreScreenOrientationDelay = 500;
312
Michael Jurka4ef207b2010-11-29 17:05:45 -0800313 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700314 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
315 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
316 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800317
Winson Chungd64a6662013-09-30 11:06:59 -0700318 private Intent mAppMarketIntent = null;
319 private static final boolean DISABLE_MARKET_BUTTON = true;
320
Craig Mautner360310b2012-10-26 15:13:08 -0700321 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700322
Adam Cohen1462de32012-07-24 22:34:36 -0700323 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700324 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700325
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700326 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700327 static Date sDateStamp = new Date();
328 static DateFormat sDateFormat =
329 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
330 static long sRunStart = System.currentTimeMillis();
331 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332
Winson Chung46353de2012-02-16 14:05:10 -0800333 // We only want to get the SharedPreferences once since it does an FS stat each time we get
334 // it from the context.
335 private SharedPreferences mSharedPrefs;
336
Adam Cohene25af792013-06-06 23:08:25 -0700337 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
338
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 // Holds the page that we need to animate to, and the icon views that we need to animate up
340 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private ImageView mFolderIconImageView;
342 private Bitmap mFolderIconBitmap;
343 private Canvas mFolderIconCanvas;
344 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345
Michael Jurkaddd62e92011-02-16 17:49:14 -0800346 private BubbleTextView mWaitingForResume;
347
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 private Runnable mBuildLayersRunnable = new Runnable() {
349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 private static ArrayList<PendingAddArguments> sPendingAddList
357 = new ArrayList<PendingAddArguments>();
358
Michael Jurka7267fa52013-09-26 15:29:57 -0700359 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800360
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 private static class PendingAddArguments {
362 int requestCode;
363 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700364 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700365 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int cellX;
367 int cellY;
368 }
369
Daniel Sandlerff02d492013-08-05 02:12:05 -0400370 private Stats mStats;
371
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800372 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800373 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700374 }
375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 @Override
377 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700378 if (DEBUG_STRICT_MODE) {
379 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
380 .detectDiskReads()
381 .detectDiskWrites()
382 .detectNetwork() // or .detectAll() for all detectable problems
383 .penaltyLog()
384 .build());
385 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
386 .detectLeakedSqlLiteObjects()
387 .detectLeakedClosableObjects()
388 .penaltyLog()
389 .penaltyDeath()
390 .build());
391 }
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400394
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400395 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400396 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700397
398 // Determine the dynamic grid properties
399 Point smallestSize = new Point();
400 Point largestSize = new Point();
401 Point realSize = new Point();
402 Display display = getWindowManager().getDefaultDisplay();
403 display.getCurrentSizeRange(smallestSize, largestSize);
404 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700405 DisplayMetrics dm = new DisplayMetrics();
406 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700407
Winson Chung5f8afe62013-08-12 16:19:28 -0700408 // Lazy-initialize the dynamic grid
409 DeviceProfile grid = app.initDynamicGrid(this,
410 Math.min(smallestSize.x, smallestSize.y),
411 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700412 realSize.x, realSize.y,
413 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414
415 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400416 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700417 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700420 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400421 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800422 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Daniel Sandlerff02d492013-08-05 02:12:05 -0400425 mStats = new Stats(this);
426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200438 android.os.Debug.startMethodTracing(
439 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441
Adam Cohen53805212013-10-01 10:39:23 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100447 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800449 registerContentObservers();
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
456 if (PROFILE_STARTUP) {
457 android.os.Debug.stopMethodTracing();
458 }
459
460 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500464 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800468 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
477 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Adam Cohenf9426d52012-06-04 17:26:21 -0700479 updateGlobalIcons();
480
481 // On large interfaces, we want the screen to auto-rotate based on the current orientation
482 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700483
Winson Chunge43a1e72014-01-15 10:33:02 -0800484 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
485 // on the device, then we always show the first run cling experience (or if there is no
486 // launcher2). Otherwise, we prompt the user upon started for migration
Adam Cohenb54a5982014-01-08 15:21:04 -0800487 showFirstRunActivity();
Winson Chunge43a1e72014-01-15 10:33:02 -0800488 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
489 if (mModel.canMigrateFromOldLauncherDb(this)) {
490 mLauncherClings.showMigrationCling();
491 } else {
492 mLauncherClings.showFirstRunCling();
493 }
Winson Chunga6945242014-01-08 14:04:34 -0800494 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800495 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800496 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700497 }
498
Winson Chung4a2afa32012-07-19 14:53:05 -0700499 protected void onUserLeaveHint() {
500 super.onUserLeaveHint();
501 sPausedFromUserAction = true;
502 }
503
Winson Chung98ca0f72013-07-29 12:58:51 -0700504 /** To be overriden by subclasses to hint to Launcher that we have custom content */
505 protected boolean hasCustomContentToLeft() {
506 return false;
507 }
508
Dave Hawkeya8881582013-09-17 15:55:33 -0600509 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500510 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600511 * {@link #addToCustomContentPage}. This will only be invoked if
512 * {@link #hasCustomContentToLeft()} is {@code true}.
513 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500514 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600515 }
516
517 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800518 * To be overridden by subclasses to indicate that there is an activity to launch
519 * before showing the standard launcher experience.
520 */
521 protected boolean hasFirstRunActivity() {
522 return false;
523 }
524
525 /**
526 * To be overridden by subclasses to launch any first run activity
527 */
528 protected Intent getFirstRunActivity() {
529 return null;
530 }
531
532 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600533 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
534 * ensure the custom content page is added or removed if necessary.
535 */
536 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600537 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600538 // Not bound yet, wait for bindScreens to be called.
539 return;
540 }
541
542 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
543 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500544 mWorkspace.createCustomContentContainer();
545 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600546 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
547 mWorkspace.removeCustomContentPage();
548 }
549 }
550
Adam Cohenf9426d52012-06-04 17:26:21 -0700551 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700552 boolean searchVisible = false;
553 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800554 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700555 int coi = getCurrentOrientationIndexForGlobalIcons();
556 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
557 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700558 if (!DISABLE_MARKET_BUTTON) {
559 updateAppMarketIcon();
560 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700561 searchVisible = updateGlobalSearchIcon();
562 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700563 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700564 if (sGlobalSearchIcon[coi] != null) {
565 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700566 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700567 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700568 if (sVoiceSearchIcon[coi] != null) {
569 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700570 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700571 }
Winson Chungd64a6662013-09-30 11:06:59 -0700572 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700573 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800574 }
Winson Chungadf0c182012-08-23 14:59:07 -0700575 if (mSearchDropTargetBar != null) {
576 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
Romain Guycbb89e42009-06-08 15:52:54 -0700579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 if (sLocaleConfiguration == null) {
582 new AsyncTask<Void, Void, LocaleConfiguration>() {
583 @Override
584 protected LocaleConfiguration doInBackground(Void... unused) {
585 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
586 readConfiguration(Launcher.this, localeConfiguration);
587 return localeConfiguration;
588 }
589
590 @Override
591 protected void onPostExecute(LocaleConfiguration result) {
592 sLocaleConfiguration = result;
593 checkForLocaleChange(); // recursive, but now with a locale configuration
594 }
595 }.execute();
596 return;
597 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final Configuration configuration = getResources().getConfiguration();
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final String locale = configuration.locale.toString();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final int mcc = configuration.mcc;
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mnc = configuration.mnc;
609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 sLocaleConfiguration.locale = locale;
614 sLocaleConfiguration.mcc = mcc;
615 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700616
Joe Onorato0589f0f2010-02-08 13:44:00 -0800617 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618
619 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100620 new AsyncTask<Void, Void, Void>() {
621 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100623 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 }
Michael Jurka43467462013-11-14 12:03:58 +0100625 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700626 }
627 }
628
629 private static class LocaleConfiguration {
630 public String locale;
631 public int mcc = -1;
632 public int mnc = -1;
633 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700634
Romain Guy98d01652009-06-30 16:21:04 -0700635 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
636 DataInputStream in = null;
637 try {
638 in = new DataInputStream(context.openFileInput(PREFERENCES));
639 configuration.locale = in.readUTF();
640 configuration.mcc = in.readInt();
641 configuration.mnc = in.readInt();
642 } catch (FileNotFoundException e) {
643 // Ignore
644 } catch (IOException e) {
645 // Ignore
646 } finally {
647 if (in != null) {
648 try {
649 in.close();
650 } catch (IOException e) {
651 // Ignore
652 }
653 }
654 }
655 }
656
657 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
658 DataOutputStream out = null;
659 try {
660 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
661 out.writeUTF(configuration.locale);
662 out.writeInt(configuration.mcc);
663 out.writeInt(configuration.mnc);
664 out.flush();
665 } catch (FileNotFoundException e) {
666 // Ignore
667 } catch (IOException e) {
668 //noinspection ResultOfMethodCallIgnored
669 context.getFileStreamPath(PREFERENCES).delete();
670 } finally {
671 if (out != null) {
672 try {
673 out.close();
674 } catch (IOException e) {
675 // Ignore
676 }
677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
679 }
680
Anton Hanssona2f665f2013-09-26 12:18:32 +0100681 public Stats getStats() {
682 return mStats;
683 }
684
Bjorn Bringertc459e522013-06-07 19:36:01 +0100685 public LayoutInflater getInflater() {
686 return mInflater;
687 }
688
Winson Chung36a62fe2012-05-06 18:04:42 -0700689 boolean isDraggingEnabled() {
690 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
691 // that is subsequently removed from the workspace in startBinding().
692 return !mModel.isLoadingWorkspace();
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 static int getScreen() {
696 synchronized (sLock) {
697 return sScreen;
698 }
699 }
700
701 static void setScreen(int screen) {
702 synchronized (sLock) {
703 sScreen = screen;
704 }
705 }
706
Winson Chung557d6ed2011-07-08 15:34:52 -0700707 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000708 * Copied from View -- the View version of the method isn't called
709 * anywhere else in our process and only exists for API level 17+,
710 * so it's ok to keep our own version with no API requirement.
711 */
712 public static int generateViewId() {
713 for (;;) {
714 final int result = sNextGeneratedId.get();
715 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
716 int newValue = result + 1;
717 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
718 if (sNextGeneratedId.compareAndSet(result, newValue)) {
719 return result;
720 }
721 }
722 }
723
724 public int getViewIdForItem(ItemInfo info) {
725 // This cast is safe given the > 2B range for int.
726 int itemId = (int) info.id;
727 if (mItemIdToViewId.containsKey(itemId)) {
728 return mItemIdToViewId.get(itemId);
729 }
730 int viewId = generateViewId();
731 mItemIdToViewId.put(itemId, viewId);
732 return viewId;
733 }
734
735 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700736 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
737 * a configuration step, this allows the proper animations to run after other transitions.
738 */
739 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700740 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 switch (args.requestCode) {
742 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 break;
746 case REQUEST_PICK_SHORTCUT:
747 processShortcut(args.intent);
748 break;
749 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700751 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700752 result = true;
753 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800754 case REQUEST_CREATE_APPWIDGET:
755 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700756 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700757 result = true;
758 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 }
Michael Jurka27614d22012-04-02 06:40:22 -0700760 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
761 // if you turned the screen off and then back while in All Apps, Launcher would not
762 // return to the workspace. Clearing mAddInfo.container here fixes this issue
763 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700764 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 protected void onActivityResult(
769 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700770 // Reset the startActivity waiting flag
771 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800772 int pendingAddWidgetId = mPendingAddWidgetId;
773 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700774
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 Runnable exitSpringLoaded = new Runnable() {
776 @Override
777 public void run() {
778 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
779 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
780 }
781 };
782
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
786 if (resultCode == RESULT_CANCELED) {
787 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
789 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
792 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 }
794 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200795 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
796 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
797 mWorkspace.exitOverviewMode(false);
798 }
799 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700800 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
803 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700804
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 // We have special handling for widgets
806 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800807 final int appWidgetId;
808 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
809 : -1;
810 if (widgetId < 0) {
811 appWidgetId = pendingAddWidgetId;
812 } else {
813 appWidgetId = widgetId;
814 }
815
Adam Cohenad4e15c2013-10-17 16:21:35 -0700816 final int result;
817 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800818 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700819 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
820 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700821 result = RESULT_CANCELED;
822 completeTwoStageWidgetDrop(result, appWidgetId);
823 onComplete = new Runnable() {
824 @Override
825 public void run() {
826 exitSpringLoadedDragModeDelayed(false, 0, null);
827 }
828 };
Winson Chung5aaab772012-04-27 15:24:02 -0700829 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700831 final CellLayout dropLayout =
832 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
833 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700834 onComplete = new Runnable() {
835 @Override
836 public void run() {
837 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700838 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700839 }
840 };
Winson Chung5aaab772012-04-27 15:24:02 -0700841 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700842 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
843 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800844 return;
845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 // The pattern used here is that a user PICKs a specific application,
848 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
851 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700852 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800853 final PendingAddArguments args = new PendingAddArguments();
854 args.requestCode = requestCode;
855 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700857 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 args.cellX = mPendingAddInfo.cellX;
859 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800860 if (isWorkspaceLocked()) {
861 sPendingAddList.add(args);
862 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700865 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700867 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 }
873
874 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700875 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700876 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 Runnable onCompleteRunnable = null;
878 int animationType = 0;
879
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700880 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 if (resultCode == RESULT_OK) {
882 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
883 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700884 mPendingAddWidgetInfo);
885 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 onCompleteRunnable = new Runnable() {
887 @Override
888 public void run() {
889 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700891 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
892 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 }
894 };
895 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800896 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899 if (mDragLayer.getAnimatedView() != null) {
900 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
901 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
902 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700903 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700904 // The animated view may be null in the case of a rotation during widget configuration
905 onCompleteRunnable.run();
906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908
909 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700910 protected void onStop() {
911 super.onStop();
912 FirstFrameAnimatorHelper.setIsVisible(false);
913 }
914
915 @Override
916 protected void onStart() {
917 super.onStart();
918 FirstFrameAnimatorHelper.setIsVisible(true);
919 }
920
921 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200923 long startTime = 0;
924 if (DEBUG_RESUME_TIME) {
925 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400926 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700929
Winson Chung4a2afa32012-07-19 14:53:05 -0700930 // Restore the previous launcher state
931 if (mOnResumeState == State.WORKSPACE) {
932 showWorkspace(false);
933 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800934 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700935 }
936 mOnResumeState = State.NONE;
937
Craig Mautner360310b2012-10-26 15:13:08 -0700938 // Background was set to gradient in onPause(), restore to black if in all apps.
939 setWorkspaceBackground(mState == State.WORKSPACE);
940
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800941 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700942 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700943 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500945 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700947 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700949 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200950 // We might have postponed some bind calls until onResume (see waitUntilResume) --
951 // execute them here
952 long startTimeCallbacks = 0;
953 if (DEBUG_RESUME_TIME) {
954 startTimeCallbacks = System.currentTimeMillis();
955 }
956
957 if (mAppsCustomizeContent != null) {
958 mAppsCustomizeContent.setBulkBind(true);
959 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700960 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
961 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200962 }
963 if (mAppsCustomizeContent != null) {
964 mAppsCustomizeContent.setBulkBind(false);
965 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700966 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200967 if (DEBUG_RESUME_TIME) {
968 Log.d(TAG, "Time spent processing callbacks in onResume: " +
969 (System.currentTimeMillis() - startTimeCallbacks));
970 }
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Michael Jurka54554252013-08-01 12:52:23 +0200972 if (mOnResumeCallbacks.size() > 0) {
973 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
974 mOnResumeCallbacks.get(i).run();
975 }
976 mOnResumeCallbacks.clear();
977 }
Winson Chunge4e50662012-01-23 14:45:13 -0800978
979 // Reset the pressed state of icons that were locked in the press state while activities
980 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800981 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800982 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800983 mWaitingForResume.setStayPressed(false);
984 }
Winson Chunge4e50662012-01-23 14:45:13 -0800985 if (mAppsCustomizeContent != null) {
986 // Resets the previous all apps icon press state
987 mAppsCustomizeContent.resetDrawableState();
988 }
Winson Chung82963d52013-10-09 11:20:57 -0700989
Adam Cohen06dff352012-06-01 17:17:08 -0700990 // It is possible that widgets can receive updates while launcher is not in the foreground.
991 // Consequently, the widgets will be inflated in the orientation of the foreground activity
992 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
993 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700994 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700995
Winson Chung780fe592013-09-26 14:48:44 -0700996 // Process any items that were added while Launcher was away.
997 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
998
Winson Chung5841efa2013-09-30 18:06:44 -0700999 // Update the voice search button proxy
1000 updateVoiceButtonProxyVisible(false);
1001
Adam Cohenf9426d52012-06-04 17:26:21 -07001002 // Again, as with the above scenario, it's possible that one or more of the global icons
1003 // were updated in the wrong orientation.
1004 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001005 if (DEBUG_RESUME_TIME) {
1006 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1007 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001008
1009 if (mWorkspace.getCustomContentCallbacks() != null) {
1010 // If we are resuming and the custom content is the current page, we call onShow().
1011 // It is also poassible that onShow will instead be called slightly after first layout
1012 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1013 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001014 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001015 }
1016 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001017 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001018 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001022 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001023 // Ensure that items added to Launcher are queued until Launcher returns
1024 InstallShortcutReceiver.enableInstallQueue();
1025
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001027 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001028 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001029 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001030
1031 // We call onHide() aggressively. The custom content callbacks should be able to
1032 // debounce excess onHide calls.
1033 if (mWorkspace.getCustomContentCallbacks() != null) {
1034 mWorkspace.getCustomContentCallbacks().onHide();
1035 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
Adam Cohenbffe7452013-07-22 18:21:45 -07001038 QSBScroller mQsbScroller = new QSBScroller() {
1039 int scrollY = 0;
1040
1041 @Override
1042 public void setScrollY(int scroll) {
1043 scrollY = scroll;
1044
1045 if (mWorkspace.isOnOrMovingToCustomContent()) {
1046 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001047 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001048 }
1049 }
1050 };
1051
1052 public void resetQSBScroll() {
1053 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001054 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001055 }
1056
1057 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001058 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1059 // by a onResume or by scrolling otherwise.
1060 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001061
1062 // Custom content is completely hidden
1063 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001064
1065 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1066 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 }
1068
Jorim Jaggid017f882014-01-14 17:08:48 -08001069 protected boolean hasSettings() {
1070 return false;
1071 }
1072
Adam Cohenbffe7452013-07-22 18:21:45 -07001073 public interface QSBScroller {
1074 public void setScrollY(int scrollY);
1075 }
1076
Winson Chung98ca0f72013-07-29 12:58:51 -07001077 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001078 CustomContentCallbacks callbacks, String description) {
1079 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001081 }
1082
Adam Cohenedb40762013-07-18 16:45:45 -07001083 // The custom content needs to offset its content to account for the QSB
1084 public int getTopOffsetForCustomContent() {
1085 return mWorkspace.getPaddingTop();
1086 }
1087
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001088 @Override
1089 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 // Flag the loader to stop early before switching
1091 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001092 if (mAppsCustomizeContent != null) {
1093 mAppsCustomizeContent.surrender();
1094 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001095 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001096 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001098 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001099 @Override
1100 public void onWindowFocusChanged(boolean hasFocus) {
1101 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001102 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001103 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 private boolean acceptFilter() {
1106 final InputMethodManager inputManager = (InputMethodManager)
1107 getSystemService(Context.INPUT_METHOD_SERVICE);
1108 return !inputManager.isFullscreenMode();
1109 }
1110
1111 @Override
1112 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001113 final int uniChar = event.getUnicodeChar();
1114 final boolean handled = super.onKeyDown(keyCode, event);
1115 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1116 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1118 keyCode, event);
1119 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001120 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001121 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001122 // showSearchDialog()
1123 // If there are multiple keystrokes before the search dialog takes focus,
1124 // onSearchRequested() will be called for every keystroke,
1125 // but it is idempotent, so it's fine.
1126 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128 }
1129
Joe Onorato8a9625e2010-01-28 15:55:35 -08001130 // Eat the long press event so the keyboard doesn't come up.
1131 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1132 return true;
1133 }
1134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 return handled;
1136 }
1137
Karl Rosaen138a0412009-04-23 19:00:21 -07001138 private String getTypedText() {
1139 return mDefaultKeySsb.toString();
1140 }
1141
1142 private void clearTypedText() {
1143 mDefaultKeySsb.clear();
1144 mDefaultKeySsb.clearSpans();
1145 Selection.setSelection(mDefaultKeySsb, 0);
1146 }
1147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001149 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1150 * State
1151 */
1152 private static State intToState(int stateOrdinal) {
1153 State state = State.WORKSPACE;
1154 final State[] stateValues = State.values();
1155 for (int i = 0; i < stateValues.length; i++) {
1156 if (stateValues[i].ordinal() == stateOrdinal) {
1157 state = stateValues[i];
1158 break;
1159 }
1160 }
1161 return state;
1162 }
1163
1164 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 * Restores the previous state, if it exists.
1166 *
1167 * @param savedState The previous state.
1168 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001169 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 private void restoreState(Bundle savedState) {
1171 if (savedState == null) {
1172 return;
1173 }
1174
Michael Jurka883f55b2010-10-21 15:47:14 -07001175 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001176 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001177 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001178 }
1179
Adam Cohen21cd0022013-10-09 18:57:02 -07001180 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1181 PagedView.INVALID_RESTORE_PAGE);
1182 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001183 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185
Winson Chung3d503fb2011-07-13 17:25:49 -07001186 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001187 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1190 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001191 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001192 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1193 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001194 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1195 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1196 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001197 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001198 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 mRestoring = true;
1200 }
1201
1202 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1203 if (renameFolder) {
1204 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001205 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 mRestoring = true;
1207 }
Winson Chunga12a2502010-12-20 14:41:35 -08001208
Winson Chung785d2eb2011-04-14 16:08:02 -07001209 // Restore the AppsCustomize tab
1210 if (mAppsCustomizeTabHost != null) {
1211 String curTab = savedState.getString("apps_customize_currentTab");
1212 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001213 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001215 mAppsCustomizeContent.loadAssociatedPages(
1216 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001217 }
1218
Winson Chung5afbf7b2011-07-25 11:53:08 -07001219 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1220 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001221 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001222 mItemIdToViewId = (HashMap<Integer, Integer>)
1223 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
1226 /**
1227 * Finds all the views we need and configure them properly.
1228 */
1229 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001230 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001231
Craig Mautner360310b2012-10-26 15:13:08 -07001232 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001233 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1234 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001235 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001236 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001237
John Spurlock77e1f472013-09-11 10:09:51 -04001238 mLauncherView.setSystemUiVisibility(
1239 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001240 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
Winson Chung4c98d922011-05-31 16:50:48 -07001242 // Setup the drag layer
1243 mDragLayer.setup(this, dragController);
1244
Winson Chung3d503fb2011-07-13 17:25:49 -07001245 // Setup the hotseat
1246 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1247 if (mHotseat != null) {
1248 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001249 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001250 }
1251
Jorim Jaggid017f882014-01-14 17:08:48 -08001252 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001253 View widgetButton = findViewById(R.id.widget_button);
1254 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001255 @Override
1256 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001257 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001258 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001259 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 }
1261 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001262 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1263
1264 View wallpaperButton = findViewById(R.id.wallpaper_button);
1265 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001266 @Override
1267 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001268 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001269 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001270 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001271 }
1272 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001273 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1274
1275 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001276 if (hasSettings()) {
1277 settingsButton.setOnClickListener(new OnClickListener() {
1278 @Override
1279 public void onClick(View arg0) {
1280 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001281 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001282 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001283 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001284 });
1285 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1286 } else {
1287 settingsButton.setVisibility(View.GONE);
1288 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1289 lp.gravity = Gravity.END | Gravity.TOP;
1290 widgetButton.requestLayout();
1291 }
1292
Adam Cohendbdff6b2013-09-18 19:09:15 -07001293 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001294
Winson Chung4c98d922011-05-31 16:50:48 -07001295 // Setup the workspace
1296 mWorkspace.setHapticFeedbackEnabled(false);
1297 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001298 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001299 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001300
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001302 mSearchDropTargetBar = (SearchDropTargetBar)
1303 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001304
Winson Chungf0ea4d32011-06-06 14:27:16 -07001305 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001306 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001307 mAppsCustomizeContent = (AppsCustomizePagedView)
1308 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1309 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001312 dragController.setDragScoller(mWorkspace);
1313 dragController.setScrollView(mDragLayer);
1314 dragController.setMoveTarget(mWorkspace);
1315 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001316 if (mSearchDropTargetBar != null) {
1317 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001318 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001319
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001320 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001321 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001322 mWeightWatcher = new WeightWatcher(this);
1323 mWeightWatcher.setAlpha(0.5f);
1324 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001325 new FrameLayout.LayoutParams(
1326 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001327 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001328 Gravity.BOTTOM)
1329 );
Adam Cohen39a06042013-07-19 14:30:12 -07001330
1331 boolean show = shouldShowWeightWatcher();
1332 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
1336 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001337 * Sets the all apps button. This method is called from {@link Hotseat}.
1338 */
1339 public void setAllAppsButton(View allAppsButton) {
1340 mAllAppsButton = allAppsButton;
1341 }
1342
1343 public View getAllAppsButton() {
1344 return mAllAppsButton;
1345 }
1346
1347 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 * Creates a view representing a shortcut.
1349 *
1350 * @param info The data structure describing the shortcut.
1351 *
1352 * @return A View inflated from R.layout.application.
1353 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001354 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001356 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
1359 /**
1360 * Creates a view representing a shortcut inflated from the specified resource.
1361 *
1362 * @param layoutResId The id of the XML layout used to create the shortcut.
1363 * @param parent The group the shortcut belongs to.
1364 * @param info The data structure describing the shortcut.
1365 *
1366 * @return A View inflated from layoutResId.
1367 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001368 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001369 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1370 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 return favorite;
1373 }
1374
1375 /**
1376 * Add an application shortcut to the workspace.
1377 *
1378 * @param data The intent describing the application.
1379 * @param cellInfo The position on screen where to create the shortcut.
1380 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001381 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001382 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001383 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384
Adam Cohenfbba09b2011-07-18 21:43:05 -07001385 // First we check if we already know the exact location where we want to add this item.
1386 if (cellX >= 0 && cellY >= 0) {
1387 cellXY[0] = cellX;
1388 cellXY[1] = cellY;
1389 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001390 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001391 return;
1392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001394 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001395
Romain Guy73b979d2009-06-09 12:57:21 -07001396 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001397 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001399 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001400 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001401 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001402 } else {
1403 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405 }
Romain Guycbb89e42009-06-08 15:52:54 -07001406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 /**
1408 * Add a shortcut to the workspace.
1409 *
1410 * @param data The intent describing the shortcut.
1411 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001413 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 int cellY) {
1415 int[] cellXY = mTmpAddItemCellCoordinates;
1416 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001417 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001418
Michael Jurkad3ef3062010-11-23 16:23:58 -08001419 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001420
Adam Cohen558baaf2011-08-15 15:22:57 -07001421 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001422 if (info == null) {
1423 return;
1424 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001425 final View view = createShortcut(info);
1426
Adam Cohenfbba09b2011-07-18 21:43:05 -07001427 // First we check if we already know the exact location where we want to add this item.
1428 if (cellX >= 0 && cellY >= 0) {
1429 cellXY[0] = cellX;
1430 cellXY[1] = cellY;
1431 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001432
1433 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001434 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001435 true, null,null)) {
1436 return;
1437 }
1438 DragObject dragObject = new DragObject();
1439 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001440 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1441 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001442 return;
1443 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001444 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001445 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001447 foundCellSpan = (result != null);
1448 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001449 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001450 }
1451
1452 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001453 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001454 return;
1455 }
1456
Adam Cohendcd297f2013-06-18 13:13:40 -07001457 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458
1459 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001461 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463 }
1464
Adam Cohen2f093b62012-04-30 18:59:53 -07001465 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1466 int minHeight) {
1467 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001468 // We want to account for the extra amount of padding that we are adding to the widget
1469 // to ensure that it gets the full amount of space that it has requested
1470 int requiredWidth = minWidth + padding.left + padding.right;
1471 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001472 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001473 }
1474
Adam Cohen2f093b62012-04-30 18:59:53 -07001475 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1476 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001477 }
1478
Adam Cohen2f093b62012-04-30 18:59:53 -07001479 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1480 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001481 }
1482
Adam Cohen2f093b62012-04-30 18:59:53 -07001483 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1484 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001485 }
1486
Adam Cohen2f093b62012-04-30 18:59:53 -07001487 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1488 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001489 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001490 }
1491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001493 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001495 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001496 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001498 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001499 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1500 if (appWidgetInfo == null) {
1501 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1502 }
Romain Guycbb89e42009-06-08 15:52:54 -07001503
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001504 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001506
Adam Cohen2f093b62012-04-30 18:59:53 -07001507 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1508 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001511 // We have saved the position to which the widget was dragged-- this really only matters
1512 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int[] cellXY = mTmpAddItemCellCoordinates;
1514 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001515 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1518 cellXY[0] = mPendingAddInfo.cellX;
1519 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001520 spanXY[0] = mPendingAddInfo.spanX;
1521 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001522 foundCellSpan = true;
1523 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001526 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1527 spanXY[1], cellXY, finalSpan);
1528 spanXY[0] = finalSpan[0];
1529 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001530 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001532 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001533 }
1534
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001536 if (appWidgetId != -1) {
1537 // Deleting an app widget ID is a void call but writes to disk before returning
1538 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001539 new AsyncTask<Void, Void, Void>() {
1540 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001541 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001542 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001543 }
Michael Jurka43467462013-11-14 12:03:58 +01001544 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001545 }
Winson Chung93eef082012-03-23 15:59:27 -07001546 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001547 return;
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001550 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001551 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1552 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 launcherInfo.spanX = spanXY[0];
1554 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001555 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1556 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 if (hostView == null) {
1563 // Perform actual inflation because we're live
1564 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1565 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1566 } else {
1567 // The AppWidgetHostView has already been inflated and instantiated
1568 launcherInfo.hostView = hostView;
1569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001572 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001573 launcherInfo.notifyWidgetSizeChanged(this);
1574
Adam Cohendcd297f2013-06-18 13:13:40 -07001575 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001577
1578 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001580 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
Romain Guycbb89e42009-06-08 15:52:54 -07001582
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1584 @Override
1585 public void onReceive(Context context, Intent intent) {
1586 final String action = intent.getAction();
1587 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1588 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001589 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001591
Winson Chungc100e8e2011-08-09 16:02:43 -07001592 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001593 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001594 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001595 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001596 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1598 mUserPresent = true;
1599 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001600 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1601 mModel.resetLoadedState(false, true);
1602 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1603 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1604 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1605 mModel.resetLoadedState(false, true);
1606 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1607 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1608 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001609 }
1610 }
1611 };
1612
1613 @Override
1614 public void onAttachedToWindow() {
1615 super.onAttachedToWindow();
1616
1617 // Listen for broadcasts related to user-presence
1618 final IntentFilter filter = new IntentFilter();
1619 filter.addAction(Intent.ACTION_SCREEN_OFF);
1620 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001621 if (ENABLE_DEBUG_INTENTS) {
1622 filter.addAction(DebugIntents.DELETE_DATABASE);
1623 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1624 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001626 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001627 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 mVisible = true;
1629 }
1630
1631 @Override
1632 public void onDetachedFromWindow() {
1633 super.onDetachedFromWindow();
1634 mVisible = false;
1635
Adam Cohend113e0c2010-11-11 10:48:05 -08001636 if (mAttached) {
1637 unregisterReceiver(mReceiver);
1638 mAttached = false;
1639 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 updateRunning();
1641 }
1642
1643 public void onWindowVisibilityChanged(int visibility) {
1644 mVisible = visibility == View.VISIBLE;
1645 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001646 // The following code used to be in onResume, but it turns out onResume is called when
1647 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1648 // is a more appropriate event to handle
1649 if (mVisible) {
1650 mAppsCustomizeTabHost.onWindowVisible();
1651 if (!mWorkspaceLoading) {
1652 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001653 // We want to let Launcher draw itself at least once before we force it to build
1654 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001655 // apps is nice and speedy.
1656 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001657 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001659 if (mStarted) return;
1660 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001661 // We delay the layer building a bit in order to give
1662 // other message processing a time to run. In particular
1663 // this avoids a delay in hiding the IME if it was
1664 // currently shown, because doing that may involve
1665 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001666 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001667 final ViewTreeObserver.OnDrawListener listener = this;
1668 mWorkspace.post(new Runnable() {
1669 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001670 if (mWorkspace != null &&
1671 mWorkspace.getViewTreeObserver() != null) {
1672 mWorkspace.getViewTreeObserver().
1673 removeOnDrawListener(listener);
1674 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001675 }
1676 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001677 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001678 }
1679 });
1680 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001681 // When Launcher comes back to foreground, a different Activity might be responsible for
1682 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001683 if (!DISABLE_MARKET_BUTTON) {
1684 updateAppMarketIcon();
1685 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001686 clearTypedText();
1687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 }
1689
1690 private void sendAdvanceMessage(long delay) {
1691 mHandler.removeMessages(ADVANCE_MSG);
1692 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1693 mHandler.sendMessageDelayed(msg, delay);
1694 mAutoAdvanceSentTime = System.currentTimeMillis();
1695 }
1696
1697 private void updateRunning() {
1698 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1699 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1700 mAutoAdvanceRunning = autoAdvanceRunning;
1701 if (autoAdvanceRunning) {
1702 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1703 sendAdvanceMessage(delay);
1704 } else {
1705 if (!mWidgetsToAdvance.isEmpty()) {
1706 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1707 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1708 }
1709 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001710 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 }
1712 }
1713 }
1714
1715 private final Handler mHandler = new Handler() {
1716 @Override
1717 public void handleMessage(Message msg) {
1718 if (msg.what == ADVANCE_MSG) {
1719 int i = 0;
1720 for (View key: mWidgetsToAdvance.keySet()) {
1721 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1722 final int delay = mAdvanceStagger * i;
1723 if (v instanceof Advanceable) {
1724 postDelayed(new Runnable() {
1725 public void run() {
1726 ((Advanceable) v).advance();
1727 }
1728 }, delay);
1729 }
1730 i++;
1731 }
1732 sendAdvanceMessage(mAdvanceInterval);
1733 }
1734 }
1735 };
1736
1737 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001738 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1740 if (v instanceof Advanceable) {
1741 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001742 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 updateRunning();
1744 }
1745 }
1746
1747 void removeWidgetToAutoAdvance(View hostView) {
1748 if (mWidgetsToAdvance.containsKey(hostView)) {
1749 mWidgetsToAdvance.remove(hostView);
1750 updateRunning();
1751 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001752 }
1753
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 launcherInfo.hostView = null;
1757 }
1758
Winson Chung93eef082012-03-23 15:59:27 -07001759 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1760 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1761 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
1763
Winson Chunga6945242014-01-08 14:04:34 -08001764 public DragLayer getDragLayer() {
1765 return mDragLayer;
1766 }
1767
1768 public Workspace getWorkspace() {
1769 return mWorkspace;
1770 }
1771
1772 public Hotseat getHotseat() {
1773 return mHotseat;
1774 }
1775
Jorim Jaggid017f882014-01-14 17:08:48 -08001776 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001777 return mOverviewPanel;
1778 }
1779
1780 public SearchDropTargetBar getSearchBar() {
1781 return mSearchDropTargetBar;
1782 }
1783
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001784 public LauncherAppWidgetHost getAppWidgetHost() {
1785 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
Romain Guycbb89e42009-06-08 15:52:54 -07001787
Winson Chunga9abd0e2010-10-27 17:18:37 -07001788 public LauncherModel getModel() {
1789 return mModel;
1790 }
1791
Winson Chunga6945242014-01-08 14:04:34 -08001792 public LauncherClings getLauncherClings() {
1793 return mLauncherClings;
1794 }
1795
1796 protected SharedPreferences getSharedPrefs() {
1797 return mSharedPrefs;
1798 }
1799
Bjorn Bringertc459e522013-06-07 19:36:01 +01001800 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001801 getWindow().closeAllPanels();
1802
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001803 // Whatever we were doing is hereby canceled.
1804 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001805 }
1806
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 @Override
1808 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001809 long startTime = 0;
1810 if (DEBUG_RESUME_TIME) {
1811 startTime = System.currentTimeMillis();
1812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 super.onNewIntent(intent);
1814
1815 // Close the menu
1816 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001817 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001818 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001819
Adam Cohened307df2013-10-02 09:37:31 -07001820 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1821 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1822 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001823
Adam Cohen6fecd412013-10-02 17:41:50 -07001824 if (mWorkspace == null) {
1825 // Can be cases where mWorkspace is null, this prevents a NPE
1826 return;
1827 }
1828 Folder openFolder = mWorkspace.getOpenFolder();
1829 // In all these cases, only animate if we're already on home
1830 mWorkspace.exitWidgetResizeMode();
1831 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001832 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001833 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001834 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001835
Adam Cohen6fecd412013-10-02 17:41:50 -07001836 closeFolder();
1837 exitSpringLoadedDragMode();
1838
1839 // If we are already on home, then just animate back to the workspace,
1840 // otherwise, just wait until onResume to set the state back to Workspace
1841 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001842 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001843 } else {
1844 mOnResumeState = State.WORKSPACE;
1845 }
1846
1847 final View v = getWindow().peekDecorView();
1848 if (v != null && v.getWindowToken() != null) {
1849 InputMethodManager imm = (InputMethodManager)getSystemService(
1850 INPUT_METHOD_SERVICE);
1851 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1852 }
1853
1854 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001855 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001856 mAppsCustomizeTabHost.reset();
1857 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001858
1859 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
Adam Cohened307df2013-10-02 09:37:31 -07001861
Michael Jurka447bf842013-05-15 14:52:15 +02001862 if (DEBUG_RESUME_TIME) {
1863 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866
Adam Coppa120b8e2013-11-12 16:26:04 +00001867 /**
1868 * Override point for subclasses to prevent movement to the default screen when the home
1869 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1870 */
1871 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1872 return true;
1873 }
1874
1875 /**
1876 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1877 */
1878 protected void onHomeIntent() {
1879 // Do nothing
1880 }
1881
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001883 public void onRestoreInstanceState(Bundle state) {
1884 super.onRestoreInstanceState(state);
1885 for (int page: mSynchronouslyBoundPages) {
1886 mWorkspace.restoreInstanceStateForChild(page);
1887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 }
1889
1890 @Override
1891 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001892 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001893 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1894 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001895 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001896 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897
Michael Jurka883f55b2010-10-21 15:47:14 -07001898 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001899 // We close any open folder since it will not be re-opened, and we need to make sure
1900 // this state is reflected.
1901 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902
Adam Cohendcd297f2013-06-18 13:13:40 -07001903 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001904 mWaitingForResult) {
1905 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1908 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001909 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1910 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1911 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001912 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914
1915 if (mFolderInfo != null && mWaitingForResult) {
1916 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1917 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1918 }
Michael Jurka946ad472010-07-09 18:05:18 -07001919
Winson Chung785d2eb2011-04-14 16:08:02 -07001920 // Save the current AppsCustomize tab
1921 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001922 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1923 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001924 if (currentTabTag != null) {
1925 outState.putString("apps_customize_currentTab", currentTabTag);
1926 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001927 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1928 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001929 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001930 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
1933 @Override
1934 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001936
Winson Chunge7a03942011-08-05 15:05:12 -07001937 // Remove all pending runnables
1938 mHandler.removeMessages(ADVANCE_MSG);
1939 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001940 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001941
Winson Chungcd2b0142011-06-08 16:02:26 -07001942 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001943 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001944 mModel.stopLoader();
1945 app.setLauncher(null);
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001948 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001950 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001952 mAppWidgetHost = null;
1953
1954 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
1956 TextKeyListener.getInstance().release();
1957
Winson Chung81b52252012-08-27 15:34:29 -07001958 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1959 // to prevent leaking Launcher activities on orientation change.
1960 if (mModel != null) {
1961 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1962 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001963
1964 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001965 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001966
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001967 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001968 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001969 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001970 mWorkspace = null;
1971 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001972
Michael Jurka2ecf9952012-06-18 12:52:28 -07001973 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
Adam Cohena9cf38f2011-05-02 15:36:58 -07001976 public DragController getDragController() {
1977 return mDragController;
1978 }
1979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 @Override
1981 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001982 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 super.startActivityForResult(intent, requestCode);
1984 }
1985
Winson Chung70d72102011-08-12 11:24:35 -07001986 /**
1987 * Indicates that we want global search for this activity by setting the globalSearch
1988 * argument for {@link #startSearch} to true.
1989 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001991 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001993
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001994 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001995
Karl Rosaen138a0412009-04-23 19:00:21 -07001996 if (initialQuery == null) {
1997 // Use any text typed in the launcher as the initial query
1998 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 if (appSearchData == null) {
2001 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002002 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
Winson Chungadf0c182012-08-23 14:59:07 -07002004 Rect sourceBounds = new Rect();
2005 if (mSearchDropTargetBar != null) {
2006 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2007 }
Romain Guycbb89e42009-06-08 15:52:54 -07002008
Bjorn Bringertc459e522013-06-07 19:36:01 +01002009 startSearch(initialQuery, selectInitialQuery,
2010 appSearchData, sourceBounds);
2011 }
2012
2013 public void startSearch(String initialQuery,
2014 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002015 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002016 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002017 }
2018
2019 /**
2020 * Starts the global search activity. This code is a copied from SearchManager
2021 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002022 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002023 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002024 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002025 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2026 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2027 if (globalSearchActivity == null) {
2028 Log.w(TAG, "No global search activity found.");
2029 return;
2030 }
2031 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2032 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2033 intent.setComponent(globalSearchActivity);
2034 // Make sure that we have a Bundle to put source in
2035 if (appSearchData == null) {
2036 appSearchData = new Bundle();
2037 } else {
2038 appSearchData = new Bundle(appSearchData);
2039 }
2040 // Set source to package name of app that starts global search, if not set already.
2041 if (!appSearchData.containsKey("source")) {
2042 appSearchData.putString("source", getPackageName());
2043 }
2044 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2045 if (!TextUtils.isEmpty(initialQuery)) {
2046 intent.putExtra(SearchManager.QUERY, initialQuery);
2047 }
2048 if (selectInitialQuery) {
2049 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2050 }
2051 intent.setSourceBounds(sourceBounds);
2052 try {
2053 startActivity(intent);
2054 } catch (ActivityNotFoundException ex) {
2055 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058
Yura4f93ec62014-02-04 14:15:21 +00002059 public boolean isOnCustomContent() {
2060 return mWorkspace.isOnOrMovingToCustomContent();
2061 }
2062
Winson Chung70d72102011-08-12 11:24:35 -07002063 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002064 public boolean onPrepareOptionsMenu(Menu menu) {
2065 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002066 if (!isOnCustomContent()) {
2067 // Close any open folders
2068 closeFolder();
2069 // Stop resizing any widgets
2070 mWorkspace.exitWidgetResizeMode();
2071 if (!mWorkspace.isInOverviewMode()) {
2072 // Show the overview mode
2073 showOverviewMode(true);
2074 } else {
2075 showWorkspace(true);
2076 }
Winson Chunge0298742014-01-17 12:03:00 -08002077 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002078 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002081 @Override
2082 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002083 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002084 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002085 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002086 }
2087
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088 public boolean isWorkspaceLocked() {
2089 return mWorkspaceLoading || mWaitingForResult;
2090 }
2091
Adam Cohen517a7f52014-03-01 12:12:59 -08002092 public boolean isWorkspaceLoading() {
2093 return mWorkspaceLoading;
2094 }
2095
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002097 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002098 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002099 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2100 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002101 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002102 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002103 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002104
Adam Cohenad4e15c2013-10-17 16:21:35 -07002105 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2106 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2107 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2108 }
2109
2110 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2111 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2112 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002113 if (appWidgetInfo.configure != null) {
2114 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002115 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002116
Bjorn Bringert7984c942009-12-09 15:38:25 +00002117 // Launch over to configure widget, if needed
2118 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002119 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002120 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002121 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002123 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002124 Runnable onComplete = new Runnable() {
2125 @Override
2126 public void run() {
2127 // Exit spring loaded mode if necessary after adding the widget
2128 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2129 null);
2130 }
2131 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002132 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002133 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002134 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136 }
Romain Guycbb89e42009-06-08 15:52:54 -07002137
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002138 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002139 // Close any folders that may be open.
2140 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002141 mWorkspace.moveToCustomContentScreen(animate);
2142 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002143 /**
2144 * Process a shortcut drop.
2145 *
2146 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002147 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002148 * @param cell The cell it should be added to, optional
2149 * @param position The location on the screen where it was dropped, optional
2150 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002151 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002152 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002153 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002154 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002155 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002156 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002157
2158 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002159 mPendingAddInfo.cellX = cell[0];
2160 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002161 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002162
2163 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2164 createShortcutIntent.setComponent(componentName);
2165 processShortcut(createShortcutIntent);
2166 }
2167
Adam Cohenfbba09b2011-07-18 21:43:05 -07002168 /**
2169 * Process a widget drop.
2170 *
2171 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002172 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002173 * @param cell The cell it should be added to, optional
2174 * @param position The location on the screen where it was dropped, optional
2175 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002176 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002177 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002178 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002179 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002180 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002182 mPendingAddInfo.minSpanX = info.minSpanX;
2183 mPendingAddInfo.minSpanY = info.minSpanY;
2184
Adam Cohenfbba09b2011-07-18 21:43:05 -07002185 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002186 mPendingAddInfo.cellX = cell[0];
2187 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002188 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002189 if (span != null) {
2190 mPendingAddInfo.spanX = span[0];
2191 mPendingAddInfo.spanY = span[1];
2192 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002193
Adam Cohened66b2b2012-01-23 17:28:51 -08002194 AppWidgetHostView hostView = info.boundWidget;
2195 int appWidgetId;
2196 if (hostView != null) {
2197 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002199 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002200 // In this case, we either need to start an activity to get permission to bind
2201 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002202 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002203 Bundle options = info.bindOptions;
2204
2205 boolean success = false;
2206 if (options != null) {
2207 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2208 info.componentName, options);
2209 } else {
2210 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2211 info.componentName);
2212 }
2213 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002215 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002216 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002217 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2218 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2219 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002220 // TODO: we need to make sure that this accounts for the options bundle.
2221 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002222 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2223 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002224 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002225 }
2226
Joe Onoratodeb98af2010-02-19 14:59:39 -08002227 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002228 // Handle case where user selected "Applications"
2229 String applicationName = getResources().getString(R.string.group_applications);
2230 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002231
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002232 if (applicationName != null && applicationName.equals(shortcutName)) {
2233 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2234 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002235
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002236 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2237 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002238 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002239 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002240 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002241 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 }
2244
Winson Chung24ab2f12010-09-16 14:10:47 -07002245 void processWallpaper(Intent intent) {
2246 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2247 }
2248
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002251 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 folderInfo.title = getText(R.string.folder_name);
2253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002257 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258
2259 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 FolderIcon newFolder =
2261 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002264 // Force measure the new folder icon
2265 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2266 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002267 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 }
Romain Guycbb89e42009-06-08 15:52:54 -07002269
Joe Onorato9c1289c2009-08-17 11:03:03 -04002270 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002271 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002272 }
2273
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002274 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002275 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002276 }
2277
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002278 /**
2279 * Registers various content observers. The current implementation registers
2280 * only a favorites observer to keep track of the favorites applications.
2281 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002282 private void registerContentObservers() {
2283 ContentResolver resolver = getContentResolver();
2284 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2285 true, mWidgetObserver);
2286 }
2287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 @Override
2289 public boolean dispatchKeyEvent(KeyEvent event) {
2290 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2291 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002293 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002294 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002295 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002296 dumpState();
2297 return true;
2298 }
2299 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002300 }
2301 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2302 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002303 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 return true;
2305 }
2306 }
2307
2308 return super.dispatchKeyEvent(event);
2309 }
2310
Joe Onorato88ec0992009-11-19 13:16:06 -08002311 @Override
2312 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002313 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002314 if (mAppsCustomizeContent.getContentType() ==
2315 AppsCustomizePagedView.ContentType.Applications) {
2316 showWorkspace(true);
2317 } else {
2318 showOverviewMode(true);
2319 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002320 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002321 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002322 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002323 Folder openFolder = mWorkspace.getOpenFolder();
2324 if (openFolder.isEditingName()) {
2325 openFolder.dismissEditingName();
2326 } else {
2327 closeFolder();
2328 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002329 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002330 mWorkspace.exitWidgetResizeMode();
2331
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002332 // Back button is a no-op here, but give at least some feedback for the button press
2333 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002334 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002335 }
2336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002338 * Re-listen when widgets are reset.
2339 */
2340 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002341 if (mAppWidgetHost != null) {
2342 mAppWidgetHost.startListening();
2343 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002344 }
2345
2346 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 * Launches the intent referred by the clicked shortcut.
2348 *
2349 * @param v The view representing the clicked shortcut.
2350 */
2351 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002352 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2353 // view has detached (it's possible for this to happen if the view is removed mid touch).
2354 if (v.getWindowToken() == null) {
2355 return;
2356 }
2357
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002358 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002359 return;
2360 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002361
Adam Cohen1697b792013-09-17 19:08:21 -07002362 if (v instanceof Workspace) {
2363 if (mWorkspace.isInOverviewMode()) {
2364 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002365 }
2366 return;
2367 }
2368
2369 if (v instanceof CellLayout) {
2370 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002371 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002372 }
2373 }
2374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002376 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002377 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002379 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002380 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002381 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002382 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002383 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 }
2385 }
2386
Michael Jurka0e260592010-06-30 17:07:39 -07002387 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002388 return false;
2389 }
2390
Michael Jurkaaf442092010-06-10 17:01:57 -07002391 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002392 * Event handler for the search button
2393 *
2394 * @param v The view that was clicked.
2395 */
2396 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002397 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2398
Amith Yamasania135ba82011-08-09 17:42:01 -07002399 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002400 }
2401
2402 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002403 * Event handler for the voice button
2404 *
2405 * @param v The view that was clicked.
2406 */
2407 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002408 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002409
Bjorn Bringertc459e522013-06-07 19:36:01 +01002410 startVoice();
2411 }
2412
2413 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002414 try {
2415 final SearchManager searchManager =
2416 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2417 ComponentName activityName = searchManager.getGlobalSearchActivity();
2418 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002419 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002420 if (activityName != null) {
2421 intent.setPackage(activityName.getPackageName());
2422 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002423 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002424 } catch (ActivityNotFoundException e) {
2425 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002426 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002427 startActivitySafely(null, intent, "onClickVoiceButton");
2428 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002429 }
2430
2431 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002432 * Event handler for the "grid" button that appears on the home screen, which
2433 * enters all apps mode.
2434 *
2435 * @param v The view that was clicked.
2436 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002437 protected void onClickAllAppsButton(View v) {
2438 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2439 if (isAllAppsVisible()) {
2440 showWorkspace(true);
2441 } else {
2442 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2443 }
2444 }
2445
2446 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002447 * Event handler for a paged view icon click.
2448 * @param v The view that was clicked.
2449 * @param appInfo The {link AppInfo} of the view.
2450 */
2451 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2452 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2453 startActivitySafely(v, appInfo.intent, appInfo);
2454 getStats().recordLaunch(appInfo.intent);
2455 }
2456
2457 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002458 * Event handler for an app shortcut click.
2459 *
2460 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2461 */
2462 protected void onClickAppShortcut(View v) {
2463 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2464 Object tag = v.getTag();
2465 if (!(tag instanceof ShortcutInfo)) {
2466 throw new IllegalArgumentException("Input must be a Shortcut");
2467 }
2468
2469 // Open shortcut
2470 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2471 final Intent intent = shortcut.intent;
2472
2473 // Check for special shortcuts
2474 if (intent.getComponent() != null) {
2475 final String shortcutClass = intent.getComponent().getClassName();
2476
2477 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2478 MemoryDumpActivity.startDump(this);
2479 return;
2480 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2481 toggleShowWeightWatcher();
2482 return;
2483 }
2484 }
2485
2486 // Start activities
2487 int[] pos = new int[2];
2488 v.getLocationOnScreen(pos);
2489 intent.setSourceBounds(new Rect(pos[0], pos[1],
2490 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2491
2492 boolean success = startActivitySafely(v, intent, tag);
2493
2494 mStats.recordLaunch(intent, shortcut);
2495
2496 if (success && v instanceof BubbleTextView) {
2497 mWaitingForResume = (BubbleTextView) v;
2498 mWaitingForResume.setStayPressed(true);
2499 }
2500 }
2501
2502 /**
2503 * Event handler for a folder icon click.
2504 *
2505 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2506 */
2507 protected void onClickFolderIcon(View v) {
2508 if (LOGD) Log.d(TAG, "onClickFolder");
2509 if (!(v instanceof FolderIcon)){
2510 throw new IllegalArgumentException("Input must be a FolderIcon");
2511 }
2512
2513 FolderIcon folderIcon = (FolderIcon) v;
2514 final FolderInfo info = folderIcon.getFolderInfo();
2515 Folder openFolder = mWorkspace.getFolderForTag(info);
2516
2517 // If the folder info reports that the associated folder is open, then verify that
2518 // it is actually opened. There have been a few instances where this gets out of sync.
2519 if (info.opened && openFolder == null) {
2520 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2521 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2522 info.opened = false;
2523 }
2524
2525 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2526 // Close any open folder
2527 closeFolder();
2528 // Open the requested folder
2529 openFolder(folderIcon);
2530 } else {
2531 // Find the open folder...
2532 int folderScreen;
2533 if (openFolder != null) {
2534 folderScreen = mWorkspace.getPageForView(openFolder);
2535 // .. and close it
2536 closeFolder(openFolder);
2537 if (folderScreen != mWorkspace.getCurrentPage()) {
2538 // Close any folder open on the current screen
2539 closeFolder();
2540 // Pull the folder onto this screen
2541 openFolder(folderIcon);
2542 }
2543 }
2544 }
Michael Jurka5130e402011-10-13 04:55:35 -07002545 }
2546
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002547 /**
2548 * Event handler for the (Add) Widgets button that appears after a long press
2549 * on the home screen.
2550 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002551 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002552 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002553 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2554 }
2555
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002556 /**
2557 * Event handler for the wallpaper picker button that appears after a long press
2558 * on the home screen.
2559 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002560 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002561 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2562 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2563 pickWallpaper.setComponent(getWallpaperPickerComponent());
2564 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2565 }
2566
2567 /**
2568 * Event handler for a click on the settings button that appears after a long press
2569 * on the home screen.
2570 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002571 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002572 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2573 }
2574
Michael Jurka5130e402011-10-13 04:55:35 -07002575 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002576 // Provide the same haptic feedback that the system offers for virtual keys.
2577 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002578 }
2579
Adam Cohen61f560d2013-09-30 15:58:20 -07002580 public void performHapticFeedbackOnTouchDown(View v) {
2581 // Provide the same haptic feedback that the system offers for virtual keys.
2582 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2583 }
2584
2585 public View.OnTouchListener getHapticFeedbackTouchListener() {
2586 if (mHapticFeedbackTouchListener == null) {
2587 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2588 @Override
2589 public boolean onTouch(View v, MotionEvent event) {
2590 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2591 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2592 }
2593 return false;
2594 }
2595 };
2596 }
2597 return mHapticFeedbackTouchListener;
2598 }
2599
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002600 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002601 if (!DISABLE_MARKET_BUTTON) {
2602 if (mAppMarketIntent != null) {
2603 startActivitySafely(v, mAppMarketIntent, "app market");
2604 } else {
2605 Log.e(TAG, "Invalid app market intent.");
2606 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002607 }
2608 }
2609
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002610 /**
2611 * Called when the user stops interacting with the launcher.
2612 * This implies that the user is now on the homescreen and is not doing housekeeping.
2613 */
2614 protected void onInteractionEnd() {}
2615
2616 /**
2617 * Called when the user starts interacting with the launcher.
2618 * The possible interactions are:
2619 * - open all apps
2620 * - reorder an app shortcut, or a widget
2621 * - open the overview mode.
2622 * This is a good time to stop doing things that only make sense
2623 * when the user is on the homescreen and not doing housekeeping.
2624 */
2625 protected void onInteractionBegin() {}
2626
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002627 void startApplicationDetailsActivity(ComponentName componentName) {
2628 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002629 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2630 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002631 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2632 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002633 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002634 }
2635
Michael Jurka1e2f4652013-07-08 18:03:46 -07002636 // returns true if the activity was started
2637 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002638 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002639 // System applications cannot be installed. For now, show a toast explaining that.
2640 // We may give them the option of disabling apps this way.
2641 int messageId = R.string.uninstall_system_app_text;
2642 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002643 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002644 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002645 String packageName = componentName.getPackageName();
2646 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002647 Intent intent = new Intent(
2648 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002649 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2650 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002651 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002652 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002653 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002654 }
2655
Michael Jurka86a720e2012-05-09 11:23:23 -07002656 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002660 // Only launch using the new animation if the shortcut has not opted out (this is a
2661 // private contract between launcher and may be ignored in the future).
2662 boolean useLaunchAnimation = (v != null) &&
2663 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2664 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002665 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2666 v.getMeasuredWidth(), v.getMeasuredHeight());
2667
2668 startActivity(intent, opts.toBundle());
2669 } else {
2670 startActivity(intent);
2671 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002672 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 } catch (SecurityException e) {
2674 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002675 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002677 "or use the exported attribute for this activity. "
2678 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002680 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002682
Michael Jurka86a720e2012-05-09 11:23:23 -07002683 boolean startActivitySafely(View v, Intent intent, Object tag) {
2684 boolean success = false;
2685 try {
2686 success = startActivity(v, intent, tag);
2687 } catch (ActivityNotFoundException e) {
2688 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2689 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2690 }
2691 return success;
2692 }
2693
Adam Cohen268c4752012-06-06 17:47:33 -07002694 /**
2695 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2696 * in the DragLayer in the exact absolute location of the original FolderIcon.
2697 */
2698 private void copyFolderIconToImage(FolderIcon fi) {
2699 final int width = fi.getMeasuredWidth();
2700 final int height = fi.getMeasuredHeight();
2701
2702 // Lazy load ImageView, Bitmap and Canvas
2703 if (mFolderIconImageView == null) {
2704 mFolderIconImageView = new ImageView(this);
2705 }
2706 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2707 mFolderIconBitmap.getHeight() != height) {
2708 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2709 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2710 }
2711
2712 DragLayer.LayoutParams lp;
2713 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2714 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2715 } else {
2716 lp = new DragLayer.LayoutParams(width, height);
2717 }
2718
Adam Cohen307fe232012-08-16 17:55:58 -07002719 // The layout from which the folder is being opened may be scaled, adjust the starting
2720 // view size by this scale factor.
2721 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002722 lp.customPosition = true;
2723 lp.x = mRectForFolderAnimation.left;
2724 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002725 lp.width = (int) (scale * width);
2726 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002727
2728 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2729 fi.draw(mFolderIconCanvas);
2730 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002731 if (fi.getFolder() != null) {
2732 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2733 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002734 }
Adam Cohen268c4752012-06-06 17:47:33 -07002735 // Just in case this image view is still in the drag layer from a previous animation,
2736 // we remove it and re-add it.
2737 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2738 mDragLayer.removeView(mFolderIconImageView);
2739 }
2740 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002741 if (fi.getFolder() != null) {
2742 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002743 }
Adam Cohen268c4752012-06-06 17:47:33 -07002744 }
2745
Adam Cohen2801caf2011-05-13 20:57:39 -07002746 private void growAndFadeOutFolderIcon(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", 0);
2749 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2750 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2751
Adam Cohenc51934b2011-07-26 21:07:43 -07002752 FolderInfo info = (FolderInfo) fi.getTag();
2753 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2754 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002755 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2756 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002757 }
2758
Adam Cohen268c4752012-06-06 17:47:33 -07002759 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2760 copyFolderIconToImage(fi);
2761 fi.setVisibility(View.INVISIBLE);
2762
Michael Jurka2ecf9952012-06-18 12:52:28 -07002763 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002764 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002765 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2766 oa.start();
2767 }
2768
Adam Cohen268c4752012-06-06 17:47:33 -07002769 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002770 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002771 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2772 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2773 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2774
Adam Cohen268c4752012-06-06 17:47:33 -07002775 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002776
Adam Cohen268c4752012-06-06 17:47:33 -07002777 // We remove and re-draw the FolderIcon in-case it has changed
2778 mDragLayer.removeView(mFolderIconImageView);
2779 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002780 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002781 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002782 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002783 oa.addListener(new AnimatorListenerAdapter() {
2784 @Override
2785 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002786 if (cl != null) {
2787 cl.clearFolderLeaveBehind();
2788 // Remove the ImageView copy of the FolderIcon and make the original visible.
2789 mDragLayer.removeView(mFolderIconImageView);
2790 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002791 }
2792 }
2793 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002794 oa.start();
2795 }
2796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002798 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799 * is animated relative to the specified View. If the View is null, no animation
2800 * is played.
2801 *
2802 * @param folderInfo The FolderInfo describing the folder to open.
2803 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002804 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002805 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002806 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807
Adam Cohena9cf38f2011-05-02 15:36:58 -07002808 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809
Adam Cohen4554ee12011-08-03 16:13:21 -07002810 // Just verify that the folder hasn't already been added to the DragLayer.
2811 // There was a one-off crash where the folder had a parent already.
2812 if (folder.getParent() == null) {
2813 mDragLayer.addView(folder);
2814 mDragController.addDropTarget((DropTarget) folder);
2815 } else {
2816 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2817 folder.getParent() + ").");
2818 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002819 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002820 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002821
2822 // Notify the accessibility manager that this folder "window" has appeared and occluded
2823 // the workspace items
2824 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2825 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002826 }
2827
2828 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002829 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002830 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002831 if (folder.isEditingName()) {
2832 folder.dismissEditingName();
2833 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002834 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002835
2836 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002837 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002838 }
2839 }
2840
2841 void closeFolder(Folder folder) {
2842 folder.getInfo().opened = false;
2843
2844 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2845 if (parent != null) {
2846 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2847 shrinkAndFadeInFolderIcon(fi);
2848 }
2849 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002850
2851 // Notify the accessibility manager that this folder "window" has disappeard and no
2852 // longer occludeds the workspace items
2853 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 }
2855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002857 if (!isDraggingEnabled()) return false;
2858 if (isWorkspaceLocked()) return false;
2859 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860
Adam Cohen1697b792013-09-17 19:08:21 -07002861 if (v instanceof Workspace) {
2862 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002863 if (mWorkspace.enterOverviewMode()) {
2864 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2865 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2866 return true;
2867 } else {
2868 return false;
2869 }
Adam Cohen1697b792013-09-17 19:08:21 -07002870 }
Adam Cohen1697b792013-09-17 19:08:21 -07002871 }
2872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002874 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 }
2876
Michael Jurka0280c3b2010-09-17 15:00:07 -07002877 resetAddInfo();
2878 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002880 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 return true;
2882 }
2883
Winson Chung3d503fb2011-07-13 17:25:49 -07002884 // The hotseat touch handling does not go through Workspace, and we always allow long press
2885 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002886 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002887 final boolean inHotseat = isHotseatLayout(v);
2888 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002889 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002890 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002891 // User long pressed on empty space
2892 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2893 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2894 // Disabling reordering until we sort out some issues.
2895 if (mWorkspace.isInOverviewMode()) {
2896 mWorkspace.startReordering(v);
2897 } else {
2898 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002901 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2902 mHotseat.getOrderInHotseat(
2903 longClickCellInfo.cellX,
2904 longClickCellInfo.cellY));
2905 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002907 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002908 }
2909 }
2910 }
2911 return true;
2912 }
2913
Winson Chung3d503fb2011-07-13 17:25:49 -07002914 boolean isHotseatLayout(View layout) {
2915 return mHotseat != null && layout != null &&
2916 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2917 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002918
Winson Chung3d503fb2011-07-13 17:25:49 -07002919 /**
2920 * Returns the CellLayout of the specified container at the specified screen.
2921 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002922 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002923 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2924 if (mHotseat != null) {
2925 return mHotseat.getLayout();
2926 } else {
2927 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002928 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002929 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002930 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002931 }
2932 }
2933
Daniel Sandler843e8602010-06-07 14:59:01 -04002934 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002935 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002936 }
2937
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002938 /**
2939 * Helper method for the cameraZoomIn/cameraZoomOut animations
2940 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002941 * @param scaleFactor The scale factor used for the zoom
2942 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002943 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002944 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002945 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002946 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002947
Craig Mautner360310b2012-10-26 15:13:08 -07002948 private void setWorkspaceBackground(boolean workspace) {
2949 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002950 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002951 }
2952
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002953 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002954 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2955 int curflags = getWindow().getAttributes().flags
2956 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2957 if (wpflags != curflags) {
2958 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2959 }
Craig Mautner360310b2012-10-26 15:13:08 -07002960 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002961 }
2962
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002963 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2964 if (v instanceof LauncherTransitionable) {
2965 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2966 }
2967 }
2968
Michael Jurkabed61d22012-02-14 22:51:29 -08002969 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2970 if (v instanceof LauncherTransitionable) {
2971 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2972 }
Winson Chung70442722012-02-10 15:43:22 -08002973
2974 // Update the workspace transition step as well
2975 dispatchOnLauncherTransitionStep(v, 0f);
2976 }
2977
2978 private void dispatchOnLauncherTransitionStep(View v, float t) {
2979 if (v instanceof LauncherTransitionable) {
2980 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2981 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002982 }
2983
2984 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2985 if (v instanceof LauncherTransitionable) {
2986 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2987 }
Winson Chung70442722012-02-10 15:43:22 -08002988
2989 // Update the workspace transition step as well
2990 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002991 }
2992
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002993 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002994 * Things to test when changing the following seven functions.
2995 * - Home from workspace
2996 * - from center screen
2997 * - from other screens
2998 * - Home from all apps
2999 * - from center screen
3000 * - from other screens
3001 * - Back from all apps
3002 * - from center screen
3003 * - from other screens
3004 * - Launch app from workspace and quit
3005 * - with back
3006 * - with home
3007 * - Launch app from all apps and quit
3008 * - with back
3009 * - with home
3010 * - Go to a screen that's not the default, then all
3011 * apps, and launch and app, and go back
3012 * - with back
3013 * -with home
3014 * - On workspace, long press power and go back
3015 * - with back
3016 * - with home
3017 * - On all apps, long press power and go back
3018 * - with back
3019 * - with home
3020 * - On workspace, power off
3021 * - On all apps, power off
3022 * - Launch an app and turn off the screen while in that app
3023 * - Go back with home key
3024 * - Go back with back key TODO: make this not go to workspace
3025 * - From all apps
3026 * - From workspace
3027 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3028 * - From all apps
3029 * - From the center workspace
3030 * - From another workspace
3031 */
3032
3033 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003034 * Zoom the camera out from the workspace to reveal 'toView'.
3035 * Assumes that the view to show is anchored at either the very top or very bottom
3036 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003037 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003038 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003039 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3040 showAppsCustomizeHelper(animated, springLoaded, contentType);
3041 }
3042 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3043 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003044 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003045 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003046 mStateAnimation.cancel();
3047 mStateAnimation = null;
3048 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003049 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003050
Winson Chungf0ea4d32011-06-06 14:27:16 -07003051 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3052 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3053 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003054 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003055 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003056 final int startDelay =
3057 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003058
Michael Jurkab3e22d92011-10-31 15:58:33 -07003059 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003060
Michael Jurkad74c9842011-07-10 12:44:21 -07003061 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003062 Animator workspaceAnim =
3063 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003064 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003065 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3066 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003067 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3068 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003069
3070 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003071 toView.setScaleX(scale);
3072 toView.setScaleY(scale);
3073 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3074 scaleAnim.
3075 scaleX(1f).scaleY(1f).
3076 setDuration(duration).
3077 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003078
Winson Chungf0ea4d32011-06-06 14:27:16 -07003079 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003080 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003081 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003082 .ofFloat(toView, "alpha", 0f, 1f)
3083 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003084 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003085 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3086 @Override
3087 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003088 if (animation == null) {
3089 throw new RuntimeException("animation is null");
3090 }
Winson Chung70442722012-02-10 15:43:22 -08003091 float t = (Float) animation.getAnimatedValue();
3092 dispatchOnLauncherTransitionStep(fromView, t);
3093 dispatchOnLauncherTransitionStep(toView, t);
3094 }
3095 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003096
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003097 // toView should appear right at the end of the workspace shrink
3098 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003099 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003100 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003101 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003102
3103 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003104 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003105 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003106 // Prepare the position
3107 toView.setTranslationX(0.0f);
3108 toView.setTranslationY(0.0f);
3109 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003110 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003111 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003112 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003113 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003114 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3115 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003116
Winson Chungc7d2b602012-05-16 17:02:20 -07003117 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003118 if (mSearchDropTargetBar != null) {
3119 mSearchDropTargetBar.hideSearchBar(false);
3120 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003121 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003122 });
3123
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003124 if (workspaceAnim != null) {
3125 mStateAnimation.play(workspaceAnim);
3126 }
Michael Jurka1899a362011-11-03 13:50:45 -07003127
3128 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003129
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003130 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3131 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003132
3133 // If any of the objects being animated haven't been measured/laid out
3134 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003135 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003136 (mWorkspace.getMeasuredWidth() == 0) ||
3137 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003138 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003139 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003140
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003141 final AnimatorSet stateAnimation = mStateAnimation;
3142 final Runnable startAnimRunnable = new Runnable() {
3143 public void run() {
3144 // Check that mStateAnimation hasn't changed while
3145 // we waited for a layout/draw pass
3146 if (mStateAnimation != stateAnimation)
3147 return;
3148 setPivotsForZoom(toView, scale);
3149 dispatchOnLauncherTransitionStart(fromView, animated, false);
3150 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003151 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003152 }
3153 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003154 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003155 final ViewTreeObserver observer = toView.getViewTreeObserver();
3156 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3157 public void onGlobalLayout() {
3158 startAnimRunnable.run();
3159 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3160 }
3161 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003162 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003163 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003164 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003165 } else {
3166 toView.setTranslationX(0.0f);
3167 toView.setTranslationY(0.0f);
3168 toView.setScaleX(1.0f);
3169 toView.setScaleY(1.0f);
3170 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003171 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003172
Daniel Sandlere4f98912013-06-25 15:13:26 -04003173 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003174 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003175 if (mSearchDropTargetBar != null) {
3176 mSearchDropTargetBar.hideSearchBar(false);
3177 }
Michael Jurkaabded662011-03-04 12:06:57 -08003178 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003179 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003180 dispatchOnLauncherTransitionStart(fromView, animated, false);
3181 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003182 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003183 dispatchOnLauncherTransitionStart(toView, animated, false);
3184 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003185 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003186 }
3187
3188 /**
3189 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003190 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003191 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003192 */
Adam Cohened307df2013-10-02 09:37:31 -07003193 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003194 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003195
Michael Jurkab3e22d92011-10-31 15:58:33 -07003196 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003197 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003198 mStateAnimation.cancel();
3199 mStateAnimation = null;
3200 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003201 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003202
Winson Chungf0ea4d32011-06-06 14:27:16 -07003203 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003204 final int fadeOutDuration =
3205 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003206 final float scaleFactor = (float)
3207 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3208 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003209 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003210 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003211 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003212 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3213 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003214 toState, animated, stagger, -1);
3215 } else if (toState == Workspace.State.SPRING_LOADED ||
3216 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003217 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003218 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003219 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003220
Michael Jurkab3e22d92011-10-31 15:58:33 -07003221 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003222 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003223 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003224 final LauncherViewPropertyAnimator scaleAnim =
3225 new LauncherViewPropertyAnimator(fromView);
3226 scaleAnim.
3227 scaleX(scaleFactor).scaleY(scaleFactor).
3228 setDuration(duration).
3229 setInterpolator(new Workspace.ZoomInInterpolator());
3230
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003231 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003232 .ofFloat(fromView, "alpha", 1f, 0f)
3233 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003234 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003235 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3236 @Override
3237 public void onAnimationUpdate(ValueAnimator animation) {
3238 float t = 1f - (Float) animation.getAnimatedValue();
3239 dispatchOnLauncherTransitionStep(fromView, t);
3240 dispatchOnLauncherTransitionStep(toView, t);
3241 }
3242 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003243
Michael Jurka2ecf9952012-06-18 12:52:28 -07003244 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003245
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003246 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3247 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003248 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003249
3250 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003251 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003252 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003253 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003254 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3255 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003256 if (onCompleteRunnable != null) {
3257 onCompleteRunnable.run();
3258 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003259 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003260 }
3261 });
3262
Winson Chungf0ea4d32011-06-06 14:27:16 -07003263 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003264 if (workspaceAnim != null) {
3265 mStateAnimation.play(workspaceAnim);
3266 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003267 dispatchOnLauncherTransitionStart(fromView, animated, true);
3268 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003269 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003270 } else {
3271 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003272 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003273 dispatchOnLauncherTransitionStart(fromView, animated, true);
3274 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003275 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003276 dispatchOnLauncherTransitionStart(toView, animated, true);
3277 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003278 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003279 }
3280
Michael Jurkae326f182011-11-21 14:05:46 -08003281 @Override
3282 public void onTrimMemory(int level) {
3283 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003284 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003285 mAppsCustomizeTabHost.onTrimMemory();
3286 }
3287 }
3288
Adam Cohened307df2013-10-02 09:37:31 -07003289 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003290 showWorkspace(animated, null);
3291 }
3292
Adam Cohened307df2013-10-02 09:37:31 -07003293 protected void showWorkspace() {
3294 showWorkspace(true);
3295 }
3296
Adam Cohened66b2b2012-01-23 17:28:51 -08003297 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003298 if (mWorkspace.isInOverviewMode()) {
3299 mWorkspace.exitOverviewMode(animated);
3300 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003302 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003304 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305
Winson Chungc7d2b602012-05-16 17:02:20 -07003306 // Show the search bar (only animate if we were showing the drop target bar in spring
3307 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003308 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003309 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003310 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003311
Michael Jurkab3e22d92011-10-31 15:58:33 -07003312 // Set focus to the AppsCustomize button
3313 if (mAllAppsButton != null) {
3314 mAllAppsButton.requestFocus();
3315 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003316 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003317
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003318 // Change the state *after* we've called all the transition code
3319 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003320
Winson Chung5fb63472011-02-02 17:03:37 -08003321 // Resume the auto-advance of widgets
3322 mUserPresent = true;
3323 updateRunning();
3324
alanv1d4fde62012-10-17 13:15:47 -07003325 // Send an accessibility event to announce the context change
3326 getWindow().getDecorView()
3327 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003328
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003329 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003330 }
3331
Adam Cohened307df2013-10-02 09:37:31 -07003332 void showOverviewMode(boolean animated) {
3333 mWorkspace.setVisibility(View.VISIBLE);
3334 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3335 mState = State.WORKSPACE;
3336 onWorkspaceShown(animated);
3337 }
3338
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003339 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003340 }
3341
Winson Chung82963d52013-10-09 11:20:57 -07003342 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3343 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344 if (mState != State.WORKSPACE) return;
3345
Winson Chung82963d52013-10-09 11:20:57 -07003346 if (resetPageToZero) {
3347 mAppsCustomizeTabHost.reset();
3348 }
Winson Chungc58497e2013-09-03 17:48:37 -07003349 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003350 mAppsCustomizeTabHost.requestFocus();
3351
Michael Jurkab3e22d92011-10-31 15:58:33 -07003352 // Change the state *after* we've called all the transition code
3353 mState = State.APPS_CUSTOMIZE;
3354
3355 // Pause the auto-advance of widgets until we are out of AllApps
3356 mUserPresent = false;
3357 updateRunning();
3358 closeFolder();
3359
3360 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003361 getWindow().getDecorView()
3362 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003363 }
3364
Adam Cohen7777d962011-08-18 18:58:38 -07003365 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003366 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003367 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003368 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003369 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003370 }
Adam Cohen7777d962011-08-18 18:58:38 -07003371
Adam Cohenad4e15c2013-10-17 16:21:35 -07003372 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003373 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003374 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3375
Winson Chunge7a03942011-08-05 15:05:12 -07003376 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003377 @Override
3378 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003379 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003380 // Before we show workspace, hide all apps again because
3381 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3382 // clean up our state transition functions
3383 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003384 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003385 } else {
3386 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003387 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003388 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003389 }, delay);
3390
Winson Chung557d6ed2011-07-08 15:34:52 -07003391 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003392
Michael Jurkad3ef3062010-11-23 16:23:58 -08003393 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003394 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003395 final boolean animated = true;
3396 final boolean springLoaded = true;
3397 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003398 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003399 }
3400 // Otherwise, we are not in spring loaded mode, so don't do anything.
3401 }
3402
Michael Jurkab3e22d92011-10-31 15:58:33 -07003403 void lockAllApps() {
3404 // TODO
3405 }
3406
3407 void unlockAllApps() {
3408 // TODO
3409 }
3410
Winson Chungf0ea4d32011-06-06 14:27:16 -07003411 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003412 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003413 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003414 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003415 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003416 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003417 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003418 int duration = 0;
3419 if (mSearchDropTargetBar != null) {
3420 duration = mSearchDropTargetBar.getTransitionInDuration();
3421 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003422 mHotseat.animate().alpha(1f).setDuration(duration);
3423 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003424 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003425 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003426 }
3427 }
3428 }
3429
3430 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003431 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003432 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003433 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003434 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003435 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003436 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003437 int duration = 0;
3438 if (mSearchDropTargetBar != null) {
3439 duration = mSearchDropTargetBar.getTransitionOutDuration();
3440 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003441 mHotseat.animate().alpha(0f).setDuration(duration);
3442 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003443 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003444 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003445 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003446 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003447 }
3448
Patrick Dubroy5f445422011-02-18 14:35:21 -08003449 /**
3450 * Add an item from all apps or customize onto the given workspace screen.
3451 * If layout is null, add to the current screen.
3452 */
3453 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003454 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003455 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003456 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003457 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003458
Winson Chungdff8ebb2011-09-08 17:25:31 -07003459 /** Maps the current orientation to an index for referencing orientation correct global icons */
3460 private int getCurrentOrientationIndexForGlobalIcons() {
3461 // default - 0, landscape - 1
3462 switch (getResources().getConfiguration().orientation) {
3463 case Configuration.ORIENTATION_LANDSCAPE:
3464 return 1;
3465 default:
3466 return 0;
3467 }
3468 }
3469
Michael Jurkaae65ee32012-04-30 11:45:54 -07003470 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003471 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003472 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003473 // Look for the toolbar icon specified in the activity meta-data
3474 Bundle metaData = packageManager.getActivityInfo(
3475 activityName, PackageManager.GET_META_DATA).metaData;
3476 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003477 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003478 if (iconResId != 0) {
3479 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003480 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003481 }
3482 }
3483 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003484 // This can happen if the activity defines an invalid drawable
3485 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3486 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003487 } catch (Resources.NotFoundException nfe) {
3488 // This can happen if the activity defines an invalid drawable
3489 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3490 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003491 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003492 return null;
3493 }
3494
3495 // if successful in getting icon, return it; otherwise, set button to use default drawable
3496 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003497 int buttonId, ComponentName activityName, int fallbackDrawableId,
3498 String toolbarResourceName) {
3499 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003500 Resources r = getResources();
3501 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3502 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003503
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003504 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003505 // If we were unable to find the icon via the meta-data, use a generic one
3506 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003507 toolbarIcon = r.getDrawable(fallbackDrawableId);
3508 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003509 if (button != null) {
3510 button.setCompoundDrawables(toolbarIcon, null, null, null);
3511 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003512 return null;
3513 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003514 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003515 if (button != null) {
3516 button.setCompoundDrawables(toolbarIcon, null, null, null);
3517 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003518 return toolbarIcon.getConstantState();
3519 }
3520 }
3521
3522 // if successful in getting icon, return it; otherwise, set button to use default drawable
3523 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003524 int buttonId, ComponentName activityName, int fallbackDrawableId,
3525 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003526 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003527 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003528
Michael Jurka19e0fc52011-07-22 18:00:21 -07003529 if (button != null) {
3530 // If we were unable to find the icon via the meta-data, use a
3531 // generic one
3532 if (toolbarIcon == null) {
3533 button.setImageResource(fallbackDrawableId);
3534 } else {
3535 button.setImageDrawable(toolbarIcon);
3536 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003537 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003538
3539 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3540
Michael Jurka0423dcf2010-10-05 14:56:18 -07003541 }
3542
Winson Chung1b884092012-06-08 17:13:02 -07003543 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003544 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003545 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003546 }
3547
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003548 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003549 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003550 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003551 }
3552
Winson Chungbb185bd2011-11-21 12:31:42 -08003553 private void invalidatePressedFocusedStates(View container, View button) {
3554 if (container instanceof HolographicLinearLayout) {
3555 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3556 layout.invalidatePressedFocusedStates();
3557 } else if (button instanceof HolographicImageView) {
3558 HolographicImageView view = (HolographicImageView) button;
3559 view.invalidatePressedFocusedStates();
3560 }
3561 }
3562
Cristina Stancu476493b2013-08-07 17:20:14 +01003563 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003564 if (mQsb == null) {
3565 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3566 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003567 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003568 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003569 }
3570
3571 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003572 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003573 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003574 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003575 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003576
Winson Chung1cad91e2011-05-25 17:41:01 -07003577 final SearchManager searchManager =
3578 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3579 ComponentName activityName = searchManager.getGlobalSearchActivity();
3580 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003581 int coi = getCurrentOrientationIndexForGlobalIcons();
3582 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003583 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3584 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3585 if (sGlobalSearchIcon[coi] == null) {
3586 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3587 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3588 TOOLBAR_ICON_METADATA_NAME);
3589 }
3590
Winson Chungc51db6a2011-10-05 11:44:49 -07003591 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3592 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003593 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003594 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003595 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003596 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003597 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3598 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003599 if (searchButton != null) searchButton.setVisibility(View.GONE);
3600 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003601 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003602 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003603 }
3604 }
3605
Cristina Stancu476493b2013-08-07 17:20:14 +01003606 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003607 final View searchButtonContainer = findViewById(R.id.search_button_container);
3608 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003609 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003610 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003611 }
3612
Cristina Stancu476493b2013-08-07 17:20:14 +01003613 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003614 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003615 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003616
Winson Chungc51db6a2011-10-05 11:44:49 -07003617 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003618 final SearchManager searchManager =
3619 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3620 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3621
3622 ComponentName activityName = null;
3623 if (globalSearchActivity != null) {
3624 // Check if the global search activity handles voice search
3625 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3626 intent.setPackage(globalSearchActivity.getPackageName());
3627 activityName = intent.resolveActivity(getPackageManager());
3628 }
3629
3630 if (activityName == null) {
3631 // Fallback: check if an activity other than the global search activity
3632 // resolves this
3633 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3634 activityName = intent.resolveActivity(getPackageManager());
3635 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003636 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003637 int coi = getCurrentOrientationIndexForGlobalIcons();
3638 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003639 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3640 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3641 if (sVoiceSearchIcon[coi] == null) {
3642 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3643 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3644 TOOLBAR_ICON_METADATA_NAME);
3645 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003646 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003647 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003648 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003649 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003650 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003651 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003652 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003653 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003654 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003655 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003656 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003657 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003658
Cristina Stancu476493b2013-08-07 17:20:14 +01003659 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003660 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3661 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003662 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003663 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003664 }
3665
Winson Chung5841efa2013-09-30 18:06:44 -07003666 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003667 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3668 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003669 boolean visible = !forceDisableVoiceButtonProxy &&
3670 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003671 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003672 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003673 }
3674 }
Winson Chung5841efa2013-09-30 18:06:44 -07003675
3676 /**
3677 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3678 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3679 */
3680 public void disableVoiceButtonProxy(boolean disabled) {
3681 updateVoiceButtonProxyVisible(disabled);
3682 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003683 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003684 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003685 */
3686 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003687 if (!DISABLE_MARKET_BUTTON) {
3688 final View marketButton = findViewById(R.id.market_button);
3689 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3690 // Find the app market activity by resolving an intent.
3691 // (If multiple app markets are installed, it will return the ResolverActivity.)
3692 ComponentName activityName = intent.resolveActivity(getPackageManager());
3693 if (activityName != null) {
3694 int coi = getCurrentOrientationIndexForGlobalIcons();
3695 mAppMarketIntent = intent;
3696 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3697 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3698 TOOLBAR_ICON_METADATA_NAME);
3699 marketButton.setVisibility(View.VISIBLE);
3700 } else {
3701 // We should hide and disable the view so that we don't try and restore the visibility
3702 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3703 marketButton.setVisibility(View.GONE);
3704 marketButton.setEnabled(false);
3705 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003706 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003707 }
3708
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003709 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003710 if (!DISABLE_MARKET_BUTTON) {
3711 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3712 Resources r = getResources();
3713 Drawable marketIconDrawable = d.newDrawable(r);
3714 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3715 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3716 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003717
Winson Chungd64a6662013-09-30 11:06:59 -07003718 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3719 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003720 }
3721
Michael Jurkad7c28052012-04-27 15:43:36 -07003722 @Override
3723 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003724 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003725 final List<CharSequence> text = event.getText();
3726 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003727 // Populate event with a fake title based on the current state.
3728 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003729 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003730 } else {
3731 text.add(getString(R.string.all_apps_home_button_label));
3732 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003733 return result;
3734 }
3735
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003736 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003737 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003738 */
3739 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3740 @Override
3741 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003742 closeSystemDialogs();
3743 }
3744 }
3745
3746 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003747 * Receives notifications whenever the appwidgets are reset.
3748 */
3749 private class AppWidgetResetObserver extends ContentObserver {
3750 public AppWidgetResetObserver() {
3751 super(new Handler());
3752 }
3753
3754 @Override
3755 public void onChange(boolean selfChange) {
3756 onAppWidgetReset();
3757 }
3758 }
3759
3760 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003761 * If the activity is currently paused, signal that we need to run the passed Runnable
3762 * in onResume.
3763 *
3764 * This needs to be called from incoming places where resources might have been loaded
3765 * while we are paused. That is becaues the Configuration might be wrong
3766 * when we're not running, and if it comes back to what it was when we
3767 * were paused, we are not restarted.
3768 *
3769 * Implementation of the method from LauncherModel.Callbacks.
3770 *
3771 * @return true if we are currently paused. The caller might be able to
3772 * skip some work in that case since we will come back again.
3773 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003774 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003775 if (mPaused) {
3776 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003777 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003778 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003779 }
3780 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003781 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003782 return true;
3783 } else {
3784 return false;
3785 }
3786 }
3787
Michael Jurkac402cd92013-05-20 15:49:32 +02003788 private boolean waitUntilResume(Runnable run) {
3789 return waitUntilResume(run, false);
3790 }
3791
Michael Jurka1e2f4652013-07-08 18:03:46 -07003792 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003793 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003794 }
3795
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003797 * If the activity is currently paused, signal that we need to re-run the loader
3798 * in onResume.
3799 *
3800 * This needs to be called from incoming places where resources might have been loaded
3801 * while we are paused. That is becaues the Configuration might be wrong
3802 * when we're not running, and if it comes back to what it was when we
3803 * were paused, we are not restarted.
3804 *
3805 * Implementation of the method from LauncherModel.Callbacks.
3806 *
3807 * @return true if we are currently paused. The caller might be able to
3808 * skip some work in that case since we will come back again.
3809 */
3810 public boolean setLoadOnResume() {
3811 if (mPaused) {
3812 Log.i(TAG, "setLoadOnResume");
3813 mOnResumeNeedsLoad = true;
3814 return true;
3815 } else {
3816 return false;
3817 }
3818 }
3819
3820 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003821 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003822 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003824 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003825 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003826 } else {
3827 return SCREEN_COUNT / 2;
3828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003830
Joe Onorato9c1289c2009-08-17 11:03:03 -04003831 /**
3832 * Refreshes the shortcuts shown on the workspace.
3833 *
3834 * Implementation of the method from LauncherModel.Callbacks.
3835 */
3836 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003837 mWorkspaceLoading = true;
3838
Michael Jurka7607c2f2013-04-03 14:33:19 -07003839 // If we're starting binding all over again, clear any bind calls we'd postponed in
3840 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3841 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003842 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003843
Winson Chungd64d1762013-08-20 14:37:16 -07003844 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003845 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003846 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003847
Michael Jurka05bf6442011-11-30 20:28:53 -08003848 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003849 if (mHotseat != null) {
3850 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003851 }
3852 }
3853
Adam Cohendcd297f2013-06-18 13:13:40 -07003854 @Override
3855 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003856 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003857
Adam Cohen5084cba2013-09-03 12:01:16 -07003858 // If there are no screens, we need to have an empty screen
3859 if (orderedScreenIds.size() == 0) {
3860 mWorkspace.addExtraEmptyScreen();
3861 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003862
3863 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003864 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003865 if (hasCustomContentToLeft()) {
3866 mWorkspace.createCustomContentContainer();
3867 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003868 }
Winson Chung64359a52013-07-08 17:17:08 -07003869 }
3870
3871 @Override
3872 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003873 // Log to disk
3874 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3875 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3876 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003877 int count = orderedScreenIds.size();
3878 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003879 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003880 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003881 }
3882
Adam Cohen39a06042013-07-19 14:30:12 -07003883 private boolean shouldShowWeightWatcher() {
3884 String spKey = LauncherAppState.getSharedPreferencesKey();
3885 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003886 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003887
3888 return show;
3889 }
3890
3891 private void toggleShowWeightWatcher() {
3892 String spKey = LauncherAppState.getSharedPreferencesKey();
3893 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3894 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3895
3896 show = !show;
3897
3898 SharedPreferences.Editor editor = sp.edit();
3899 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3900 editor.commit();
3901
3902 if (mWeightWatcher != null) {
3903 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3904 }
3905 }
3906
Winson Chungd64d1762013-08-20 14:37:16 -07003907 public void bindAppsAdded(final ArrayList<Long> newScreens,
3908 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003909 final ArrayList<ItemInfo> addAnimated,
3910 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003911 Runnable r = new Runnable() {
3912 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003913 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003914 }
3915 };
3916 if (waitUntilResume(r)) {
3917 return;
3918 }
3919
Winson Chungd64d1762013-08-20 14:37:16 -07003920 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003921 if (newScreens != null) {
3922 bindAddScreens(newScreens);
3923 }
Winson Chungd64d1762013-08-20 14:37:16 -07003924
3925 // We add the items without animation on non-visible pages, and with
3926 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003927 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003928 bindItems(addNotAnimated, 0,
3929 addNotAnimated.size(), false);
3930 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003931 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003932 bindItems(addAnimated, 0,
3933 addAnimated.size(), true);
3934 }
Winson Chungc58497e2013-09-03 17:48:37 -07003935
Winson Chung87412982013-10-03 18:34:14 -07003936 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003937 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003938
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003939 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003940 addedApps != null && mAppsCustomizeContent != null) {
3941 mAppsCustomizeContent.addApps(addedApps);
3942 }
Winson Chungd64d1762013-08-20 14:37:16 -07003943 }
3944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003945 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 * Bind the items start-end from the list.
3947 *
3948 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003949 */
Winson Chung64359a52013-07-08 17:17:08 -07003950 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3951 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003952 Runnable r = new Runnable() {
3953 public void run() {
3954 bindItems(shortcuts, start, end, forceAnimateIcons);
3955 }
3956 };
3957 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003958 return;
3959 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003960
Winson Chungf0c6ae02012-03-21 16:10:31 -07003961 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003962 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3963 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003964 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003965 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003966 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003967 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003969
3970 // Short circuit if we are loading dock items for a configuration which has no dock
3971 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3972 mHotseat == null) {
3973 continue;
3974 }
3975
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976 switch (item.itemType) {
3977 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3978 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003979 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003980 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003981
Winson Chung64359a52013-07-08 17:17:08 -07003982 /*
3983 * TODO: FIX collision case
3984 */
Winson Chungce376632013-07-11 16:12:41 -07003985 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3986 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3987 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3988 throw new RuntimeException("OCCUPIED");
3989 }
Winson Chung64359a52013-07-08 17:17:08 -07003990 }
3991
Adam Cohendcd297f2013-06-18 13:13:40 -07003992 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3993 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003994 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003995 // Animate all the applications up now
3996 shortcut.setAlpha(0f);
3997 shortcut.setScaleX(0f);
3998 shortcut.setScaleY(0f);
3999 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004000 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004003 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004004 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004005 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004006 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004007 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4008 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004010 default:
4011 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004013 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004014
Winson Chung997a9232013-07-24 15:33:46 -07004015 if (animateIcons) {
4016 // Animate to the correct page
4017 if (newShortcutsScreenId > -1) {
4018 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004019 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004020 final Runnable startBounceAnimRunnable = new Runnable() {
4021 public void run() {
4022 anim.playTogether(bounceAnims);
4023 anim.start();
4024 }
4025 };
Winson Chung997a9232013-07-24 15:33:46 -07004026 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004027 // We post the animation slightly delayed to prevent slowdowns
4028 // when we are loading right after we return to launcher.
4029 mWorkspace.postDelayed(new Runnable() {
4030 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004031 if (mWorkspace != null) {
4032 mWorkspace.snapToPage(newScreenIndex);
4033 mWorkspace.postDelayed(startBounceAnimRunnable,
4034 NEW_APPS_ANIMATION_DELAY);
4035 }
Winson Chung94d67682013-09-25 16:29:40 -07004036 }
4037 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004038 } else {
4039 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004040 }
4041 }
Winson Chung64359a52013-07-08 17:17:08 -07004042 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004044 }
4045
Joe Onorato9c1289c2009-08-17 11:03:03 -04004046 /**
4047 * Implementation of the method from LauncherModel.Callbacks.
4048 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004049 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004050 Runnable r = new Runnable() {
4051 public void run() {
4052 bindFolders(folders);
4053 }
4054 };
4055 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004056 return;
4057 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004058 sFolders.clear();
4059 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061
4062 /**
4063 * Add the views for a widget to the workspace.
4064 *
4065 * Implementation of the method from LauncherModel.Callbacks.
4066 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004067 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004068 Runnable r = new Runnable() {
4069 public void run() {
4070 bindAppWidget(item);
4071 }
4072 };
4073 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004074 return;
4075 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004076
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4078 if (DEBUG_WIDGETS) {
4079 Log.d(TAG, "bindAppWidget: " + item);
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 final Workspace workspace = mWorkspace;
4082
4083 final int appWidgetId = item.appWidgetId;
4084 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004085 if (DEBUG_WIDGETS) {
4086 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4087 }
4088
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4090
Joe Onorato9c1289c2009-08-17 11:03:03 -04004091 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004092 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093
Adam Cohendcd297f2013-06-18 13:13:40 -07004094 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004096 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4097
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 workspace.requestLayout();
4099
Daniel Sandler843e8602010-06-07 14:59:01 -04004100 if (DEBUG_WIDGETS) {
4101 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4102 + (SystemClock.uptimeMillis()-start) + "ms");
4103 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004104 }
4105
Adam Cohen1462de32012-07-24 22:34:36 -07004106 public void onPageBoundSynchronously(int page) {
4107 mSynchronouslyBoundPages.add(page);
4108 }
4109
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 /**
4111 * Callback saying that there aren't any more items to bind.
4112 *
4113 * Implementation of the method from LauncherModel.Callbacks.
4114 */
Adam Cohene25af792013-06-06 23:08:25 -07004115 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004116 Runnable r = new Runnable() {
4117 public void run() {
4118 finishBindingItems(upgradePath);
4119 }
4120 };
4121 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004122 return;
4123 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124 if (mSavedState != null) {
4125 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004126 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004128 mSavedState = null;
4129 }
4130
Adam Cohen1462de32012-07-24 22:34:36 -07004131 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004132
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004133 // If we received the result of any pending adds while the loader was running (e.g. the
4134 // widget configuration forced an orientation change), process them now.
4135 for (int i = 0; i < sPendingAddList.size(); i++) {
4136 completeAdd(sPendingAddList.get(i));
4137 }
4138 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139
Winson Chungc51db6a2011-10-05 11:44:49 -07004140 // Update the market app icon as necessary (the other icons will be managed in response to
4141 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004142 if (!DISABLE_MARKET_BUTTON) {
4143 updateAppMarketIcon();
4144 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004145
Winson Chungf0c6ae02012-03-21 16:10:31 -07004146 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004147 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004148 mWorkspace.getUniqueComponents(true, null);
4149 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004150 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004151 }
4152
Winson Chunga0b7e862013-09-05 16:03:15 -07004153 public boolean isAllAppsButtonRank(int rank) {
4154 if (mHotseat != null) {
4155 return mHotseat.isAllAppsButtonRank(rank);
4156 }
4157 return false;
4158 }
4159
Winson Chunga2413752012-04-03 14:22:34 -07004160 private boolean canRunNewAppsAnimation() {
4161 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4162 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4163 }
4164
Winson Chungc9168342013-06-26 14:54:55 -07004165 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4166 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4167 PropertyValuesHolder.ofFloat("alpha", 1f),
4168 PropertyValuesHolder.ofFloat("scaleX", 1f),
4169 PropertyValuesHolder.ofFloat("scaleY", 1f));
4170 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4171 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4172 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4173 return bounceAnim;
4174 }
4175
Adam Cohen27772732013-11-11 14:00:56 +00004176 public boolean useVerticalBarLayout() {
4177 return LauncherAppState.getInstance().getDynamicGrid().
4178 getDeviceProfile().isVerticalBarLayout();
4179 }
4180
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004181 protected Rect getSearchBarBounds() {
4182 return LauncherAppState.getInstance().getDynamicGrid().
4183 getDeviceProfile().getSearchBarBounds();
4184 }
4185
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004186 @Override
4187 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004188 boolean searchVisible = updateGlobalSearchIcon();
4189 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004190 if (mSearchDropTargetBar != null) {
4191 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4192 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004193 }
4194
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004195 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004196 * Add the icons for all apps.
4197 *
4198 * Implementation of the method from LauncherModel.Callbacks.
4199 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004200 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004201 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004202 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4203 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4204 getHotseat().addAllAppsFolder(mIconCache, apps,
4205 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4206 }
4207 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004208 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004209 if (mAppsCustomizeContent != null) {
4210 mAppsCustomizeContent.onPackagesUpdated(
4211 LauncherModel.getSortedWidgetsAndShortcuts(this));
4212 }
Winson Chungc58497e2013-09-03 17:48:37 -07004213 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004214 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004215 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004216 mAppsCustomizeContent.onPackagesUpdated(
4217 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004218 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004220 }
4221
4222 /**
4223 * A package was updated.
4224 *
4225 * Implementation of the method from LauncherModel.Callbacks.
4226 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004227 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004228 Runnable r = new Runnable() {
4229 public void run() {
4230 bindAppsUpdated(apps);
4231 }
4232 };
4233 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004234 return;
4235 }
4236
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004237 if (mWorkspace != null) {
4238 mWorkspace.updateShortcuts(apps);
4239 }
Winson Chungc58497e2013-09-03 17:48:37 -07004240
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004241 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004242 mAppsCustomizeContent != null) {
4243 mAppsCustomizeContent.updateApps(apps);
4244 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004245 }
4246
4247 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004248 * Update the state of a package, typically related to install state.
4249 *
4250 * Implementation of the method from LauncherModel.Callbacks.
4251 */
4252 public void updatePackageState(String pkgName, int state) {
4253 if (mWorkspace != null) {
4254 mWorkspace.updatePackageState(pkgName, state);
4255 }
4256 }
4257
4258 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004259 * A package was uninstalled. We take both the super set of packageNames
4260 * in addition to specific applications to remove, the reason being that
4261 * this can be called when a package is updated as well. In that scenario,
4262 * we only remove specific components from the workspace, where as
4263 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004264 *
4265 * Implementation of the method from LauncherModel.Callbacks.
4266 */
Winson Chung83892cc2013-05-01 16:53:33 -07004267 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004268 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004269 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004270 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004271 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004272 }
Winson Chungd64d1762013-08-20 14:37:16 -07004273 };
4274 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004275 return;
4276 }
4277
Winson Chungdf95eb12013-10-16 14:57:07 -07004278 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004279 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004280 }
4281 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004282 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004283 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004284
Winson Chunga1820962011-10-03 16:31:06 -07004285 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004286 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004287
Winson Chungdf95eb12013-10-16 14:57:07 -07004288 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004289 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004290 mAppsCustomizeContent != null) {
4291 mAppsCustomizeContent.removeApps(appInfos);
4292 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004293 }
Joe Onoratobe386092009-11-17 17:32:16 -08004294
4295 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004296 * A number of packages were updated.
4297 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004298 private ArrayList<Object> mWidgetsAndShortcuts;
4299 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004300 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004301 bindPackagesUpdated(mWidgetsAndShortcuts);
4302 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004303 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004304 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004305 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4306 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4307 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004308 return;
4309 }
4310
Winson Chung64359a52013-07-08 17:17:08 -07004311 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004312 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004313 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004314 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004315 }
4316
Winson Chung400438b2011-01-16 17:53:48 -08004317 private int mapConfigurationOriActivityInfoOri(int configOri) {
4318 final Display d = getWindowManager().getDefaultDisplay();
4319 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4320 switch (d.getRotation()) {
4321 case Surface.ROTATION_0:
4322 case Surface.ROTATION_180:
4323 // We are currently in the same basic orientation as the natural orientation
4324 naturalOri = configOri;
4325 break;
4326 case Surface.ROTATION_90:
4327 case Surface.ROTATION_270:
4328 // We are currently in the other basic orientation to the natural orientation
4329 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4330 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4331 break;
4332 }
4333
4334 int[] oriMap = {
4335 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4336 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4337 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4338 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4339 };
4340 // Since the map starts at portrait, we need to offset if this device's natural orientation
4341 // is landscape.
4342 int indexOffset = 0;
4343 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4344 indexOffset = 1;
4345 }
4346 return oriMap[(d.getRotation() + indexOffset) % 4];
4347 }
Adam Cohen446e9402011-09-15 18:21:21 -07004348
Winson Chung4b919f82012-05-01 10:44:08 -07004349 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004350 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004351 getResources().getBoolean(R.bool.allow_rotation);
4352 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004353 }
Winson Chung4b919f82012-05-01 10:44:08 -07004354 public void lockScreenOrientation() {
4355 if (isRotationEnabled()) {
4356 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4357 .getConfiguration().orientation));
4358 }
4359 }
4360 public void unlockScreenOrientation(boolean immediate) {
4361 if (isRotationEnabled()) {
4362 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004363 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004364 } else {
4365 mHandler.postDelayed(new Runnable() {
4366 public void run() {
4367 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4368 }
4369 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004370 }
Winson Chung4b919f82012-05-01 10:44:08 -07004371 }
Winson Chung400438b2011-01-16 17:53:48 -08004372 }
4373
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004374 /**
4375 * Called when the SearchBar hint should be changed.
4376 *
4377 * @param hint the hint to be displayed in the search bar.
4378 */
4379 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004380 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004381 }
4382
Winson Chunge43a1e72014-01-15 10:33:02 -08004383 protected boolean isLauncherPreinstalled() {
4384 PackageManager pm = getPackageManager();
4385 try {
4386 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4387 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4388 return true;
4389 } else {
4390 return false;
4391 }
4392 } catch (NameNotFoundException e) {
4393 e.printStackTrace();
4394 return false;
4395 }
4396 }
4397
Winson Chunge6eabff2013-09-24 11:01:23 -07004398 protected String getFirstRunClingSearchBarHint() {
4399 return "";
4400 }
4401 protected String getFirstRunCustomContentHint() {
4402 return "";
4403 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004404 protected int getFirstRunFocusedHotseatAppDrawableId() {
4405 return -1;
4406 }
4407 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4408 return null;
4409 }
4410 protected int getFirstRunFocusedHotseatAppRank() {
4411 return -1;
4412 }
4413 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4414 return "";
4415 }
4416 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4417 return "";
4418 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004419
Winson Chungaf40f202013-09-18 18:26:31 -07004420 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004421 mLauncherClings.dismissFirstRunCling(v);
4422 }
4423 public void dismissMigrationClingCopyApps(View v) {
4424 mLauncherClings.dismissMigrationClingCopyApps(v);
4425 }
4426 public void dismissMigrationClingUseDefault(View v) {
4427 mLauncherClings.dismissMigrationClingUseDefault(v);
4428 }
4429 public void dismissMigrationWorkspaceCling(View v) {
4430 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004431 }
Winson Chung82f55532011-08-09 14:14:23 -07004432 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004433 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004434 }
4435 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004436 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004437 }
4438
Winson Chunga6945242014-01-08 14:04:34 -08004439 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004440 return !ActivityManager.isRunningInTestHarness() &&
4441 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004442 }
4443
4444 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004445 if (shouldRunFirstRunActivity() &&
4446 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004447 Intent firstRunIntent = getFirstRunActivity();
4448 if (firstRunIntent != null) {
4449 startActivity(firstRunIntent);
4450 markFirstRunActivityShown();
4451 }
4452 }
4453 }
4454
4455 private void markFirstRunActivityShown() {
4456 SharedPreferences.Editor editor = mSharedPrefs.edit();
4457 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4458 editor.apply();
4459 }
4460
4461 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004462 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4463 if (mHotseat != null) mHotseat.setAlpha(1f);
4464 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4465 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004466 }
4467
4468 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004469 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4470 if (mHotseat != null) mHotseat.setAlpha(0f);
4471 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4472 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004473 }
4474
4475
Mathew Inwood72fbec12013-11-19 15:45:07 +00004476 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4477 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4478 if (ri == null) {
4479 return null;
4480 }
4481 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4482 }
4483
4484 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4485 Bitmap icon) {
4486 return new ShortcutInfo(shortcutIntent, caption, icon);
4487 }
4488
4489 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4490 dragView.setTag(dragInfo);
4491 mWorkspace.onDragStartedWithItem(dragView);
4492 mWorkspace.beginDragShared(dragView, source);
4493 }
4494
Anjali Koppalf5d29132014-02-28 13:33:27 -08004495 @Override
4496 public void onPageSwitch(View newPage, int newPageIndex) {
4497 }
4498
Winson Chung80baf5a2010-08-09 16:03:15 -07004499 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004500 * Prints out out state for debugging.
4501 */
4502 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004503 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004504 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004505 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4506 Log.d(TAG, "mRestoring=" + mRestoring);
4507 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4508 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004509 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004510 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004511
Winson Chung785d2eb2011-04-14 16:08:02 -07004512 if (mAppsCustomizeContent != null) {
4513 mAppsCustomizeContent.dumpState();
4514 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004515 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004516 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004517
4518 @Override
4519 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4520 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004521 synchronized (sDumpLogs) {
4522 writer.println(" ");
4523 writer.println("Debug logs: ");
4524 for (int i = 0; i < sDumpLogs.size(); i++) {
4525 writer.println(" " + sDumpLogs.get(i));
4526 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004527 }
4528 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004529
4530 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004531 if (DEBUG_DUMP_LOG) {
4532 synchronized (sDumpLogs) {
4533 Log.d(TAG, "");
4534 Log.d(TAG, "*********************");
4535 Log.d(TAG, "Launcher debug logs: ");
4536 for (int i = 0; i < sDumpLogs.size(); i++) {
4537 Log.d(TAG, " " + sDumpLogs.get(i));
4538 }
4539 Log.d(TAG, "*********************");
4540 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004541 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004542 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004543 }
4544
4545 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004546 addDumpLog(tag, log, null, debugLog);
4547 }
4548
4549 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004550 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004551 if (e != null) {
4552 Log.d(tag, log, e);
4553 } else {
4554 Log.d(tag, log);
4555 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004556 }
Winson Chungede41292013-09-19 16:27:36 -07004557 if (DEBUG_DUMP_LOG) {
4558 sDateStamp.setTime(System.currentTimeMillis());
4559 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004560 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4561 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004562 }
Winson Chungede41292013-09-19 16:27:36 -07004563 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004564 }
4565
Winson Chungede41292013-09-19 16:27:36 -07004566 public void dumpLogsToLocalData() {
4567 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004568 new AsyncTask<Void, Void, Void>() {
4569 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004570 boolean success = false;
4571 sDateStamp.setTime(sRunStart);
4572 String FILENAME = sDateStamp.getMonth() + "-"
4573 + sDateStamp.getDay() + "_"
4574 + sDateStamp.getHours() + "-"
4575 + sDateStamp.getMinutes() + "_"
4576 + sDateStamp.getSeconds() + ".txt";
4577
4578 FileOutputStream fos = null;
4579 File outFile = null;
4580 try {
4581 outFile = new File(getFilesDir(), FILENAME);
4582 outFile.createNewFile();
4583 fos = new FileOutputStream(outFile);
4584 } catch (Exception e) {
4585 e.printStackTrace();
4586 }
4587 if (fos != null) {
4588 PrintWriter writer = new PrintWriter(fos);
4589
4590 writer.println(" ");
4591 writer.println("Debug logs: ");
4592 synchronized (sDumpLogs) {
4593 for (int i = 0; i < sDumpLogs.size(); i++) {
4594 writer.println(" " + sDumpLogs.get(i));
4595 }
4596 }
4597 writer.close();
4598 }
4599 try {
4600 if (fos != null) {
4601 fos.close();
4602 success = true;
4603 }
4604 } catch (IOException e) {
4605 e.printStackTrace();
4606 }
Michael Jurka43467462013-11-14 12:03:58 +01004607 return null;
Winson Chungede41292013-09-19 16:27:36 -07004608 }
Michael Jurka43467462013-11-14 12:03:58 +01004609 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004610 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004612}
Michael Jurka2763be32011-02-24 11:19:57 -08004613
Michael Jurkaabded662011-03-04 12:06:57 -08004614interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004615 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004616 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004617 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004618 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004619 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004620}
Dan Sandlerd5024042014-01-09 15:01:33 -05004621
4622interface DebugIntents {
4623 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4624 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004625}