blob: d70967d37aa3f47c2b3bc8978f1db6e28438eee0 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070052import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070070import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
94import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Daniel Sandler325dc232013-06-05 22:57:57 -040097import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700101import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000113import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
Winson Chunga6945242014-01-08 14:04:34 -0800115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100119public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Daniel Sandlerf061f822013-06-27 13:59:36 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400128 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700129 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700130
Dan Sandlerd5024042014-01-09 15:01:33 -0500131 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 private static final int REQUEST_BIND_APPWIDGET = 11;
141
Mathew Inwood876a8462013-06-14 14:12:41 +0100142 /**
143 * IntentStarter uses request codes starting with this. This must be greater than all activity
144 * request codes used internally.
145 */
146 protected static final int REQUEST_LAST = 100;
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Romain Guy98d01652009-06-30 16:21:04 -0700153 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800154 // To turn on these properties, type
155 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
156 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
157 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800158 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Winson Chung2672ff92012-05-04 16:22:30 -0700160 // The Intent extra that defines whether to ignore the launch animation
161 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400162 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: int
165 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700166 // Type: int
167 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700169 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
170 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: boolean
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
178 // Type: long
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
182 // Type: int
183 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
184 // Type: parcelable
185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000186 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: int[]
189 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
Adam Cohenb54a5982014-01-08 15:21:04 -0800191
192 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
193
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700195 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700199
Adam Cohen39a06042013-07-19 14:30:12 -0700200 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700201 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700202
Winson Chunga6945242014-01-08 14:04:34 -0800203 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700206 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700207 private State mState = State.WORKSPACE;
208 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800215 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000217 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
218 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
219
Winson Chunga2413752012-04-03 14:22:34 -0700220 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700221 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
222 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700223 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700224
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800225 private final BroadcastReceiver mCloseSystemDialogsReceiver
226 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800227 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private LayoutInflater mInflater;
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700232 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800233 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800234 private DragLayer mDragLayer;
235 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700236 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800237 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 private AppWidgetManager mAppWidgetManager;
240 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Michael Jurkac9d95c52011-08-29 14:03:34 -0700242 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700243 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800244 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700245
Michael Jurka0280c3b2010-09-17 15:00:07 -0700246 private int[] mTmpAddItemCellCoordinates = new int[2];
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private FolderInfo mFolderInfo;
249
Winson Chung3d503fb2011-07-13 17:25:49 -0700250 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800251 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700252
Michael Jurka838a4ca2011-02-07 13:33:06 -0800253 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Winson Chungc51db6a2011-10-05 11:44:49 -0700255 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700256 private AppsCustomizeTabHost mAppsCustomizeTabHost;
257 private AppsCustomizePagedView mAppsCustomizeContent;
258 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100259 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700262 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
263 // scroll issues (because the workspace may not have been measured yet) and extra work.
264 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
265 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
267 private SpannableStringBuilder mDefaultKeySsb = null;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private boolean mWorkspaceLoading = true;
270
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800271 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private boolean mRestoring;
273 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700274 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
278
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // Keep track of whether the user has left launcher
280 private static boolean sPausedFromUserAction = false;
281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedInstanceState;
283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mUserPresent = true;
287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700291 private static LocaleConfiguration sLocaleConfiguration = null;
292
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700293 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Adam Cohen61f560d2013-09-30 15:58:20 -0700295 private View.OnTouchListener mHapticFeedbackTouchListener;
296
Adam Cohended9f8d2010-11-03 13:25:16 -0700297 // Related to the auto-advancing of widgets
298 private final int ADVANCE_MSG = 1;
299 private final int mAdvanceInterval = 20000;
300 private final int mAdvanceStagger = 250;
301 private long mAutoAdvanceSentTime;
302 private long mAutoAdvanceTimeLeft = -1;
303 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
304 new HashMap<View, AppWidgetProviderInfo>();
305
Winson Chung400438b2011-01-16 17:53:48 -0800306 // Determines how long to wait after a rotation before restoring the screen orientation to
307 // match the sensor state.
308 private final int mRestoreScreenOrientationDelay = 500;
309
Michael Jurka4ef207b2010-11-29 17:05:45 -0800310 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700311 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
312 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
313 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800314
Winson Chungd64a6662013-09-30 11:06:59 -0700315 private Intent mAppMarketIntent = null;
316 private static final boolean DISABLE_MARKET_BUTTON = true;
317
Craig Mautner360310b2012-10-26 15:13:08 -0700318 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700319
Adam Cohen1462de32012-07-24 22:34:36 -0700320 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700322
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700323 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700324 static Date sDateStamp = new Date();
325 static DateFormat sDateFormat =
326 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
327 static long sRunStart = System.currentTimeMillis();
328 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329
Winson Chung46353de2012-02-16 14:05:10 -0800330 // We only want to get the SharedPreferences once since it does an FS stat each time we get
331 // it from the context.
332 private SharedPreferences mSharedPrefs;
333
Adam Cohene25af792013-06-06 23:08:25 -0700334 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
335
Winson Chungf0c6ae02012-03-21 16:10:31 -0700336 // Holds the page that we need to animate to, and the icon views that we need to animate up
337 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700338 private ImageView mFolderIconImageView;
339 private Bitmap mFolderIconBitmap;
340 private Canvas mFolderIconCanvas;
341 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700342
Michael Jurkaddd62e92011-02-16 17:49:14 -0800343 private BubbleTextView mWaitingForResume;
344
Michael Jurkac1f5d262011-09-30 19:32:27 -0700345 private Runnable mBuildLayersRunnable = new Runnable() {
346 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700347 if (mWorkspace != null) {
348 mWorkspace.buildPageHardwareLayers();
349 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 }
351 };
352
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800353 private static ArrayList<PendingAddArguments> sPendingAddList
354 = new ArrayList<PendingAddArguments>();
355
Michael Jurka7267fa52013-09-26 15:29:57 -0700356 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800357
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 private static class PendingAddArguments {
359 int requestCode;
360 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700361 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700362 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 int cellX;
364 int cellY;
365 }
366
Daniel Sandlerff02d492013-08-05 02:12:05 -0400367 private Stats mStats;
368
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800369 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800370 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700371 }
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 @Override
374 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700375 if (DEBUG_STRICT_MODE) {
376 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
377 .detectDiskReads()
378 .detectDiskWrites()
379 .detectNetwork() // or .detectAll() for all detectable problems
380 .penaltyLog()
381 .build());
382 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
383 .detectLeakedSqlLiteObjects()
384 .detectLeakedClosableObjects()
385 .penaltyLog()
386 .penaltyDeath()
387 .build());
388 }
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400391
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400392 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400393 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700394
395 // Determine the dynamic grid properties
396 Point smallestSize = new Point();
397 Point largestSize = new Point();
398 Point realSize = new Point();
399 Display display = getWindowManager().getDefaultDisplay();
400 display.getCurrentSizeRange(smallestSize, largestSize);
401 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700402 DisplayMetrics dm = new DisplayMetrics();
403 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700404
Winson Chung5f8afe62013-08-12 16:19:28 -0700405 // Lazy-initialize the dynamic grid
406 DeviceProfile grid = app.initDynamicGrid(this,
407 Math.min(smallestSize.x, smallestSize.y),
408 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700409 realSize.x, realSize.y,
410 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700411
412 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700414 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mModel = app.setLauncher(this);
416 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700417 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400418 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800419 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Daniel Sandlerff02d492013-08-05 02:12:05 -0400422 mStats = new Stats(this);
423
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700424 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700425
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700426 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
427 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
430 // this also ensures that any synchronous binding below doesn't re-trigger another
431 // LauncherModel load.
432 mPaused = false;
433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200435 android.os.Debug.startMethodTracing(
436 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 }
438
Adam Cohen53805212013-10-01 10:39:23 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100444 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800446 registerContentObservers();
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
453 if (PROFILE_STARTUP) {
454 android.os.Debug.stopMethodTracing();
455 }
456
457 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500461 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800465 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
474 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Adam Cohenf9426d52012-06-04 17:26:21 -0700476 updateGlobalIcons();
477
478 // On large interfaces, we want the screen to auto-rotate based on the current orientation
479 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700480
Adam Cohenb54a5982014-01-08 15:21:04 -0800481 showFirstRunActivity();
Winson Chunga6945242014-01-08 14:04:34 -0800482 if (mModel.canMigrateFromOldLauncherDb()) {
483 mLauncherClings.showMigrationCling();
484 } else {
485 mLauncherClings.showFirstRunCling();
486 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 }
488
Winson Chung4a2afa32012-07-19 14:53:05 -0700489 protected void onUserLeaveHint() {
490 super.onUserLeaveHint();
491 sPausedFromUserAction = true;
492 }
493
Winson Chung98ca0f72013-07-29 12:58:51 -0700494 /** To be overriden by subclasses to hint to Launcher that we have custom content */
495 protected boolean hasCustomContentToLeft() {
496 return false;
497 }
498
Dave Hawkeya8881582013-09-17 15:55:33 -0600499 /**
500 * To be overridden by subclasses to create the custom content and call
501 * {@link #addToCustomContentPage}. This will only be invoked if
502 * {@link #hasCustomContentToLeft()} is {@code true}.
503 */
504 protected void addCustomContentToLeft() {
505 }
506
507 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800508 * To be overridden by subclasses to indicate that there is an activity to launch
509 * before showing the standard launcher experience.
510 */
511 protected boolean hasFirstRunActivity() {
512 return false;
513 }
514
515 /**
516 * To be overridden by subclasses to launch any first run activity
517 */
518 protected Intent getFirstRunActivity() {
519 return null;
520 }
521
522 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600523 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
524 * ensure the custom content page is added or removed if necessary.
525 */
526 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600527 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 // Not bound yet, wait for bindScreens to be called.
529 return;
530 }
531
532 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
533 // Create the custom content page and call the subclass to populate it.
534 mWorkspace.createCustomContentPage();
535 addCustomContentToLeft();
536 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
537 mWorkspace.removeCustomContentPage();
538 }
539 }
540
Adam Cohenf9426d52012-06-04 17:26:21 -0700541 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700542 boolean searchVisible = false;
543 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800544 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700545 int coi = getCurrentOrientationIndexForGlobalIcons();
546 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
547 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700548 if (!DISABLE_MARKET_BUTTON) {
549 updateAppMarketIcon();
550 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700551 searchVisible = updateGlobalSearchIcon();
552 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700553 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700554 if (sGlobalSearchIcon[coi] != null) {
555 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700556 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700557 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700558 if (sVoiceSearchIcon[coi] != null) {
559 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700560 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungd64a6662013-09-30 11:06:59 -0700562 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700563 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800564 }
Winson Chungadf0c182012-08-23 14:59:07 -0700565 if (mSearchDropTargetBar != null) {
566 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
Romain Guycbb89e42009-06-08 15:52:54 -0700569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700571 if (sLocaleConfiguration == null) {
572 new AsyncTask<Void, Void, LocaleConfiguration>() {
573 @Override
574 protected LocaleConfiguration doInBackground(Void... unused) {
575 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
576 readConfiguration(Launcher.this, localeConfiguration);
577 return localeConfiguration;
578 }
579
580 @Override
581 protected void onPostExecute(LocaleConfiguration result) {
582 sLocaleConfiguration = result;
583 checkForLocaleChange(); // recursive, but now with a locale configuration
584 }
585 }.execute();
586 return;
587 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final Configuration configuration = getResources().getConfiguration();
590
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 final String locale = configuration.locale.toString();
593
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 final int mcc = configuration.mcc;
596
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 final int mnc = configuration.mnc;
599
Romain Guy84f296c2009-11-04 15:00:44 -0800600 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601
Romain Guy84f296c2009-11-04 15:00:44 -0800602 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 sLocaleConfiguration.locale = locale;
604 sLocaleConfiguration.mcc = mcc;
605 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700606
Joe Onorato0589f0f2010-02-08 13:44:00 -0800607 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608
609 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100610 new AsyncTask<Void, Void, Void>() {
611 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100613 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 }
Michael Jurka43467462013-11-14 12:03:58 +0100615 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700616 }
617 }
618
619 private static class LocaleConfiguration {
620 public String locale;
621 public int mcc = -1;
622 public int mnc = -1;
623 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700624
Romain Guy98d01652009-06-30 16:21:04 -0700625 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
626 DataInputStream in = null;
627 try {
628 in = new DataInputStream(context.openFileInput(PREFERENCES));
629 configuration.locale = in.readUTF();
630 configuration.mcc = in.readInt();
631 configuration.mnc = in.readInt();
632 } catch (FileNotFoundException e) {
633 // Ignore
634 } catch (IOException e) {
635 // Ignore
636 } finally {
637 if (in != null) {
638 try {
639 in.close();
640 } catch (IOException e) {
641 // Ignore
642 }
643 }
644 }
645 }
646
647 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
648 DataOutputStream out = null;
649 try {
650 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
651 out.writeUTF(configuration.locale);
652 out.writeInt(configuration.mcc);
653 out.writeInt(configuration.mnc);
654 out.flush();
655 } catch (FileNotFoundException e) {
656 // Ignore
657 } catch (IOException e) {
658 //noinspection ResultOfMethodCallIgnored
659 context.getFileStreamPath(PREFERENCES).delete();
660 } finally {
661 if (out != null) {
662 try {
663 out.close();
664 } catch (IOException e) {
665 // Ignore
666 }
667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
670
Anton Hanssona2f665f2013-09-26 12:18:32 +0100671 public Stats getStats() {
672 return mStats;
673 }
674
Bjorn Bringertc459e522013-06-07 19:36:01 +0100675 public LayoutInflater getInflater() {
676 return mInflater;
677 }
678
Winson Chung36a62fe2012-05-06 18:04:42 -0700679 boolean isDraggingEnabled() {
680 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
681 // that is subsequently removed from the workspace in startBinding().
682 return !mModel.isLoadingWorkspace();
683 }
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 static int getScreen() {
686 synchronized (sLock) {
687 return sScreen;
688 }
689 }
690
691 static void setScreen(int screen) {
692 synchronized (sLock) {
693 sScreen = screen;
694 }
695 }
696
Winson Chung557d6ed2011-07-08 15:34:52 -0700697 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000698 * Copied from View -- the View version of the method isn't called
699 * anywhere else in our process and only exists for API level 17+,
700 * so it's ok to keep our own version with no API requirement.
701 */
702 public static int generateViewId() {
703 for (;;) {
704 final int result = sNextGeneratedId.get();
705 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
706 int newValue = result + 1;
707 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
708 if (sNextGeneratedId.compareAndSet(result, newValue)) {
709 return result;
710 }
711 }
712 }
713
714 public int getViewIdForItem(ItemInfo info) {
715 // This cast is safe given the > 2B range for int.
716 int itemId = (int) info.id;
717 if (mItemIdToViewId.containsKey(itemId)) {
718 return mItemIdToViewId.get(itemId);
719 }
720 int viewId = generateViewId();
721 mItemIdToViewId.put(itemId, viewId);
722 return viewId;
723 }
724
725 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700726 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
727 * a configuration step, this allows the proper animations to run after other transitions.
728 */
729 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700730 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 switch (args.requestCode) {
732 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700733 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 break;
736 case REQUEST_PICK_SHORTCUT:
737 processShortcut(args.intent);
738 break;
739 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700740 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700741 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700742 result = true;
743 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 case REQUEST_CREATE_APPWIDGET:
745 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700746 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700747 result = true;
748 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800749 }
Michael Jurka27614d22012-04-02 06:40:22 -0700750 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
751 // if you turned the screen off and then back while in All Apps, Launcher would not
752 // return to the workspace. Clearing mAddInfo.container here fixes this issue
753 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700754 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 }
756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 protected void onActivityResult(
759 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700760 // Reset the startActivity waiting flag
761 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 int pendingAddWidgetId = mPendingAddWidgetId;
763 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700764
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 Runnable exitSpringLoaded = new Runnable() {
766 @Override
767 public void run() {
768 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
769 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
770 }
771 };
772
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
776 if (resultCode == RESULT_CANCELED) {
777 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
779 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
782 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 }
784 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200785 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
786 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
787 mWorkspace.exitOverviewMode(false);
788 }
789 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700790 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200791
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
793 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700794
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 // We have special handling for widgets
796 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800797 final int appWidgetId;
798 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
799 : -1;
800 if (widgetId < 0) {
801 appWidgetId = pendingAddWidgetId;
802 } else {
803 appWidgetId = widgetId;
804 }
805
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 final int result;
807 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800808 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700809 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
810 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700811 result = RESULT_CANCELED;
812 completeTwoStageWidgetDrop(result, appWidgetId);
813 onComplete = new Runnable() {
814 @Override
815 public void run() {
816 exitSpringLoadedDragModeDelayed(false, 0, null);
817 }
818 };
Winson Chung5aaab772012-04-27 15:24:02 -0700819 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700820 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700821 final CellLayout dropLayout =
822 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
823 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 onComplete = new Runnable() {
825 @Override
826 public void run() {
827 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700828 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 }
830 };
Winson Chung5aaab772012-04-27 15:24:02 -0700831 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700832 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
833 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800834 return;
835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 // The pattern used here is that a user PICKs a specific application,
838 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
841 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700842 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800843 final PendingAddArguments args = new PendingAddArguments();
844 args.requestCode = requestCode;
845 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 args.cellX = mPendingAddInfo.cellX;
849 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800850 if (isWorkspaceLocked()) {
851 sPendingAddList.add(args);
852 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700853 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700855 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700857 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700858 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800861 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800862 }
863
864 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700865 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700866 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800867 Runnable onCompleteRunnable = null;
868 int animationType = 0;
869
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700870 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 if (resultCode == RESULT_OK) {
872 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
873 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700874 mPendingAddWidgetInfo);
875 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 onCompleteRunnable = new Runnable() {
877 @Override
878 public void run() {
879 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700880 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
882 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884 };
885 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800886 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800888 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700889 if (mDragLayer.getAnimatedView() != null) {
890 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
891 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
892 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700893 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 // The animated view may be null in the case of a rotation during widget configuration
895 onCompleteRunnable.run();
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700900 protected void onStop() {
901 super.onStop();
902 FirstFrameAnimatorHelper.setIsVisible(false);
903 }
904
905 @Override
906 protected void onStart() {
907 super.onStart();
908 FirstFrameAnimatorHelper.setIsVisible(true);
909 }
910
911 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200913 long startTime = 0;
914 if (DEBUG_RESUME_TIME) {
915 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400916 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700919
Winson Chung4a2afa32012-07-19 14:53:05 -0700920 // Restore the previous launcher state
921 if (mOnResumeState == State.WORKSPACE) {
922 showWorkspace(false);
923 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800924 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700925 }
926 mOnResumeState = State.NONE;
927
Craig Mautner360310b2012-10-26 15:13:08 -0700928 // Background was set to gradient in onPause(), restore to black if in all apps.
929 setWorkspaceBackground(mState == State.WORKSPACE);
930
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800931 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700932 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700933 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400934 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500935 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400936 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700937 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700939 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200940 // We might have postponed some bind calls until onResume (see waitUntilResume) --
941 // execute them here
942 long startTimeCallbacks = 0;
943 if (DEBUG_RESUME_TIME) {
944 startTimeCallbacks = System.currentTimeMillis();
945 }
946
947 if (mAppsCustomizeContent != null) {
948 mAppsCustomizeContent.setBulkBind(true);
949 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700950 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
951 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200952 }
953 if (mAppsCustomizeContent != null) {
954 mAppsCustomizeContent.setBulkBind(false);
955 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700956 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200957 if (DEBUG_RESUME_TIME) {
958 Log.d(TAG, "Time spent processing callbacks in onResume: " +
959 (System.currentTimeMillis() - startTimeCallbacks));
960 }
Michael Jurka447bf842013-05-15 14:52:15 +0200961 }
Michael Jurka54554252013-08-01 12:52:23 +0200962 if (mOnResumeCallbacks.size() > 0) {
963 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
964 mOnResumeCallbacks.get(i).run();
965 }
966 mOnResumeCallbacks.clear();
967 }
Winson Chunge4e50662012-01-23 14:45:13 -0800968
969 // Reset the pressed state of icons that were locked in the press state while activities
970 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800971 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800972 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800973 mWaitingForResume.setStayPressed(false);
974 }
Winson Chunge4e50662012-01-23 14:45:13 -0800975 if (mAppsCustomizeContent != null) {
976 // Resets the previous all apps icon press state
977 mAppsCustomizeContent.resetDrawableState();
978 }
Winson Chung82963d52013-10-09 11:20:57 -0700979
Adam Cohen06dff352012-06-01 17:17:08 -0700980 // It is possible that widgets can receive updates while launcher is not in the foreground.
981 // Consequently, the widgets will be inflated in the orientation of the foreground activity
982 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
983 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700984 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700985
Winson Chung780fe592013-09-26 14:48:44 -0700986 // Process any items that were added while Launcher was away.
987 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
988
Winson Chung5841efa2013-09-30 18:06:44 -0700989 // Update the voice search button proxy
990 updateVoiceButtonProxyVisible(false);
991
Adam Cohenf9426d52012-06-04 17:26:21 -0700992 // Again, as with the above scenario, it's possible that one or more of the global icons
993 // were updated in the wrong orientation.
994 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200995 if (DEBUG_RESUME_TIME) {
996 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
997 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700998
999 if (mWorkspace.getCustomContentCallbacks() != null) {
1000 // If we are resuming and the custom content is the current page, we call onShow().
1001 // It is also poassible that onShow will instead be called slightly after first layout
1002 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1003 if (mWorkspace.isOnOrMovingToCustomContent()) {
1004 mWorkspace.getCustomContentCallbacks().onShow();
1005 }
1006 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001007 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001008 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001009 }
1010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001012 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001013 // Ensure that items added to Launcher are queued until Launcher returns
1014 InstallShortcutReceiver.enableInstallQueue();
1015
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001017 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001018 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001019 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001020
1021 // We call onHide() aggressively. The custom content callbacks should be able to
1022 // debounce excess onHide calls.
1023 if (mWorkspace.getCustomContentCallbacks() != null) {
1024 mWorkspace.getCustomContentCallbacks().onHide();
1025 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 }
Romain Guycbb89e42009-06-08 15:52:54 -07001027
Adam Cohen66a01fd2013-06-11 12:48:00 -07001028 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001029 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001030 addCustomContentToLeft();
1031 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001032 }
1033
Adam Cohenbffe7452013-07-22 18:21:45 -07001034 QSBScroller mQsbScroller = new QSBScroller() {
1035 int scrollY = 0;
1036
1037 @Override
1038 public void setScrollY(int scroll) {
1039 scrollY = scroll;
1040
1041 if (mWorkspace.isOnOrMovingToCustomContent()) {
1042 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001043 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001044 }
1045 }
1046 };
1047
1048 public void resetQSBScroll() {
1049 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001050 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001051 }
1052
1053 public interface CustomContentCallbacks {
1054 // Custom content is completely shown
1055 public void onShow();
1056
1057 // Custom content is completely hidden
1058 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001059
1060 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1061 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001062 }
1063
Jorim Jaggid017f882014-01-14 17:08:48 -08001064 protected boolean hasSettings() {
1065 return false;
1066 }
1067
Adam Cohen30bacb22013-08-29 14:25:25 -07001068 protected void startSettings() {
1069 }
1070
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 public interface QSBScroller {
1072 public void setScrollY(int scrollY);
1073 }
1074
Winson Chung98ca0f72013-07-29 12:58:51 -07001075 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001076 CustomContentCallbacks callbacks, String description) {
1077 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001078 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001079 }
1080
Adam Cohenedb40762013-07-18 16:45:45 -07001081 // The custom content needs to offset its content to account for the QSB
1082 public int getTopOffsetForCustomContent() {
1083 return mWorkspace.getPaddingTop();
1084 }
1085
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001086 @Override
1087 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 // Flag the loader to stop early before switching
1089 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 if (mAppsCustomizeContent != null) {
1091 mAppsCustomizeContent.surrender();
1092 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001093 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001094 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001096 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 @Override
1098 public void onWindowFocusChanged(boolean hasFocus) {
1099 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001100 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001101 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 private boolean acceptFilter() {
1104 final InputMethodManager inputManager = (InputMethodManager)
1105 getSystemService(Context.INPUT_METHOD_SERVICE);
1106 return !inputManager.isFullscreenMode();
1107 }
1108
1109 @Override
1110 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001111 final int uniChar = event.getUnicodeChar();
1112 final boolean handled = super.onKeyDown(keyCode, event);
1113 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1114 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1116 keyCode, event);
1117 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001118 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001119 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001120 // showSearchDialog()
1121 // If there are multiple keystrokes before the search dialog takes focus,
1122 // onSearchRequested() will be called for every keystroke,
1123 // but it is idempotent, so it's fine.
1124 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126 }
1127
Joe Onorato8a9625e2010-01-28 15:55:35 -08001128 // Eat the long press event so the keyboard doesn't come up.
1129 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1130 return true;
1131 }
1132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 return handled;
1134 }
1135
Karl Rosaen138a0412009-04-23 19:00:21 -07001136 private String getTypedText() {
1137 return mDefaultKeySsb.toString();
1138 }
1139
1140 private void clearTypedText() {
1141 mDefaultKeySsb.clear();
1142 mDefaultKeySsb.clearSpans();
1143 Selection.setSelection(mDefaultKeySsb, 0);
1144 }
1145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001147 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1148 * State
1149 */
1150 private static State intToState(int stateOrdinal) {
1151 State state = State.WORKSPACE;
1152 final State[] stateValues = State.values();
1153 for (int i = 0; i < stateValues.length; i++) {
1154 if (stateValues[i].ordinal() == stateOrdinal) {
1155 state = stateValues[i];
1156 break;
1157 }
1158 }
1159 return state;
1160 }
1161
1162 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 * Restores the previous state, if it exists.
1164 *
1165 * @param savedState The previous state.
1166 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001167 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 private void restoreState(Bundle savedState) {
1169 if (savedState == null) {
1170 return;
1171 }
1172
Michael Jurka883f55b2010-10-21 15:47:14 -07001173 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001174 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001175 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001176 }
1177
Adam Cohen21cd0022013-10-09 18:57:02 -07001178 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1179 PagedView.INVALID_RESTORE_PAGE);
1180 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001181 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183
Winson Chung3d503fb2011-07-13 17:25:49 -07001184 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1188 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001189 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1191 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001192 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1193 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1194 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001195 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001196 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 mRestoring = true;
1198 }
1199
1200 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1201 if (renameFolder) {
1202 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001203 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 mRestoring = true;
1205 }
Winson Chunga12a2502010-12-20 14:41:35 -08001206
Winson Chung785d2eb2011-04-14 16:08:02 -07001207 // Restore the AppsCustomize tab
1208 if (mAppsCustomizeTabHost != null) {
1209 String curTab = savedState.getString("apps_customize_currentTab");
1210 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001211 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001213 mAppsCustomizeContent.loadAssociatedPages(
1214 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
1216
Winson Chung5afbf7b2011-07-25 11:53:08 -07001217 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1218 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001219 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001220 mItemIdToViewId = (HashMap<Integer, Integer>)
1221 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223
1224 /**
1225 * Finds all the views we need and configure them properly.
1226 */
1227 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001228 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001229
Craig Mautner360310b2012-10-26 15:13:08 -07001230 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001231 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1232 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001233 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001234
John Spurlock77e1f472013-09-11 10:09:51 -04001235 mLauncherView.setSystemUiVisibility(
1236 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001237 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
Winson Chung4c98d922011-05-31 16:50:48 -07001239 // Setup the drag layer
1240 mDragLayer.setup(this, dragController);
1241
Winson Chung3d503fb2011-07-13 17:25:49 -07001242 // Setup the hotseat
1243 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1244 if (mHotseat != null) {
1245 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001246 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001247 }
1248
Jorim Jaggid017f882014-01-14 17:08:48 -08001249 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001250 View widgetButton = findViewById(R.id.widget_button);
1251 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001252 @Override
1253 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001254 if (!mWorkspace.isSwitchingState()) {
1255 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1256 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001257 }
1258 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001259 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1260
1261 View wallpaperButton = findViewById(R.id.wallpaper_button);
1262 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001263 @Override
1264 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001265 if (!mWorkspace.isSwitchingState()) {
1266 startWallpaper();
1267 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001268 }
1269 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001270 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1271
1272 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001273 if (hasSettings()) {
1274 settingsButton.setOnClickListener(new OnClickListener() {
1275 @Override
1276 public void onClick(View arg0) {
1277 if (!mWorkspace.isSwitchingState()) {
1278 startSettings();
1279 }
1280 }
1281 });
1282 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1283 } else {
1284 settingsButton.setVisibility(View.GONE);
1285 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1286 lp.gravity = Gravity.END | Gravity.TOP;
1287 widgetButton.requestLayout();
1288 }
1289
Adam Cohendbdff6b2013-09-18 19:09:15 -07001290 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001291
Winson Chung4c98d922011-05-31 16:50:48 -07001292 // Setup the workspace
1293 mWorkspace.setHapticFeedbackEnabled(false);
1294 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001295 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001296 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001297
Winson Chungf0ea4d32011-06-06 14:27:16 -07001298 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001299 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001300
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001302 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001303 mAppsCustomizeContent = (AppsCustomizePagedView)
1304 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1305 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001306
Winson Chung3d503fb2011-07-13 17:25:49 -07001307 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001308 dragController.setDragScoller(mWorkspace);
1309 dragController.setScrollView(mDragLayer);
1310 dragController.setMoveTarget(mWorkspace);
1311 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001312 if (mSearchDropTargetBar != null) {
1313 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001314 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001315
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001316 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001317 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001318 mWeightWatcher = new WeightWatcher(this);
1319 mWeightWatcher.setAlpha(0.5f);
1320 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001321 new FrameLayout.LayoutParams(
1322 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001323 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001324 Gravity.BOTTOM)
1325 );
Adam Cohen39a06042013-07-19 14:30:12 -07001326
1327 boolean show = shouldShowWeightWatcher();
1328 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
1332 /**
1333 * Creates a view representing a shortcut.
1334 *
1335 * @param info The data structure describing the shortcut.
1336 *
1337 * @return A View inflated from R.layout.application.
1338 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001339 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001341 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
1344 /**
1345 * Creates a view representing a shortcut inflated from the specified resource.
1346 *
1347 * @param layoutResId The id of the XML layout used to create the shortcut.
1348 * @param parent The group the shortcut belongs to.
1349 * @param info The data structure describing the shortcut.
1350 *
1351 * @return A View inflated from layoutResId.
1352 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001353 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001354 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1355 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 return favorite;
1358 }
1359
1360 /**
1361 * Add an application shortcut to the workspace.
1362 *
1363 * @param data The intent describing the application.
1364 * @param cellInfo The position on screen where to create the shortcut.
1365 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001366 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001368 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001369
Adam Cohenfbba09b2011-07-18 21:43:05 -07001370 // First we check if we already know the exact location where we want to add this item.
1371 if (cellX >= 0 && cellY >= 0) {
1372 cellXY[0] = cellX;
1373 cellXY[1] = cellY;
1374 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001375 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001376 return;
1377 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001379 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001380
Romain Guy73b979d2009-06-09 12:57:21 -07001381 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001382 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001384 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001385 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001386 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001387 } else {
1388 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390 }
Romain Guycbb89e42009-06-08 15:52:54 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 /**
1393 * Add a shortcut to the workspace.
1394 *
1395 * @param data The intent describing the shortcut.
1396 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001398 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 int cellY) {
1400 int[] cellXY = mTmpAddItemCellCoordinates;
1401 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Michael Jurkad3ef3062010-11-23 16:23:58 -08001404 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001405
Adam Cohen558baaf2011-08-15 15:22:57 -07001406 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001407 if (info == null) {
1408 return;
1409 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001410 final View view = createShortcut(info);
1411
Adam Cohenfbba09b2011-07-18 21:43:05 -07001412 // First we check if we already know the exact location where we want to add this item.
1413 if (cellX >= 0 && cellY >= 0) {
1414 cellXY[0] = cellX;
1415 cellXY[1] = cellY;
1416 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001417
1418 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001419 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001420 true, null,null)) {
1421 return;
1422 }
1423 DragObject dragObject = new DragObject();
1424 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001425 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1426 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001427 return;
1428 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001429 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001430 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001432 foundCellSpan = (result != null);
1433 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001434 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001435 }
1436
1437 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001438 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001439 return;
1440 }
1441
Adam Cohendcd297f2013-06-18 13:13:40 -07001442 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443
1444 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448 }
1449
Adam Cohen2f093b62012-04-30 18:59:53 -07001450 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1451 int minHeight) {
1452 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001453 // We want to account for the extra amount of padding that we are adding to the widget
1454 // to ensure that it gets the full amount of space that it has requested
1455 int requiredWidth = minWidth + padding.left + padding.right;
1456 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001457 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001458 }
1459
Adam Cohen2f093b62012-04-30 18:59:53 -07001460 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1461 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001462 }
1463
Adam Cohen2f093b62012-04-30 18:59:53 -07001464 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1465 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1469 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001470 }
1471
Adam Cohen2f093b62012-04-30 18:59:53 -07001472 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1473 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001474 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001475 }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001478 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001480 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001481 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001483 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001484 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1485 if (appWidgetInfo == null) {
1486 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1487 }
Romain Guycbb89e42009-06-08 15:52:54 -07001488
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001489 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001490 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001491
Adam Cohen2f093b62012-04-30 18:59:53 -07001492 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1493 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001496 // We have saved the position to which the widget was dragged-- this really only matters
1497 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int[] cellXY = mTmpAddItemCellCoordinates;
1499 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001500 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1503 cellXY[0] = mPendingAddInfo.cellX;
1504 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001505 spanXY[0] = mPendingAddInfo.spanX;
1506 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 foundCellSpan = true;
1508 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001511 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1512 spanXY[1], cellXY, finalSpan);
1513 spanXY[0] = finalSpan[0];
1514 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001515 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001517 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001518 }
1519
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001521 if (appWidgetId != -1) {
1522 // Deleting an app widget ID is a void call but writes to disk before returning
1523 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001524 new AsyncTask<Void, Void, Void>() {
1525 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001526 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001527 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001528 }
Michael Jurka43467462013-11-14 12:03:58 +01001529 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001530 }
Winson Chung93eef082012-03-23 15:59:27 -07001531 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001532 return;
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001535 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001536 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1537 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 launcherInfo.spanX = spanXY[0];
1539 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001540 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1541 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
1546 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 if (hostView == null) {
1548 // Perform actual inflation because we're live
1549 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1550 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1551 } else {
1552 // The AppWidgetHostView has already been inflated and instantiated
1553 launcherInfo.hostView = hostView;
1554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001557 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001558 launcherInfo.notifyWidgetSizeChanged(this);
1559
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001562
1563 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001565 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1569 @Override
1570 public void onReceive(Context context, Intent intent) {
1571 final String action = intent.getAction();
1572 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1573 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001574 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001575 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001576
Winson Chungc100e8e2011-08-09 16:02:43 -07001577 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001578 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001579 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001580 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001582 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1583 mUserPresent = true;
1584 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001585 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1586 mModel.resetLoadedState(false, true);
1587 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1588 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1589 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1590 mModel.resetLoadedState(false, true);
1591 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1592 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1593 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 }
1595 }
1596 };
1597
1598 @Override
1599 public void onAttachedToWindow() {
1600 super.onAttachedToWindow();
1601
1602 // Listen for broadcasts related to user-presence
1603 final IntentFilter filter = new IntentFilter();
1604 filter.addAction(Intent.ACTION_SCREEN_OFF);
1605 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001606 if (ENABLE_DEBUG_INTENTS) {
1607 filter.addAction(DebugIntents.DELETE_DATABASE);
1608 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1609 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001611 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001612 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 mVisible = true;
1614 }
1615
1616 @Override
1617 public void onDetachedFromWindow() {
1618 super.onDetachedFromWindow();
1619 mVisible = false;
1620
Adam Cohend113e0c2010-11-11 10:48:05 -08001621 if (mAttached) {
1622 unregisterReceiver(mReceiver);
1623 mAttached = false;
1624 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 updateRunning();
1626 }
1627
1628 public void onWindowVisibilityChanged(int visibility) {
1629 mVisible = visibility == View.VISIBLE;
1630 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001631 // The following code used to be in onResume, but it turns out onResume is called when
1632 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1633 // is a more appropriate event to handle
1634 if (mVisible) {
1635 mAppsCustomizeTabHost.onWindowVisible();
1636 if (!mWorkspaceLoading) {
1637 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001638 // We want to let Launcher draw itself at least once before we force it to build
1639 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001640 // apps is nice and speedy.
1641 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001642 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001643 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001644 if (mStarted) return;
1645 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001646 // We delay the layer building a bit in order to give
1647 // other message processing a time to run. In particular
1648 // this avoids a delay in hiding the IME if it was
1649 // currently shown, because doing that may involve
1650 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001651 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001652 final ViewTreeObserver.OnDrawListener listener = this;
1653 mWorkspace.post(new Runnable() {
1654 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001655 if (mWorkspace != null &&
1656 mWorkspace.getViewTreeObserver() != null) {
1657 mWorkspace.getViewTreeObserver().
1658 removeOnDrawListener(listener);
1659 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001660 }
1661 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001662 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001663 }
1664 });
1665 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001666 // When Launcher comes back to foreground, a different Activity might be responsible for
1667 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001668 if (!DISABLE_MARKET_BUTTON) {
1669 updateAppMarketIcon();
1670 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001671 clearTypedText();
1672 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674
1675 private void sendAdvanceMessage(long delay) {
1676 mHandler.removeMessages(ADVANCE_MSG);
1677 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1678 mHandler.sendMessageDelayed(msg, delay);
1679 mAutoAdvanceSentTime = System.currentTimeMillis();
1680 }
1681
1682 private void updateRunning() {
1683 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1684 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1685 mAutoAdvanceRunning = autoAdvanceRunning;
1686 if (autoAdvanceRunning) {
1687 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1688 sendAdvanceMessage(delay);
1689 } else {
1690 if (!mWidgetsToAdvance.isEmpty()) {
1691 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1692 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1693 }
1694 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001695 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 }
1697 }
1698 }
1699
1700 private final Handler mHandler = new Handler() {
1701 @Override
1702 public void handleMessage(Message msg) {
1703 if (msg.what == ADVANCE_MSG) {
1704 int i = 0;
1705 for (View key: mWidgetsToAdvance.keySet()) {
1706 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1707 final int delay = mAdvanceStagger * i;
1708 if (v instanceof Advanceable) {
1709 postDelayed(new Runnable() {
1710 public void run() {
1711 ((Advanceable) v).advance();
1712 }
1713 }, delay);
1714 }
1715 i++;
1716 }
1717 sendAdvanceMessage(mAdvanceInterval);
1718 }
1719 }
1720 };
1721
1722 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001723 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1725 if (v instanceof Advanceable) {
1726 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001727 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 updateRunning();
1729 }
1730 }
1731
1732 void removeWidgetToAutoAdvance(View hostView) {
1733 if (mWidgetsToAdvance.containsKey(hostView)) {
1734 mWidgetsToAdvance.remove(hostView);
1735 updateRunning();
1736 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 }
1738
Joe Onorato9c1289c2009-08-17 11:03:03 -04001739 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741 launcherInfo.hostView = null;
1742 }
1743
Winson Chung93eef082012-03-23 15:59:27 -07001744 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1745 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1746 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 }
1748
Winson Chunga6945242014-01-08 14:04:34 -08001749 public DragLayer getDragLayer() {
1750 return mDragLayer;
1751 }
1752
1753 public Workspace getWorkspace() {
1754 return mWorkspace;
1755 }
1756
1757 public Hotseat getHotseat() {
1758 return mHotseat;
1759 }
1760
Jorim Jaggid017f882014-01-14 17:08:48 -08001761 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001762 return mOverviewPanel;
1763 }
1764
1765 public SearchDropTargetBar getSearchBar() {
1766 return mSearchDropTargetBar;
1767 }
1768
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001769 public LauncherAppWidgetHost getAppWidgetHost() {
1770 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Winson Chunga9abd0e2010-10-27 17:18:37 -07001773 public LauncherModel getModel() {
1774 return mModel;
1775 }
1776
Winson Chunga6945242014-01-08 14:04:34 -08001777 public LauncherClings getLauncherClings() {
1778 return mLauncherClings;
1779 }
1780
1781 protected SharedPreferences getSharedPrefs() {
1782 return mSharedPrefs;
1783 }
1784
Bjorn Bringertc459e522013-06-07 19:36:01 +01001785 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001786 getWindow().closeAllPanels();
1787
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001788 // Whatever we were doing is hereby canceled.
1789 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001790 }
1791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 @Override
1793 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001794 long startTime = 0;
1795 if (DEBUG_RESUME_TIME) {
1796 startTime = System.currentTimeMillis();
1797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 super.onNewIntent(intent);
1799
1800 // Close the menu
1801 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001802 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001803 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001804
Adam Cohened307df2013-10-02 09:37:31 -07001805 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1806 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1807 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001808
Adam Cohen6fecd412013-10-02 17:41:50 -07001809 if (mWorkspace == null) {
1810 // Can be cases where mWorkspace is null, this prevents a NPE
1811 return;
1812 }
1813 Folder openFolder = mWorkspace.getOpenFolder();
1814 // In all these cases, only animate if we're already on home
1815 mWorkspace.exitWidgetResizeMode();
1816 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001817 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001818 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001819 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001820
Adam Cohen6fecd412013-10-02 17:41:50 -07001821 closeFolder();
1822 exitSpringLoadedDragMode();
1823
1824 // If we are already on home, then just animate back to the workspace,
1825 // otherwise, just wait until onResume to set the state back to Workspace
1826 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001827 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001828 } else {
1829 mOnResumeState = State.WORKSPACE;
1830 }
1831
1832 final View v = getWindow().peekDecorView();
1833 if (v != null && v.getWindowToken() != null) {
1834 InputMethodManager imm = (InputMethodManager)getSystemService(
1835 INPUT_METHOD_SERVICE);
1836 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1837 }
1838
1839 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001840 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001841 mAppsCustomizeTabHost.reset();
1842 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001843
1844 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Adam Cohened307df2013-10-02 09:37:31 -07001846
Michael Jurka447bf842013-05-15 14:52:15 +02001847 if (DEBUG_RESUME_TIME) {
1848 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
Adam Coppa120b8e2013-11-12 16:26:04 +00001852 /**
1853 * Override point for subclasses to prevent movement to the default screen when the home
1854 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1855 */
1856 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1857 return true;
1858 }
1859
1860 /**
1861 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1862 */
1863 protected void onHomeIntent() {
1864 // Do nothing
1865 }
1866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001868 public void onRestoreInstanceState(Bundle state) {
1869 super.onRestoreInstanceState(state);
1870 for (int page: mSynchronouslyBoundPages) {
1871 mWorkspace.restoreInstanceStateForChild(page);
1872 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 }
1874
1875 @Override
1876 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001877 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001878 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1879 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001880 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001881 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
Michael Jurka883f55b2010-10-21 15:47:14 -07001883 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001884 // We close any open folder since it will not be re-opened, and we need to make sure
1885 // this state is reflected.
1886 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887
Adam Cohendcd297f2013-06-18 13:13:40 -07001888 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001889 mWaitingForResult) {
1890 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1893 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001894 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1895 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1896 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001897 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
1899
1900 if (mFolderInfo != null && mWaitingForResult) {
1901 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1902 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1903 }
Michael Jurka946ad472010-07-09 18:05:18 -07001904
Winson Chung785d2eb2011-04-14 16:08:02 -07001905 // Save the current AppsCustomize tab
1906 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001907 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1908 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001909 if (currentTabTag != null) {
1910 outState.putString("apps_customize_currentTab", currentTabTag);
1911 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001912 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1913 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001914 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001915 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
1918 @Override
1919 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001921
Winson Chunge7a03942011-08-05 15:05:12 -07001922 // Remove all pending runnables
1923 mHandler.removeMessages(ADVANCE_MSG);
1924 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001925 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001926
Winson Chungcd2b0142011-06-08 16:02:26 -07001927 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001928 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001929 mModel.stopLoader();
1930 app.setLauncher(null);
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001933 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001935 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001937 mAppWidgetHost = null;
1938
1939 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
1941 TextKeyListener.getInstance().release();
1942
Winson Chung81b52252012-08-27 15:34:29 -07001943 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1944 // to prevent leaking Launcher activities on orientation change.
1945 if (mModel != null) {
1946 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1947 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001948
1949 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001950 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001951
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001952 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001953 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001954 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001955 mWorkspace = null;
1956 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001957
Michael Jurka2ecf9952012-06-18 12:52:28 -07001958 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
Adam Cohena9cf38f2011-05-02 15:36:58 -07001961 public DragController getDragController() {
1962 return mDragController;
1963 }
1964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 @Override
1966 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001967 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 super.startActivityForResult(intent, requestCode);
1969 }
1970
Winson Chung70d72102011-08-12 11:24:35 -07001971 /**
1972 * Indicates that we want global search for this activity by setting the globalSearch
1973 * argument for {@link #startSearch} to true.
1974 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001976 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001978
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001979 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001980
Karl Rosaen138a0412009-04-23 19:00:21 -07001981 if (initialQuery == null) {
1982 // Use any text typed in the launcher as the initial query
1983 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 if (appSearchData == null) {
1986 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001987 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
Winson Chungadf0c182012-08-23 14:59:07 -07001989 Rect sourceBounds = new Rect();
1990 if (mSearchDropTargetBar != null) {
1991 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1992 }
Romain Guycbb89e42009-06-08 15:52:54 -07001993
Bjorn Bringertc459e522013-06-07 19:36:01 +01001994 startSearch(initialQuery, selectInitialQuery,
1995 appSearchData, sourceBounds);
1996 }
1997
1998 public void startSearch(String initialQuery,
1999 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002000 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002001 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002002 }
2003
2004 /**
2005 * Starts the global search activity. This code is a copied from SearchManager
2006 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002007 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002008 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002009 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002010 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2011 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2012 if (globalSearchActivity == null) {
2013 Log.w(TAG, "No global search activity found.");
2014 return;
2015 }
2016 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2017 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2018 intent.setComponent(globalSearchActivity);
2019 // Make sure that we have a Bundle to put source in
2020 if (appSearchData == null) {
2021 appSearchData = new Bundle();
2022 } else {
2023 appSearchData = new Bundle(appSearchData);
2024 }
2025 // Set source to package name of app that starts global search, if not set already.
2026 if (!appSearchData.containsKey("source")) {
2027 appSearchData.putString("source", getPackageName());
2028 }
2029 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2030 if (!TextUtils.isEmpty(initialQuery)) {
2031 intent.putExtra(SearchManager.QUERY, initialQuery);
2032 }
2033 if (selectInitialQuery) {
2034 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2035 }
2036 intent.setSourceBounds(sourceBounds);
2037 try {
2038 startActivity(intent);
2039 } catch (ActivityNotFoundException ex) {
2040 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
2043
Winson Chung70d72102011-08-12 11:24:35 -07002044 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002045 public boolean onPrepareOptionsMenu(Menu menu) {
2046 super.onPrepareOptionsMenu(menu);
Dan Sandler14c6a3f2014-01-07 14:12:27 -05002047 showOverviewMode(true);
Michael Jurkab94f3f82013-09-11 18:08:54 +02002048 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002051 @Override
2052 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002053 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002054 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002055 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002056 }
2057
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 public boolean isWorkspaceLocked() {
2059 return mWorkspaceLoading || mWaitingForResult;
2060 }
2061
Michael Jurka0280c3b2010-09-17 15:00:07 -07002062 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002063 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002064 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002065 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2066 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002067 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002068 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002069 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002070
Adam Cohenad4e15c2013-10-17 16:21:35 -07002071 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2072 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2073 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2074 }
2075
2076 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2077 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2078 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002079 if (appWidgetInfo.configure != null) {
2080 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002081 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002082
Bjorn Bringert7984c942009-12-09 15:38:25 +00002083 // Launch over to configure widget, if needed
2084 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002085 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002086 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002087 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002089 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002090 Runnable onComplete = new Runnable() {
2091 @Override
2092 public void run() {
2093 // Exit spring loaded mode if necessary after adding the widget
2094 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2095 null);
2096 }
2097 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002098 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002099 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002100 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 }
2102 }
Romain Guycbb89e42009-06-08 15:52:54 -07002103
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002104 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002105 // Close any folders that may be open.
2106 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002107 mWorkspace.moveToCustomContentScreen(animate);
2108 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002109 /**
2110 * Process a shortcut drop.
2111 *
2112 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002113 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002114 * @param cell The cell it should be added to, optional
2115 * @param position The location on the screen where it was dropped, optional
2116 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002117 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002118 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002119 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002120 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002121 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002122 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002123
2124 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002125 mPendingAddInfo.cellX = cell[0];
2126 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002127 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002128
2129 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2130 createShortcutIntent.setComponent(componentName);
2131 processShortcut(createShortcutIntent);
2132 }
2133
Adam Cohenfbba09b2011-07-18 21:43:05 -07002134 /**
2135 * Process a widget drop.
2136 *
2137 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002138 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002139 * @param cell The cell it should be added to, optional
2140 * @param position The location on the screen where it was dropped, optional
2141 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002142 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002143 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002144 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002145 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002146 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002147 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002148 mPendingAddInfo.minSpanX = info.minSpanX;
2149 mPendingAddInfo.minSpanY = info.minSpanY;
2150
Adam Cohenfbba09b2011-07-18 21:43:05 -07002151 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002152 mPendingAddInfo.cellX = cell[0];
2153 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002154 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002155 if (span != null) {
2156 mPendingAddInfo.spanX = span[0];
2157 mPendingAddInfo.spanY = span[1];
2158 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002159
Adam Cohened66b2b2012-01-23 17:28:51 -08002160 AppWidgetHostView hostView = info.boundWidget;
2161 int appWidgetId;
2162 if (hostView != null) {
2163 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002164 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002165 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002166 // In this case, we either need to start an activity to get permission to bind
2167 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002168 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002169 Bundle options = info.bindOptions;
2170
2171 boolean success = false;
2172 if (options != null) {
2173 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2174 info.componentName, options);
2175 } else {
2176 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2177 info.componentName);
2178 }
2179 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002180 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002181 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002182 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002183 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2184 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2185 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002186 // TODO: we need to make sure that this accounts for the options bundle.
2187 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002188 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2189 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002190 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002191 }
2192
Joe Onoratodeb98af2010-02-19 14:59:39 -08002193 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002194 // Handle case where user selected "Applications"
2195 String applicationName = getResources().getString(R.string.group_applications);
2196 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002197
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002198 if (applicationName != null && applicationName.equals(shortcutName)) {
2199 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2200 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002201
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002202 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2203 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002204 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002205 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002206 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002207 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
2210
Winson Chung24ab2f12010-09-16 14:10:47 -07002211 void processWallpaper(Intent intent) {
2212 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2213 }
2214
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002216 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002217 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 folderInfo.title = getText(R.string.folder_name);
2219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002221 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002222 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002223 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224
2225 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002226 FolderIcon newFolder =
2227 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002229 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002230 // Force measure the new folder icon
2231 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2232 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002233 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 }
Romain Guycbb89e42009-06-08 15:52:54 -07002235
Joe Onorato9c1289c2009-08-17 11:03:03 -04002236 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002237 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002238 }
2239
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002240 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002242 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002243 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 }
2245
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002246 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002247 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002248 }
2249
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002250 /**
2251 * Registers various content observers. The current implementation registers
2252 * only a favorites observer to keep track of the favorites applications.
2253 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002254 private void registerContentObservers() {
2255 ContentResolver resolver = getContentResolver();
2256 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2257 true, mWidgetObserver);
2258 }
2259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 @Override
2261 public boolean dispatchKeyEvent(KeyEvent event) {
2262 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2263 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002265 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002266 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002267 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002268 dumpState();
2269 return true;
2270 }
2271 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002272 }
2273 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2274 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002275 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 return true;
2277 }
2278 }
2279
2280 return super.dispatchKeyEvent(event);
2281 }
2282
Joe Onorato88ec0992009-11-19 13:16:06 -08002283 @Override
2284 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002285 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002286 if (mAppsCustomizeContent.getContentType() ==
2287 AppsCustomizePagedView.ContentType.Applications) {
2288 showWorkspace(true);
2289 } else {
2290 showOverviewMode(true);
2291 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002292 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002293 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002294 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002295 Folder openFolder = mWorkspace.getOpenFolder();
2296 if (openFolder.isEditingName()) {
2297 openFolder.dismissEditingName();
2298 } else {
2299 closeFolder();
2300 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002301 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002302 mWorkspace.exitWidgetResizeMode();
2303
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002304 // Back button is a no-op here, but give at least some feedback for the button press
2305 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002306 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002307 }
2308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002310 * Re-listen when widgets are reset.
2311 */
2312 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002313 if (mAppWidgetHost != null) {
2314 mAppWidgetHost.startListening();
2315 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002316 }
2317
2318 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 * Launches the intent referred by the clicked shortcut.
2320 *
2321 * @param v The view representing the clicked shortcut.
2322 */
2323 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002324 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2325 // view has detached (it's possible for this to happen if the view is removed mid touch).
2326 if (v.getWindowToken() == null) {
2327 return;
2328 }
2329
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002330 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002331 return;
2332 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002333
Adam Cohen1697b792013-09-17 19:08:21 -07002334 if (v instanceof Workspace) {
2335 if (mWorkspace.isInOverviewMode()) {
2336 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002337 }
2338 return;
2339 }
2340
2341 if (v instanceof CellLayout) {
2342 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002343 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002344 }
2345 }
2346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002348 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002350 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2351 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002352
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002353 // Check for special shortcuts
2354 if (intent.getComponent() != null) {
2355 final String shortcutClass = intent.getComponent().getClassName();
2356
2357 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002358 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002359 return;
2360 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2361 MemoryDumpActivity.startDump(this);
2362 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002363 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2364 toggleShowWeightWatcher();
2365 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002366 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002367 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002368
2369 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002370 int[] pos = new int[2];
2371 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002372 intent.setSourceBounds(new Rect(pos[0], pos[1],
2373 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002374
2375 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002376
Daniel Sandlerff02d492013-08-05 02:12:05 -04002377 mStats.recordLaunch(intent, shortcut);
2378
Michael Jurkaddd62e92011-02-16 17:49:14 -08002379 if (success && v instanceof BubbleTextView) {
2380 mWaitingForResume = (BubbleTextView) v;
2381 mWaitingForResume.setStayPressed(true);
2382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002384 if (v instanceof FolderIcon) {
2385 FolderIcon fi = (FolderIcon) v;
2386 handleFolderClick(fi);
2387 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002389 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002390 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002391 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002392 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 }
2395 }
2396
Michael Jurka0e260592010-06-30 17:07:39 -07002397 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002398 return false;
2399 }
2400
Michael Jurkaaf442092010-06-10 17:01:57 -07002401 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002402 * Event handler for the search button
2403 *
2404 * @param v The view that was clicked.
2405 */
2406 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002407 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2408
Amith Yamasania135ba82011-08-09 17:42:01 -07002409 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002410 }
2411
2412 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002413 * Event handler for the voice button
2414 *
2415 * @param v The view that was clicked.
2416 */
2417 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002418 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002419
Bjorn Bringertc459e522013-06-07 19:36:01 +01002420 startVoice();
2421 }
2422
2423 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002424 try {
2425 final SearchManager searchManager =
2426 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2427 ComponentName activityName = searchManager.getGlobalSearchActivity();
2428 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002429 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002430 if (activityName != null) {
2431 intent.setPackage(activityName.getPackageName());
2432 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002433 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002434 } catch (ActivityNotFoundException e) {
2435 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002436 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002437 startActivitySafely(null, intent, "onClickVoiceButton");
2438 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002439 }
2440
2441 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002442 * Event handler for the "grid" button that appears on the home screen, which
2443 * enters all apps mode.
2444 *
2445 * @param v The view that was clicked.
2446 */
2447 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002448 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002449 }
2450
2451 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002452 // Provide the same haptic feedback that the system offers for virtual keys.
2453 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002454 }
2455
Adam Cohen61f560d2013-09-30 15:58:20 -07002456 public void performHapticFeedbackOnTouchDown(View v) {
2457 // Provide the same haptic feedback that the system offers for virtual keys.
2458 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2459 }
2460
2461 public View.OnTouchListener getHapticFeedbackTouchListener() {
2462 if (mHapticFeedbackTouchListener == null) {
2463 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2464 @Override
2465 public boolean onTouch(View v, MotionEvent event) {
2466 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2467 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2468 }
2469 return false;
2470 }
2471 };
2472 }
2473 return mHapticFeedbackTouchListener;
2474 }
2475
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002476 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002477 if (!DISABLE_MARKET_BUTTON) {
2478 if (mAppMarketIntent != null) {
2479 startActivitySafely(v, mAppMarketIntent, "app market");
2480 } else {
2481 Log.e(TAG, "Invalid app market intent.");
2482 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002483 }
2484 }
2485
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002486 /**
2487 * Called when the user stops interacting with the launcher.
2488 * This implies that the user is now on the homescreen and is not doing housekeeping.
2489 */
2490 protected void onInteractionEnd() {}
2491
2492 /**
2493 * Called when the user starts interacting with the launcher.
2494 * The possible interactions are:
2495 * - open all apps
2496 * - reorder an app shortcut, or a widget
2497 * - open the overview mode.
2498 * This is a good time to stop doing things that only make sense
2499 * when the user is on the homescreen and not doing housekeeping.
2500 */
2501 protected void onInteractionBegin() {}
2502
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002503 void startApplicationDetailsActivity(ComponentName componentName) {
2504 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002505 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2506 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002507 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002508 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002509 }
2510
Michael Jurka1e2f4652013-07-08 18:03:46 -07002511 // returns true if the activity was started
2512 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002513 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002514 // System applications cannot be installed. For now, show a toast explaining that.
2515 // We may give them the option of disabling apps this way.
2516 int messageId = R.string.uninstall_system_app_text;
2517 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002518 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002519 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002520 String packageName = componentName.getPackageName();
2521 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002522 Intent intent = new Intent(
2523 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002524 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2525 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002526 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002527 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002528 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002529 }
2530
Michael Jurka86a720e2012-05-09 11:23:23 -07002531 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002535 // Only launch using the new animation if the shortcut has not opted out (this is a
2536 // private contract between launcher and may be ignored in the future).
2537 boolean useLaunchAnimation = (v != null) &&
2538 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2539 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002540 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2541 v.getMeasuredWidth(), v.getMeasuredHeight());
2542
2543 startActivity(intent, opts.toBundle());
2544 } else {
2545 startActivity(intent);
2546 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002547 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 } catch (SecurityException e) {
2549 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002550 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002552 "or use the exported attribute for this activity. "
2553 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002555 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002557
Michael Jurka86a720e2012-05-09 11:23:23 -07002558 boolean startActivitySafely(View v, Intent intent, Object tag) {
2559 boolean success = false;
2560 try {
2561 success = startActivity(v, intent, tag);
2562 } catch (ActivityNotFoundException e) {
2563 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2564 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2565 }
2566 return success;
2567 }
2568
Adam Cohena9cf38f2011-05-02 15:36:58 -07002569 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002570 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002571 Folder openFolder = mWorkspace.getFolderForTag(info);
2572
2573 // If the folder info reports that the associated folder is open, then verify that
2574 // it is actually opened. There have been a few instances where this gets out of sync.
2575 if (info.opened && openFolder == null) {
2576 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002577 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002578 info.opened = false;
2579 }
2580
Adam Cohenfb91f302012-06-11 15:45:18 -07002581 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 // Close any open folder
2583 closeFolder();
2584 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002585 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 } else {
2587 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 int folderScreen;
2589 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002590 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 // .. and close it
2592 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002593 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002594 // Close any folder open on the current screen
2595 closeFolder();
2596 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002597 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 }
2599 }
2600 }
2601 }
2602
Adam Cohen268c4752012-06-06 17:47:33 -07002603 /**
2604 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2605 * in the DragLayer in the exact absolute location of the original FolderIcon.
2606 */
2607 private void copyFolderIconToImage(FolderIcon fi) {
2608 final int width = fi.getMeasuredWidth();
2609 final int height = fi.getMeasuredHeight();
2610
2611 // Lazy load ImageView, Bitmap and Canvas
2612 if (mFolderIconImageView == null) {
2613 mFolderIconImageView = new ImageView(this);
2614 }
2615 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2616 mFolderIconBitmap.getHeight() != height) {
2617 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2618 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2619 }
2620
2621 DragLayer.LayoutParams lp;
2622 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2623 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2624 } else {
2625 lp = new DragLayer.LayoutParams(width, height);
2626 }
2627
Adam Cohen307fe232012-08-16 17:55:58 -07002628 // The layout from which the folder is being opened may be scaled, adjust the starting
2629 // view size by this scale factor.
2630 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002631 lp.customPosition = true;
2632 lp.x = mRectForFolderAnimation.left;
2633 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002634 lp.width = (int) (scale * width);
2635 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002636
2637 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2638 fi.draw(mFolderIconCanvas);
2639 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002640 if (fi.getFolder() != null) {
2641 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2642 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002643 }
Adam Cohen268c4752012-06-06 17:47:33 -07002644 // Just in case this image view is still in the drag layer from a previous animation,
2645 // we remove it and re-add it.
2646 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2647 mDragLayer.removeView(mFolderIconImageView);
2648 }
2649 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002650 if (fi.getFolder() != null) {
2651 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002652 }
Adam Cohen268c4752012-06-06 17:47:33 -07002653 }
2654
Adam Cohen2801caf2011-05-13 20:57:39 -07002655 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002656 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002657 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2658 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2659 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2660
Adam Cohenc51934b2011-07-26 21:07:43 -07002661 FolderInfo info = (FolderInfo) fi.getTag();
2662 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2663 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002664 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2665 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002666 }
2667
Adam Cohen268c4752012-06-06 17:47:33 -07002668 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2669 copyFolderIconToImage(fi);
2670 fi.setVisibility(View.INVISIBLE);
2671
Michael Jurka2ecf9952012-06-18 12:52:28 -07002672 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002673 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002674 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2675 oa.start();
2676 }
2677
Adam Cohen268c4752012-06-06 17:47:33 -07002678 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002679 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002680 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2681 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2682 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2683
Adam Cohen268c4752012-06-06 17:47:33 -07002684 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002685
Adam Cohen268c4752012-06-06 17:47:33 -07002686 // We remove and re-draw the FolderIcon in-case it has changed
2687 mDragLayer.removeView(mFolderIconImageView);
2688 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002689 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002690 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002691 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002692 oa.addListener(new AnimatorListenerAdapter() {
2693 @Override
2694 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002695 if (cl != null) {
2696 cl.clearFolderLeaveBehind();
2697 // Remove the ImageView copy of the FolderIcon and make the original visible.
2698 mDragLayer.removeView(mFolderIconImageView);
2699 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002700 }
2701 }
2702 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002703 oa.start();
2704 }
2705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002707 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 * is animated relative to the specified View. If the View is null, no animation
2709 * is played.
2710 *
2711 * @param folderInfo The FolderInfo describing the folder to open.
2712 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002713 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002714 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002715 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716
Adam Cohena9cf38f2011-05-02 15:36:58 -07002717 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718
Adam Cohen4554ee12011-08-03 16:13:21 -07002719 // Just verify that the folder hasn't already been added to the DragLayer.
2720 // There was a one-off crash where the folder had a parent already.
2721 if (folder.getParent() == null) {
2722 mDragLayer.addView(folder);
2723 mDragController.addDropTarget((DropTarget) folder);
2724 } else {
2725 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2726 folder.getParent() + ").");
2727 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002728 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002729 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002730
2731 // Notify the accessibility manager that this folder "window" has appeared and occluded
2732 // the workspace items
2733 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2734 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002735 }
2736
2737 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002738 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002739 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002740 if (folder.isEditingName()) {
2741 folder.dismissEditingName();
2742 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002743 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002744
2745 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002746 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002747 }
2748 }
2749
2750 void closeFolder(Folder folder) {
2751 folder.getInfo().opened = false;
2752
2753 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2754 if (parent != null) {
2755 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2756 shrinkAndFadeInFolderIcon(fi);
2757 }
2758 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002759
2760 // Notify the accessibility manager that this folder "window" has disappeard and no
2761 // longer occludeds the workspace items
2762 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
2764
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002766 if (!isDraggingEnabled()) return false;
2767 if (isWorkspaceLocked()) return false;
2768 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769
Adam Cohen1697b792013-09-17 19:08:21 -07002770 if (v instanceof Workspace) {
2771 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002772 if (mWorkspace.enterOverviewMode()) {
2773 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2774 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2775 return true;
2776 } else {
2777 return false;
2778 }
Adam Cohen1697b792013-09-17 19:08:21 -07002779 }
Adam Cohen1697b792013-09-17 19:08:21 -07002780 }
2781
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002783 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 }
2785
Michael Jurka0280c3b2010-09-17 15:00:07 -07002786 resetAddInfo();
2787 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002789 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 return true;
2791 }
2792
Winson Chung3d503fb2011-07-13 17:25:49 -07002793 // The hotseat touch handling does not go through Workspace, and we always allow long press
2794 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002795 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002796 final boolean inHotseat = isHotseatLayout(v);
2797 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002798 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002799 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002800 // User long pressed on empty space
2801 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2802 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2803 // Disabling reordering until we sort out some issues.
2804 if (mWorkspace.isInOverviewMode()) {
2805 mWorkspace.startReordering(v);
2806 } else {
2807 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002810 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2811 mHotseat.getOrderInHotseat(
2812 longClickCellInfo.cellX,
2813 longClickCellInfo.cellY));
2814 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002816 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 }
2818 }
2819 }
2820 return true;
2821 }
2822
Winson Chung3d503fb2011-07-13 17:25:49 -07002823 boolean isHotseatLayout(View layout) {
2824 return mHotseat != null && layout != null &&
2825 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2826 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002827
Winson Chung3d503fb2011-07-13 17:25:49 -07002828 /**
2829 * Returns the CellLayout of the specified container at the specified screen.
2830 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002831 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002832 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2833 if (mHotseat != null) {
2834 return mHotseat.getLayout();
2835 } else {
2836 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002837 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002838 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002839 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002840 }
2841 }
2842
Daniel Sandler843e8602010-06-07 14:59:01 -04002843 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002844 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002845 }
2846
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002847 /**
2848 * Helper method for the cameraZoomIn/cameraZoomOut animations
2849 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002850 * @param scaleFactor The scale factor used for the zoom
2851 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002852 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002853 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002854 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002855 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002856
Craig Mautner360310b2012-10-26 15:13:08 -07002857 private void setWorkspaceBackground(boolean workspace) {
2858 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002859 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002860 }
2861
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002862 void updateWallpaperVisibility(boolean visible) {
2863 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2864 int curflags = getWindow().getAttributes().flags
2865 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2866 if (wpflags != curflags) {
2867 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2868 }
Craig Mautner360310b2012-10-26 15:13:08 -07002869 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002870 }
2871
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002872 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2873 if (v instanceof LauncherTransitionable) {
2874 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2875 }
2876 }
2877
Michael Jurkabed61d22012-02-14 22:51:29 -08002878 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2879 if (v instanceof LauncherTransitionable) {
2880 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2881 }
Winson Chung70442722012-02-10 15:43:22 -08002882
2883 // Update the workspace transition step as well
2884 dispatchOnLauncherTransitionStep(v, 0f);
2885 }
2886
2887 private void dispatchOnLauncherTransitionStep(View v, float t) {
2888 if (v instanceof LauncherTransitionable) {
2889 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2890 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002891 }
2892
2893 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2894 if (v instanceof LauncherTransitionable) {
2895 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2896 }
Winson Chung70442722012-02-10 15:43:22 -08002897
2898 // Update the workspace transition step as well
2899 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002900 }
2901
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002902 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002903 * Things to test when changing the following seven functions.
2904 * - Home from workspace
2905 * - from center screen
2906 * - from other screens
2907 * - Home from all apps
2908 * - from center screen
2909 * - from other screens
2910 * - Back from all apps
2911 * - from center screen
2912 * - from other screens
2913 * - Launch app from workspace and quit
2914 * - with back
2915 * - with home
2916 * - Launch app from all apps and quit
2917 * - with back
2918 * - with home
2919 * - Go to a screen that's not the default, then all
2920 * apps, and launch and app, and go back
2921 * - with back
2922 * -with home
2923 * - On workspace, long press power and go back
2924 * - with back
2925 * - with home
2926 * - On all apps, long press power and go back
2927 * - with back
2928 * - with home
2929 * - On workspace, power off
2930 * - On all apps, power off
2931 * - Launch an app and turn off the screen while in that app
2932 * - Go back with home key
2933 * - Go back with back key TODO: make this not go to workspace
2934 * - From all apps
2935 * - From workspace
2936 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2937 * - From all apps
2938 * - From the center workspace
2939 * - From another workspace
2940 */
2941
2942 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002943 * Zoom the camera out from the workspace to reveal 'toView'.
2944 * Assumes that the view to show is anchored at either the very top or very bottom
2945 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002946 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002947 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002948 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2949 showAppsCustomizeHelper(animated, springLoaded, contentType);
2950 }
2951 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2952 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002953 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002954 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002955 mStateAnimation.cancel();
2956 mStateAnimation = null;
2957 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002958 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002959
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2961 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2962 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002963 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002964 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002965 final int startDelay =
2966 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002967
Michael Jurkab3e22d92011-10-31 15:58:33 -07002968 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002969
Michael Jurkad74c9842011-07-10 12:44:21 -07002970 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002971 Animator workspaceAnim =
2972 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08002973 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002974 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2975 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002976 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2977 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002978
2979 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002980 toView.setScaleX(scale);
2981 toView.setScaleY(scale);
2982 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2983 scaleAnim.
2984 scaleX(1f).scaleY(1f).
2985 setDuration(duration).
2986 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002987
Winson Chungf0ea4d32011-06-06 14:27:16 -07002988 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002989 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002990 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002991 .ofFloat(toView, "alpha", 0f, 1f)
2992 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002993 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002994 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2995 @Override
2996 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002997 if (animation == null) {
2998 throw new RuntimeException("animation is null");
2999 }
Winson Chung70442722012-02-10 15:43:22 -08003000 float t = (Float) animation.getAnimatedValue();
3001 dispatchOnLauncherTransitionStep(fromView, t);
3002 dispatchOnLauncherTransitionStep(toView, t);
3003 }
3004 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003005
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003006 // toView should appear right at the end of the workspace shrink
3007 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003008 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003009 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003010 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003011
3012 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003013 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003014 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003015 // Prepare the position
3016 toView.setTranslationX(0.0f);
3017 toView.setTranslationY(0.0f);
3018 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003019 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003020 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003021 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003022 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003023 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3024 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003025
Winson Chungc7d2b602012-05-16 17:02:20 -07003026 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003027 if (mSearchDropTargetBar != null) {
3028 mSearchDropTargetBar.hideSearchBar(false);
3029 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003030 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003031 });
3032
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003033 if (workspaceAnim != null) {
3034 mStateAnimation.play(workspaceAnim);
3035 }
Michael Jurka1899a362011-11-03 13:50:45 -07003036
3037 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003038
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003039 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3040 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003041
3042 // If any of the objects being animated haven't been measured/laid out
3043 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003044 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003045 (mWorkspace.getMeasuredWidth() == 0) ||
3046 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003047 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003048 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003049
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003050 final AnimatorSet stateAnimation = mStateAnimation;
3051 final Runnable startAnimRunnable = new Runnable() {
3052 public void run() {
3053 // Check that mStateAnimation hasn't changed while
3054 // we waited for a layout/draw pass
3055 if (mStateAnimation != stateAnimation)
3056 return;
3057 setPivotsForZoom(toView, scale);
3058 dispatchOnLauncherTransitionStart(fromView, animated, false);
3059 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003060 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003061 }
3062 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003063 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003064 final ViewTreeObserver observer = toView.getViewTreeObserver();
3065 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3066 public void onGlobalLayout() {
3067 startAnimRunnable.run();
3068 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3069 }
3070 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003071 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003072 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003073 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003074 } else {
3075 toView.setTranslationX(0.0f);
3076 toView.setTranslationY(0.0f);
3077 toView.setScaleX(1.0f);
3078 toView.setScaleY(1.0f);
3079 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003080 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003081
Daniel Sandlere4f98912013-06-25 15:13:26 -04003082 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003083 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003084 if (mSearchDropTargetBar != null) {
3085 mSearchDropTargetBar.hideSearchBar(false);
3086 }
Michael Jurkaabded662011-03-04 12:06:57 -08003087 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003088 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003089 dispatchOnLauncherTransitionStart(fromView, animated, false);
3090 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003091 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003092 dispatchOnLauncherTransitionStart(toView, animated, false);
3093 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003094 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003095 }
3096
3097 /**
3098 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003099 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003100 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003101 */
Adam Cohened307df2013-10-02 09:37:31 -07003102 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003103 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003104
Michael Jurkab3e22d92011-10-31 15:58:33 -07003105 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003106 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003107 mStateAnimation.cancel();
3108 mStateAnimation = null;
3109 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003110 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003111
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003113 final int fadeOutDuration =
3114 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003115 final float scaleFactor = (float)
3116 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3117 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003118 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003119 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003120 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003121 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3122 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003123 toState, animated, stagger, -1);
3124 } else if (toState == Workspace.State.SPRING_LOADED ||
3125 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003126 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003127 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003128 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003129
Michael Jurkab3e22d92011-10-31 15:58:33 -07003130 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003131 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003132 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003133 final LauncherViewPropertyAnimator scaleAnim =
3134 new LauncherViewPropertyAnimator(fromView);
3135 scaleAnim.
3136 scaleX(scaleFactor).scaleY(scaleFactor).
3137 setDuration(duration).
3138 setInterpolator(new Workspace.ZoomInInterpolator());
3139
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003140 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003141 .ofFloat(fromView, "alpha", 1f, 0f)
3142 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003143 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003144 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3145 @Override
3146 public void onAnimationUpdate(ValueAnimator animation) {
3147 float t = 1f - (Float) animation.getAnimatedValue();
3148 dispatchOnLauncherTransitionStep(fromView, t);
3149 dispatchOnLauncherTransitionStep(toView, t);
3150 }
3151 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003152
Michael Jurka2ecf9952012-06-18 12:52:28 -07003153 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003154
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003155 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3156 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003157 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003158
3159 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003160 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003161 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003162 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003163 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3164 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003165 if (onCompleteRunnable != null) {
3166 onCompleteRunnable.run();
3167 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003168 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003169 }
3170 });
3171
Winson Chungf0ea4d32011-06-06 14:27:16 -07003172 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003173 if (workspaceAnim != null) {
3174 mStateAnimation.play(workspaceAnim);
3175 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003176 dispatchOnLauncherTransitionStart(fromView, animated, true);
3177 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003178 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003179 } else {
3180 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003181 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003182 dispatchOnLauncherTransitionStart(fromView, animated, true);
3183 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003184 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003185 dispatchOnLauncherTransitionStart(toView, animated, true);
3186 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003187 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003188 }
3189
Michael Jurkae326f182011-11-21 14:05:46 -08003190 @Override
3191 public void onTrimMemory(int level) {
3192 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003193 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003194 mAppsCustomizeTabHost.onTrimMemory();
3195 }
3196 }
3197
Adam Cohened307df2013-10-02 09:37:31 -07003198 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003199 showWorkspace(animated, null);
3200 }
3201
Adam Cohened307df2013-10-02 09:37:31 -07003202 protected void showWorkspace() {
3203 showWorkspace(true);
3204 }
3205
Adam Cohened66b2b2012-01-23 17:28:51 -08003206 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003207 if (mWorkspace.isInOverviewMode()) {
3208 mWorkspace.exitOverviewMode(animated);
3209 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003210 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003211 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003212 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003213 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003214
Winson Chungc7d2b602012-05-16 17:02:20 -07003215 // Show the search bar (only animate if we were showing the drop target bar in spring
3216 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003217 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003218 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003219 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003220
Michael Jurkab3e22d92011-10-31 15:58:33 -07003221 // Set focus to the AppsCustomize button
3222 if (mAllAppsButton != null) {
3223 mAllAppsButton.requestFocus();
3224 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003225 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003226
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003227 // Change the state *after* we've called all the transition code
3228 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003229
Winson Chung5fb63472011-02-02 17:03:37 -08003230 // Resume the auto-advance of widgets
3231 mUserPresent = true;
3232 updateRunning();
3233
alanv1d4fde62012-10-17 13:15:47 -07003234 // Send an accessibility event to announce the context change
3235 getWindow().getDecorView()
3236 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003237
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003238 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003239 }
3240
Adam Cohened307df2013-10-02 09:37:31 -07003241 void showOverviewMode(boolean animated) {
3242 mWorkspace.setVisibility(View.VISIBLE);
3243 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3244 mState = State.WORKSPACE;
3245 onWorkspaceShown(animated);
3246 }
3247
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003248 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003249 }
3250
Winson Chung82963d52013-10-09 11:20:57 -07003251 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3252 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003253 if (mState != State.WORKSPACE) return;
3254
Winson Chung82963d52013-10-09 11:20:57 -07003255 if (resetPageToZero) {
3256 mAppsCustomizeTabHost.reset();
3257 }
Winson Chungc58497e2013-09-03 17:48:37 -07003258 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003259 mAppsCustomizeTabHost.requestFocus();
3260
Michael Jurkab3e22d92011-10-31 15:58:33 -07003261 // Change the state *after* we've called all the transition code
3262 mState = State.APPS_CUSTOMIZE;
3263
3264 // Pause the auto-advance of widgets until we are out of AllApps
3265 mUserPresent = false;
3266 updateRunning();
3267 closeFolder();
3268
3269 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003270 getWindow().getDecorView()
3271 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 }
3273
Adam Cohen7777d962011-08-18 18:58:38 -07003274 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003275 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003276 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003277 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003278 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003279 }
Adam Cohen7777d962011-08-18 18:58:38 -07003280
Adam Cohenad4e15c2013-10-17 16:21:35 -07003281 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003282 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003283 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3284
Winson Chunge7a03942011-08-05 15:05:12 -07003285 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003286 @Override
3287 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003288 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003289 // Before we show workspace, hide all apps again because
3290 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3291 // clean up our state transition functions
3292 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003293 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003294 } else {
3295 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003296 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003297 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003298 }, delay);
3299
Winson Chung557d6ed2011-07-08 15:34:52 -07003300 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301
Michael Jurkad3ef3062010-11-23 16:23:58 -08003302 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003303 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003304 final boolean animated = true;
3305 final boolean springLoaded = true;
3306 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003307 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003308 }
3309 // Otherwise, we are not in spring loaded mode, so don't do anything.
3310 }
3311
Michael Jurkab3e22d92011-10-31 15:58:33 -07003312 void lockAllApps() {
3313 // TODO
3314 }
3315
3316 void unlockAllApps() {
3317 // TODO
3318 }
3319
Winson Chungf0ea4d32011-06-06 14:27:16 -07003320 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003321 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003322 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003323 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003324 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003325 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003326 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003327 int duration = 0;
3328 if (mSearchDropTargetBar != null) {
3329 duration = mSearchDropTargetBar.getTransitionInDuration();
3330 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003331 mHotseat.animate().alpha(1f).setDuration(duration);
3332 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003333 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003334 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003335 }
3336 }
3337 }
3338
3339 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003340 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003341 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003342 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003343 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003344 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003345 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003346 int duration = 0;
3347 if (mSearchDropTargetBar != null) {
3348 duration = mSearchDropTargetBar.getTransitionOutDuration();
3349 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003350 mHotseat.animate().alpha(0f).setDuration(duration);
3351 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003352 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003353 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003354 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003355 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003356 }
3357
Patrick Dubroy5f445422011-02-18 14:35:21 -08003358 /**
3359 * Add an item from all apps or customize onto the given workspace screen.
3360 * If layout is null, add to the current screen.
3361 */
3362 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003363 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003364 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003365 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003366 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003367
Winson Chungdff8ebb2011-09-08 17:25:31 -07003368 /** Maps the current orientation to an index for referencing orientation correct global icons */
3369 private int getCurrentOrientationIndexForGlobalIcons() {
3370 // default - 0, landscape - 1
3371 switch (getResources().getConfiguration().orientation) {
3372 case Configuration.ORIENTATION_LANDSCAPE:
3373 return 1;
3374 default:
3375 return 0;
3376 }
3377 }
3378
Michael Jurkaae65ee32012-04-30 11:45:54 -07003379 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003380 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003381 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003382 // Look for the toolbar icon specified in the activity meta-data
3383 Bundle metaData = packageManager.getActivityInfo(
3384 activityName, PackageManager.GET_META_DATA).metaData;
3385 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003386 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003387 if (iconResId != 0) {
3388 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003389 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003390 }
3391 }
3392 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003393 // This can happen if the activity defines an invalid drawable
3394 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3395 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003396 } catch (Resources.NotFoundException nfe) {
3397 // This can happen if the activity defines an invalid drawable
3398 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3399 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003400 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003401 return null;
3402 }
3403
3404 // if successful in getting icon, return it; otherwise, set button to use default drawable
3405 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003406 int buttonId, ComponentName activityName, int fallbackDrawableId,
3407 String toolbarResourceName) {
3408 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003409 Resources r = getResources();
3410 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3411 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003412
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003413 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003414 // If we were unable to find the icon via the meta-data, use a generic one
3415 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003416 toolbarIcon = r.getDrawable(fallbackDrawableId);
3417 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003418 if (button != null) {
3419 button.setCompoundDrawables(toolbarIcon, null, null, null);
3420 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003421 return null;
3422 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003423 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003424 if (button != null) {
3425 button.setCompoundDrawables(toolbarIcon, null, null, null);
3426 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003427 return toolbarIcon.getConstantState();
3428 }
3429 }
3430
3431 // if successful in getting icon, return it; otherwise, set button to use default drawable
3432 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003433 int buttonId, ComponentName activityName, int fallbackDrawableId,
3434 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003435 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003436 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003437
Michael Jurka19e0fc52011-07-22 18:00:21 -07003438 if (button != null) {
3439 // If we were unable to find the icon via the meta-data, use a
3440 // generic one
3441 if (toolbarIcon == null) {
3442 button.setImageResource(fallbackDrawableId);
3443 } else {
3444 button.setImageDrawable(toolbarIcon);
3445 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003446 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003447
3448 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3449
Michael Jurka0423dcf2010-10-05 14:56:18 -07003450 }
3451
Winson Chung1b884092012-06-08 17:13:02 -07003452 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003453 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003454 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003455 }
3456
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003457 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003458 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003459 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003460 }
3461
Winson Chungbb185bd2011-11-21 12:31:42 -08003462 private void invalidatePressedFocusedStates(View container, View button) {
3463 if (container instanceof HolographicLinearLayout) {
3464 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3465 layout.invalidatePressedFocusedStates();
3466 } else if (button instanceof HolographicImageView) {
3467 HolographicImageView view = (HolographicImageView) button;
3468 view.invalidatePressedFocusedStates();
3469 }
3470 }
3471
Cristina Stancu476493b2013-08-07 17:20:14 +01003472 public View getQsbBar() {
3473 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003474 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3475 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003476 }
3477 return mQsbBar;
3478 }
3479
3480 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003481 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003482 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003483 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003484 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003485
Winson Chung1cad91e2011-05-25 17:41:01 -07003486 final SearchManager searchManager =
3487 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3488 ComponentName activityName = searchManager.getGlobalSearchActivity();
3489 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003490 int coi = getCurrentOrientationIndexForGlobalIcons();
3491 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003492 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3493 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3494 if (sGlobalSearchIcon[coi] == null) {
3495 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3496 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3497 TOOLBAR_ICON_METADATA_NAME);
3498 }
3499
Winson Chungc51db6a2011-10-05 11:44:49 -07003500 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3501 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003502 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003503 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003504 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003505 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003506 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3507 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003508 if (searchButton != null) searchButton.setVisibility(View.GONE);
3509 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003510 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003511 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003512 }
3513 }
3514
Cristina Stancu476493b2013-08-07 17:20:14 +01003515 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003516 final View searchButtonContainer = findViewById(R.id.search_button_container);
3517 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003518 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003519 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003520 }
3521
Cristina Stancu476493b2013-08-07 17:20:14 +01003522 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003523 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003524 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003525
Winson Chungc51db6a2011-10-05 11:44:49 -07003526 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003527 final SearchManager searchManager =
3528 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3529 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3530
3531 ComponentName activityName = null;
3532 if (globalSearchActivity != null) {
3533 // Check if the global search activity handles voice search
3534 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3535 intent.setPackage(globalSearchActivity.getPackageName());
3536 activityName = intent.resolveActivity(getPackageManager());
3537 }
3538
3539 if (activityName == null) {
3540 // Fallback: check if an activity other than the global search activity
3541 // resolves this
3542 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3543 activityName = intent.resolveActivity(getPackageManager());
3544 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003545 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003546 int coi = getCurrentOrientationIndexForGlobalIcons();
3547 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003548 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3549 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3550 if (sVoiceSearchIcon[coi] == null) {
3551 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3552 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3553 TOOLBAR_ICON_METADATA_NAME);
3554 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003555 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003556 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003557 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003558 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003559 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003560 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003561 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003562 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003563 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003564 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003565 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003566 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003567
Cristina Stancu476493b2013-08-07 17:20:14 +01003568 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003569 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3570 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003571 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003572 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003573 }
3574
Winson Chung5841efa2013-09-30 18:06:44 -07003575 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003576 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3577 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003578 boolean visible = !forceDisableVoiceButtonProxy &&
3579 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003580 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003581 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003582 }
3583 }
Winson Chung5841efa2013-09-30 18:06:44 -07003584
3585 /**
3586 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3587 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3588 */
3589 public void disableVoiceButtonProxy(boolean disabled) {
3590 updateVoiceButtonProxyVisible(disabled);
3591 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003592 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003593 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003594 */
3595 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003596 if (!DISABLE_MARKET_BUTTON) {
3597 final View marketButton = findViewById(R.id.market_button);
3598 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3599 // Find the app market activity by resolving an intent.
3600 // (If multiple app markets are installed, it will return the ResolverActivity.)
3601 ComponentName activityName = intent.resolveActivity(getPackageManager());
3602 if (activityName != null) {
3603 int coi = getCurrentOrientationIndexForGlobalIcons();
3604 mAppMarketIntent = intent;
3605 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3606 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3607 TOOLBAR_ICON_METADATA_NAME);
3608 marketButton.setVisibility(View.VISIBLE);
3609 } else {
3610 // We should hide and disable the view so that we don't try and restore the visibility
3611 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3612 marketButton.setVisibility(View.GONE);
3613 marketButton.setEnabled(false);
3614 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003615 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003616 }
3617
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003618 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003619 if (!DISABLE_MARKET_BUTTON) {
3620 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3621 Resources r = getResources();
3622 Drawable marketIconDrawable = d.newDrawable(r);
3623 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3624 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3625 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003626
Winson Chungd64a6662013-09-30 11:06:59 -07003627 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3628 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003629 }
3630
Michael Jurkad7c28052012-04-27 15:43:36 -07003631 @Override
3632 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003633 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003634 final List<CharSequence> text = event.getText();
3635 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003636 // Populate event with a fake title based on the current state.
3637 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003638 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003639 } else {
3640 text.add(getString(R.string.all_apps_home_button_label));
3641 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003642 return result;
3643 }
3644
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003645 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003646 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003647 */
3648 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3649 @Override
3650 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003651 closeSystemDialogs();
3652 }
3653 }
3654
3655 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003656 * Receives notifications whenever the appwidgets are reset.
3657 */
3658 private class AppWidgetResetObserver extends ContentObserver {
3659 public AppWidgetResetObserver() {
3660 super(new Handler());
3661 }
3662
3663 @Override
3664 public void onChange(boolean selfChange) {
3665 onAppWidgetReset();
3666 }
3667 }
3668
3669 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003670 * If the activity is currently paused, signal that we need to run the passed Runnable
3671 * in onResume.
3672 *
3673 * This needs to be called from incoming places where resources might have been loaded
3674 * while we are paused. That is becaues the Configuration might be wrong
3675 * when we're not running, and if it comes back to what it was when we
3676 * were paused, we are not restarted.
3677 *
3678 * Implementation of the method from LauncherModel.Callbacks.
3679 *
3680 * @return true if we are currently paused. The caller might be able to
3681 * skip some work in that case since we will come back again.
3682 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003683 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003684 if (mPaused) {
3685 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003686 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003687 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003688 }
3689 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003690 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003691 return true;
3692 } else {
3693 return false;
3694 }
3695 }
3696
Michael Jurkac402cd92013-05-20 15:49:32 +02003697 private boolean waitUntilResume(Runnable run) {
3698 return waitUntilResume(run, false);
3699 }
3700
Michael Jurka1e2f4652013-07-08 18:03:46 -07003701 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003702 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003703 }
3704
Michael Jurka7607c2f2013-04-03 14:33:19 -07003705 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003706 * If the activity is currently paused, signal that we need to re-run the loader
3707 * in onResume.
3708 *
3709 * This needs to be called from incoming places where resources might have been loaded
3710 * while we are paused. That is becaues the Configuration might be wrong
3711 * when we're not running, and if it comes back to what it was when we
3712 * were paused, we are not restarted.
3713 *
3714 * Implementation of the method from LauncherModel.Callbacks.
3715 *
3716 * @return true if we are currently paused. The caller might be able to
3717 * skip some work in that case since we will come back again.
3718 */
3719 public boolean setLoadOnResume() {
3720 if (mPaused) {
3721 Log.i(TAG, "setLoadOnResume");
3722 mOnResumeNeedsLoad = true;
3723 return true;
3724 } else {
3725 return false;
3726 }
3727 }
3728
3729 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003733 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003734 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003735 } else {
3736 return SCREEN_COUNT / 2;
3737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003739
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 /**
3741 * Refreshes the shortcuts shown on the workspace.
3742 *
3743 * Implementation of the method from LauncherModel.Callbacks.
3744 */
3745 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 // If we're starting binding all over again, clear any bind calls we'd postponed in
3747 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3748 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003749 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003750
Winson Chungd64d1762013-08-20 14:37:16 -07003751 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003752 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003753 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003754
Michael Jurka05bf6442011-11-30 20:28:53 -08003755 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003756 if (mHotseat != null) {
3757 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003758 }
3759 }
3760
Adam Cohendcd297f2013-06-18 13:13:40 -07003761 @Override
3762 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003763 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003764
Adam Cohen5084cba2013-09-03 12:01:16 -07003765 // If there are no screens, we need to have an empty screen
3766 if (orderedScreenIds.size() == 0) {
3767 mWorkspace.addExtraEmptyScreen();
3768 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003769
3770 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003771 // setCurrentPage() so ensure that all pages are added before calling this).
3772 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003773 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3774 mWorkspace.createCustomContentPage();
3775 }
Winson Chung64359a52013-07-08 17:17:08 -07003776 }
3777
3778 @Override
3779 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003780 // Log to disk
3781 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3782 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3783 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 int count = orderedScreenIds.size();
3785 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003786 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003787 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003788 }
3789
Adam Cohen39a06042013-07-19 14:30:12 -07003790 private boolean shouldShowWeightWatcher() {
3791 String spKey = LauncherAppState.getSharedPreferencesKey();
3792 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003793 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003794
3795 return show;
3796 }
3797
3798 private void toggleShowWeightWatcher() {
3799 String spKey = LauncherAppState.getSharedPreferencesKey();
3800 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3801 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3802
3803 show = !show;
3804
3805 SharedPreferences.Editor editor = sp.edit();
3806 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3807 editor.commit();
3808
3809 if (mWeightWatcher != null) {
3810 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3811 }
3812 }
3813
Winson Chungd64d1762013-08-20 14:37:16 -07003814 public void bindAppsAdded(final ArrayList<Long> newScreens,
3815 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003816 final ArrayList<ItemInfo> addAnimated,
3817 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003818 Runnable r = new Runnable() {
3819 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003820 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003821 }
3822 };
3823 if (waitUntilResume(r)) {
3824 return;
3825 }
3826
Winson Chungd64d1762013-08-20 14:37:16 -07003827 // Add the new screens
3828 bindAddScreens(newScreens);
3829
3830 // We add the items without animation on non-visible pages, and with
3831 // animations on the new page (which we will try and snap to).
3832 if (!addNotAnimated.isEmpty()) {
3833 bindItems(addNotAnimated, 0,
3834 addNotAnimated.size(), false);
3835 }
3836 if (!addAnimated.isEmpty()) {
3837 bindItems(addAnimated, 0,
3838 addAnimated.size(), true);
3839 }
Winson Chungc58497e2013-09-03 17:48:37 -07003840
Winson Chung87412982013-10-03 18:34:14 -07003841 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003842 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003843
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003844 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003845 addedApps != null && mAppsCustomizeContent != null) {
3846 mAppsCustomizeContent.addApps(addedApps);
3847 }
Winson Chungd64d1762013-08-20 14:37:16 -07003848 }
3849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003850 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 * Bind the items start-end from the list.
3852 *
3853 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 */
Winson Chung64359a52013-07-08 17:17:08 -07003855 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3856 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003857 Runnable r = new Runnable() {
3858 public void run() {
3859 bindItems(shortcuts, start, end, forceAnimateIcons);
3860 }
3861 };
3862 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003863 return;
3864 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003865
Winson Chungf0c6ae02012-03-21 16:10:31 -07003866 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003867 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3868 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003869 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003871 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003872 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003874
3875 // Short circuit if we are loading dock items for a configuration which has no dock
3876 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3877 mHotseat == null) {
3878 continue;
3879 }
3880
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 switch (item.itemType) {
3882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003884 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003885 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003886
Winson Chung64359a52013-07-08 17:17:08 -07003887 /*
3888 * TODO: FIX collision case
3889 */
Winson Chungce376632013-07-11 16:12:41 -07003890 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3891 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3892 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3893 throw new RuntimeException("OCCUPIED");
3894 }
Winson Chung64359a52013-07-08 17:17:08 -07003895 }
3896
Adam Cohendcd297f2013-06-18 13:13:40 -07003897 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3898 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003899 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003900 // Animate all the applications up now
3901 shortcut.setAlpha(0f);
3902 shortcut.setScaleX(0f);
3903 shortcut.setScaleY(0f);
3904 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003905 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003906 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003907 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003908 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003909 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003910 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003911 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003912 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3913 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003915 default:
3916 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003918 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003919
Winson Chung997a9232013-07-24 15:33:46 -07003920 if (animateIcons) {
3921 // Animate to the correct page
3922 if (newShortcutsScreenId > -1) {
3923 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003924 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003925 final Runnable startBounceAnimRunnable = new Runnable() {
3926 public void run() {
3927 anim.playTogether(bounceAnims);
3928 anim.start();
3929 }
3930 };
Winson Chung997a9232013-07-24 15:33:46 -07003931 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003932 // We post the animation slightly delayed to prevent slowdowns
3933 // when we are loading right after we return to launcher.
3934 mWorkspace.postDelayed(new Runnable() {
3935 public void run() {
3936 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003937 mWorkspace.postDelayed(startBounceAnimRunnable,
3938 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003939 }
3940 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003941 } else {
3942 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003943 }
3944 }
Winson Chung64359a52013-07-08 17:17:08 -07003945 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003947 }
3948
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 /**
3950 * Implementation of the method from LauncherModel.Callbacks.
3951 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003952 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003953 Runnable r = new Runnable() {
3954 public void run() {
3955 bindFolders(folders);
3956 }
3957 };
3958 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003959 return;
3960 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003961 sFolders.clear();
3962 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003963 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964
3965 /**
3966 * Add the views for a widget to the workspace.
3967 *
3968 * Implementation of the method from LauncherModel.Callbacks.
3969 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003970 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003971 Runnable r = new Runnable() {
3972 public void run() {
3973 bindAppWidget(item);
3974 }
3975 };
3976 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003977 return;
3978 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003979
Daniel Sandler843e8602010-06-07 14:59:01 -04003980 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3981 if (DEBUG_WIDGETS) {
3982 Log.d(TAG, "bindAppWidget: " + item);
3983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 final Workspace workspace = mWorkspace;
3985
3986 final int appWidgetId = item.appWidgetId;
3987 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003988 if (DEBUG_WIDGETS) {
3989 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3990 }
3991
Joe Onorato9c1289c2009-08-17 11:03:03 -04003992 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3993
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003995 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003996
Adam Cohendcd297f2013-06-18 13:13:40 -07003997 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003999 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4000
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 workspace.requestLayout();
4002
Daniel Sandler843e8602010-06-07 14:59:01 -04004003 if (DEBUG_WIDGETS) {
4004 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4005 + (SystemClock.uptimeMillis()-start) + "ms");
4006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 }
4008
Adam Cohen1462de32012-07-24 22:34:36 -07004009 public void onPageBoundSynchronously(int page) {
4010 mSynchronouslyBoundPages.add(page);
4011 }
4012
Joe Onorato9c1289c2009-08-17 11:03:03 -04004013 /**
4014 * Callback saying that there aren't any more items to bind.
4015 *
4016 * Implementation of the method from LauncherModel.Callbacks.
4017 */
Adam Cohene25af792013-06-06 23:08:25 -07004018 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004019 Runnable r = new Runnable() {
4020 public void run() {
4021 finishBindingItems(upgradePath);
4022 }
4023 };
4024 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004025 return;
4026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004027 if (mSavedState != null) {
4028 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004029 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031 mSavedState = null;
4032 }
4033
Adam Cohen1462de32012-07-24 22:34:36 -07004034 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004036 // If we received the result of any pending adds while the loader was running (e.g. the
4037 // widget configuration forced an orientation change), process them now.
4038 for (int i = 0; i < sPendingAddList.size(); i++) {
4039 completeAdd(sPendingAddList.get(i));
4040 }
4041 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004042
Winson Chungc51db6a2011-10-05 11:44:49 -07004043 // Update the market app icon as necessary (the other icons will be managed in response to
4044 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004045 if (!DISABLE_MARKET_BUTTON) {
4046 updateAppMarketIcon();
4047 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004048
Winson Chungf0c6ae02012-03-21 16:10:31 -07004049 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004050 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004051 mWorkspace.getUniqueComponents(true, null);
4052 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004053 }
Adam Cohen99894d92013-06-14 11:22:59 -07004054
Adam Cohen66a01fd2013-06-11 12:48:00 -07004055 mWorkspace.post(new Runnable() {
4056 @Override
4057 public void run() {
4058 onFinishBindingItems();
4059 }
4060 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004061 }
4062
Winson Chunga0b7e862013-09-05 16:03:15 -07004063 public boolean isAllAppsButtonRank(int rank) {
4064 if (mHotseat != null) {
4065 return mHotseat.isAllAppsButtonRank(rank);
4066 }
4067 return false;
4068 }
4069
Winson Chunga2413752012-04-03 14:22:34 -07004070 private boolean canRunNewAppsAnimation() {
4071 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4072 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4073 }
4074
Winson Chungc9168342013-06-26 14:54:55 -07004075 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4076 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4077 PropertyValuesHolder.ofFloat("alpha", 1f),
4078 PropertyValuesHolder.ofFloat("scaleX", 1f),
4079 PropertyValuesHolder.ofFloat("scaleY", 1f));
4080 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4081 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4082 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4083 return bounceAnim;
4084 }
4085
Adam Cohen27772732013-11-11 14:00:56 +00004086 public boolean useVerticalBarLayout() {
4087 return LauncherAppState.getInstance().getDynamicGrid().
4088 getDeviceProfile().isVerticalBarLayout();
4089 }
4090
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004091 protected Rect getSearchBarBounds() {
4092 return LauncherAppState.getInstance().getDynamicGrid().
4093 getDeviceProfile().getSearchBarBounds();
4094 }
4095
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004096 @Override
4097 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004098 boolean searchVisible = updateGlobalSearchIcon();
4099 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004100 if (mSearchDropTargetBar != null) {
4101 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4102 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004103 }
4104
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004105 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 * Add the icons for all apps.
4107 *
4108 * Implementation of the method from LauncherModel.Callbacks.
4109 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004110 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004111 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004112 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4113 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4114 getHotseat().addAllAppsFolder(mIconCache, apps,
4115 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4116 }
4117 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004118 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004119 if (mAppsCustomizeContent != null) {
4120 mAppsCustomizeContent.onPackagesUpdated(
4121 LauncherModel.getSortedWidgetsAndShortcuts(this));
4122 }
Winson Chungc58497e2013-09-03 17:48:37 -07004123 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004124 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004125 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004126 mAppsCustomizeContent.onPackagesUpdated(
4127 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004128 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004129 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 }
4131
4132 /**
4133 * A package was updated.
4134 *
4135 * Implementation of the method from LauncherModel.Callbacks.
4136 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004137 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004138 Runnable r = new Runnable() {
4139 public void run() {
4140 bindAppsUpdated(apps);
4141 }
4142 };
4143 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004144 return;
4145 }
4146
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004147 if (mWorkspace != null) {
4148 mWorkspace.updateShortcuts(apps);
4149 }
Winson Chungc58497e2013-09-03 17:48:37 -07004150
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004151 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004152 mAppsCustomizeContent != null) {
4153 mAppsCustomizeContent.updateApps(apps);
4154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004155 }
4156
4157 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004158 * A package was uninstalled. We take both the super set of packageNames
4159 * in addition to specific applications to remove, the reason being that
4160 * this can be called when a package is updated as well. In that scenario,
4161 * we only remove specific components from the workspace, where as
4162 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 *
4164 * Implementation of the method from LauncherModel.Callbacks.
4165 */
Winson Chung83892cc2013-05-01 16:53:33 -07004166 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004167 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004168 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004169 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004170 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004171 }
Winson Chungd64d1762013-08-20 14:37:16 -07004172 };
4173 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004174 return;
4175 }
4176
Winson Chungdf95eb12013-10-16 14:57:07 -07004177 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004178 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004179 }
4180 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004181 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004182 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004183
Winson Chunga1820962011-10-03 16:31:06 -07004184 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004185 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004186
Winson Chungdf95eb12013-10-16 14:57:07 -07004187 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004188 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004189 mAppsCustomizeContent != null) {
4190 mAppsCustomizeContent.removeApps(appInfos);
4191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 }
Joe Onoratobe386092009-11-17 17:32:16 -08004193
4194 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004195 * A number of packages were updated.
4196 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004197 private ArrayList<Object> mWidgetsAndShortcuts;
4198 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004199 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004200 bindPackagesUpdated(mWidgetsAndShortcuts);
4201 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004202 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004203 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004204 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4205 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4206 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004207 return;
4208 }
4209
Winson Chung64359a52013-07-08 17:17:08 -07004210 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004211 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004212 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004213 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004214 }
4215
Winson Chung400438b2011-01-16 17:53:48 -08004216 private int mapConfigurationOriActivityInfoOri(int configOri) {
4217 final Display d = getWindowManager().getDefaultDisplay();
4218 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4219 switch (d.getRotation()) {
4220 case Surface.ROTATION_0:
4221 case Surface.ROTATION_180:
4222 // We are currently in the same basic orientation as the natural orientation
4223 naturalOri = configOri;
4224 break;
4225 case Surface.ROTATION_90:
4226 case Surface.ROTATION_270:
4227 // We are currently in the other basic orientation to the natural orientation
4228 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4229 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4230 break;
4231 }
4232
4233 int[] oriMap = {
4234 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4235 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4236 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4237 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4238 };
4239 // Since the map starts at portrait, we need to offset if this device's natural orientation
4240 // is landscape.
4241 int indexOffset = 0;
4242 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4243 indexOffset = 1;
4244 }
4245 return oriMap[(d.getRotation() + indexOffset) % 4];
4246 }
Adam Cohen446e9402011-09-15 18:21:21 -07004247
Winson Chung4b919f82012-05-01 10:44:08 -07004248 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004249 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004250 getResources().getBoolean(R.bool.allow_rotation);
4251 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004252 }
Winson Chung4b919f82012-05-01 10:44:08 -07004253 public void lockScreenOrientation() {
4254 if (isRotationEnabled()) {
4255 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4256 .getConfiguration().orientation));
4257 }
4258 }
4259 public void unlockScreenOrientation(boolean immediate) {
4260 if (isRotationEnabled()) {
4261 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004262 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004263 } else {
4264 mHandler.postDelayed(new Runnable() {
4265 public void run() {
4266 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4267 }
4268 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004269 }
Winson Chung4b919f82012-05-01 10:44:08 -07004270 }
Winson Chung400438b2011-01-16 17:53:48 -08004271 }
4272
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004273 /**
4274 * Called when the SearchBar hint should be changed.
4275 *
4276 * @param hint the hint to be displayed in the search bar.
4277 */
4278 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004279 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004280 }
4281
Winson Chunge6eabff2013-09-24 11:01:23 -07004282 protected String getFirstRunClingSearchBarHint() {
4283 return "";
4284 }
4285 protected String getFirstRunCustomContentHint() {
4286 return "";
4287 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004288 protected int getFirstRunFocusedHotseatAppDrawableId() {
4289 return -1;
4290 }
4291 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4292 return null;
4293 }
4294 protected int getFirstRunFocusedHotseatAppRank() {
4295 return -1;
4296 }
4297 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4298 return "";
4299 }
4300 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4301 return "";
4302 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004303
Winson Chungaf40f202013-09-18 18:26:31 -07004304 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004305 mLauncherClings.dismissFirstRunCling(v);
4306 }
4307 public void dismissMigrationClingCopyApps(View v) {
4308 mLauncherClings.dismissMigrationClingCopyApps(v);
4309 }
4310 public void dismissMigrationClingUseDefault(View v) {
4311 mLauncherClings.dismissMigrationClingUseDefault(v);
4312 }
4313 public void dismissMigrationWorkspaceCling(View v) {
4314 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004315 }
Winson Chung82f55532011-08-09 14:14:23 -07004316 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004317 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004318 }
4319 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004320 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004321 }
4322
Winson Chunga6945242014-01-08 14:04:34 -08004323 private boolean shouldRunFirstRunActivity() {
4324 return !ActivityManager.isRunningInTestHarness();
4325 }
4326
4327 public void showFirstRunActivity() {
4328 if (shouldRunFirstRunActivity() && hasFirstRunActivity()
4329 && !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false)) {
4330 Intent firstRunIntent = getFirstRunActivity();
4331 if (firstRunIntent != null) {
4332 startActivity(firstRunIntent);
4333 markFirstRunActivityShown();
4334 }
4335 }
4336 }
4337
4338 private void markFirstRunActivityShown() {
4339 SharedPreferences.Editor editor = mSharedPrefs.edit();
4340 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4341 editor.apply();
4342 }
4343
4344 void showWorkspaceSearchAndHotseat() {
4345 mWorkspace.setAlpha(1f);
4346 mHotseat.setAlpha(1f);
4347 mPageIndicators.setAlpha(1f);
4348 mSearchDropTargetBar.showSearchBar(false);
4349 }
4350
4351 void hideWorkspaceSearchAndHotseat() {
4352 mWorkspace.setAlpha(0f);
4353 mHotseat.setAlpha(0f);
4354 mPageIndicators.setAlpha(0f);
4355 mSearchDropTargetBar.hideSearchBar(false);
4356 }
4357
4358
Mathew Inwood72fbec12013-11-19 15:45:07 +00004359 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4360 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4361 if (ri == null) {
4362 return null;
4363 }
4364 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4365 }
4366
4367 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4368 Bitmap icon) {
4369 return new ShortcutInfo(shortcutIntent, caption, icon);
4370 }
4371
4372 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4373 dragView.setTag(dragInfo);
4374 mWorkspace.onDragStartedWithItem(dragView);
4375 mWorkspace.beginDragShared(dragView, source);
4376 }
4377
Winson Chung80baf5a2010-08-09 16:03:15 -07004378 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004379 * Prints out out state for debugging.
4380 */
4381 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004382 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004383 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004384 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4385 Log.d(TAG, "mRestoring=" + mRestoring);
4386 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4387 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004388 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004389 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004390
Winson Chung785d2eb2011-04-14 16:08:02 -07004391 if (mAppsCustomizeContent != null) {
4392 mAppsCustomizeContent.dumpState();
4393 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004394 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004395 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004396
4397 @Override
4398 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4399 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004400 synchronized (sDumpLogs) {
4401 writer.println(" ");
4402 writer.println("Debug logs: ");
4403 for (int i = 0; i < sDumpLogs.size(); i++) {
4404 writer.println(" " + sDumpLogs.get(i));
4405 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004406 }
4407 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004408
4409 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004410 if (DEBUG_DUMP_LOG) {
4411 synchronized (sDumpLogs) {
4412 Log.d(TAG, "");
4413 Log.d(TAG, "*********************");
4414 Log.d(TAG, "Launcher debug logs: ");
4415 for (int i = 0; i < sDumpLogs.size(); i++) {
4416 Log.d(TAG, " " + sDumpLogs.get(i));
4417 }
4418 Log.d(TAG, "*********************");
4419 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004420 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004421 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004422 }
4423
4424 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004425 addDumpLog(tag, log, null, debugLog);
4426 }
4427
4428 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004429 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004430 if (e != null) {
4431 Log.d(tag, log, e);
4432 } else {
4433 Log.d(tag, log);
4434 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004435 }
Winson Chungede41292013-09-19 16:27:36 -07004436 if (DEBUG_DUMP_LOG) {
4437 sDateStamp.setTime(System.currentTimeMillis());
4438 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004439 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4440 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004441 }
Winson Chungede41292013-09-19 16:27:36 -07004442 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004443 }
4444
Winson Chungede41292013-09-19 16:27:36 -07004445 public void dumpLogsToLocalData() {
4446 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004447 new AsyncTask<Void, Void, Void>() {
4448 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004449 boolean success = false;
4450 sDateStamp.setTime(sRunStart);
4451 String FILENAME = sDateStamp.getMonth() + "-"
4452 + sDateStamp.getDay() + "_"
4453 + sDateStamp.getHours() + "-"
4454 + sDateStamp.getMinutes() + "_"
4455 + sDateStamp.getSeconds() + ".txt";
4456
4457 FileOutputStream fos = null;
4458 File outFile = null;
4459 try {
4460 outFile = new File(getFilesDir(), FILENAME);
4461 outFile.createNewFile();
4462 fos = new FileOutputStream(outFile);
4463 } catch (Exception e) {
4464 e.printStackTrace();
4465 }
4466 if (fos != null) {
4467 PrintWriter writer = new PrintWriter(fos);
4468
4469 writer.println(" ");
4470 writer.println("Debug logs: ");
4471 synchronized (sDumpLogs) {
4472 for (int i = 0; i < sDumpLogs.size(); i++) {
4473 writer.println(" " + sDumpLogs.get(i));
4474 }
4475 }
4476 writer.close();
4477 }
4478 try {
4479 if (fos != null) {
4480 fos.close();
4481 success = true;
4482 }
4483 } catch (IOException e) {
4484 e.printStackTrace();
4485 }
Michael Jurka43467462013-11-14 12:03:58 +01004486 return null;
Winson Chungede41292013-09-19 16:27:36 -07004487 }
Michael Jurka43467462013-11-14 12:03:58 +01004488 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004489 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004491}
Michael Jurka2763be32011-02-24 11:19:57 -08004492
Michael Jurkaabded662011-03-04 12:06:57 -08004493interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004494 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004495 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004496 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004497 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004498 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004499}
Dan Sandlerd5024042014-01-09 15:01:33 -05004500
4501interface DebugIntents {
4502 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4503 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004504}