blob: 0a630fe6485707a66ba5ec0dfc73c8806eebf24a [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
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070089import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
96import android.widget.TextView;
97import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070098
Daniel Sandler325dc232013-06-05 22:57:57 -040099import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700103import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000115import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_PICK_APPLICATION = 6;
135 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 private static final int REQUEST_BIND_APPWIDGET = 11;
140
Mathew Inwood876a8462013-06-14 14:12:41 +0100141 /**
142 * IntentStarter uses request codes starting with this. This must be greater than all activity
143 * request codes used internally.
144 */
145 protected static final int REQUEST_LAST = 100;
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Romain Guy98d01652009-06-30 16:21:04 -0700152 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800153 // To turn on these properties, type
154 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
155 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
156 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Winson Chung2672ff92012-05-04 16:22:30 -0700158 // The Intent extra that defines whether to ignore the launch animation
159 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400160 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
163 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700164 // Type: int
165 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
168 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
170 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: boolean
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
176 // Type: long
177 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700178 // Type: int
179 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
180 // Type: int
181 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
182 // Type: parcelable
183 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000184 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000186 // Type: int[]
187 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700190 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100191 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700192 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100193 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700194
Adam Cohen39a06042013-07-19 14:30:12 -0700195 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700196 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700197
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700199 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700200 private State mState = State.WORKSPACE;
201 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700204 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
205 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Winson Chungaf40f202013-09-18 18:26:31 -0700206 private static final int SHOW_CLING_DURATION = 250;
207 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800210 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000212 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
213 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
214
Winson Chunga2413752012-04-03 14:22:34 -0700215 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700216 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
217 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700218 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700219
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800220 private final BroadcastReceiver mCloseSystemDialogsReceiver
221 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800222 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private LayoutInflater mInflater;
225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700227 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800228 private DragLayer mDragLayer;
229 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700230 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700232 private AppWidgetManager mAppWidgetManager;
233 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Michael Jurkac9d95c52011-08-29 14:03:34 -0700235 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700236 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800237 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238
Michael Jurka0280c3b2010-09-17 15:00:07 -0700239 private int[] mTmpAddItemCellCoordinates = new int[2];
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private FolderInfo mFolderInfo;
242
Winson Chung3d503fb2011-07-13 17:25:49 -0700243 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700244 private View mOverviewPanel;
245
Michael Jurka838a4ca2011-02-07 13:33:06 -0800246 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247
Winson Chungc51db6a2011-10-05 11:44:49 -0700248 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700249 private AppsCustomizeTabHost mAppsCustomizeTabHost;
250 private AppsCustomizePagedView mAppsCustomizeContent;
251 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100252 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700255 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
256 // scroll issues (because the workspace may not have been measured yet) and extra work.
257 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
258 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259
260 private SpannableStringBuilder mDefaultKeySsb = null;
261
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262 private boolean mWorkspaceLoading = true;
263
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800264 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private boolean mRestoring;
266 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700267 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
Michael Jurka1e2f4652013-07-08 18:03:46 -0700269 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700270 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
271
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // Keep track of whether the user has left launcher
273 private static boolean sPausedFromUserAction = false;
274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private Bundle mSavedInstanceState;
276
Joe Onorato9c1289c2009-08-17 11:03:03 -0400277 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800278 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800279 private boolean mUserPresent = true;
280 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700281 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800282 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700283 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700284 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700286 private static LocaleConfiguration sLocaleConfiguration = null;
287
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700288 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700289
Adam Cohen61f560d2013-09-30 15:58:20 -0700290 private View.OnTouchListener mHapticFeedbackTouchListener;
291
Adam Cohended9f8d2010-11-03 13:25:16 -0700292 // Related to the auto-advancing of widgets
293 private final int ADVANCE_MSG = 1;
294 private final int mAdvanceInterval = 20000;
295 private final int mAdvanceStagger = 250;
296 private long mAutoAdvanceSentTime;
297 private long mAutoAdvanceTimeLeft = -1;
298 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
299 new HashMap<View, AppWidgetProviderInfo>();
300
Winson Chung400438b2011-01-16 17:53:48 -0800301 // Determines how long to wait after a rotation before restoring the screen orientation to
302 // match the sensor state.
303 private final int mRestoreScreenOrientationDelay = 500;
304
Michael Jurka4ef207b2010-11-29 17:05:45 -0800305 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700306 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
307 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
308 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800309
Winson Chungd64a6662013-09-30 11:06:59 -0700310 private Intent mAppMarketIntent = null;
311 private static final boolean DISABLE_MARKET_BUTTON = true;
312
Craig Mautner360310b2012-10-26 15:13:08 -0700313 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700314
Adam Cohen1462de32012-07-24 22:34:36 -0700315 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700316 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700317
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700318 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700319 static Date sDateStamp = new Date();
320 static DateFormat sDateFormat =
321 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
322 static long sRunStart = System.currentTimeMillis();
323 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700324
Winson Chung46353de2012-02-16 14:05:10 -0800325 // We only want to get the SharedPreferences once since it does an FS stat each time we get
326 // it from the context.
327 private SharedPreferences mSharedPrefs;
328
Adam Cohene25af792013-06-06 23:08:25 -0700329 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
330
Winson Chungf0c6ae02012-03-21 16:10:31 -0700331 // Holds the page that we need to animate to, and the icon views that we need to animate up
332 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700333 private ImageView mFolderIconImageView;
334 private Bitmap mFolderIconBitmap;
335 private Canvas mFolderIconCanvas;
336 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700337
Michael Jurkaddd62e92011-02-16 17:49:14 -0800338 private BubbleTextView mWaitingForResume;
339
Michael Jurka22143132012-10-04 17:42:38 +0200340 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
341 = new HideFromAccessibilityHelper();
342
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 private Runnable mBuildLayersRunnable = new Runnable() {
344 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700345 if (mWorkspace != null) {
346 mWorkspace.buildPageHardwareLayers();
347 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 }
349 };
350
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800351 private static ArrayList<PendingAddArguments> sPendingAddList
352 = new ArrayList<PendingAddArguments>();
353
Michael Jurka7267fa52013-09-26 15:29:57 -0700354 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800355
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 private static class PendingAddArguments {
357 int requestCode;
358 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700359 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700360 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 int cellX;
362 int cellY;
363 }
364
Daniel Sandlerff02d492013-08-05 02:12:05 -0400365 private Stats mStats;
366
Michael Jurka0a457bf2012-11-19 14:05:05 -0800367 private static boolean isPropertyEnabled(String propertyName) {
368 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700369 }
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 @Override
372 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700373 if (DEBUG_STRICT_MODE) {
374 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
375 .detectDiskReads()
376 .detectDiskWrites()
377 .detectNetwork() // or .detectAll() for all detectable problems
378 .penaltyLog()
379 .build());
380 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
381 .detectLeakedSqlLiteObjects()
382 .detectLeakedClosableObjects()
383 .penaltyLog()
384 .penaltyDeath()
385 .build());
386 }
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400389
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400390 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400391 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700392
393 // Determine the dynamic grid properties
394 Point smallestSize = new Point();
395 Point largestSize = new Point();
396 Point realSize = new Point();
397 Display display = getWindowManager().getDefaultDisplay();
398 display.getCurrentSizeRange(smallestSize, largestSize);
399 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700400 DisplayMetrics dm = new DisplayMetrics();
401 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700402
Winson Chung5f8afe62013-08-12 16:19:28 -0700403 // Lazy-initialize the dynamic grid
404 DeviceProfile grid = app.initDynamicGrid(this,
405 Math.min(smallestSize.x, smallestSize.y),
406 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700407 realSize.x, realSize.y,
408 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700409
410 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700412 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800413 mModel = app.setLauncher(this);
414 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700415 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400416 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Daniel Sandlerff02d492013-08-05 02:12:05 -0400419 mStats = new Stats(this);
420
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700421 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700422
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
424 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
427 // this also ensures that any synchronous binding below doesn't re-trigger another
428 // LauncherModel load.
429 mPaused = false;
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200432 android.os.Debug.startMethodTracing(
433 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
Adam Cohen53805212013-10-01 10:39:23 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100441 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800443 registerContentObservers();
444
Joe Onorato7c312c12009-08-13 21:36:53 -0700445 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 mSavedState = savedInstanceState;
448 restoreState(mSavedState);
449
450 if (PROFILE_STARTUP) {
451 android.os.Debug.stopMethodTracing();
452 }
453
454 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700455 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700456 // If the user leaves launcher, then we should just load items asynchronously when
457 // they return.
458 mModel.startLoader(true, -1);
459 } else {
460 // We only load the page synchronously if the user rotates (or triggers a
461 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800462 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465
466 // For handling default keys
467 mDefaultKeySsb = new SpannableStringBuilder();
468 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800469
470 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
471 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800472
Adam Cohenf9426d52012-06-04 17:26:21 -0700473 updateGlobalIcons();
474
475 // On large interfaces, we want the screen to auto-rotate based on the current orientation
476 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700477
478 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700479 }
480
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 protected void onUserLeaveHint() {
482 super.onUserLeaveHint();
483 sPausedFromUserAction = true;
484 }
485
Winson Chung98ca0f72013-07-29 12:58:51 -0700486 /** To be overriden by subclasses to hint to Launcher that we have custom content */
487 protected boolean hasCustomContentToLeft() {
488 return false;
489 }
490
Dave Hawkeya8881582013-09-17 15:55:33 -0600491 /**
492 * To be overridden by subclasses to create the custom content and call
493 * {@link #addToCustomContentPage}. This will only be invoked if
494 * {@link #hasCustomContentToLeft()} is {@code true}.
495 */
496 protected void addCustomContentToLeft() {
497 }
498
499 /**
500 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
501 * ensure the custom content page is added or removed if necessary.
502 */
503 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600504 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600505 // Not bound yet, wait for bindScreens to be called.
506 return;
507 }
508
509 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
510 // Create the custom content page and call the subclass to populate it.
511 mWorkspace.createCustomContentPage();
512 addCustomContentToLeft();
513 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
514 mWorkspace.removeCustomContentPage();
515 }
516 }
517
Adam Cohenf9426d52012-06-04 17:26:21 -0700518 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700519 boolean searchVisible = false;
520 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800521 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700522 int coi = getCurrentOrientationIndexForGlobalIcons();
523 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
524 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700525 if (!DISABLE_MARKET_BUTTON) {
526 updateAppMarketIcon();
527 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700528 searchVisible = updateGlobalSearchIcon();
529 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700530 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700531 if (sGlobalSearchIcon[coi] != null) {
532 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700533 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700534 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700535 if (sVoiceSearchIcon[coi] != null) {
536 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700537 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700538 }
Winson Chungd64a6662013-09-30 11:06:59 -0700539 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700540 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800541 }
Winson Chungadf0c182012-08-23 14:59:07 -0700542 if (mSearchDropTargetBar != null) {
543 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 }
Romain Guycbb89e42009-06-08 15:52:54 -0700546
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700548 if (sLocaleConfiguration == null) {
549 new AsyncTask<Void, Void, LocaleConfiguration>() {
550 @Override
551 protected LocaleConfiguration doInBackground(Void... unused) {
552 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
553 readConfiguration(Launcher.this, localeConfiguration);
554 return localeConfiguration;
555 }
556
557 @Override
558 protected void onPostExecute(LocaleConfiguration result) {
559 sLocaleConfiguration = result;
560 checkForLocaleChange(); // recursive, but now with a locale configuration
561 }
562 }.execute();
563 return;
564 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 final Configuration configuration = getResources().getConfiguration();
567
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700568 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 final String locale = configuration.locale.toString();
570
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700571 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 final int mcc = configuration.mcc;
573
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700574 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 final int mnc = configuration.mnc;
576
Romain Guy84f296c2009-11-04 15:00:44 -0800577 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578
Romain Guy84f296c2009-11-04 15:00:44 -0800579 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580 sLocaleConfiguration.locale = locale;
581 sLocaleConfiguration.mcc = mcc;
582 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700583
Joe Onorato0589f0f2010-02-08 13:44:00 -0800584 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585
586 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
587 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700588 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 public void run() {
590 writeConfiguration(Launcher.this, localeConfiguration);
591 }
592 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700593 }
594 }
595
596 private static class LocaleConfiguration {
597 public String locale;
598 public int mcc = -1;
599 public int mnc = -1;
600 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700601
Romain Guy98d01652009-06-30 16:21:04 -0700602 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
603 DataInputStream in = null;
604 try {
605 in = new DataInputStream(context.openFileInput(PREFERENCES));
606 configuration.locale = in.readUTF();
607 configuration.mcc = in.readInt();
608 configuration.mnc = in.readInt();
609 } catch (FileNotFoundException e) {
610 // Ignore
611 } catch (IOException e) {
612 // Ignore
613 } finally {
614 if (in != null) {
615 try {
616 in.close();
617 } catch (IOException e) {
618 // Ignore
619 }
620 }
621 }
622 }
623
624 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
625 DataOutputStream out = null;
626 try {
627 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
628 out.writeUTF(configuration.locale);
629 out.writeInt(configuration.mcc);
630 out.writeInt(configuration.mnc);
631 out.flush();
632 } catch (FileNotFoundException e) {
633 // Ignore
634 } catch (IOException e) {
635 //noinspection ResultOfMethodCallIgnored
636 context.getFileStreamPath(PREFERENCES).delete();
637 } finally {
638 if (out != null) {
639 try {
640 out.close();
641 } catch (IOException e) {
642 // Ignore
643 }
644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
646 }
647
Anton Hanssona2f665f2013-09-26 12:18:32 +0100648 public Stats getStats() {
649 return mStats;
650 }
651
Bjorn Bringertc459e522013-06-07 19:36:01 +0100652 public LayoutInflater getInflater() {
653 return mInflater;
654 }
655
Adam Cohen716b51e2011-06-30 12:09:54 -0700656 public DragLayer getDragLayer() {
657 return mDragLayer;
658 }
659
Winson Chung36a62fe2012-05-06 18:04:42 -0700660 boolean isDraggingEnabled() {
661 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
662 // that is subsequently removed from the workspace in startBinding().
663 return !mModel.isLoadingWorkspace();
664 }
665
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 static int getScreen() {
667 synchronized (sLock) {
668 return sScreen;
669 }
670 }
671
672 static void setScreen(int screen) {
673 synchronized (sLock) {
674 sScreen = screen;
675 }
676 }
677
Winson Chung557d6ed2011-07-08 15:34:52 -0700678 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000679 * Copied from View -- the View version of the method isn't called
680 * anywhere else in our process and only exists for API level 17+,
681 * so it's ok to keep our own version with no API requirement.
682 */
683 public static int generateViewId() {
684 for (;;) {
685 final int result = sNextGeneratedId.get();
686 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
687 int newValue = result + 1;
688 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
689 if (sNextGeneratedId.compareAndSet(result, newValue)) {
690 return result;
691 }
692 }
693 }
694
695 public int getViewIdForItem(ItemInfo info) {
696 // This cast is safe given the > 2B range for int.
697 int itemId = (int) info.id;
698 if (mItemIdToViewId.containsKey(itemId)) {
699 return mItemIdToViewId.get(itemId);
700 }
701 int viewId = generateViewId();
702 mItemIdToViewId.put(itemId, viewId);
703 return viewId;
704 }
705
706 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700707 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
708 * a configuration step, this allows the proper animations to run after other transitions.
709 */
710 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700711 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800712 switch (args.requestCode) {
713 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700714 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700715 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800716 break;
717 case REQUEST_PICK_SHORTCUT:
718 processShortcut(args.intent);
719 break;
720 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700721 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700722 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700723 result = true;
724 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 case REQUEST_CREATE_APPWIDGET:
726 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700727 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700728 result = true;
729 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800730 }
Michael Jurka27614d22012-04-02 06:40:22 -0700731 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
732 // if you turned the screen off and then back while in All Apps, Launcher would not
733 // return to the workspace. Clearing mAddInfo.container here fixes this issue
734 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700735 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800736 }
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700739 protected void onActivityResult(
740 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700741 // Reset the startActivity waiting flag
742 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800743 int pendingAddWidgetId = mPendingAddWidgetId;
744 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700745
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 Runnable exitSpringLoaded = new Runnable() {
747 @Override
748 public void run() {
749 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
750 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
751 }
752 };
753
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
757 if (resultCode == RESULT_CANCELED) {
758 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
760 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
763 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 }
765 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200766 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
767 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
768 mWorkspace.exitOverviewMode(false);
769 }
770 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200772
Adam Cohened66b2b2012-01-23 17:28:51 -0800773 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
774 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700775
Adam Cohened66b2b2012-01-23 17:28:51 -0800776 // We have special handling for widgets
777 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800778 final int appWidgetId;
779 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
780 : -1;
781 if (widgetId < 0) {
782 appWidgetId = pendingAddWidgetId;
783 } else {
784 appWidgetId = widgetId;
785 }
786
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 final int result;
788 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800789 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700790 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
791 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700792 result = RESULT_CANCELED;
793 completeTwoStageWidgetDrop(result, appWidgetId);
794 onComplete = new Runnable() {
795 @Override
796 public void run() {
797 exitSpringLoadedDragModeDelayed(false, 0, null);
798 }
799 };
Winson Chung5aaab772012-04-27 15:24:02 -0700800 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700802 final CellLayout dropLayout =
803 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
804 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700805 onComplete = new Runnable() {
806 @Override
807 public void run() {
808 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700809 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 }
811 };
Winson Chung5aaab772012-04-27 15:24:02 -0700812 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
814 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 return;
816 }
817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 // The pattern used here is that a user PICKs a specific application,
819 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
822 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700823 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800824 final PendingAddArguments args = new PendingAddArguments();
825 args.requestCode = requestCode;
826 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700827 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700829 args.cellX = mPendingAddInfo.cellX;
830 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800831 if (isWorkspaceLocked()) {
832 sPendingAddList.add(args);
833 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700834 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700838 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700839 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
840 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 }
844
845 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700846 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700847 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 Runnable onCompleteRunnable = null;
849 int animationType = 0;
850
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700851 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800852 if (resultCode == RESULT_OK) {
853 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
854 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700855 mPendingAddWidgetInfo);
856 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800857 onCompleteRunnable = new Runnable() {
858 @Override
859 public void run() {
860 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700861 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700862 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
863 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 }
865 };
866 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800867 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700870 if (mDragLayer.getAnimatedView() != null) {
871 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
872 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
873 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700874 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700875 // The animated view may be null in the case of a rotation during widget configuration
876 onCompleteRunnable.run();
877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
879
880 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700881 protected void onStop() {
882 super.onStop();
883 FirstFrameAnimatorHelper.setIsVisible(false);
884 }
885
886 @Override
887 protected void onStart() {
888 super.onStart();
889 FirstFrameAnimatorHelper.setIsVisible(true);
890 }
891
892 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200894 long startTime = 0;
895 if (DEBUG_RESUME_TIME) {
896 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400897 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700900
Winson Chung4a2afa32012-07-19 14:53:05 -0700901 // Restore the previous launcher state
902 if (mOnResumeState == State.WORKSPACE) {
903 showWorkspace(false);
904 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800905 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700906 }
907 mOnResumeState = State.NONE;
908
Craig Mautner360310b2012-10-26 15:13:08 -0700909 // Background was set to gradient in onPause(), restore to black if in all apps.
910 setWorkspaceBackground(mState == State.WORKSPACE);
911
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800912 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700913 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700914 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700916 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400917 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700918 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700920 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200921 // We might have postponed some bind calls until onResume (see waitUntilResume) --
922 // execute them here
923 long startTimeCallbacks = 0;
924 if (DEBUG_RESUME_TIME) {
925 startTimeCallbacks = System.currentTimeMillis();
926 }
927
928 if (mAppsCustomizeContent != null) {
929 mAppsCustomizeContent.setBulkBind(true);
930 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700931 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
932 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200933 }
934 if (mAppsCustomizeContent != null) {
935 mAppsCustomizeContent.setBulkBind(false);
936 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700937 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200938 if (DEBUG_RESUME_TIME) {
939 Log.d(TAG, "Time spent processing callbacks in onResume: " +
940 (System.currentTimeMillis() - startTimeCallbacks));
941 }
Michael Jurka447bf842013-05-15 14:52:15 +0200942 }
Michael Jurka54554252013-08-01 12:52:23 +0200943 if (mOnResumeCallbacks.size() > 0) {
944 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
945 mOnResumeCallbacks.get(i).run();
946 }
947 mOnResumeCallbacks.clear();
948 }
Winson Chunge4e50662012-01-23 14:45:13 -0800949
950 // Reset the pressed state of icons that were locked in the press state while activities
951 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800952 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800953 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800954 mWaitingForResume.setStayPressed(false);
955 }
Winson Chunge4e50662012-01-23 14:45:13 -0800956 if (mAppsCustomizeContent != null) {
957 // Resets the previous all apps icon press state
958 mAppsCustomizeContent.resetDrawableState();
959 }
Winson Chung82963d52013-10-09 11:20:57 -0700960
Adam Cohen06dff352012-06-01 17:17:08 -0700961 // It is possible that widgets can receive updates while launcher is not in the foreground.
962 // Consequently, the widgets will be inflated in the orientation of the foreground activity
963 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
964 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700965 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700966
Winson Chung780fe592013-09-26 14:48:44 -0700967 // Process any items that were added while Launcher was away.
968 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
969
Winson Chung5841efa2013-09-30 18:06:44 -0700970 // Update the voice search button proxy
971 updateVoiceButtonProxyVisible(false);
972
Adam Cohenf9426d52012-06-04 17:26:21 -0700973 // Again, as with the above scenario, it's possible that one or more of the global icons
974 // were updated in the wrong orientation.
975 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200976 if (DEBUG_RESUME_TIME) {
977 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
978 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700979
980 if (mWorkspace.getCustomContentCallbacks() != null) {
981 // If we are resuming and the custom content is the current page, we call onShow().
982 // It is also poassible that onShow will instead be called slightly after first layout
983 // if PagedView#setRestorePage was set to the custom content page in onCreate().
984 if (mWorkspace.isOnOrMovingToCustomContent()) {
985 mWorkspace.getCustomContentCallbacks().onShow();
986 }
987 }
Adam Cohenedaaa302013-10-01 17:33:27 -0700988 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -0700989 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -0700990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -0700994 // Ensure that items added to Launcher are queued until Launcher returns
995 InstallShortcutReceiver.enableInstallQueue();
996
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700997 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700998 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800999 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001000 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001001
1002 // We call onHide() aggressively. The custom content callbacks should be able to
1003 // debounce excess onHide calls.
1004 if (mWorkspace.getCustomContentCallbacks() != null) {
1005 mWorkspace.getCustomContentCallbacks().onHide();
1006 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001007 }
Romain Guycbb89e42009-06-08 15:52:54 -07001008
Adam Cohen66a01fd2013-06-11 12:48:00 -07001009 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001010 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001011 addCustomContentToLeft();
1012 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001013 }
1014
Adam Cohenbffe7452013-07-22 18:21:45 -07001015 QSBScroller mQsbScroller = new QSBScroller() {
1016 int scrollY = 0;
1017
1018 @Override
1019 public void setScrollY(int scroll) {
1020 scrollY = scroll;
1021
1022 if (mWorkspace.isOnOrMovingToCustomContent()) {
1023 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001024 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001025 }
1026 }
1027 };
1028
1029 public void resetQSBScroll() {
1030 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001031 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001032 }
1033
1034 public interface CustomContentCallbacks {
1035 // Custom content is completely shown
1036 public void onShow();
1037
1038 // Custom content is completely hidden
1039 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001040
1041 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1042 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001043 }
1044
Adam Cohen30bacb22013-08-29 14:25:25 -07001045 protected void startSettings() {
1046 }
1047
Adam Cohenbffe7452013-07-22 18:21:45 -07001048 public interface QSBScroller {
1049 public void setScrollY(int scrollY);
1050 }
1051
Winson Chung98ca0f72013-07-29 12:58:51 -07001052 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001053 CustomContentCallbacks callbacks, String description) {
1054 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001055 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001056 }
1057
Adam Cohenedb40762013-07-18 16:45:45 -07001058 // The custom content needs to offset its content to account for the QSB
1059 public int getTopOffsetForCustomContent() {
1060 return mWorkspace.getPaddingTop();
1061 }
1062
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001063 @Override
1064 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 // Flag the loader to stop early before switching
1066 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 if (mAppsCustomizeContent != null) {
1068 mAppsCustomizeContent.surrender();
1069 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001070 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001071 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001073 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001074 @Override
1075 public void onWindowFocusChanged(boolean hasFocus) {
1076 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001077 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001078 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 private boolean acceptFilter() {
1081 final InputMethodManager inputManager = (InputMethodManager)
1082 getSystemService(Context.INPUT_METHOD_SERVICE);
1083 return !inputManager.isFullscreenMode();
1084 }
1085
1086 @Override
1087 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001088 final int uniChar = event.getUnicodeChar();
1089 final boolean handled = super.onKeyDown(keyCode, event);
1090 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1091 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1093 keyCode, event);
1094 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001095 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001096 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001097 // showSearchDialog()
1098 // If there are multiple keystrokes before the search dialog takes focus,
1099 // onSearchRequested() will be called for every keystroke,
1100 // but it is idempotent, so it's fine.
1101 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103 }
1104
Joe Onorato8a9625e2010-01-28 15:55:35 -08001105 // Eat the long press event so the keyboard doesn't come up.
1106 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1107 return true;
1108 }
1109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 return handled;
1111 }
1112
Karl Rosaen138a0412009-04-23 19:00:21 -07001113 private String getTypedText() {
1114 return mDefaultKeySsb.toString();
1115 }
1116
1117 private void clearTypedText() {
1118 mDefaultKeySsb.clear();
1119 mDefaultKeySsb.clearSpans();
1120 Selection.setSelection(mDefaultKeySsb, 0);
1121 }
1122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001124 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1125 * State
1126 */
1127 private static State intToState(int stateOrdinal) {
1128 State state = State.WORKSPACE;
1129 final State[] stateValues = State.values();
1130 for (int i = 0; i < stateValues.length; i++) {
1131 if (stateValues[i].ordinal() == stateOrdinal) {
1132 state = stateValues[i];
1133 break;
1134 }
1135 }
1136 return state;
1137 }
1138
1139 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 * Restores the previous state, if it exists.
1141 *
1142 * @param savedState The previous state.
1143 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001144 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 private void restoreState(Bundle savedState) {
1146 if (savedState == null) {
1147 return;
1148 }
1149
Michael Jurka883f55b2010-10-21 15:47:14 -07001150 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001151 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001152 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001153 }
1154
Adam Cohen21cd0022013-10-09 18:57:02 -07001155 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1156 PagedView.INVALID_RESTORE_PAGE);
1157 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001158 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160
Winson Chung3d503fb2011-07-13 17:25:49 -07001161 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001162 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163
Winson Chung3d503fb2011-07-13 17:25:49 -07001164 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1165 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001167 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1168 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001169 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1170 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1171 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001172 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001173 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 mRestoring = true;
1175 }
1176
1177 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1178 if (renameFolder) {
1179 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001180 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 mRestoring = true;
1182 }
Winson Chunga12a2502010-12-20 14:41:35 -08001183
Winson Chung785d2eb2011-04-14 16:08:02 -07001184 // Restore the AppsCustomize tab
1185 if (mAppsCustomizeTabHost != null) {
1186 String curTab = savedState.getString("apps_customize_currentTab");
1187 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001188 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001189 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001190 mAppsCustomizeContent.loadAssociatedPages(
1191 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001192 }
1193
Winson Chung5afbf7b2011-07-25 11:53:08 -07001194 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1195 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001196 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001197 mItemIdToViewId = (HashMap<Integer, Integer>)
1198 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200
1201 /**
1202 * Finds all the views we need and configure them properly.
1203 */
1204 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001205 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001206
Craig Mautner360310b2012-10-26 15:13:08 -07001207 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001208 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1209 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001210
John Spurlock77e1f472013-09-11 10:09:51 -04001211 mLauncherView.setSystemUiVisibility(
1212 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001213 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
Winson Chung4c98d922011-05-31 16:50:48 -07001215 // Setup the drag layer
1216 mDragLayer.setup(this, dragController);
1217
Winson Chung3d503fb2011-07-13 17:25:49 -07001218 // Setup the hotseat
1219 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1220 if (mHotseat != null) {
1221 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001222 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001223 }
1224
Adam Cohenf358a4b2013-07-23 16:47:31 -07001225 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001226 View widgetButton = findViewById(R.id.widget_button);
1227 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001228 @Override
1229 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001230 if (!mWorkspace.isSwitchingState()) {
1231 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1232 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001233 }
1234 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001235 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1236
1237 View wallpaperButton = findViewById(R.id.wallpaper_button);
1238 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001239 @Override
1240 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001241 if (!mWorkspace.isSwitchingState()) {
1242 startWallpaper();
1243 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001244 }
1245 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001246 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1247
1248 View settingsButton = findViewById(R.id.settings_button);
1249 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001250 @Override
1251 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001252 if (!mWorkspace.isSwitchingState()) {
1253 startSettings();
1254 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001255 }
1256 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001257 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001258 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001259
Winson Chung4c98d922011-05-31 16:50:48 -07001260 // Setup the workspace
1261 mWorkspace.setHapticFeedbackEnabled(false);
1262 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001263 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001264 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001265
Winson Chungf0ea4d32011-06-06 14:27:16 -07001266 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001267 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001268
Winson Chungf0ea4d32011-06-06 14:27:16 -07001269 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001270 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001271 mAppsCustomizeContent = (AppsCustomizePagedView)
1272 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1273 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001274
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001276 dragController.setDragScoller(mWorkspace);
1277 dragController.setScrollView(mDragLayer);
1278 dragController.setMoveTarget(mWorkspace);
1279 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001280 if (mSearchDropTargetBar != null) {
1281 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001282 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001283
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001284 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001285 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001286 mWeightWatcher = new WeightWatcher(this);
1287 mWeightWatcher.setAlpha(0.5f);
1288 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001289 new FrameLayout.LayoutParams(
1290 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001291 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001292 Gravity.BOTTOM)
1293 );
Adam Cohen39a06042013-07-19 14:30:12 -07001294
1295 boolean show = shouldShowWeightWatcher();
1296 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299
1300 /**
1301 * Creates a view representing a shortcut.
1302 *
1303 * @param info The data structure describing the shortcut.
1304 *
1305 * @return A View inflated from R.layout.application.
1306 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001307 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001309 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311
1312 /**
1313 * Creates a view representing a shortcut inflated from the specified resource.
1314 *
1315 * @param layoutResId The id of the XML layout used to create the shortcut.
1316 * @param parent The group the shortcut belongs to.
1317 * @param info The data structure describing the shortcut.
1318 *
1319 * @return A View inflated from layoutResId.
1320 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001321 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001322 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1323 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 return favorite;
1326 }
1327
1328 /**
1329 * Add an application shortcut to the workspace.
1330 *
1331 * @param data The intent describing the application.
1332 * @param cellInfo The position on screen where to create the shortcut.
1333 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001335 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001336 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001337
Adam Cohenfbba09b2011-07-18 21:43:05 -07001338 // First we check if we already know the exact location where we want to add this item.
1339 if (cellX >= 0 && cellY >= 0) {
1340 cellXY[0] = cellX;
1341 cellXY[1] = cellY;
1342 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001343 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001344 return;
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001347 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001348
Romain Guy73b979d2009-06-09 12:57:21 -07001349 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001350 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001352 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001353 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001354 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001355 } else {
1356 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358 }
Romain Guycbb89e42009-06-08 15:52:54 -07001359
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 /**
1361 * Add a shortcut to the workspace.
1362 *
1363 * @param data The intent describing the shortcut.
1364 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001366 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 int cellY) {
1368 int[] cellXY = mTmpAddItemCellCoordinates;
1369 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001370 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001371
Michael Jurkad3ef3062010-11-23 16:23:58 -08001372 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001373
Adam Cohen558baaf2011-08-15 15:22:57 -07001374 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001375 if (info == null) {
1376 return;
1377 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001378 final View view = createShortcut(info);
1379
Adam Cohenfbba09b2011-07-18 21:43:05 -07001380 // First we check if we already know the exact location where we want to add this item.
1381 if (cellX >= 0 && cellY >= 0) {
1382 cellXY[0] = cellX;
1383 cellXY[1] = cellY;
1384 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001385
1386 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001387 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001388 true, null,null)) {
1389 return;
1390 }
1391 DragObject dragObject = new DragObject();
1392 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001393 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1394 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001395 return;
1396 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001397 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001398 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001400 foundCellSpan = (result != null);
1401 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001402 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001403 }
1404
1405 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001406 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001407 return;
1408 }
1409
Adam Cohendcd297f2013-06-18 13:13:40 -07001410 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411
1412 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001413 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
1416 }
1417
Adam Cohen2f093b62012-04-30 18:59:53 -07001418 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1419 int minHeight) {
1420 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001421 // We want to account for the extra amount of padding that we are adding to the widget
1422 // to ensure that it gets the full amount of space that it has requested
1423 int requiredWidth = minWidth + padding.left + padding.right;
1424 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001425 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001426 }
1427
Adam Cohen2f093b62012-04-30 18:59:53 -07001428 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1429 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001430 }
1431
Adam Cohen2f093b62012-04-30 18:59:53 -07001432 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1433 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001434 }
1435
Adam Cohen2f093b62012-04-30 18:59:53 -07001436 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1437 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001438 }
1439
Adam Cohen2f093b62012-04-30 18:59:53 -07001440 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1441 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001442 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001443 }
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001446 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001448 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001449 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001451 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001452 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1453 if (appWidgetInfo == null) {
1454 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1455 }
Romain Guycbb89e42009-06-08 15:52:54 -07001456
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001457 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001458 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001459
Adam Cohen2f093b62012-04-30 18:59:53 -07001460 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1461 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001464 // We have saved the position to which the widget was dragged-- this really only matters
1465 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001466 int[] cellXY = mTmpAddItemCellCoordinates;
1467 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001468 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001469 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001470 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1471 cellXY[0] = mPendingAddInfo.cellX;
1472 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001473 spanXY[0] = mPendingAddInfo.spanX;
1474 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001475 foundCellSpan = true;
1476 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001479 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1480 spanXY[1], cellXY, finalSpan);
1481 spanXY[0] = finalSpan[0];
1482 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001483 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001484 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001485 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001486 }
1487
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001489 if (appWidgetId != -1) {
1490 // Deleting an app widget ID is a void call but writes to disk before returning
1491 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001492 new Thread("deleteAppWidgetId") {
1493 public void run() {
1494 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1495 }
1496 }.start();
1497 }
Winson Chung93eef082012-03-23 15:59:27 -07001498 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001499 return;
1500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001502 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001503 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1504 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 launcherInfo.spanX = spanXY[0];
1506 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001507 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1508 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001511 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512
1513 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001514 if (hostView == null) {
1515 // Perform actual inflation because we're live
1516 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1517 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1518 } else {
1519 // The AppWidgetHostView has already been inflated and instantiated
1520 launcherInfo.hostView = hostView;
1521 }
Romain Guycbb89e42009-06-08 15:52:54 -07001522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001524 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001525 launcherInfo.notifyWidgetSizeChanged(this);
1526
Adam Cohendcd297f2013-06-18 13:13:40 -07001527 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001529
1530 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001532 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
Romain Guycbb89e42009-06-08 15:52:54 -07001534
Adam Cohended9f8d2010-11-03 13:25:16 -07001535 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1536 @Override
1537 public void onReceive(Context context, Intent intent) {
1538 final String action = intent.getAction();
1539 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1540 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001541 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001542 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001543
Winson Chungc100e8e2011-08-09 16:02:43 -07001544 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001545 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001546 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001547 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001548 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001549 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1550 mUserPresent = true;
1551 updateRunning();
1552 }
1553 }
1554 };
1555
1556 @Override
1557 public void onAttachedToWindow() {
1558 super.onAttachedToWindow();
1559
1560 // Listen for broadcasts related to user-presence
1561 final IntentFilter filter = new IntentFilter();
1562 filter.addAction(Intent.ACTION_SCREEN_OFF);
1563 filter.addAction(Intent.ACTION_USER_PRESENT);
1564 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001565 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001566 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001567 mVisible = true;
1568 }
1569
1570 @Override
1571 public void onDetachedFromWindow() {
1572 super.onDetachedFromWindow();
1573 mVisible = false;
1574
Adam Cohend113e0c2010-11-11 10:48:05 -08001575 if (mAttached) {
1576 unregisterReceiver(mReceiver);
1577 mAttached = false;
1578 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 updateRunning();
1580 }
1581
1582 public void onWindowVisibilityChanged(int visibility) {
1583 mVisible = visibility == View.VISIBLE;
1584 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001585 // The following code used to be in onResume, but it turns out onResume is called when
1586 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1587 // is a more appropriate event to handle
1588 if (mVisible) {
1589 mAppsCustomizeTabHost.onWindowVisible();
1590 if (!mWorkspaceLoading) {
1591 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001592 // We want to let Launcher draw itself at least once before we force it to build
1593 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001594 // apps is nice and speedy.
1595 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001596 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001597 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001598 if (mStarted) return;
1599 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001600 // We delay the layer building a bit in order to give
1601 // other message processing a time to run. In particular
1602 // this avoids a delay in hiding the IME if it was
1603 // currently shown, because doing that may involve
1604 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001605 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001606 final ViewTreeObserver.OnDrawListener listener = this;
1607 mWorkspace.post(new Runnable() {
1608 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001609 if (mWorkspace != null &&
1610 mWorkspace.getViewTreeObserver() != null) {
1611 mWorkspace.getViewTreeObserver().
1612 removeOnDrawListener(listener);
1613 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001614 }
1615 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001616 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001617 }
1618 });
1619 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001620 // When Launcher comes back to foreground, a different Activity might be responsible for
1621 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001622 if (!DISABLE_MARKET_BUTTON) {
1623 updateAppMarketIcon();
1624 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001625 clearTypedText();
1626 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001627 }
1628
1629 private void sendAdvanceMessage(long delay) {
1630 mHandler.removeMessages(ADVANCE_MSG);
1631 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1632 mHandler.sendMessageDelayed(msg, delay);
1633 mAutoAdvanceSentTime = System.currentTimeMillis();
1634 }
1635
1636 private void updateRunning() {
1637 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1638 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1639 mAutoAdvanceRunning = autoAdvanceRunning;
1640 if (autoAdvanceRunning) {
1641 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1642 sendAdvanceMessage(delay);
1643 } else {
1644 if (!mWidgetsToAdvance.isEmpty()) {
1645 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1646 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1647 }
1648 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001649 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 }
1651 }
1652 }
1653
1654 private final Handler mHandler = new Handler() {
1655 @Override
1656 public void handleMessage(Message msg) {
1657 if (msg.what == ADVANCE_MSG) {
1658 int i = 0;
1659 for (View key: mWidgetsToAdvance.keySet()) {
1660 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1661 final int delay = mAdvanceStagger * i;
1662 if (v instanceof Advanceable) {
1663 postDelayed(new Runnable() {
1664 public void run() {
1665 ((Advanceable) v).advance();
1666 }
1667 }, delay);
1668 }
1669 i++;
1670 }
1671 sendAdvanceMessage(mAdvanceInterval);
1672 }
1673 }
1674 };
1675
1676 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001677 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1679 if (v instanceof Advanceable) {
1680 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001681 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 updateRunning();
1683 }
1684 }
1685
1686 void removeWidgetToAutoAdvance(View hostView) {
1687 if (mWidgetsToAdvance.containsKey(hostView)) {
1688 mWidgetsToAdvance.remove(hostView);
1689 updateRunning();
1690 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001691 }
1692
Joe Onorato9c1289c2009-08-17 11:03:03 -04001693 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001695 launcherInfo.hostView = null;
1696 }
1697
Winson Chung93eef082012-03-23 15:59:27 -07001698 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1699 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1700 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 }
1702
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001703 public LauncherAppWidgetHost getAppWidgetHost() {
1704 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
Romain Guycbb89e42009-06-08 15:52:54 -07001706
Winson Chunga9abd0e2010-10-27 17:18:37 -07001707 public LauncherModel getModel() {
1708 return mModel;
1709 }
1710
Bjorn Bringertc459e522013-06-07 19:36:01 +01001711 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001712 getWindow().closeAllPanels();
1713
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001714 // Whatever we were doing is hereby canceled.
1715 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001716 }
1717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 @Override
1719 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001720 long startTime = 0;
1721 if (DEBUG_RESUME_TIME) {
1722 startTime = System.currentTimeMillis();
1723 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 super.onNewIntent(intent);
1725
1726 // Close the menu
1727 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001728 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001729 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001730
Adam Cohened307df2013-10-02 09:37:31 -07001731 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1732 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1733 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001734
Adam Cohen6fecd412013-10-02 17:41:50 -07001735 if (mWorkspace == null) {
1736 // Can be cases where mWorkspace is null, this prevents a NPE
1737 return;
1738 }
1739 Folder openFolder = mWorkspace.getOpenFolder();
1740 // In all these cases, only animate if we're already on home
1741 mWorkspace.exitWidgetResizeMode();
1742 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001743 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001744 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001745 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001746
Adam Cohen6fecd412013-10-02 17:41:50 -07001747 closeFolder();
1748 exitSpringLoadedDragMode();
1749
1750 // If we are already on home, then just animate back to the workspace,
1751 // otherwise, just wait until onResume to set the state back to Workspace
1752 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001753 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001754 } else {
1755 mOnResumeState = State.WORKSPACE;
1756 }
1757
1758 final View v = getWindow().peekDecorView();
1759 if (v != null && v.getWindowToken() != null) {
1760 InputMethodManager imm = (InputMethodManager)getSystemService(
1761 INPUT_METHOD_SERVICE);
1762 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1763 }
1764
1765 // Reset the apps customize page
1766 if (mAppsCustomizeTabHost != null) {
1767 mAppsCustomizeTabHost.reset();
1768 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001769
1770 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Adam Cohened307df2013-10-02 09:37:31 -07001772
Michael Jurka447bf842013-05-15 14:52:15 +02001773 if (DEBUG_RESUME_TIME) {
1774 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 }
1777
Adam Coppa120b8e2013-11-12 16:26:04 +00001778 /**
1779 * Override point for subclasses to prevent movement to the default screen when the home
1780 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1781 */
1782 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1783 return true;
1784 }
1785
1786 /**
1787 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1788 */
1789 protected void onHomeIntent() {
1790 // Do nothing
1791 }
1792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001794 public void onRestoreInstanceState(Bundle state) {
1795 super.onRestoreInstanceState(state);
1796 for (int page: mSynchronouslyBoundPages) {
1797 mWorkspace.restoreInstanceStateForChild(page);
1798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
1800
1801 @Override
1802 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001803 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001804 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1805 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001806 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001807 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808
Michael Jurka883f55b2010-10-21 15:47:14 -07001809 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001810 // We close any open folder since it will not be re-opened, and we need to make sure
1811 // this state is reflected.
1812 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813
Adam Cohendcd297f2013-06-18 13:13:40 -07001814 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001815 mWaitingForResult) {
1816 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001817 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001818 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1819 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001820 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1821 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1822 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001823 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 }
1825
1826 if (mFolderInfo != null && mWaitingForResult) {
1827 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1828 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1829 }
Michael Jurka946ad472010-07-09 18:05:18 -07001830
Winson Chung785d2eb2011-04-14 16:08:02 -07001831 // Save the current AppsCustomize tab
1832 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001833 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1834 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001835 if (currentTabTag != null) {
1836 outState.putString("apps_customize_currentTab", currentTabTag);
1837 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001838 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1839 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001840 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001841 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
1843
1844 @Override
1845 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001847
Winson Chunge7a03942011-08-05 15:05:12 -07001848 // Remove all pending runnables
1849 mHandler.removeMessages(ADVANCE_MSG);
1850 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001851 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001852
Winson Chungcd2b0142011-06-08 16:02:26 -07001853 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001854 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001855 mModel.stopLoader();
1856 app.setLauncher(null);
1857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001859 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001861 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001863 mAppWidgetHost = null;
1864
1865 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866
1867 TextKeyListener.getInstance().release();
1868
Winson Chung81b52252012-08-27 15:34:29 -07001869 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1870 // to prevent leaking Launcher activities on orientation change.
1871 if (mModel != null) {
1872 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1873 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001874
1875 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001876 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001877
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001878 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001879 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001880 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001881 mWorkspace = null;
1882 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001883
Michael Jurka2ecf9952012-06-18 12:52:28 -07001884 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
Adam Cohena9cf38f2011-05-02 15:36:58 -07001887 public DragController getDragController() {
1888 return mDragController;
1889 }
1890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 @Override
1892 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001893 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 super.startActivityForResult(intent, requestCode);
1895 }
1896
Winson Chung70d72102011-08-12 11:24:35 -07001897 /**
1898 * Indicates that we want global search for this activity by setting the globalSearch
1899 * argument for {@link #startSearch} to true.
1900 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001902 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001904
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001905 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001906
Karl Rosaen138a0412009-04-23 19:00:21 -07001907 if (initialQuery == null) {
1908 // Use any text typed in the launcher as the initial query
1909 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 if (appSearchData == null) {
1912 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001913 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
Winson Chungadf0c182012-08-23 14:59:07 -07001915 Rect sourceBounds = new Rect();
1916 if (mSearchDropTargetBar != null) {
1917 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1918 }
Romain Guycbb89e42009-06-08 15:52:54 -07001919
Bjorn Bringertc459e522013-06-07 19:36:01 +01001920 startSearch(initialQuery, selectInitialQuery,
1921 appSearchData, sourceBounds);
1922 }
1923
1924 public void startSearch(String initialQuery,
1925 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001926 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001927 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001928 }
1929
1930 /**
1931 * Starts the global search activity. This code is a copied from SearchManager
1932 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001933 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001934 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001935 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001936 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1937 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1938 if (globalSearchActivity == null) {
1939 Log.w(TAG, "No global search activity found.");
1940 return;
1941 }
1942 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1943 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1944 intent.setComponent(globalSearchActivity);
1945 // Make sure that we have a Bundle to put source in
1946 if (appSearchData == null) {
1947 appSearchData = new Bundle();
1948 } else {
1949 appSearchData = new Bundle(appSearchData);
1950 }
1951 // Set source to package name of app that starts global search, if not set already.
1952 if (!appSearchData.containsKey("source")) {
1953 appSearchData.putString("source", getPackageName());
1954 }
1955 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1956 if (!TextUtils.isEmpty(initialQuery)) {
1957 intent.putExtra(SearchManager.QUERY, initialQuery);
1958 }
1959 if (selectInitialQuery) {
1960 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1961 }
1962 intent.setSourceBounds(sourceBounds);
1963 try {
1964 startActivity(intent);
1965 } catch (ActivityNotFoundException ex) {
1966 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969
Winson Chung70d72102011-08-12 11:24:35 -07001970 @Override
Winson Chung70d72102011-08-12 11:24:35 -07001971 public boolean onPrepareOptionsMenu(Menu menu) {
1972 super.onPrepareOptionsMenu(menu);
Michael Jurkab94f3f82013-09-11 18:08:54 +02001973 if (!mWorkspace.isInOverviewMode()) {
1974 mWorkspace.enterOverviewMode();
Winson Chung70d72102011-08-12 11:24:35 -07001975 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02001976 return false;
Winson Chung70d72102011-08-12 11:24:35 -07001977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001979 @Override
1980 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001981 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001982 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001983 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001984 }
1985
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 public boolean isWorkspaceLocked() {
1987 return mWorkspaceLoading || mWaitingForResult;
1988 }
1989
Michael Jurka0280c3b2010-09-17 15:00:07 -07001990 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001992 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001993 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1994 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001995 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001997 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001998
Adam Cohenad4e15c2013-10-17 16:21:35 -07001999 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2000 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2001 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2002 }
2003
2004 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2005 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2006 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002007 if (appWidgetInfo.configure != null) {
2008 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002009 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002010
Bjorn Bringert7984c942009-12-09 15:38:25 +00002011 // Launch over to configure widget, if needed
2012 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002013 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002014 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002015 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002017 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002018 Runnable onComplete = new Runnable() {
2019 @Override
2020 public void run() {
2021 // Exit spring loaded mode if necessary after adding the widget
2022 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2023 null);
2024 }
2025 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002027 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002028 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030 }
Romain Guycbb89e42009-06-08 15:52:54 -07002031
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002032 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002033 // Close any folders that may be open.
2034 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002035 mWorkspace.moveToCustomContentScreen(animate);
2036 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002037 /**
2038 * Process a shortcut drop.
2039 *
2040 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002041 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002042 * @param cell The cell it should be added to, optional
2043 * @param position The location on the screen where it was dropped, optional
2044 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002045 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002046 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002047 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002048 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002049 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002050 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002051
2052 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002053 mPendingAddInfo.cellX = cell[0];
2054 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002055 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002056
2057 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2058 createShortcutIntent.setComponent(componentName);
2059 processShortcut(createShortcutIntent);
2060 }
2061
Adam Cohenfbba09b2011-07-18 21:43:05 -07002062 /**
2063 * Process a widget drop.
2064 *
2065 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002066 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002067 * @param cell The cell it should be added to, optional
2068 * @param position The location on the screen where it was dropped, optional
2069 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002070 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002071 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002072 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002073 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002074 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002075 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002076 mPendingAddInfo.minSpanX = info.minSpanX;
2077 mPendingAddInfo.minSpanY = info.minSpanY;
2078
Adam Cohenfbba09b2011-07-18 21:43:05 -07002079 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002080 mPendingAddInfo.cellX = cell[0];
2081 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002082 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002083 if (span != null) {
2084 mPendingAddInfo.spanX = span[0];
2085 mPendingAddInfo.spanY = span[1];
2086 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002087
Adam Cohened66b2b2012-01-23 17:28:51 -08002088 AppWidgetHostView hostView = info.boundWidget;
2089 int appWidgetId;
2090 if (hostView != null) {
2091 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002092 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002093 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002094 // In this case, we either need to start an activity to get permission to bind
2095 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002096 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002097 Bundle options = info.bindOptions;
2098
2099 boolean success = false;
2100 if (options != null) {
2101 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2102 info.componentName, options);
2103 } else {
2104 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2105 info.componentName);
2106 }
2107 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002108 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002109 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002110 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002111 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2112 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2113 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002114 // TODO: we need to make sure that this accounts for the options bundle.
2115 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002116 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2117 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002118 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002119 }
2120
Joe Onoratodeb98af2010-02-19 14:59:39 -08002121 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002122 // Handle case where user selected "Applications"
2123 String applicationName = getResources().getString(R.string.group_applications);
2124 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002125
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002126 if (applicationName != null && applicationName.equals(shortcutName)) {
2127 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2128 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002129
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002130 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2131 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002132 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002133 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002134 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002135 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 }
2138
Winson Chung24ab2f12010-09-16 14:10:47 -07002139 void processWallpaper(Intent intent) {
2140 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2141 }
2142
Adam Cohendcd297f2013-06-18 13:13:40 -07002143 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002144 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002145 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 folderInfo.title = getText(R.string.folder_name);
2147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002149 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002150 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002151 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152
2153 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002154 FolderIcon newFolder =
2155 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002156 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002157 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002158 // Force measure the new folder icon
2159 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2160 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002161 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 }
Romain Guycbb89e42009-06-08 15:52:54 -07002163
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002165 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 }
2167
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002168 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002170 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002171 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 }
2173
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002174 protected ComponentName getWallpaperPickerComponent() {
2175 return new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName());
2176 }
2177
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002178 /**
2179 * Registers various content observers. The current implementation registers
2180 * only a favorites observer to keep track of the favorites applications.
2181 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002182 private void registerContentObservers() {
2183 ContentResolver resolver = getContentResolver();
2184 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2185 true, mWidgetObserver);
2186 }
2187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 @Override
2189 public boolean dispatchKeyEvent(KeyEvent event) {
2190 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2191 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002193 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002194 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002195 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002196 dumpState();
2197 return true;
2198 }
2199 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002200 }
2201 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2202 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002203 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 return true;
2205 }
2206 }
2207
2208 return super.dispatchKeyEvent(event);
2209 }
2210
Joe Onorato88ec0992009-11-19 13:16:06 -08002211 @Override
2212 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002213 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002214 if (mAppsCustomizeContent.getContentType() ==
2215 AppsCustomizePagedView.ContentType.Applications) {
2216 showWorkspace(true);
2217 } else {
2218 showOverviewMode(true);
2219 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002220 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002221 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002222 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002223 Folder openFolder = mWorkspace.getOpenFolder();
2224 if (openFolder.isEditingName()) {
2225 openFolder.dismissEditingName();
2226 } else {
2227 closeFolder();
2228 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002229 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002230 mWorkspace.exitWidgetResizeMode();
2231
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002232 // Back button is a no-op here, but give at least some feedback for the button press
2233 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002234 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002235 }
2236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002238 * Re-listen when widgets are reset.
2239 */
2240 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002241 if (mAppWidgetHost != null) {
2242 mAppWidgetHost.startListening();
2243 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002244 }
2245
2246 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 * Launches the intent referred by the clicked shortcut.
2248 *
2249 * @param v The view representing the clicked shortcut.
2250 */
2251 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002252 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2253 // view has detached (it's possible for this to happen if the view is removed mid touch).
2254 if (v.getWindowToken() == null) {
2255 return;
2256 }
2257
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002258 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002259 return;
2260 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002261
Adam Cohen1697b792013-09-17 19:08:21 -07002262 if (v instanceof Workspace) {
2263 if (mWorkspace.isInOverviewMode()) {
2264 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002265 }
2266 return;
2267 }
2268
2269 if (v instanceof CellLayout) {
2270 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002271 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002272 }
2273 }
2274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002276 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002278 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2279 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002280
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002281 // Check for special shortcuts
2282 if (intent.getComponent() != null) {
2283 final String shortcutClass = intent.getComponent().getClassName();
2284
2285 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002286 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002287 return;
2288 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2289 MemoryDumpActivity.startDump(this);
2290 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002291 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2292 toggleShowWeightWatcher();
2293 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002294 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002295 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002296
2297 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002298 int[] pos = new int[2];
2299 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002300 intent.setSourceBounds(new Rect(pos[0], pos[1],
2301 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002302
2303 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002304
Daniel Sandlerff02d492013-08-05 02:12:05 -04002305 mStats.recordLaunch(intent, shortcut);
2306
Michael Jurkaddd62e92011-02-16 17:49:14 -08002307 if (success && v instanceof BubbleTextView) {
2308 mWaitingForResume = (BubbleTextView) v;
2309 mWaitingForResume.setStayPressed(true);
2310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002312 if (v instanceof FolderIcon) {
2313 FolderIcon fi = (FolderIcon) v;
2314 handleFolderClick(fi);
2315 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002316 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002317 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002318 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002319 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002320 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 }
2323 }
2324
Michael Jurka0e260592010-06-30 17:07:39 -07002325 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002326 return false;
2327 }
2328
Michael Jurkaaf442092010-06-10 17:01:57 -07002329 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002330 * Event handler for the search button
2331 *
2332 * @param v The view that was clicked.
2333 */
2334 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002335 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2336
Amith Yamasania135ba82011-08-09 17:42:01 -07002337 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002338 }
2339
2340 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002341 * Event handler for the voice button
2342 *
2343 * @param v The view that was clicked.
2344 */
2345 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002346 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002347
Bjorn Bringertc459e522013-06-07 19:36:01 +01002348 startVoice();
2349 }
2350
2351 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002352 try {
2353 final SearchManager searchManager =
2354 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2355 ComponentName activityName = searchManager.getGlobalSearchActivity();
2356 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002357 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002358 if (activityName != null) {
2359 intent.setPackage(activityName.getPackageName());
2360 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002361 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002362 } catch (ActivityNotFoundException e) {
2363 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002364 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002365 startActivitySafely(null, intent, "onClickVoiceButton");
2366 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002367 }
2368
2369 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002370 * Event handler for the "grid" button that appears on the home screen, which
2371 * enters all apps mode.
2372 *
2373 * @param v The view that was clicked.
2374 */
2375 public void onClickAllAppsButton(View v) {
Winson Chung82963d52013-10-09 11:20:57 -07002376 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, true);
Michael Jurka5130e402011-10-13 04:55:35 -07002377 }
2378
2379 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002380 // Provide the same haptic feedback that the system offers for virtual keys.
2381 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002382 }
2383
Adam Cohen61f560d2013-09-30 15:58:20 -07002384 public void performHapticFeedbackOnTouchDown(View v) {
2385 // Provide the same haptic feedback that the system offers for virtual keys.
2386 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2387 }
2388
2389 public View.OnTouchListener getHapticFeedbackTouchListener() {
2390 if (mHapticFeedbackTouchListener == null) {
2391 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2392 @Override
2393 public boolean onTouch(View v, MotionEvent event) {
2394 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2395 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2396 }
2397 return false;
2398 }
2399 };
2400 }
2401 return mHapticFeedbackTouchListener;
2402 }
2403
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002404 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002405 if (!DISABLE_MARKET_BUTTON) {
2406 if (mAppMarketIntent != null) {
2407 startActivitySafely(v, mAppMarketIntent, "app market");
2408 } else {
2409 Log.e(TAG, "Invalid app market intent.");
2410 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002411 }
2412 }
2413
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002414 /**
2415 * Called when the user stops interacting with the launcher.
2416 * This implies that the user is now on the homescreen and is not doing housekeeping.
2417 */
2418 protected void onInteractionEnd() {}
2419
2420 /**
2421 * Called when the user starts interacting with the launcher.
2422 * The possible interactions are:
2423 * - open all apps
2424 * - reorder an app shortcut, or a widget
2425 * - open the overview mode.
2426 * This is a good time to stop doing things that only make sense
2427 * when the user is on the homescreen and not doing housekeeping.
2428 */
2429 protected void onInteractionBegin() {}
2430
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002431 void startApplicationDetailsActivity(ComponentName componentName) {
2432 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002433 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2434 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002435 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002436 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002437 }
2438
Michael Jurka1e2f4652013-07-08 18:03:46 -07002439 // returns true if the activity was started
2440 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002441 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002442 // System applications cannot be installed. For now, show a toast explaining that.
2443 // We may give them the option of disabling apps this way.
2444 int messageId = R.string.uninstall_system_app_text;
2445 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002446 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002447 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002448 String packageName = componentName.getPackageName();
2449 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002450 Intent intent = new Intent(
2451 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002452 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2453 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002454 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002455 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002456 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002457 }
2458
Michael Jurka86a720e2012-05-09 11:23:23 -07002459 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002463 // Only launch using the new animation if the shortcut has not opted out (this is a
2464 // private contract between launcher and may be ignored in the future).
2465 boolean useLaunchAnimation = (v != null) &&
2466 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2467 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002468 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2469 v.getMeasuredWidth(), v.getMeasuredHeight());
2470
2471 startActivity(intent, opts.toBundle());
2472 } else {
2473 startActivity(intent);
2474 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002475 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 } catch (SecurityException e) {
2477 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002478 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002480 "or use the exported attribute for this activity. "
2481 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002483 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002485
Michael Jurka86a720e2012-05-09 11:23:23 -07002486 boolean startActivitySafely(View v, Intent intent, Object tag) {
2487 boolean success = false;
2488 try {
2489 success = startActivity(v, intent, tag);
2490 } catch (ActivityNotFoundException e) {
2491 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2492 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2493 }
2494 return success;
2495 }
2496
Adam Cohena9cf38f2011-05-02 15:36:58 -07002497 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002498 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002499 Folder openFolder = mWorkspace.getFolderForTag(info);
2500
2501 // If the folder info reports that the associated folder is open, then verify that
2502 // it is actually opened. There have been a few instances where this gets out of sync.
2503 if (info.opened && openFolder == null) {
2504 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002505 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002506 info.opened = false;
2507 }
2508
Adam Cohenfb91f302012-06-11 15:45:18 -07002509 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 // Close any open folder
2511 closeFolder();
2512 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002513 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 } else {
2515 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 int folderScreen;
2517 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002518 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 // .. and close it
2520 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002521 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 // Close any folder open on the current screen
2523 closeFolder();
2524 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002525 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527 }
2528 }
2529 }
2530
Adam Cohen268c4752012-06-06 17:47:33 -07002531 /**
2532 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2533 * in the DragLayer in the exact absolute location of the original FolderIcon.
2534 */
2535 private void copyFolderIconToImage(FolderIcon fi) {
2536 final int width = fi.getMeasuredWidth();
2537 final int height = fi.getMeasuredHeight();
2538
2539 // Lazy load ImageView, Bitmap and Canvas
2540 if (mFolderIconImageView == null) {
2541 mFolderIconImageView = new ImageView(this);
2542 }
2543 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2544 mFolderIconBitmap.getHeight() != height) {
2545 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2546 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2547 }
2548
2549 DragLayer.LayoutParams lp;
2550 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2551 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2552 } else {
2553 lp = new DragLayer.LayoutParams(width, height);
2554 }
2555
Adam Cohen307fe232012-08-16 17:55:58 -07002556 // The layout from which the folder is being opened may be scaled, adjust the starting
2557 // view size by this scale factor.
2558 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002559 lp.customPosition = true;
2560 lp.x = mRectForFolderAnimation.left;
2561 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002562 lp.width = (int) (scale * width);
2563 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002564
2565 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2566 fi.draw(mFolderIconCanvas);
2567 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002568 if (fi.getFolder() != null) {
2569 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2570 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002571 }
Adam Cohen268c4752012-06-06 17:47:33 -07002572 // Just in case this image view is still in the drag layer from a previous animation,
2573 // we remove it and re-add it.
2574 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2575 mDragLayer.removeView(mFolderIconImageView);
2576 }
2577 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002578 if (fi.getFolder() != null) {
2579 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002580 }
Adam Cohen268c4752012-06-06 17:47:33 -07002581 }
2582
Adam Cohen2801caf2011-05-13 20:57:39 -07002583 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002584 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002585 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2586 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2587 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2588
Adam Cohenc51934b2011-07-26 21:07:43 -07002589 FolderInfo info = (FolderInfo) fi.getTag();
2590 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2591 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002592 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2593 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002594 }
2595
Adam Cohen268c4752012-06-06 17:47:33 -07002596 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2597 copyFolderIconToImage(fi);
2598 fi.setVisibility(View.INVISIBLE);
2599
Michael Jurka2ecf9952012-06-18 12:52:28 -07002600 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002601 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002602 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2603 oa.start();
2604 }
2605
Adam Cohen268c4752012-06-06 17:47:33 -07002606 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002607 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002608 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2609 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2610 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2611
Adam Cohen268c4752012-06-06 17:47:33 -07002612 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002613
Adam Cohen268c4752012-06-06 17:47:33 -07002614 // We remove and re-draw the FolderIcon in-case it has changed
2615 mDragLayer.removeView(mFolderIconImageView);
2616 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002617 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002618 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002619 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002620 oa.addListener(new AnimatorListenerAdapter() {
2621 @Override
2622 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002623 if (cl != null) {
2624 cl.clearFolderLeaveBehind();
2625 // Remove the ImageView copy of the FolderIcon and make the original visible.
2626 mDragLayer.removeView(mFolderIconImageView);
2627 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002628 }
2629 }
2630 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002631 oa.start();
2632 }
2633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002635 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 * is animated relative to the specified View. If the View is null, no animation
2637 * is played.
2638 *
2639 * @param folderInfo The FolderInfo describing the folder to open.
2640 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002641 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002642 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002643 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644
Adam Cohena9cf38f2011-05-02 15:36:58 -07002645 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646
Adam Cohen4554ee12011-08-03 16:13:21 -07002647 // Just verify that the folder hasn't already been added to the DragLayer.
2648 // There was a one-off crash where the folder had a parent already.
2649 if (folder.getParent() == null) {
2650 mDragLayer.addView(folder);
2651 mDragController.addDropTarget((DropTarget) folder);
2652 } else {
2653 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2654 folder.getParent() + ").");
2655 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002656 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002657 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002658
2659 // Notify the accessibility manager that this folder "window" has appeared and occluded
2660 // the workspace items
2661 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2662 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002663 }
2664
2665 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002666 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002667 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002668 if (folder.isEditingName()) {
2669 folder.dismissEditingName();
2670 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002671 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002672
2673 // Dismiss the folder cling
2674 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002675 }
2676 }
2677
2678 void closeFolder(Folder folder) {
2679 folder.getInfo().opened = false;
2680
2681 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2682 if (parent != null) {
2683 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2684 shrinkAndFadeInFolderIcon(fi);
2685 }
2686 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002687
2688 // Notify the accessibility manager that this folder "window" has disappeard and no
2689 // longer occludeds the workspace items
2690 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 }
2692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002694 if (!isDraggingEnabled()) return false;
2695 if (isWorkspaceLocked()) return false;
2696 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697
Adam Cohen1697b792013-09-17 19:08:21 -07002698 if (v instanceof Workspace) {
2699 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002700 if (mWorkspace.enterOverviewMode()) {
2701 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2702 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2703 return true;
2704 } else {
2705 return false;
2706 }
Adam Cohen1697b792013-09-17 19:08:21 -07002707 }
Adam Cohen1697b792013-09-17 19:08:21 -07002708 }
2709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002711 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 }
2713
Michael Jurka0280c3b2010-09-17 15:00:07 -07002714 resetAddInfo();
2715 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002717 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718 return true;
2719 }
2720
Winson Chung3d503fb2011-07-13 17:25:49 -07002721 // The hotseat touch handling does not go through Workspace, and we always allow long press
2722 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002723 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002724 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2725 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002726 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002727 // User long pressed on empty space
2728 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2729 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2730 // Disabling reordering until we sort out some issues.
2731 if (mWorkspace.isInOverviewMode()) {
2732 mWorkspace.startReordering(v);
2733 } else {
2734 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002736 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002737 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002739 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 }
2741 }
2742 }
2743 return true;
2744 }
2745
Winson Chung3d503fb2011-07-13 17:25:49 -07002746 boolean isHotseatLayout(View layout) {
2747 return mHotseat != null && layout != null &&
2748 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2749 }
2750 Hotseat getHotseat() {
2751 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002752 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002753 View getOverviewPanel() {
2754 return mOverviewPanel;
2755 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002756 SearchDropTargetBar getSearchBar() {
2757 return mSearchDropTargetBar;
2758 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002759
Winson Chung3d503fb2011-07-13 17:25:49 -07002760 /**
2761 * Returns the CellLayout of the specified container at the specified screen.
2762 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002763 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002764 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2765 if (mHotseat != null) {
2766 return mHotseat.getLayout();
2767 } else {
2768 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002769 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002770 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002771 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002772 }
2773 }
2774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 Workspace getWorkspace() {
2776 return mWorkspace;
2777 }
2778
Daniel Sandler843e8602010-06-07 14:59:01 -04002779 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002780 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002781 }
2782
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002783 /**
2784 * Helper method for the cameraZoomIn/cameraZoomOut animations
2785 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002786 * @param scaleFactor The scale factor used for the zoom
2787 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002788 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002789 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002790 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002791 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002792
Craig Mautner360310b2012-10-26 15:13:08 -07002793 private void setWorkspaceBackground(boolean workspace) {
2794 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002795 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002796 }
2797
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002798 void updateWallpaperVisibility(boolean visible) {
2799 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2800 int curflags = getWindow().getAttributes().flags
2801 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2802 if (wpflags != curflags) {
2803 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2804 }
Craig Mautner360310b2012-10-26 15:13:08 -07002805 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002806 }
2807
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002808 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2809 if (v instanceof LauncherTransitionable) {
2810 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2811 }
2812 }
2813
Michael Jurkabed61d22012-02-14 22:51:29 -08002814 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2815 if (v instanceof LauncherTransitionable) {
2816 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2817 }
Winson Chung70442722012-02-10 15:43:22 -08002818
2819 // Update the workspace transition step as well
2820 dispatchOnLauncherTransitionStep(v, 0f);
2821 }
2822
2823 private void dispatchOnLauncherTransitionStep(View v, float t) {
2824 if (v instanceof LauncherTransitionable) {
2825 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2826 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002827 }
2828
2829 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2830 if (v instanceof LauncherTransitionable) {
2831 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2832 }
Winson Chung70442722012-02-10 15:43:22 -08002833
2834 // Update the workspace transition step as well
2835 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002836 }
2837
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002838 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002839 * Things to test when changing the following seven functions.
2840 * - Home from workspace
2841 * - from center screen
2842 * - from other screens
2843 * - Home from all apps
2844 * - from center screen
2845 * - from other screens
2846 * - Back from all apps
2847 * - from center screen
2848 * - from other screens
2849 * - Launch app from workspace and quit
2850 * - with back
2851 * - with home
2852 * - Launch app from all apps and quit
2853 * - with back
2854 * - with home
2855 * - Go to a screen that's not the default, then all
2856 * apps, and launch and app, and go back
2857 * - with back
2858 * -with home
2859 * - On workspace, long press power and go back
2860 * - with back
2861 * - with home
2862 * - On all apps, long press power and go back
2863 * - with back
2864 * - with home
2865 * - On workspace, power off
2866 * - On all apps, power off
2867 * - Launch an app and turn off the screen while in that app
2868 * - Go back with home key
2869 * - Go back with back key TODO: make this not go to workspace
2870 * - From all apps
2871 * - From workspace
2872 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2873 * - From all apps
2874 * - From the center workspace
2875 * - From another workspace
2876 */
2877
2878 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002879 * Zoom the camera out from the workspace to reveal 'toView'.
2880 * Assumes that the view to show is anchored at either the very top or very bottom
2881 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002882 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002883 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002884 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2885 showAppsCustomizeHelper(animated, springLoaded, contentType);
2886 }
2887 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2888 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002889 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002890 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002891 mStateAnimation.cancel();
2892 mStateAnimation = null;
2893 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002894 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002895
Winson Chungf0ea4d32011-06-06 14:27:16 -07002896 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2897 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2898 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002899 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002900 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002901 final int startDelay =
2902 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002903
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002905
Michael Jurkad74c9842011-07-10 12:44:21 -07002906 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002907 Animator workspaceAnim =
2908 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Winson Chungc58497e2013-09-03 17:48:37 -07002909 if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
2910 // Set the content type for the all apps space
2911 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2912 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002913
2914 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002915 toView.setScaleX(scale);
2916 toView.setScaleY(scale);
2917 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2918 scaleAnim.
2919 scaleX(1f).scaleY(1f).
2920 setDuration(duration).
2921 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002922
Winson Chungf0ea4d32011-06-06 14:27:16 -07002923 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002924 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002925 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002926 .ofFloat(toView, "alpha", 0f, 1f)
2927 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002928 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002929 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2930 @Override
2931 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002932 if (animation == null) {
2933 throw new RuntimeException("animation is null");
2934 }
Winson Chung70442722012-02-10 15:43:22 -08002935 float t = (Float) animation.getAnimatedValue();
2936 dispatchOnLauncherTransitionStep(fromView, t);
2937 dispatchOnLauncherTransitionStep(toView, t);
2938 }
2939 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002940
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002941 // toView should appear right at the end of the workspace shrink
2942 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002943 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002944 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002945 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002946
2947 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002948 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002949 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002950 // Prepare the position
2951 toView.setTranslationX(0.0f);
2952 toView.setTranslationY(0.0f);
2953 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002954 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002955 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002956 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002957 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002958 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2959 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002960
Winson Chungc7d2b602012-05-16 17:02:20 -07002961 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002962 if (mSearchDropTargetBar != null) {
2963 mSearchDropTargetBar.hideSearchBar(false);
2964 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002965 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002966 });
2967
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002968 if (workspaceAnim != null) {
2969 mStateAnimation.play(workspaceAnim);
2970 }
Michael Jurka1899a362011-11-03 13:50:45 -07002971
2972 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002973
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002974 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2975 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002976
2977 // If any of the objects being animated haven't been measured/laid out
2978 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002979 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002980 (mWorkspace.getMeasuredWidth() == 0) ||
2981 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002982 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002983 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002984
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002985 final AnimatorSet stateAnimation = mStateAnimation;
2986 final Runnable startAnimRunnable = new Runnable() {
2987 public void run() {
2988 // Check that mStateAnimation hasn't changed while
2989 // we waited for a layout/draw pass
2990 if (mStateAnimation != stateAnimation)
2991 return;
2992 setPivotsForZoom(toView, scale);
2993 dispatchOnLauncherTransitionStart(fromView, animated, false);
2994 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002995 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002996 }
2997 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002998 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002999 final ViewTreeObserver observer = toView.getViewTreeObserver();
3000 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3001 public void onGlobalLayout() {
3002 startAnimRunnable.run();
3003 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3004 }
3005 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003006 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003007 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003008 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003009 } else {
3010 toView.setTranslationX(0.0f);
3011 toView.setTranslationY(0.0f);
3012 toView.setScaleX(1.0f);
3013 toView.setScaleY(1.0f);
3014 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003015 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003016
Daniel Sandlere4f98912013-06-25 15:13:26 -04003017 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003018 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003019 if (mSearchDropTargetBar != null) {
3020 mSearchDropTargetBar.hideSearchBar(false);
3021 }
Michael Jurkaabded662011-03-04 12:06:57 -08003022 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003023 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003024 dispatchOnLauncherTransitionStart(fromView, animated, false);
3025 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003026 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003027 dispatchOnLauncherTransitionStart(toView, animated, false);
3028 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003029 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003030 }
3031
3032 /**
3033 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003034 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003035 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003036 */
Adam Cohened307df2013-10-02 09:37:31 -07003037 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003038 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003039
Michael Jurkab3e22d92011-10-31 15:58:33 -07003040 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003041 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003042 mStateAnimation.cancel();
3043 mStateAnimation = null;
3044 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003045 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003046
Winson Chungf0ea4d32011-06-06 14:27:16 -07003047 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003048 final int fadeOutDuration =
3049 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003050 final float scaleFactor = (float)
3051 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3052 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003053 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003054 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003055 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003056 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3057 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003058 toState, animated, stagger, -1);
3059 } else if (toState == Workspace.State.SPRING_LOADED ||
3060 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003061 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003062 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003063 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003064
Michael Jurkab3e22d92011-10-31 15:58:33 -07003065 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003066 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003067 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003068 final LauncherViewPropertyAnimator scaleAnim =
3069 new LauncherViewPropertyAnimator(fromView);
3070 scaleAnim.
3071 scaleX(scaleFactor).scaleY(scaleFactor).
3072 setDuration(duration).
3073 setInterpolator(new Workspace.ZoomInInterpolator());
3074
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003075 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003076 .ofFloat(fromView, "alpha", 1f, 0f)
3077 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003078 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003079 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3080 @Override
3081 public void onAnimationUpdate(ValueAnimator animation) {
3082 float t = 1f - (Float) animation.getAnimatedValue();
3083 dispatchOnLauncherTransitionStep(fromView, t);
3084 dispatchOnLauncherTransitionStep(toView, t);
3085 }
3086 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003087
Michael Jurka2ecf9952012-06-18 12:52:28 -07003088 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003089
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003090 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3091 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003092 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003093
3094 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003095 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003096 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003097 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003098 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3099 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003100 if (onCompleteRunnable != null) {
3101 onCompleteRunnable.run();
3102 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003103 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003104 }
3105 });
3106
Winson Chungf0ea4d32011-06-06 14:27:16 -07003107 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003108 if (workspaceAnim != null) {
3109 mStateAnimation.play(workspaceAnim);
3110 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003111 dispatchOnLauncherTransitionStart(fromView, animated, true);
3112 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003113 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003114 } else {
3115 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003116 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003117 dispatchOnLauncherTransitionStart(fromView, animated, true);
3118 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003119 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003120 dispatchOnLauncherTransitionStart(toView, animated, true);
3121 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003122 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003123 }
3124
Michael Jurkae326f182011-11-21 14:05:46 -08003125 @Override
3126 public void onTrimMemory(int level) {
3127 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003128 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003129 mAppsCustomizeTabHost.onTrimMemory();
3130 }
3131 }
3132
Adam Cohened307df2013-10-02 09:37:31 -07003133 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003134 showWorkspace(animated, null);
3135 }
3136
Adam Cohened307df2013-10-02 09:37:31 -07003137 protected void showWorkspace() {
3138 showWorkspace(true);
3139 }
3140
Adam Cohened66b2b2012-01-23 17:28:51 -08003141 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003142 if (mWorkspace.isInOverviewMode()) {
3143 mWorkspace.exitOverviewMode(animated);
3144 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003145 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003146 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003147 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003148 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003149
Winson Chungc7d2b602012-05-16 17:02:20 -07003150 // Show the search bar (only animate if we were showing the drop target bar in spring
3151 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003152 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003153 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003154 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003155
Michael Jurkab3e22d92011-10-31 15:58:33 -07003156 // Set focus to the AppsCustomize button
3157 if (mAllAppsButton != null) {
3158 mAllAppsButton.requestFocus();
3159 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003160 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003161
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003162 // Change the state *after* we've called all the transition code
3163 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003164
Winson Chung5fb63472011-02-02 17:03:37 -08003165 // Resume the auto-advance of widgets
3166 mUserPresent = true;
3167 updateRunning();
3168
alanv1d4fde62012-10-17 13:15:47 -07003169 // Send an accessibility event to announce the context change
3170 getWindow().getDecorView()
3171 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003172
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003173 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003174 }
3175
Adam Cohened307df2013-10-02 09:37:31 -07003176 void showOverviewMode(boolean animated) {
3177 mWorkspace.setVisibility(View.VISIBLE);
3178 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3179 mState = State.WORKSPACE;
3180 onWorkspaceShown(animated);
3181 }
3182
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003183 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003184 }
3185
Winson Chung82963d52013-10-09 11:20:57 -07003186 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3187 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003188 if (mState != State.WORKSPACE) return;
3189
Winson Chung82963d52013-10-09 11:20:57 -07003190 if (resetPageToZero) {
3191 mAppsCustomizeTabHost.reset();
3192 }
Winson Chungc58497e2013-09-03 17:48:37 -07003193 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003194 mAppsCustomizeTabHost.requestFocus();
3195
Michael Jurkab3e22d92011-10-31 15:58:33 -07003196 // Change the state *after* we've called all the transition code
3197 mState = State.APPS_CUSTOMIZE;
3198
3199 // Pause the auto-advance of widgets until we are out of AllApps
3200 mUserPresent = false;
3201 updateRunning();
3202 closeFolder();
3203
3204 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003205 getWindow().getDecorView()
3206 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003207 }
3208
Adam Cohen7777d962011-08-18 18:58:38 -07003209 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003210 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003211 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003212 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003213 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003214 }
Adam Cohen7777d962011-08-18 18:58:38 -07003215
Adam Cohenad4e15c2013-10-17 16:21:35 -07003216 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003217 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003218 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3219
Winson Chunge7a03942011-08-05 15:05:12 -07003220 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003221 @Override
3222 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003223 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003224 // Before we show workspace, hide all apps again because
3225 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3226 // clean up our state transition functions
3227 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003228 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003229 } else {
3230 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003231 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003232 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003233 }, delay);
3234
Winson Chung557d6ed2011-07-08 15:34:52 -07003235 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003236
Michael Jurkad3ef3062010-11-23 16:23:58 -08003237 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003238 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003239 final boolean animated = true;
3240 final boolean springLoaded = true;
3241 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003242 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003243 }
3244 // Otherwise, we are not in spring loaded mode, so don't do anything.
3245 }
3246
Michael Jurkab3e22d92011-10-31 15:58:33 -07003247 void lockAllApps() {
3248 // TODO
3249 }
3250
3251 void unlockAllApps() {
3252 // TODO
3253 }
3254
Winson Chungf0ea4d32011-06-06 14:27:16 -07003255 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003256 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003257 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003258 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003259 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003260 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003261 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003262 int duration = 0;
3263 if (mSearchDropTargetBar != null) {
3264 duration = mSearchDropTargetBar.getTransitionInDuration();
3265 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003266 mHotseat.animate().alpha(1f).setDuration(duration);
3267 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003268 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003269 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003270 }
3271 }
3272 }
3273
3274 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003275 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003276 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003277 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003278 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003279 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003280 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003281 int duration = 0;
3282 if (mSearchDropTargetBar != null) {
3283 duration = mSearchDropTargetBar.getTransitionOutDuration();
3284 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003285 mHotseat.animate().alpha(0f).setDuration(duration);
3286 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003287 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003288 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003289 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003290 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003291 }
3292
Patrick Dubroy5f445422011-02-18 14:35:21 -08003293 /**
3294 * Add an item from all apps or customize onto the given workspace screen.
3295 * If layout is null, add to the current screen.
3296 */
3297 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003298 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003299 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003300 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003301 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003302
Winson Chungdff8ebb2011-09-08 17:25:31 -07003303 /** Maps the current orientation to an index for referencing orientation correct global icons */
3304 private int getCurrentOrientationIndexForGlobalIcons() {
3305 // default - 0, landscape - 1
3306 switch (getResources().getConfiguration().orientation) {
3307 case Configuration.ORIENTATION_LANDSCAPE:
3308 return 1;
3309 default:
3310 return 0;
3311 }
3312 }
3313
Michael Jurkaae65ee32012-04-30 11:45:54 -07003314 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003315 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003316 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003317 // Look for the toolbar icon specified in the activity meta-data
3318 Bundle metaData = packageManager.getActivityInfo(
3319 activityName, PackageManager.GET_META_DATA).metaData;
3320 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003321 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003322 if (iconResId != 0) {
3323 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003324 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003325 }
3326 }
3327 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003328 // This can happen if the activity defines an invalid drawable
3329 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3330 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003331 } catch (Resources.NotFoundException nfe) {
3332 // This can happen if the activity defines an invalid drawable
3333 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3334 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003335 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003336 return null;
3337 }
3338
3339 // if successful in getting icon, return it; otherwise, set button to use default drawable
3340 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003341 int buttonId, ComponentName activityName, int fallbackDrawableId,
3342 String toolbarResourceName) {
3343 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003344 Resources r = getResources();
3345 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3346 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003347
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003348 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003349 // If we were unable to find the icon via the meta-data, use a generic one
3350 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003351 toolbarIcon = r.getDrawable(fallbackDrawableId);
3352 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003353 if (button != null) {
3354 button.setCompoundDrawables(toolbarIcon, null, null, null);
3355 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003356 return null;
3357 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003358 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003359 if (button != null) {
3360 button.setCompoundDrawables(toolbarIcon, null, null, null);
3361 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003362 return toolbarIcon.getConstantState();
3363 }
3364 }
3365
3366 // if successful in getting icon, return it; otherwise, set button to use default drawable
3367 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003368 int buttonId, ComponentName activityName, int fallbackDrawableId,
3369 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003370 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003371 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003372
Michael Jurka19e0fc52011-07-22 18:00:21 -07003373 if (button != null) {
3374 // If we were unable to find the icon via the meta-data, use a
3375 // generic one
3376 if (toolbarIcon == null) {
3377 button.setImageResource(fallbackDrawableId);
3378 } else {
3379 button.setImageDrawable(toolbarIcon);
3380 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003381 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003382
3383 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3384
Michael Jurka0423dcf2010-10-05 14:56:18 -07003385 }
3386
Winson Chung1b884092012-06-08 17:13:02 -07003387 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003388 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003389 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003390 }
3391
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003392 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003393 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003394 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003395 }
3396
Winson Chungbb185bd2011-11-21 12:31:42 -08003397 private void invalidatePressedFocusedStates(View container, View button) {
3398 if (container instanceof HolographicLinearLayout) {
3399 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3400 layout.invalidatePressedFocusedStates();
3401 } else if (button instanceof HolographicImageView) {
3402 HolographicImageView view = (HolographicImageView) button;
3403 view.invalidatePressedFocusedStates();
3404 }
3405 }
3406
Cristina Stancu476493b2013-08-07 17:20:14 +01003407 public View getQsbBar() {
3408 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003409 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3410 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003411 }
3412 return mQsbBar;
3413 }
3414
3415 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003416 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003417 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003418 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003419 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003420
Winson Chung1cad91e2011-05-25 17:41:01 -07003421 final SearchManager searchManager =
3422 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3423 ComponentName activityName = searchManager.getGlobalSearchActivity();
3424 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003425 int coi = getCurrentOrientationIndexForGlobalIcons();
3426 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003427 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3428 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3429 if (sGlobalSearchIcon[coi] == null) {
3430 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3431 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3432 TOOLBAR_ICON_METADATA_NAME);
3433 }
3434
Winson Chungc51db6a2011-10-05 11:44:49 -07003435 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3436 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003437 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003438 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003439 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003440 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003441 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3442 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003443 if (searchButton != null) searchButton.setVisibility(View.GONE);
3444 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003445 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003446 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003447 }
3448 }
3449
Cristina Stancu476493b2013-08-07 17:20:14 +01003450 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003451 final View searchButtonContainer = findViewById(R.id.search_button_container);
3452 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003453 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003454 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003455 }
3456
Cristina Stancu476493b2013-08-07 17:20:14 +01003457 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003458 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003459 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003460
Winson Chungc51db6a2011-10-05 11:44:49 -07003461 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003462 final SearchManager searchManager =
3463 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3464 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3465
3466 ComponentName activityName = null;
3467 if (globalSearchActivity != null) {
3468 // Check if the global search activity handles voice search
3469 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3470 intent.setPackage(globalSearchActivity.getPackageName());
3471 activityName = intent.resolveActivity(getPackageManager());
3472 }
3473
3474 if (activityName == null) {
3475 // Fallback: check if an activity other than the global search activity
3476 // resolves this
3477 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3478 activityName = intent.resolveActivity(getPackageManager());
3479 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003480 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003481 int coi = getCurrentOrientationIndexForGlobalIcons();
3482 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003483 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3484 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3485 if (sVoiceSearchIcon[coi] == null) {
3486 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3487 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3488 TOOLBAR_ICON_METADATA_NAME);
3489 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003490 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003491 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003492 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003493 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003494 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003495 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003496 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003497 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003498 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003499 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003500 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003501 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003502
Cristina Stancu476493b2013-08-07 17:20:14 +01003503 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003504 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3505 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003506 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003507 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003508 }
3509
Winson Chung5841efa2013-09-30 18:06:44 -07003510 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003511 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3512 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003513 boolean visible = !forceDisableVoiceButtonProxy &&
3514 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003515 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003516 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003517 }
3518 }
Winson Chung5841efa2013-09-30 18:06:44 -07003519
3520 /**
3521 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3522 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3523 */
3524 public void disableVoiceButtonProxy(boolean disabled) {
3525 updateVoiceButtonProxyVisible(disabled);
3526 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003527 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003528 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003529 */
3530 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003531 if (!DISABLE_MARKET_BUTTON) {
3532 final View marketButton = findViewById(R.id.market_button);
3533 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3534 // Find the app market activity by resolving an intent.
3535 // (If multiple app markets are installed, it will return the ResolverActivity.)
3536 ComponentName activityName = intent.resolveActivity(getPackageManager());
3537 if (activityName != null) {
3538 int coi = getCurrentOrientationIndexForGlobalIcons();
3539 mAppMarketIntent = intent;
3540 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3541 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3542 TOOLBAR_ICON_METADATA_NAME);
3543 marketButton.setVisibility(View.VISIBLE);
3544 } else {
3545 // We should hide and disable the view so that we don't try and restore the visibility
3546 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3547 marketButton.setVisibility(View.GONE);
3548 marketButton.setEnabled(false);
3549 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003550 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003551 }
3552
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003553 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003554 if (!DISABLE_MARKET_BUTTON) {
3555 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3556 Resources r = getResources();
3557 Drawable marketIconDrawable = d.newDrawable(r);
3558 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3559 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3560 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003561
Winson Chungd64a6662013-09-30 11:06:59 -07003562 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3563 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003564 }
3565
Michael Jurkad7c28052012-04-27 15:43:36 -07003566 @Override
3567 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003568 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003569 final List<CharSequence> text = event.getText();
3570 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003571 // Populate event with a fake title based on the current state.
3572 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003573 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003574 } else {
3575 text.add(getString(R.string.all_apps_home_button_label));
3576 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003577 return result;
3578 }
3579
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003580 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003581 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003582 */
3583 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3584 @Override
3585 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003586 closeSystemDialogs();
3587 }
3588 }
3589
3590 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003591 * Receives notifications whenever the appwidgets are reset.
3592 */
3593 private class AppWidgetResetObserver extends ContentObserver {
3594 public AppWidgetResetObserver() {
3595 super(new Handler());
3596 }
3597
3598 @Override
3599 public void onChange(boolean selfChange) {
3600 onAppWidgetReset();
3601 }
3602 }
3603
3604 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 * If the activity is currently paused, signal that we need to run the passed Runnable
3606 * in onResume.
3607 *
3608 * This needs to be called from incoming places where resources might have been loaded
3609 * while we are paused. That is becaues the Configuration might be wrong
3610 * when we're not running, and if it comes back to what it was when we
3611 * were paused, we are not restarted.
3612 *
3613 * Implementation of the method from LauncherModel.Callbacks.
3614 *
3615 * @return true if we are currently paused. The caller might be able to
3616 * skip some work in that case since we will come back again.
3617 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003618 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003619 if (mPaused) {
3620 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003621 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003622 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003623 }
3624 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003625 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003626 return true;
3627 } else {
3628 return false;
3629 }
3630 }
3631
Michael Jurkac402cd92013-05-20 15:49:32 +02003632 private boolean waitUntilResume(Runnable run) {
3633 return waitUntilResume(run, false);
3634 }
3635
Michael Jurka1e2f4652013-07-08 18:03:46 -07003636 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003637 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003638 }
3639
Michael Jurka7607c2f2013-04-03 14:33:19 -07003640 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003641 * If the activity is currently paused, signal that we need to re-run the loader
3642 * in onResume.
3643 *
3644 * This needs to be called from incoming places where resources might have been loaded
3645 * while we are paused. That is becaues the Configuration might be wrong
3646 * when we're not running, and if it comes back to what it was when we
3647 * were paused, we are not restarted.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 *
3651 * @return true if we are currently paused. The caller might be able to
3652 * skip some work in that case since we will come back again.
3653 */
3654 public boolean setLoadOnResume() {
3655 if (mPaused) {
3656 Log.i(TAG, "setLoadOnResume");
3657 mOnResumeNeedsLoad = true;
3658 return true;
3659 } else {
3660 return false;
3661 }
3662 }
3663
3664 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003665 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003668 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003669 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003670 } else {
3671 return SCREEN_COUNT / 2;
3672 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003673 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003674
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675 /**
3676 * Refreshes the shortcuts shown on the workspace.
3677 *
3678 * Implementation of the method from LauncherModel.Callbacks.
3679 */
3680 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003681 // If we're starting binding all over again, clear any bind calls we'd postponed in
3682 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3683 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003685
Winson Chungd64d1762013-08-20 14:37:16 -07003686 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003687 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003688 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003689
Michael Jurka05bf644e2011-11-30 20:28:53 -08003690 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003691 if (mHotseat != null) {
3692 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003693 }
3694 }
3695
Adam Cohendcd297f2013-06-18 13:13:40 -07003696 @Override
3697 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003698 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003699
Adam Cohen5084cba2013-09-03 12:01:16 -07003700 // If there are no screens, we need to have an empty screen
3701 if (orderedScreenIds.size() == 0) {
3702 mWorkspace.addExtraEmptyScreen();
3703 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003704
3705 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003706 // setCurrentPage() so ensure that all pages are added before calling this).
3707 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003708 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3709 mWorkspace.createCustomContentPage();
3710 }
Winson Chung64359a52013-07-08 17:17:08 -07003711 }
3712
3713 @Override
3714 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003715 // Log to disk
3716 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3717 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3718 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003719 int count = orderedScreenIds.size();
3720 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003721 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003722 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003723 }
3724
Adam Cohen39a06042013-07-19 14:30:12 -07003725 private boolean shouldShowWeightWatcher() {
3726 String spKey = LauncherAppState.getSharedPreferencesKey();
3727 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003728 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003729
3730 return show;
3731 }
3732
3733 private void toggleShowWeightWatcher() {
3734 String spKey = LauncherAppState.getSharedPreferencesKey();
3735 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3736 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3737
3738 show = !show;
3739
3740 SharedPreferences.Editor editor = sp.edit();
3741 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3742 editor.commit();
3743
3744 if (mWeightWatcher != null) {
3745 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3746 }
3747 }
3748
Winson Chungd64d1762013-08-20 14:37:16 -07003749 public void bindAppsAdded(final ArrayList<Long> newScreens,
3750 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003751 final ArrayList<ItemInfo> addAnimated,
3752 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003753 Runnable r = new Runnable() {
3754 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003755 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003756 }
3757 };
3758 if (waitUntilResume(r)) {
3759 return;
3760 }
3761
Winson Chungd64d1762013-08-20 14:37:16 -07003762 // Add the new screens
3763 bindAddScreens(newScreens);
3764
3765 // We add the items without animation on non-visible pages, and with
3766 // animations on the new page (which we will try and snap to).
3767 if (!addNotAnimated.isEmpty()) {
3768 bindItems(addNotAnimated, 0,
3769 addNotAnimated.size(), false);
3770 }
3771 if (!addAnimated.isEmpty()) {
3772 bindItems(addAnimated, 0,
3773 addAnimated.size(), true);
3774 }
Winson Chungc58497e2013-09-03 17:48:37 -07003775
Winson Chung87412982013-10-03 18:34:14 -07003776 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003777 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003778
Winson Chungc58497e2013-09-03 17:48:37 -07003779 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3780 addedApps != null && mAppsCustomizeContent != null) {
3781 mAppsCustomizeContent.addApps(addedApps);
3782 }
Winson Chungd64d1762013-08-20 14:37:16 -07003783 }
3784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003785 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 * Bind the items start-end from the list.
3787 *
3788 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003789 */
Winson Chung64359a52013-07-08 17:17:08 -07003790 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3791 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003792 Runnable r = new Runnable() {
3793 public void run() {
3794 bindItems(shortcuts, start, end, forceAnimateIcons);
3795 }
3796 };
3797 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003798 return;
3799 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003800
Winson Chungf0c6ae02012-03-21 16:10:31 -07003801 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003802 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3803 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003804 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003805 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003806 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003807 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003809
3810 // Short circuit if we are loading dock items for a configuration which has no dock
3811 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3812 mHotseat == null) {
3813 continue;
3814 }
3815
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 switch (item.itemType) {
3817 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3818 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003819 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003820 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003821
Winson Chung64359a52013-07-08 17:17:08 -07003822 /*
3823 * TODO: FIX collision case
3824 */
Winson Chungce376632013-07-11 16:12:41 -07003825 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3826 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3827 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3828 throw new RuntimeException("OCCUPIED");
3829 }
Winson Chung64359a52013-07-08 17:17:08 -07003830 }
3831
Adam Cohendcd297f2013-06-18 13:13:40 -07003832 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3833 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003834 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003835 // Animate all the applications up now
3836 shortcut.setAlpha(0f);
3837 shortcut.setScaleX(0f);
3838 shortcut.setScaleY(0f);
3839 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003840 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003841 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003842 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003843 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003844 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003845 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003846 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003847 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3848 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003850 default:
3851 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003853 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003854
Winson Chung997a9232013-07-24 15:33:46 -07003855 if (animateIcons) {
3856 // Animate to the correct page
3857 if (newShortcutsScreenId > -1) {
3858 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003859 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003860 final Runnable startBounceAnimRunnable = new Runnable() {
3861 public void run() {
3862 anim.playTogether(bounceAnims);
3863 anim.start();
3864 }
3865 };
Winson Chung997a9232013-07-24 15:33:46 -07003866 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003867 // We post the animation slightly delayed to prevent slowdowns
3868 // when we are loading right after we return to launcher.
3869 mWorkspace.postDelayed(new Runnable() {
3870 public void run() {
3871 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003872 mWorkspace.postDelayed(startBounceAnimRunnable,
3873 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003874 }
3875 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003876 } else {
3877 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003878 }
3879 }
Winson Chung64359a52013-07-08 17:17:08 -07003880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003882 }
3883
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 /**
3885 * Implementation of the method from LauncherModel.Callbacks.
3886 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003887 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003888 Runnable r = new Runnable() {
3889 public void run() {
3890 bindFolders(folders);
3891 }
3892 };
3893 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003894 return;
3895 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003896 sFolders.clear();
3897 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003899
3900 /**
3901 * Add the views for a widget to the workspace.
3902 *
3903 * Implementation of the method from LauncherModel.Callbacks.
3904 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003905 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003906 Runnable r = new Runnable() {
3907 public void run() {
3908 bindAppWidget(item);
3909 }
3910 };
3911 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003912 return;
3913 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003914
Daniel Sandler843e8602010-06-07 14:59:01 -04003915 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3916 if (DEBUG_WIDGETS) {
3917 Log.d(TAG, "bindAppWidget: " + item);
3918 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 final Workspace workspace = mWorkspace;
3920
3921 final int appWidgetId = item.appWidgetId;
3922 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003923 if (DEBUG_WIDGETS) {
3924 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3925 }
3926
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3928
Joe Onorato9c1289c2009-08-17 11:03:03 -04003929 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003930 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003931
Adam Cohendcd297f2013-06-18 13:13:40 -07003932 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003933 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003934 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3935
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 workspace.requestLayout();
3937
Daniel Sandler843e8602010-06-07 14:59:01 -04003938 if (DEBUG_WIDGETS) {
3939 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3940 + (SystemClock.uptimeMillis()-start) + "ms");
3941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 }
3943
Adam Cohen1462de32012-07-24 22:34:36 -07003944 public void onPageBoundSynchronously(int page) {
3945 mSynchronouslyBoundPages.add(page);
3946 }
3947
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948 /**
3949 * Callback saying that there aren't any more items to bind.
3950 *
3951 * Implementation of the method from LauncherModel.Callbacks.
3952 */
Adam Cohene25af792013-06-06 23:08:25 -07003953 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003954 Runnable r = new Runnable() {
3955 public void run() {
3956 finishBindingItems(upgradePath);
3957 }
3958 };
3959 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003960 return;
3961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003962 if (mSavedState != null) {
3963 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003964 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 mSavedState = null;
3967 }
3968
Adam Cohen1462de32012-07-24 22:34:36 -07003969 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003971 // If we received the result of any pending adds while the loader was running (e.g. the
3972 // widget configuration forced an orientation change), process them now.
3973 for (int i = 0; i < sPendingAddList.size(); i++) {
3974 completeAdd(sPendingAddList.get(i));
3975 }
3976 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977
Winson Chungc51db6a2011-10-05 11:44:49 -07003978 // Update the market app icon as necessary (the other icons will be managed in response to
3979 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07003980 if (!DISABLE_MARKET_BUTTON) {
3981 updateAppMarketIcon();
3982 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07003983
Winson Chungf0c6ae02012-03-21 16:10:31 -07003984 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003985 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003986 mWorkspace.getUniqueComponents(true, null);
3987 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003988 }
Adam Cohen99894d92013-06-14 11:22:59 -07003989
Adam Cohen66a01fd2013-06-11 12:48:00 -07003990 mWorkspace.post(new Runnable() {
3991 @Override
3992 public void run() {
3993 onFinishBindingItems();
3994 }
3995 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003996 }
3997
Winson Chunga0b7e862013-09-05 16:03:15 -07003998 public boolean isAllAppsButtonRank(int rank) {
3999 if (mHotseat != null) {
4000 return mHotseat.isAllAppsButtonRank(rank);
4001 }
4002 return false;
4003 }
4004
Winson Chunga2413752012-04-03 14:22:34 -07004005 private boolean canRunNewAppsAnimation() {
4006 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4007 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4008 }
4009
Winson Chungc9168342013-06-26 14:54:55 -07004010 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4011 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4012 PropertyValuesHolder.ofFloat("alpha", 1f),
4013 PropertyValuesHolder.ofFloat("scaleX", 1f),
4014 PropertyValuesHolder.ofFloat("scaleY", 1f));
4015 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4016 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4017 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4018 return bounceAnim;
4019 }
4020
Adam Cohen27772732013-11-11 14:00:56 +00004021 public boolean useVerticalBarLayout() {
4022 return LauncherAppState.getInstance().getDynamicGrid().
4023 getDeviceProfile().isVerticalBarLayout();
4024 }
4025
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004026 @Override
4027 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004028 boolean searchVisible = updateGlobalSearchIcon();
4029 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004030 if (mSearchDropTargetBar != null) {
4031 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4032 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004033 }
4034
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004035 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 * Add the icons for all apps.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004040 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07004041 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
4042 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4043 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4044 getHotseat().addAllAppsFolder(mIconCache, apps,
4045 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4046 }
4047 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004048 }
Winson Chungc58497e2013-09-03 17:48:37 -07004049 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004050 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004051 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004052 mAppsCustomizeContent.onPackagesUpdated(
4053 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004054 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 }
4057
4058 /**
4059 * A package was updated.
4060 *
4061 * Implementation of the method from LauncherModel.Callbacks.
4062 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004063 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004064 Runnable r = new Runnable() {
4065 public void run() {
4066 bindAppsUpdated(apps);
4067 }
4068 };
4069 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004070 return;
4071 }
4072
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004073 if (mWorkspace != null) {
4074 mWorkspace.updateShortcuts(apps);
4075 }
Winson Chungc58497e2013-09-03 17:48:37 -07004076
4077 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4078 mAppsCustomizeContent != null) {
4079 mAppsCustomizeContent.updateApps(apps);
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
4083 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004084 * A package was uninstalled. We take both the super set of packageNames
4085 * in addition to specific applications to remove, the reason being that
4086 * this can be called when a package is updated as well. In that scenario,
4087 * we only remove specific components from the workspace, where as
4088 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 *
4090 * Implementation of the method from LauncherModel.Callbacks.
4091 */
Winson Chung83892cc2013-05-01 16:53:33 -07004092 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004093 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004094 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004095 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004096 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004097 }
Winson Chungd64d1762013-08-20 14:37:16 -07004098 };
4099 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004100 return;
4101 }
4102
Winson Chungdf95eb12013-10-16 14:57:07 -07004103 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004104 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004105 }
4106 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004107 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004108 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004109
Winson Chunga1820962011-10-03 16:31:06 -07004110 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004111 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004112
Winson Chungdf95eb12013-10-16 14:57:07 -07004113 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004114 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4115 mAppsCustomizeContent != null) {
4116 mAppsCustomizeContent.removeApps(appInfos);
4117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 }
Joe Onoratobe386092009-11-17 17:32:16 -08004119
4120 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004121 * A number of packages were updated.
4122 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004123 private ArrayList<Object> mWidgetsAndShortcuts;
4124 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004125 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004126 bindPackagesUpdated(mWidgetsAndShortcuts);
4127 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004128 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004129 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004130 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4131 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4132 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004133 return;
4134 }
4135
Winson Chung64359a52013-07-08 17:17:08 -07004136 // Update the widgets pane
Winson Chungc58497e2013-09-03 17:48:37 -07004137 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4138 mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004139 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004140 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004141 }
4142
Winson Chung400438b2011-01-16 17:53:48 -08004143 private int mapConfigurationOriActivityInfoOri(int configOri) {
4144 final Display d = getWindowManager().getDefaultDisplay();
4145 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4146 switch (d.getRotation()) {
4147 case Surface.ROTATION_0:
4148 case Surface.ROTATION_180:
4149 // We are currently in the same basic orientation as the natural orientation
4150 naturalOri = configOri;
4151 break;
4152 case Surface.ROTATION_90:
4153 case Surface.ROTATION_270:
4154 // We are currently in the other basic orientation to the natural orientation
4155 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4156 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4157 break;
4158 }
4159
4160 int[] oriMap = {
4161 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4162 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4163 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4164 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4165 };
4166 // Since the map starts at portrait, we need to offset if this device's natural orientation
4167 // is landscape.
4168 int indexOffset = 0;
4169 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4170 indexOffset = 1;
4171 }
4172 return oriMap[(d.getRotation() + indexOffset) % 4];
4173 }
Adam Cohen446e9402011-09-15 18:21:21 -07004174
Winson Chung4b919f82012-05-01 10:44:08 -07004175 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004176 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004177 getResources().getBoolean(R.bool.allow_rotation);
4178 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004179 }
Winson Chung4b919f82012-05-01 10:44:08 -07004180 public void lockScreenOrientation() {
4181 if (isRotationEnabled()) {
4182 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4183 .getConfiguration().orientation));
4184 }
4185 }
4186 public void unlockScreenOrientation(boolean immediate) {
4187 if (isRotationEnabled()) {
4188 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004189 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004190 } else {
4191 mHandler.postDelayed(new Runnable() {
4192 public void run() {
4193 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4194 }
4195 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004196 }
Winson Chung4b919f82012-05-01 10:44:08 -07004197 }
Winson Chung400438b2011-01-16 17:53:48 -08004198 }
4199
Winson Chung82f55532011-08-09 14:14:23 -07004200 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004201 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004202 if (DISABLE_CLINGS) {
4203 return false;
4204 }
4205
Winson Chung3a6e7f32013-10-09 15:50:52 -07004206 // For now, limit only to phones
4207 LauncherAppState app = LauncherAppState.getInstance();
4208 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4209 if (grid.isTablet()) {
4210 return false;
4211 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004212 if (grid.isLandscape) {
4213 return false;
4214 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004215
Brett Chabot2a9e2282011-08-23 15:03:13 -07004216 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004217 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004218
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004219 // Disable clings for accessibility when explore by touch is enabled
4220 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4221 ACCESSIBILITY_SERVICE);
4222 if (a11yManager.isTouchExplorationEnabled()) {
4223 return false;
4224 }
4225
Michael Jurkae233a8b2013-03-19 13:49:20 +01004226 // Restricted secondary users (child mode) will potentially have very few apps
4227 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004228// boolean supportsLimitedUsers =
4229// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4230// Account[] accounts = AccountManager.get(this).getAccounts();
4231// if (supportsLimitedUsers && accounts.length == 0) {
4232// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4233// Bundle restrictions = um.getUserRestrictions();
4234// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4235// return false;
4236// }
4237// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004238 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004239 }
Michael Jurka22143132012-10-04 17:42:38 +02004240
Winson Chungfa545132013-09-26 17:45:32 -07004241 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004242 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004243 Cling cling = (Cling) findViewById(clingId);
4244 View scrim = null;
4245 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004246 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004247 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004248 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004249 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004250 cling.show(animate, SHOW_CLING_DURATION);
4251
4252 if (dimNavBarVisibilty) {
4253 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4254 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004255 }
4256 }
4257 return cling;
4258 }
Michael Jurka22143132012-10-04 17:42:38 +02004259
Winson Chungaf40f202013-09-18 18:26:31 -07004260 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4261 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004262 // To catch cases where siblings of top-level views are made invisible, just check whether
4263 // the cling is directly set to GONE before dismissing it.
4264 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004265 final Runnable cleanUpClingCb = new Runnable() {
4266 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004267 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004268 // We should update the shared preferences on a background thread
4269 new Thread("dismissClingThread") {
4270 public void run() {
4271 SharedPreferences.Editor editor = mSharedPrefs.edit();
4272 editor.putBoolean(flag, true);
4273 editor.commit();
4274 }
4275 }.start();
Winson Chungaf40f202013-09-18 18:26:31 -07004276 if (postAnimationCb != null) {
4277 postAnimationCb.run();
4278 }
4279 }
4280 };
4281 if (duration <= 0) {
4282 cleanUpClingCb.run();
4283 } else {
4284 cling.hide(duration, cleanUpClingCb);
4285 }
Michael Jurka22143132012-10-04 17:42:38 +02004286 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004287
4288 if (restoreNavBarVisibilty) {
4289 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4290 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4291 }
Winson Chung82f55532011-08-09 14:14:23 -07004292 }
4293 }
Michael Jurka22143132012-10-04 17:42:38 +02004294
Winson Chung9d9d74f2011-09-19 11:49:12 -07004295 private void removeCling(int id) {
4296 final View cling = findViewById(id);
4297 if (cling != null) {
4298 final ViewGroup parent = (ViewGroup) cling.getParent();
4299 parent.post(new Runnable() {
4300 @Override
4301 public void run() {
4302 parent.removeView(cling);
4303 }
4304 });
Michael Jurka22143132012-10-04 17:42:38 +02004305 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004306 }
4307 }
Michael Jurka974c3862012-05-22 22:00:31 -07004308
4309 private boolean skipCustomClingIfNoAccounts() {
4310 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4311 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4312 if (customCling) {
4313 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004314 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004315 Account[] accounts = am.getAccountsByType("com.google");
4316 return accounts.length == 0;
4317 }
4318 return false;
4319 }
4320
Winson Chung82e5c982013-10-09 12:04:50 -07004321 public void updateCustomContentHintVisibility() {
4322 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4323 String ccHintStr = getFirstRunCustomContentHint();
4324
4325 if (mWorkspace.hasCustomContent()) {
4326 // Show the custom content hint if ccHintStr is not empty
4327 if (cling != null) {
4328 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4329 }
4330 } else {
4331 // Hide the custom content hint
4332 if (cling != null) {
4333 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4334 }
4335 }
4336 }
4337
4338 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4339 boolean animate) {
4340 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4341 if (ccHint != null) {
4342 if (visible && !ccHintStr.isEmpty()) {
4343 ccHint.setText(ccHintStr);
4344 ccHint.setVisibility(View.VISIBLE);
4345 if (animate) {
4346 ccHint.setAlpha(0f);
4347 ccHint.animate().alpha(1f)
4348 .setDuration(SHOW_CLING_DURATION)
4349 .start();
4350 } else {
4351 ccHint.setAlpha(1f);
4352 }
4353 } else {
4354 if (animate) {
4355 ccHint.animate().alpha(0f)
4356 .setDuration(SHOW_CLING_DURATION)
4357 .setListener(new AnimatorListenerAdapter() {
4358 @Override
4359 public void onAnimationEnd(Animator animation) {
4360 ccHint.setVisibility(View.GONE);
4361 }
4362 })
4363 .start();
4364 } else {
4365 ccHint.setAlpha(0f);
4366 ccHint.setVisibility(View.GONE);
4367 }
4368 }
4369 }
4370 }
4371
Winson Chungaf40f202013-09-18 18:26:31 -07004372 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004373 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004374 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004375 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004376 // If we're not using the default workspace layout, replace workspace cling
4377 // with a custom workspace cling (usually specified in an overlay)
4378 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004379 if (!DISABLE_CUSTOM_CLINGS) {
4380 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4381 getResources().getBoolean(R.bool.config_useCustomClings)) {
4382 // Use a custom cling
4383 View cling = findViewById(R.id.workspace_cling);
4384 ViewGroup clingParent = (ViewGroup) cling.getParent();
4385 int clingIndex = clingParent.indexOfChild(cling);
4386 clingParent.removeViewAt(clingIndex);
4387 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4388 clingParent.addView(customCling, clingIndex);
4389 customCling.setId(R.id.workspace_cling);
4390 }
4391 }
4392 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4393 if (cling != null) {
4394 String sbHintStr = getFirstRunClingSearchBarHint();
4395 String ccHintStr = getFirstRunCustomContentHint();
4396 if (!sbHintStr.isEmpty()) {
4397 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4398 sbHint.setText(sbHintStr);
4399 sbHint.setVisibility(View.VISIBLE);
4400 }
Winson Chung82e5c982013-10-09 12:04:50 -07004401 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004402 }
Winson Chungfa545132013-09-26 17:45:32 -07004403 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004404 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004405 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004406 }
4407 }
Winson Chungaf40f202013-09-18 18:26:31 -07004408
Winson Chunge6eabff2013-09-24 11:01:23 -07004409 protected String getFirstRunClingSearchBarHint() {
4410 return "";
4411 }
4412 protected String getFirstRunCustomContentHint() {
4413 return "";
4414 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004415 protected int getFirstRunFocusedHotseatAppDrawableId() {
4416 return -1;
4417 }
4418 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4419 return null;
4420 }
4421 protected int getFirstRunFocusedHotseatAppRank() {
4422 return -1;
4423 }
4424 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4425 return "";
4426 }
4427 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4428 return "";
4429 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004430
Winson Chungaf40f202013-09-18 18:26:31 -07004431 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004432 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004433 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004434 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004435 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4436
4437 // Set the focused hotseat app if there is one
4438 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4439 getFirstRunFocusedHotseatAppRank(),
4440 getFirstRunFocusedHotseatAppComponentName(),
4441 getFirstRunFocusedHotseatAppBubbleTitle(),
4442 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004443 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004444 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004445 }
4446 }
4447 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004448 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004449 if (isClingsEnabled() &&
4450 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004451 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4452 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004453 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004454 } else {
4455 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004456 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004457 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004458 }
Michael Jurka104c4562013-07-08 18:03:46 -07004459 protected SharedPreferences getSharedPrefs() {
4460 return mSharedPrefs;
4461 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004462 public boolean isFolderClingVisible() {
4463 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004464 if (cling != null) {
4465 return cling.getVisibility() == View.VISIBLE;
4466 }
4467 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004468 }
Winson Chungaf40f202013-09-18 18:26:31 -07004469 public void dismissFirstRunCling(View v) {
4470 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4471 Runnable cb = new Runnable() {
4472 public void run() {
4473 // Show the workspace cling next
4474 showFirstRunWorkspaceCling();
4475 }
4476 };
4477 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4478 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004479
4480 // Fade out the search bar for the workspace cling coming up
4481 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004482 }
Winson Chung82f55532011-08-09 14:14:23 -07004483 public void dismissWorkspaceCling(View v) {
4484 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004485 Runnable cb = null;
4486 if (v == null) {
4487 cb = new Runnable() {
4488 public void run() {
4489 mWorkspace.enterOverviewMode();
4490 }
4491 };
4492 }
4493 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4494 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004495
4496 // Fade in the search bar
4497 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004498 }
4499 public void dismissFolderCling(View v) {
4500 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004501 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4502 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004503 }
4504
Winson Chung80baf5a2010-08-09 16:03:15 -07004505 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004506 * Prints out out state for debugging.
4507 */
4508 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004509 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004510 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004511 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4512 Log.d(TAG, "mRestoring=" + mRestoring);
4513 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4514 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004515 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004516 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004517
Winson Chung785d2eb2011-04-14 16:08:02 -07004518 if (mAppsCustomizeContent != null) {
4519 mAppsCustomizeContent.dumpState();
4520 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004521 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004522 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004523
4524 @Override
4525 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4526 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004527 synchronized (sDumpLogs) {
4528 writer.println(" ");
4529 writer.println("Debug logs: ");
4530 for (int i = 0; i < sDumpLogs.size(); i++) {
4531 writer.println(" " + sDumpLogs.get(i));
4532 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004533 }
4534 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004535
4536 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004537 if (DEBUG_DUMP_LOG) {
4538 synchronized (sDumpLogs) {
4539 Log.d(TAG, "");
4540 Log.d(TAG, "*********************");
4541 Log.d(TAG, "Launcher debug logs: ");
4542 for (int i = 0; i < sDumpLogs.size(); i++) {
4543 Log.d(TAG, " " + sDumpLogs.get(i));
4544 }
4545 Log.d(TAG, "*********************");
4546 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004547 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004548 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004549 }
4550
4551 public static void addDumpLog(String tag, String log, boolean debugLog) {
4552 if (debugLog) {
4553 Log.d(tag, log);
4554 }
Winson Chungede41292013-09-19 16:27:36 -07004555 if (DEBUG_DUMP_LOG) {
4556 sDateStamp.setTime(System.currentTimeMillis());
4557 synchronized (sDumpLogs) {
4558 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
Adam Cohen4caf2982013-08-20 18:54:31 -07004559 }
Winson Chungede41292013-09-19 16:27:36 -07004560 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004561 }
4562
Winson Chungede41292013-09-19 16:27:36 -07004563 public void dumpLogsToLocalData() {
4564 if (DEBUG_DUMP_LOG) {
4565 new Thread("DumpLogsToLocalData") {
4566 @Override
4567 public void run() {
4568 boolean success = false;
4569 sDateStamp.setTime(sRunStart);
4570 String FILENAME = sDateStamp.getMonth() + "-"
4571 + sDateStamp.getDay() + "_"
4572 + sDateStamp.getHours() + "-"
4573 + sDateStamp.getMinutes() + "_"
4574 + sDateStamp.getSeconds() + ".txt";
4575
4576 FileOutputStream fos = null;
4577 File outFile = null;
4578 try {
4579 outFile = new File(getFilesDir(), FILENAME);
4580 outFile.createNewFile();
4581 fos = new FileOutputStream(outFile);
4582 } catch (Exception e) {
4583 e.printStackTrace();
4584 }
4585 if (fos != null) {
4586 PrintWriter writer = new PrintWriter(fos);
4587
4588 writer.println(" ");
4589 writer.println("Debug logs: ");
4590 synchronized (sDumpLogs) {
4591 for (int i = 0; i < sDumpLogs.size(); i++) {
4592 writer.println(" " + sDumpLogs.get(i));
4593 }
4594 }
4595 writer.close();
4596 }
4597 try {
4598 if (fos != null) {
4599 fos.close();
4600 success = true;
4601 }
4602 } catch (IOException e) {
4603 e.printStackTrace();
4604 }
4605 }
4606 }.start();
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004609}
Michael Jurka2763be32011-02-24 11:19:57 -08004610
Michael Jurkaabded662011-03-04 12:06:57 -08004611interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004612 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004613 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004614 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004615 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004616 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004617}