blob: e85567977f89ad852e1e60f6d0425eee79aaa612 [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;
79import android.view.MenuItem;
80import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070091import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.IOException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.InputStream;
111import java.io.OutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.util.Collections;
117import java.util.Comparator;
118import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700120import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122/**
123 * Default launcher application.
124 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100125public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700126 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700127 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800128 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Daniel Sandlerf061f822013-06-27 13:59:36 -0400131 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700133 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400134 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700135
Winson Chung70d72102011-08-12 11:24:35 -0700136 private static final int MENU_GROUP_WALLPAPER = 1;
137 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
138 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700139 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
140 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700143 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_PICK_APPLICATION = 6;
145 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
150
Mathew Inwood876a8462013-06-14 14:12:41 +0100151 /**
152 * IntentStarter uses request codes starting with this. This must be greater than all activity
153 * request codes used internally.
154 */
155 protected static final int REQUEST_LAST = 100;
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
158
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 static final int SCREEN_COUNT = 5;
160 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Romain Guy98d01652009-06-30 16:21:04 -0700162 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 // To turn on these properties, type
164 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
165 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
166 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100195 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700196 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100197 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700198 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100199 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700200
Adam Cohen39a06042013-07-19 14:30:12 -0700201 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700202 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700203
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700205 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700206 private State mState = State.WORKSPACE;
207 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700210 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
211 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700212 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700213 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800216 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Winson Chunga2413752012-04-03 14:22:34 -0700218 // How long to wait before the new-shortcut animation automatically pans the workspace
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700220 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private LayoutInflater mInflater;
227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800230 private DragLayer mDragLayer;
231 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700232 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private AppWidgetManager mAppWidgetManager;
235 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Michael Jurkac9d95c52011-08-29 14:03:34 -0700237 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238 private AppWidgetProviderInfo mPendingAddWidgetInfo;
239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private FolderInfo mFolderInfo;
243
Winson Chung3d503fb2011-07-13 17:25:49 -0700244 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700245 private View mOverviewPanel;
246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700248
Winson Chungc51db6a2011-10-05 11:44:49 -0700249 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700250 private AppsCustomizeTabHost mAppsCustomizeTabHost;
251 private AppsCustomizePagedView mAppsCustomizeContent;
252 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100253 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700256 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
257 // scroll issues (because the workspace may not have been measured yet) and extra work.
258 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
259 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
261 private SpannableStringBuilder mDefaultKeySsb = null;
262
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 private boolean mWorkspaceLoading = true;
264
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800265 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private boolean mRestoring;
267 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700268 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
272
Winson Chung4a2afa32012-07-19 14:53:05 -0700273 // Keep track of whether the user has left launcher
274 private static boolean sPausedFromUserAction = false;
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedInstanceState;
277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800280 private boolean mUserPresent = true;
281 private boolean mVisible = false;
282 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700283 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700285 private static LocaleConfiguration sLocaleConfiguration = null;
286
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700287 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700289 private Intent mAppMarketIntent = null;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
293 private final int mAdvanceInterval = 20000;
294 private final int mAdvanceStagger = 250;
295 private long mAutoAdvanceSentTime;
296 private long mAutoAdvanceTimeLeft = -1;
297 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
298 new HashMap<View, AppWidgetProviderInfo>();
299
Winson Chung400438b2011-01-16 17:53:48 -0800300 // Determines how long to wait after a rotation before restoring the screen orientation to
301 // match the sensor state.
302 private final int mRestoreScreenOrientationDelay = 500;
303
Michael Jurka4ef207b2010-11-29 17:05:45 -0800304 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700305 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
306 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
307 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800308
Craig Mautner360310b2012-10-26 15:13:08 -0700309 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
312
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700313 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700314 static Date sDateStamp = new Date();
315 static DateFormat sDateFormat =
316 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
317 static long sRunStart = System.currentTimeMillis();
318 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319
Winson Chung46353de2012-02-16 14:05:10 -0800320 // We only want to get the SharedPreferences once since it does an FS stat each time we get
321 // it from the context.
322 private SharedPreferences mSharedPrefs;
323
Adam Cohene25af792013-06-06 23:08:25 -0700324 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700328 private ImageView mFolderIconImageView;
329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Michael Jurka22143132012-10-04 17:42:38 +0200335 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
336 = new HideFromAccessibilityHelper();
337
Michael Jurkac1f5d262011-09-30 19:32:27 -0700338 private Runnable mBuildLayersRunnable = new Runnable() {
339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800346 private static ArrayList<PendingAddArguments> sPendingAddList
347 = new ArrayList<PendingAddArguments>();
348
Michael Jurka0a457bf2012-11-19 14:05:05 -0800349 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
350
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800351 private static class PendingAddArguments {
352 int requestCode;
353 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700354 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700355 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int cellX;
357 int cellY;
358 }
359
Daniel Sandlerff02d492013-08-05 02:12:05 -0400360 private Stats mStats;
361
Michael Jurka0a457bf2012-11-19 14:05:05 -0800362 private static boolean isPropertyEnabled(String propertyName) {
363 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 @Override
367 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700368 if (DEBUG_STRICT_MODE) {
369 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
370 .detectDiskReads()
371 .detectDiskWrites()
372 .detectNetwork() // or .detectAll() for all detectable problems
373 .penaltyLog()
374 .build());
375 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
376 .detectLeakedSqlLiteObjects()
377 .detectLeakedClosableObjects()
378 .penaltyLog()
379 .penaltyDeath()
380 .build());
381 }
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400384
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400385 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400386 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700387
388 // Determine the dynamic grid properties
389 Point smallestSize = new Point();
390 Point largestSize = new Point();
391 Point realSize = new Point();
392 Display display = getWindowManager().getDefaultDisplay();
393 display.getCurrentSizeRange(smallestSize, largestSize);
394 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700395 DisplayMetrics dm = new DisplayMetrics();
396 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700397 // Lazy-initialize the dynamic grid
398 DeviceProfile grid = app.initDynamicGrid(this,
399 Math.min(smallestSize.x, smallestSize.y),
400 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700401 realSize.x, realSize.y,
402 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700403
404 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400405 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700406 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400409 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700411
Daniel Sandlerff02d492013-08-05 02:12:05 -0400412 mStats = new Stats(this);
413
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700414 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
416 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
419 // this also ensures that any synchronous binding below doesn't re-trigger another
420 // LauncherModel load.
421 mPaused = false;
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200424 android.os.Debug.startMethodTracing(
425 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
428 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setContentView(R.layout.launcher);
430 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100431 grid.layout(this);
Winson Chung7d7541e2011-09-16 20:14:36 -0700432 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800434 registerContentObservers();
435
Joe Onorato7c312c12009-08-13 21:36:53 -0700436 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 mSavedState = savedInstanceState;
439 restoreState(mSavedState);
440
Winson Chunga12a2502010-12-20 14:41:35 -0800441 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700442 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200443 mAppsCustomizeContent.onPackagesUpdated(
444 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700445 }
Winson Chunga12a2502010-12-20 14:41:35 -0800446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (PROFILE_STARTUP) {
448 android.os.Debug.stopMethodTracing();
449 }
450
451 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700452 if (sPausedFromUserAction) {
453 // If the user leaves launcher, then we should just load items asynchronously when
454 // they return.
455 mModel.startLoader(true, -1);
456 } else {
457 // We only load the page synchronously if the user rotates (or triggers a
458 // configuration change) while launcher is in the foreground
459 mModel.startLoader(true, mWorkspace.getCurrentPage());
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462
463 // For handling default keys
464 mDefaultKeySsb = new SpannableStringBuilder();
465 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800466
467 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
468 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800469
Adam Cohenf9426d52012-06-04 17:26:21 -0700470 updateGlobalIcons();
471
472 // On large interfaces, we want the screen to auto-rotate based on the current orientation
473 unlockScreenOrientation(true);
474 }
475
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 protected void onUserLeaveHint() {
477 super.onUserLeaveHint();
478 sPausedFromUserAction = true;
479 }
480
Winson Chung98ca0f72013-07-29 12:58:51 -0700481 /** To be overriden by subclasses to hint to Launcher that we have custom content */
482 protected boolean hasCustomContentToLeft() {
483 return false;
484 }
485
Adam Cohenf9426d52012-06-04 17:26:21 -0700486 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700487 boolean searchVisible = false;
488 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800489 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700490 int coi = getCurrentOrientationIndexForGlobalIcons();
491 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
492 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700493 updateAppMarketIcon();
494 searchVisible = updateGlobalSearchIcon();
495 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700496 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700497 if (sGlobalSearchIcon[coi] != null) {
498 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700499 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700500 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700501 if (sVoiceSearchIcon[coi] != null) {
502 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700503 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700504 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700505 if (sAppMarketIcon[coi] != null) {
506 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800507 }
Winson Chungadf0c182012-08-23 14:59:07 -0700508 if (mSearchDropTargetBar != null) {
509 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 }
Romain Guycbb89e42009-06-08 15:52:54 -0700512
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700514 if (sLocaleConfiguration == null) {
515 new AsyncTask<Void, Void, LocaleConfiguration>() {
516 @Override
517 protected LocaleConfiguration doInBackground(Void... unused) {
518 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
519 readConfiguration(Launcher.this, localeConfiguration);
520 return localeConfiguration;
521 }
522
523 @Override
524 protected void onPostExecute(LocaleConfiguration result) {
525 sLocaleConfiguration = result;
526 checkForLocaleChange(); // recursive, but now with a locale configuration
527 }
528 }.execute();
529 return;
530 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700531
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 final Configuration configuration = getResources().getConfiguration();
533
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700534 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 final String locale = configuration.locale.toString();
536
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700537 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 final int mcc = configuration.mcc;
539
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700540 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 final int mnc = configuration.mnc;
542
Romain Guy84f296c2009-11-04 15:00:44 -0800543 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544
Romain Guy84f296c2009-11-04 15:00:44 -0800545 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700546 sLocaleConfiguration.locale = locale;
547 sLocaleConfiguration.mcc = mcc;
548 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700549
Joe Onorato0589f0f2010-02-08 13:44:00 -0800550 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700551
552 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
553 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700554 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700555 public void run() {
556 writeConfiguration(Launcher.this, localeConfiguration);
557 }
558 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700559 }
560 }
561
562 private static class LocaleConfiguration {
563 public String locale;
564 public int mcc = -1;
565 public int mnc = -1;
566 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700567
Romain Guy98d01652009-06-30 16:21:04 -0700568 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
569 DataInputStream in = null;
570 try {
571 in = new DataInputStream(context.openFileInput(PREFERENCES));
572 configuration.locale = in.readUTF();
573 configuration.mcc = in.readInt();
574 configuration.mnc = in.readInt();
575 } catch (FileNotFoundException e) {
576 // Ignore
577 } catch (IOException e) {
578 // Ignore
579 } finally {
580 if (in != null) {
581 try {
582 in.close();
583 } catch (IOException e) {
584 // Ignore
585 }
586 }
587 }
588 }
589
590 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
591 DataOutputStream out = null;
592 try {
593 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
594 out.writeUTF(configuration.locale);
595 out.writeInt(configuration.mcc);
596 out.writeInt(configuration.mnc);
597 out.flush();
598 } catch (FileNotFoundException e) {
599 // Ignore
600 } catch (IOException e) {
601 //noinspection ResultOfMethodCallIgnored
602 context.getFileStreamPath(PREFERENCES).delete();
603 } finally {
604 if (out != null) {
605 try {
606 out.close();
607 } catch (IOException e) {
608 // Ignore
609 }
610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612 }
613
Bjorn Bringertc459e522013-06-07 19:36:01 +0100614 public LayoutInflater getInflater() {
615 return mInflater;
616 }
617
Adam Cohen716b51e2011-06-30 12:09:54 -0700618 public DragLayer getDragLayer() {
619 return mDragLayer;
620 }
621
Winson Chung36a62fe2012-05-06 18:04:42 -0700622 boolean isDraggingEnabled() {
623 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
624 // that is subsequently removed from the workspace in startBinding().
625 return !mModel.isLoadingWorkspace();
626 }
627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 static int getScreen() {
629 synchronized (sLock) {
630 return sScreen;
631 }
632 }
633
634 static void setScreen(int screen) {
635 synchronized (sLock) {
636 sScreen = screen;
637 }
638 }
639
Winson Chung557d6ed2011-07-08 15:34:52 -0700640 /**
641 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
642 * a configuration step, this allows the proper animations to run after other transitions.
643 */
644 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700645 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800646 switch (args.requestCode) {
647 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800650 break;
651 case REQUEST_PICK_SHORTCUT:
652 processShortcut(args.intent);
653 break;
654 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700655 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700657 result = true;
658 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 case REQUEST_CREATE_APPWIDGET:
660 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700661 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700662 result = true;
663 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 case REQUEST_PICK_WALLPAPER:
665 // We just wanted the activity result here so we can clear mWaitingForResult
666 break;
667 }
Michael Jurka27614d22012-04-02 06:40:22 -0700668 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
669 // if you turned the screen off and then back while in All Apps, Launcher would not
670 // return to the workspace. Clearing mAddInfo.container here fixes this issue
671 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700672 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 }
674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 protected void onActivityResult(
677 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700678 // Reset the startActivity waiting flag
679 mWaitingForResult = false;
680
Michael Jurka8b805b12012-04-18 14:23:14 -0700681 if (requestCode == REQUEST_BIND_APPWIDGET) {
682 int appWidgetId = data != null ?
683 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
684 if (resultCode == RESULT_CANCELED) {
685 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
686 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700688 }
689 return;
690 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700691 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
693 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700694
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 // We have special handling for widgets
696 if (isWidgetDrop) {
697 int appWidgetId = data != null ?
698 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700699 if (appWidgetId < 0) {
700 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
701 "widget configuration activity.");
702 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
703 } else {
704 completeTwoStageWidgetDrop(resultCode, appWidgetId);
705 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800706 return;
707 }
708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 // The pattern used here is that a user PICKs a specific application,
710 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
713 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700714 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 final PendingAddArguments args = new PendingAddArguments();
716 args.requestCode = requestCode;
717 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700718 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700720 args.cellX = mPendingAddInfo.cellX;
721 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 if (isWorkspaceLocked()) {
723 sPendingAddList.add(args);
724 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700725 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700729 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
731 null);
732 }
733
734 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 Runnable onCompleteRunnable = null;
738 int animationType = 0;
739
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 if (resultCode == RESULT_OK) {
742 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
743 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 mPendingAddWidgetInfo);
745 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 onCompleteRunnable = new Runnable() {
747 @Override
748 public void run() {
749 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
752 null);
753 }
754 };
755 } else if (resultCode == RESULT_CANCELED) {
756 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
757 onCompleteRunnable = new Runnable() {
758 @Override
759 public void run() {
760 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
761 null);
762 }
763 };
764 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 if (mDragLayer.getAnimatedView() != null) {
766 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
767 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
768 animationType, boundWidget, true);
769 } else {
770 // The animated view may be null in the case of a rotation during widget configuration
771 onCompleteRunnable.run();
772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774
775 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700776 protected void onStop() {
777 super.onStop();
778 FirstFrameAnimatorHelper.setIsVisible(false);
779 }
780
781 @Override
782 protected void onStart() {
783 super.onStart();
784 FirstFrameAnimatorHelper.setIsVisible(true);
785 }
786
787 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200789 long startTime = 0;
790 if (DEBUG_RESUME_TIME) {
791 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400792 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700795
Winson Chung4a2afa32012-07-19 14:53:05 -0700796 // Restore the previous launcher state
797 if (mOnResumeState == State.WORKSPACE) {
798 showWorkspace(false);
799 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
800 showAllApps(false);
801 }
802 mOnResumeState = State.NONE;
803
Craig Mautner360310b2012-10-26 15:13:08 -0700804 // Background was set to gradient in onPause(), restore to black if in all apps.
805 setWorkspaceBackground(mState == State.WORKSPACE);
806
Winson Chungde0fb8f2012-05-08 14:37:08 -0700807 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700808 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700809
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800810 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700811 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700812 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700814 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700816 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700818 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200819 // We might have postponed some bind calls until onResume (see waitUntilResume) --
820 // execute them here
821 long startTimeCallbacks = 0;
822 if (DEBUG_RESUME_TIME) {
823 startTimeCallbacks = System.currentTimeMillis();
824 }
825
826 if (mAppsCustomizeContent != null) {
827 mAppsCustomizeContent.setBulkBind(true);
828 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700829 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
830 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200831 }
832 if (mAppsCustomizeContent != null) {
833 mAppsCustomizeContent.setBulkBind(false);
834 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700835 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200836 if (DEBUG_RESUME_TIME) {
837 Log.d(TAG, "Time spent processing callbacks in onResume: " +
838 (System.currentTimeMillis() - startTimeCallbacks));
839 }
Michael Jurka447bf842013-05-15 14:52:15 +0200840 }
Michael Jurka54554252013-08-01 12:52:23 +0200841 if (mOnResumeCallbacks.size() > 0) {
842 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
843 mOnResumeCallbacks.get(i).run();
844 }
845 mOnResumeCallbacks.clear();
846 }
Winson Chunge4e50662012-01-23 14:45:13 -0800847
848 // Reset the pressed state of icons that were locked in the press state while activities
849 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800850 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800851 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800852 mWaitingForResume.setStayPressed(false);
853 }
Winson Chunge4e50662012-01-23 14:45:13 -0800854 if (mAppsCustomizeContent != null) {
855 // Resets the previous all apps icon press state
856 mAppsCustomizeContent.resetDrawableState();
857 }
Adam Cohen06dff352012-06-01 17:17:08 -0700858 // It is possible that widgets can receive updates while launcher is not in the foreground.
859 // Consequently, the widgets will be inflated in the orientation of the foreground activity
860 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
861 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700862 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700863
864 // Again, as with the above scenario, it's possible that one or more of the global icons
865 // were updated in the wrong orientation.
866 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200867 if (DEBUG_RESUME_TIME) {
868 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
869 }
Adam Cohen4caf2982013-08-20 18:54:31 -0700870
871 // Write all the logs to disk
872 Launcher.addDumpLog(TAG, "10249126 - onResume() - dumping logs to disk", true);
873 dumpLogsToLocalData(false);
Adam Cohen06dff352012-06-01 17:17:08 -0700874 }
875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700877 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700878 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
879 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
880 // when Launcher resumes and we are still in AllApps.
881 updateWallpaperVisibility(true);
882
Winson Chung997a9232013-07-24 15:33:46 -0700883 // Ensure that items added to Launcher are queued until Launcher returns
884 InstallShortcutReceiver.enableInstallQueue();
885
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700887 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800888 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700889 mDragController.resetLastGestureUpTime();
Adam Cohen4caf2982013-08-20 18:54:31 -0700890
891 // Write all the logs to disk
892 Launcher.addDumpLog(TAG, "10249126 - onPause() - dumping logs to disk", true);
893 dumpLogsToLocalData(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 }
Romain Guycbb89e42009-06-08 15:52:54 -0700895
Adam Cohen66a01fd2013-06-11 12:48:00 -0700896 protected void onFinishBindingItems() {
897 }
898
Adam Cohenbffe7452013-07-22 18:21:45 -0700899 QSBScroller mQsbScroller = new QSBScroller() {
900 int scrollY = 0;
901
902 @Override
903 public void setScrollY(int scroll) {
904 scrollY = scroll;
905
906 if (mWorkspace.isOnOrMovingToCustomContent()) {
907 mSearchDropTargetBar.setTranslationY(- scrollY);
908 }
909 }
910 };
911
912 public void resetQSBScroll() {
913 mSearchDropTargetBar.animate().translationY(0).start();
914 }
915
916 public interface CustomContentCallbacks {
917 // Custom content is completely shown
918 public void onShow();
919
920 // Custom content is completely hidden
921 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700922
923 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
924 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700925 }
926
927 public interface QSBScroller {
928 public void setScrollY(int scrollY);
929 }
930
Adam Cohen66a01fd2013-06-11 12:48:00 -0700931 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700932 public QSBScroller addToCustomContentPage(View customContent) {
933 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700934 }
935
Winson Chung98ca0f72013-07-29 12:58:51 -0700936 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700937 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700938 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700939 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700940 }
941
Adam Cohenedb40762013-07-18 16:45:45 -0700942 // The custom content needs to offset its content to account for the QSB
943 public int getTopOffsetForCustomContent() {
944 return mWorkspace.getPaddingTop();
945 }
946
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700947 @Override
948 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 // Flag the loader to stop early before switching
950 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700951 if (mAppsCustomizeContent != null) {
952 mAppsCustomizeContent.surrender();
953 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800954 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700955 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800957 // We can't hide the IME if it was forced open. So don't bother
958 /*
959 @Override
960 public void onWindowFocusChanged(boolean hasFocus) {
961 super.onWindowFocusChanged(hasFocus);
962
963 if (hasFocus) {
964 final InputMethodManager inputManager = (InputMethodManager)
965 getSystemService(Context.INPUT_METHOD_SERVICE);
966 WindowManager.LayoutParams lp = getWindow().getAttributes();
967 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
968 android.os.Handler()) {
969 protected void onReceiveResult(int resultCode, Bundle resultData) {
970 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
971 }
972 });
973 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
974 }
975 }
976 */
977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 private boolean acceptFilter() {
979 final InputMethodManager inputManager = (InputMethodManager)
980 getSystemService(Context.INPUT_METHOD_SERVICE);
981 return !inputManager.isFullscreenMode();
982 }
983
984 @Override
985 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700986 final int uniChar = event.getUnicodeChar();
987 final boolean handled = super.onKeyDown(keyCode, event);
988 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
989 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
991 keyCode, event);
992 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700993 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700994 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700995 // showSearchDialog()
996 // If there are multiple keystrokes before the search dialog takes focus,
997 // onSearchRequested() will be called for every keystroke,
998 // but it is idempotent, so it's fine.
999 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001 }
1002
Joe Onorato8a9625e2010-01-28 15:55:35 -08001003 // Eat the long press event so the keyboard doesn't come up.
1004 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1005 return true;
1006 }
1007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 return handled;
1009 }
1010
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 private String getTypedText() {
1012 return mDefaultKeySsb.toString();
1013 }
1014
1015 private void clearTypedText() {
1016 mDefaultKeySsb.clear();
1017 mDefaultKeySsb.clearSpans();
1018 Selection.setSelection(mDefaultKeySsb, 0);
1019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001022 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1023 * State
1024 */
1025 private static State intToState(int stateOrdinal) {
1026 State state = State.WORKSPACE;
1027 final State[] stateValues = State.values();
1028 for (int i = 0; i < stateValues.length; i++) {
1029 if (stateValues[i].ordinal() == stateOrdinal) {
1030 state = stateValues[i];
1031 break;
1032 }
1033 }
1034 return state;
1035 }
1036
1037 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 * Restores the previous state, if it exists.
1039 *
1040 * @param savedState The previous state.
1041 */
1042 private void restoreState(Bundle savedState) {
1043 if (savedState == null) {
1044 return;
1045 }
1046
Michael Jurka883f55b2010-10-21 15:47:14 -07001047 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001048 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001049 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001050 }
1051
Winson Chungf0c6ae02012-03-21 16:10:31 -07001052 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001054 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001059
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1061 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1064 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001065 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1066 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1067 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001068 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 mRestoring = true;
1070 }
1071
1072 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1073 if (renameFolder) {
1074 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001075 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 mRestoring = true;
1077 }
Winson Chunga12a2502010-12-20 14:41:35 -08001078
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 // Restore the AppsCustomize tab
1080 if (mAppsCustomizeTabHost != null) {
1081 String curTab = savedState.getString("apps_customize_currentTab");
1082 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001083 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001084 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001085 mAppsCustomizeContent.loadAssociatedPages(
1086 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001087 }
1088
Winson Chung5afbf7b2011-07-25 11:53:08 -07001089 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1090 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
1094 /**
1095 * Finds all the views we need and configure them properly.
1096 */
1097 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001098 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001099
Craig Mautner360310b2012-10-26 15:13:08 -07001100 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001101 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1102 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001103
1104 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1105 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106
Winson Chung4c98d922011-05-31 16:50:48 -07001107 // Setup the drag layer
1108 mDragLayer.setup(this, dragController);
1109
Winson Chung3d503fb2011-07-13 17:25:49 -07001110 // Setup the hotseat
1111 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1112 if (mHotseat != null) {
1113 mHotseat.setup(this);
1114 }
1115
Adam Cohenf358a4b2013-07-23 16:47:31 -07001116 mOverviewPanel = findViewById(R.id.overview_panel);
1117 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1118 @Override
1119 public void onClick(View arg0) {
1120 showAllApps(true);
1121 }
1122 });
1123 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1124 @Override
1125 public void onClick(View arg0) {
1126 startWallpaper();
1127 }
1128 });
1129
Winson Chung4c98d922011-05-31 16:50:48 -07001130 // Setup the workspace
1131 mWorkspace.setHapticFeedbackEnabled(false);
1132 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001133 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001134 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001135
Winson Chungf0ea4d32011-06-06 14:27:16 -07001136 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001137 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001138
Winson Chungf0ea4d32011-06-06 14:27:16 -07001139 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001140 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001141 mAppsCustomizeContent = (AppsCustomizePagedView)
1142 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1143 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001144
Winson Chung3d503fb2011-07-13 17:25:49 -07001145 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001146 dragController.setDragScoller(mWorkspace);
1147 dragController.setScrollView(mDragLayer);
1148 dragController.setMoveTarget(mWorkspace);
1149 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001150 if (mSearchDropTargetBar != null) {
1151 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001152 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001153
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001154 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001155 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001156 mWeightWatcher = new WeightWatcher(this);
1157 mWeightWatcher.setAlpha(0.5f);
1158 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001159 new FrameLayout.LayoutParams(
1160 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001161 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001162 Gravity.BOTTOM)
1163 );
Adam Cohen39a06042013-07-19 14:30:12 -07001164
1165 boolean show = shouldShowWeightWatcher();
1166 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
1170 /**
1171 * Creates a view representing a shortcut.
1172 *
1173 * @param info The data structure describing the shortcut.
1174 *
1175 * @return A View inflated from R.layout.application.
1176 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001177 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001179 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181
1182 /**
1183 * Creates a view representing a shortcut inflated from the specified resource.
1184 *
1185 * @param layoutResId The id of the XML layout used to create the shortcut.
1186 * @param parent The group the shortcut belongs to.
1187 * @param info The data structure describing the shortcut.
1188 *
1189 * @return A View inflated from layoutResId.
1190 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001191 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001192 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1193 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 return favorite;
1196 }
1197
1198 /**
1199 * Add an application shortcut to the workspace.
1200 *
1201 * @param data The intent describing the application.
1202 * @param cellInfo The position on screen where to create the shortcut.
1203 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001204 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001206 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207
Adam Cohenfbba09b2011-07-18 21:43:05 -07001208 // First we check if we already know the exact location where we want to add this item.
1209 if (cellX >= 0 && cellY >= 0) {
1210 cellXY[0] = cellX;
1211 cellXY[1] = cellY;
1212 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001213 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 return;
1215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001217 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001218
Romain Guy73b979d2009-06-09 12:57:21 -07001219 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001220 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001222 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001223 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001224 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001225 } else {
1226 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228 }
Romain Guycbb89e42009-06-08 15:52:54 -07001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 /**
1231 * Add a shortcut to the workspace.
1232 *
1233 * @param data The intent describing the shortcut.
1234 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001236 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001237 int cellY) {
1238 int[] cellXY = mTmpAddItemCellCoordinates;
1239 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001240 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001241
Michael Jurkad3ef3062010-11-23 16:23:58 -08001242 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001243
Adam Cohen558baaf2011-08-15 15:22:57 -07001244 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001245 if (info == null) {
1246 return;
1247 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001248 final View view = createShortcut(info);
1249
Adam Cohenfbba09b2011-07-18 21:43:05 -07001250 // First we check if we already know the exact location where we want to add this item.
1251 if (cellX >= 0 && cellY >= 0) {
1252 cellXY[0] = cellX;
1253 cellXY[1] = cellY;
1254 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001255
1256 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001257 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001258 true, null,null)) {
1259 return;
1260 }
1261 DragObject dragObject = new DragObject();
1262 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001263 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1264 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001265 return;
1266 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001267 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001268 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001269 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001270 foundCellSpan = (result != null);
1271 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001272 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001273 }
1274
1275 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001276 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001277 return;
1278 }
1279
Adam Cohendcd297f2013-06-18 13:13:40 -07001280 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281
1282 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001283 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001284 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286 }
1287
Adam Cohen2f093b62012-04-30 18:59:53 -07001288 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1289 int minHeight) {
1290 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001291 // We want to account for the extra amount of padding that we are adding to the widget
1292 // to ensure that it gets the full amount of space that it has requested
1293 int requiredWidth = minWidth + padding.left + padding.right;
1294 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001295 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001296 }
1297
Adam Cohen2f093b62012-04-30 18:59:53 -07001298 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1299 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001300 }
1301
Adam Cohen2f093b62012-04-30 18:59:53 -07001302 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1303 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001304 }
1305
Adam Cohen2f093b62012-04-30 18:59:53 -07001306 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1307 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001308 }
1309
Adam Cohen2f093b62012-04-30 18:59:53 -07001310 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1311 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001312 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001313 }
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001316 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001318 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001319 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001321 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001322 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1323 if (appWidgetInfo == null) {
1324 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1325 }
Romain Guycbb89e42009-06-08 15:52:54 -07001326
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001327 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001328 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001329
Adam Cohen2f093b62012-04-30 18:59:53 -07001330 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1331 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001334 // We have saved the position to which the widget was dragged-- this really only matters
1335 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001336 int[] cellXY = mTmpAddItemCellCoordinates;
1337 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001338 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001339 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1341 cellXY[0] = mPendingAddInfo.cellX;
1342 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001343 spanXY[0] = mPendingAddInfo.spanX;
1344 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001345 foundCellSpan = true;
1346 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001348 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001349 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1350 spanXY[1], cellXY, finalSpan);
1351 spanXY[0] = finalSpan[0];
1352 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001353 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001354 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001355 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001356 }
1357
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001359 if (appWidgetId != -1) {
1360 // Deleting an app widget ID is a void call but writes to disk before returning
1361 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001362 new Thread("deleteAppWidgetId") {
1363 public void run() {
1364 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1365 }
1366 }.start();
1367 }
Winson Chung93eef082012-03-23 15:59:27 -07001368 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001369 return;
1370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001372 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001373 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1374 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001375 launcherInfo.spanX = spanXY[0];
1376 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001377 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1378 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001381 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
1383 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001384 if (hostView == null) {
1385 // Perform actual inflation because we're live
1386 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1387 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1388 } else {
1389 // The AppWidgetHostView has already been inflated and instantiated
1390 launcherInfo.hostView = hostView;
1391 }
Romain Guycbb89e42009-06-08 15:52:54 -07001392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001394 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001395 launcherInfo.notifyWidgetSizeChanged(this);
1396
Adam Cohendcd297f2013-06-18 13:13:40 -07001397 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001398 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001399
1400 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001402 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
Romain Guycbb89e42009-06-08 15:52:54 -07001404
Adam Cohended9f8d2010-11-03 13:25:16 -07001405 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1406 @Override
1407 public void onReceive(Context context, Intent intent) {
1408 final String action = intent.getAction();
1409 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1410 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001411 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001412 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001413
Winson Chungc100e8e2011-08-09 16:02:43 -07001414 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001415 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001416 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1417 mAppsCustomizeTabHost.reset();
1418 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001419 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001420 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1421 mUserPresent = true;
1422 updateRunning();
1423 }
1424 }
1425 };
1426
1427 @Override
1428 public void onAttachedToWindow() {
1429 super.onAttachedToWindow();
1430
1431 // Listen for broadcasts related to user-presence
1432 final IntentFilter filter = new IntentFilter();
1433 filter.addAction(Intent.ACTION_SCREEN_OFF);
1434 filter.addAction(Intent.ACTION_USER_PRESENT);
1435 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001436 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001437 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001438 mVisible = true;
1439 }
1440
1441 @Override
1442 public void onDetachedFromWindow() {
1443 super.onDetachedFromWindow();
1444 mVisible = false;
1445
Adam Cohend113e0c2010-11-11 10:48:05 -08001446 if (mAttached) {
1447 unregisterReceiver(mReceiver);
1448 mAttached = false;
1449 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001450 updateRunning();
1451 }
1452
1453 public void onWindowVisibilityChanged(int visibility) {
1454 mVisible = visibility == View.VISIBLE;
1455 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001456 // The following code used to be in onResume, but it turns out onResume is called when
1457 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1458 // is a more appropriate event to handle
1459 if (mVisible) {
1460 mAppsCustomizeTabHost.onWindowVisible();
1461 if (!mWorkspaceLoading) {
1462 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001463 // We want to let Launcher draw itself at least once before we force it to build
1464 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001465 // apps is nice and speedy.
1466 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001467 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001468 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001469 if (mStarted) return;
1470 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001471 // We delay the layer building a bit in order to give
1472 // other message processing a time to run. In particular
1473 // this avoids a delay in hiding the IME if it was
1474 // currently shown, because doing that may involve
1475 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001476 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001477 final ViewTreeObserver.OnDrawListener listener = this;
1478 mWorkspace.post(new Runnable() {
1479 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001480 if (mWorkspace != null &&
1481 mWorkspace.getViewTreeObserver() != null) {
1482 mWorkspace.getViewTreeObserver().
1483 removeOnDrawListener(listener);
1484 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001485 }
1486 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001487 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001488 }
1489 });
1490 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001491 // When Launcher comes back to foreground, a different Activity might be responsible for
1492 // the app market intent, so refresh the icon
1493 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001494 clearTypedText();
1495 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001496 }
1497
1498 private void sendAdvanceMessage(long delay) {
1499 mHandler.removeMessages(ADVANCE_MSG);
1500 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1501 mHandler.sendMessageDelayed(msg, delay);
1502 mAutoAdvanceSentTime = System.currentTimeMillis();
1503 }
1504
1505 private void updateRunning() {
1506 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1507 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1508 mAutoAdvanceRunning = autoAdvanceRunning;
1509 if (autoAdvanceRunning) {
1510 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1511 sendAdvanceMessage(delay);
1512 } else {
1513 if (!mWidgetsToAdvance.isEmpty()) {
1514 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1515 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1516 }
1517 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001518 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001519 }
1520 }
1521 }
1522
1523 private final Handler mHandler = new Handler() {
1524 @Override
1525 public void handleMessage(Message msg) {
1526 if (msg.what == ADVANCE_MSG) {
1527 int i = 0;
1528 for (View key: mWidgetsToAdvance.keySet()) {
1529 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1530 final int delay = mAdvanceStagger * i;
1531 if (v instanceof Advanceable) {
1532 postDelayed(new Runnable() {
1533 public void run() {
1534 ((Advanceable) v).advance();
1535 }
1536 }, delay);
1537 }
1538 i++;
1539 }
1540 sendAdvanceMessage(mAdvanceInterval);
1541 }
1542 }
1543 };
1544
1545 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001546 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001547 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1548 if (v instanceof Advanceable) {
1549 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001550 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001551 updateRunning();
1552 }
1553 }
1554
1555 void removeWidgetToAutoAdvance(View hostView) {
1556 if (mWidgetsToAdvance.containsKey(hostView)) {
1557 mWidgetsToAdvance.remove(hostView);
1558 updateRunning();
1559 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001560 }
1561
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001563 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 launcherInfo.hostView = null;
1565 }
1566
Winson Chung93eef082012-03-23 15:59:27 -07001567 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1568 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1569 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001570 }
1571
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001572 public LauncherAppWidgetHost getAppWidgetHost() {
1573 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 }
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Winson Chunga9abd0e2010-10-27 17:18:37 -07001576 public LauncherModel getModel() {
1577 return mModel;
1578 }
1579
Bjorn Bringertc459e522013-06-07 19:36:01 +01001580 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001581 getWindow().closeAllPanels();
1582
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001583 // Whatever we were doing is hereby canceled.
1584 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001585 }
1586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 @Override
1588 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001589 long startTime = 0;
1590 if (DEBUG_RESUME_TIME) {
1591 startTime = System.currentTimeMillis();
1592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 super.onNewIntent(intent);
1594
1595 // Close the menu
1596 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001597 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001598 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001599
Jamie Genniscb222e82012-10-23 15:44:26 -07001600 final boolean alreadyOnHome =
1601 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001602 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001603
Jamie Genniscb222e82012-10-23 15:44:26 -07001604 Runnable processIntent = new Runnable() {
1605 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001606 if (mWorkspace == null) {
1607 // Can be cases where mWorkspace is null, this prevents a NPE
1608 return;
1609 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001610 Folder openFolder = mWorkspace.getOpenFolder();
1611 // In all these cases, only animate if we're already on home
1612 mWorkspace.exitWidgetResizeMode();
1613 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1614 openFolder == null) {
1615 mWorkspace.moveToDefaultScreen(true);
1616 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001617
Jamie Genniscb222e82012-10-23 15:44:26 -07001618 closeFolder();
1619 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001620
Jamie Genniscb222e82012-10-23 15:44:26 -07001621 // If we are already on home, then just animate back to the workspace,
1622 // otherwise, just wait until onResume to set the state back to Workspace
1623 if (alreadyOnHome) {
1624 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001625 if (mWorkspace.isInOverviewMode()) {
1626 mWorkspace.exitOverviewMode();
1627 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001628 } else {
1629 mOnResumeState = State.WORKSPACE;
1630 }
1631
1632 final View v = getWindow().peekDecorView();
1633 if (v != null && v.getWindowToken() != null) {
1634 InputMethodManager imm = (InputMethodManager)getSystemService(
1635 INPUT_METHOD_SERVICE);
1636 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1637 }
1638
1639 // Reset AllApps to its initial state
1640 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1641 mAppsCustomizeTabHost.reset();
1642 }
1643 }
1644 };
1645
1646 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1647 // Delay processing of the intent to allow the status bar animation to finish
1648 // first in order to avoid janky animations.
1649 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001650 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001651 // Process the intent immediately.
1652 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001653 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 }
Michael Jurka447bf842013-05-15 14:52:15 +02001656 if (DEBUG_RESUME_TIME) {
1657 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 }
1660
1661 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001662 public void onRestoreInstanceState(Bundle state) {
1663 super.onRestoreInstanceState(state);
1664 for (int page: mSynchronouslyBoundPages) {
1665 mWorkspace.restoreInstanceStateForChild(page);
1666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
1668
1669 @Override
1670 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001671 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001672 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673
Michael Jurka883f55b2010-10-21 15:47:14 -07001674 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001675 // We close any open folder since it will not be re-opened, and we need to make sure
1676 // this state is reflected.
1677 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678
Adam Cohendcd297f2013-06-18 13:13:40 -07001679 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 mWaitingForResult) {
1681 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001682 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001683 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1684 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001685 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1686 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1687 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
1689
1690 if (mFolderInfo != null && mWaitingForResult) {
1691 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1692 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1693 }
Michael Jurka946ad472010-07-09 18:05:18 -07001694
Winson Chung785d2eb2011-04-14 16:08:02 -07001695 // Save the current AppsCustomize tab
1696 if (mAppsCustomizeTabHost != null) {
1697 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1698 if (currentTabTag != null) {
1699 outState.putString("apps_customize_currentTab", currentTabTag);
1700 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001701 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1702 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705
1706 @Override
1707 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Winson Chunge7a03942011-08-05 15:05:12 -07001710 // Remove all pending runnables
1711 mHandler.removeMessages(ADVANCE_MSG);
1712 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001713 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001714
Winson Chungcd2b0142011-06-08 16:02:26 -07001715 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001716 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001717 mModel.stopLoader();
1718 app.setLauncher(null);
1719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001721 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001723 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001725 mAppWidgetHost = null;
1726
1727 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
1729 TextKeyListener.getInstance().release();
1730
Winson Chung81b52252012-08-27 15:34:29 -07001731 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1732 // to prevent leaking Launcher activities on orientation change.
1733 if (mModel != null) {
1734 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1735 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001736
1737 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001738 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001739
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001740 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001741 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1742 mWorkspace.removeAllViews();
1743 mWorkspace = null;
1744 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001745
Michael Jurka2ecf9952012-06-18 12:52:28 -07001746 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 }
1748
Adam Cohena9cf38f2011-05-02 15:36:58 -07001749 public DragController getDragController() {
1750 return mDragController;
1751 }
1752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 @Override
1754 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001755 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 super.startActivityForResult(intent, requestCode);
1757 }
1758
Winson Chung70d72102011-08-12 11:24:35 -07001759 /**
1760 * Indicates that we want global search for this activity by setting the globalSearch
1761 * argument for {@link #startSearch} to true.
1762 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001764 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001766
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001767 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001768
Karl Rosaen138a0412009-04-23 19:00:21 -07001769 if (initialQuery == null) {
1770 // Use any text typed in the launcher as the initial query
1771 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 if (appSearchData == null) {
1774 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001775 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 }
Winson Chungadf0c182012-08-23 14:59:07 -07001777 Rect sourceBounds = new Rect();
1778 if (mSearchDropTargetBar != null) {
1779 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1780 }
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Bjorn Bringertc459e522013-06-07 19:36:01 +01001782 startSearch(initialQuery, selectInitialQuery,
1783 appSearchData, sourceBounds);
1784 }
1785
1786 public void startSearch(String initialQuery,
1787 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001788 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001789 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001790 }
1791
1792 /**
1793 * Starts the global search activity. This code is a copied from SearchManager
1794 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001795 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001796 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001797 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001798 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1799 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1800 if (globalSearchActivity == null) {
1801 Log.w(TAG, "No global search activity found.");
1802 return;
1803 }
1804 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1805 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1806 intent.setComponent(globalSearchActivity);
1807 // Make sure that we have a Bundle to put source in
1808 if (appSearchData == null) {
1809 appSearchData = new Bundle();
1810 } else {
1811 appSearchData = new Bundle(appSearchData);
1812 }
1813 // Set source to package name of app that starts global search, if not set already.
1814 if (!appSearchData.containsKey("source")) {
1815 appSearchData.putString("source", getPackageName());
1816 }
1817 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1818 if (!TextUtils.isEmpty(initialQuery)) {
1819 intent.putExtra(SearchManager.QUERY, initialQuery);
1820 }
1821 if (selectInitialQuery) {
1822 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1823 }
1824 intent.setSourceBounds(sourceBounds);
1825 try {
1826 startActivity(intent);
1827 } catch (ActivityNotFoundException ex) {
1828 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 }
1831
Winson Chung70d72102011-08-12 11:24:35 -07001832 @Override
1833 public boolean onCreateOptionsMenu(Menu menu) {
1834 if (isWorkspaceLocked()) {
1835 return false;
1836 }
1837
1838 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001839
1840 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1841 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1842 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001843 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1844 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1845 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001846 String helpUrl = getString(R.string.help_url);
1847 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001848 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1849 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1850
Winson Chung70d72102011-08-12 11:24:35 -07001851 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1852 .setIcon(android.R.drawable.ic_menu_gallery)
1853 .setAlphabeticShortcut('W');
1854 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1855 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001856 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001857 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001858 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1859 .setIcon(android.R.drawable.ic_menu_preferences)
1860 .setIntent(settings)
1861 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001862 if (!helpUrl.isEmpty()) {
1863 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1864 .setIcon(android.R.drawable.ic_menu_help)
1865 .setIntent(help)
1866 .setAlphabeticShortcut('H');
1867 }
Winson Chung70d72102011-08-12 11:24:35 -07001868 return true;
1869 }
1870
1871 @Override
1872 public boolean onPrepareOptionsMenu(Menu menu) {
1873 super.onPrepareOptionsMenu(menu);
1874
1875 if (mAppsCustomizeTabHost.isTransitioning()) {
1876 return false;
1877 }
1878 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1879 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1880
1881 return true;
1882 }
1883
1884 @Override
1885 public boolean onOptionsItemSelected(MenuItem item) {
1886 switch (item.getItemId()) {
1887 case MENU_WALLPAPER_SETTINGS:
1888 startWallpaper();
1889 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001890 }
1891
1892 return super.onOptionsItemSelected(item);
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001895 @Override
1896 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001897 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001898 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001899 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001900 }
1901
Joe Onorato9c1289c2009-08-17 11:03:03 -04001902 public boolean isWorkspaceLocked() {
1903 return mWorkspaceLoading || mWaitingForResult;
1904 }
1905
Michael Jurka0280c3b2010-09-17 15:00:07 -07001906 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001908 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1910 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001911 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001913 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001914
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001915 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1916 AppWidgetProviderInfo appWidgetInfo) {
1917 if (appWidgetInfo.configure != null) {
1918 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001919
Bjorn Bringert7984c942009-12-09 15:38:25 +00001920 // Launch over to configure widget, if needed
1921 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001922 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001923 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001924 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001926 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001928 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001929 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001930 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932 }
Romain Guycbb89e42009-06-08 15:52:54 -07001933
Adam Cohenfbba09b2011-07-18 21:43:05 -07001934 /**
1935 * Process a shortcut drop.
1936 *
1937 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001939 * @param cell The cell it should be added to, optional
1940 * @param position The location on the screen where it was dropped, optional
1941 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001942 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001943 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001944 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001948
1949 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001950 mPendingAddInfo.cellX = cell[0];
1951 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001952 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001953
1954 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1955 createShortcutIntent.setComponent(componentName);
1956 processShortcut(createShortcutIntent);
1957 }
1958
Adam Cohenfbba09b2011-07-18 21:43:05 -07001959 /**
1960 * Process a widget drop.
1961 *
1962 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001964 * @param cell The cell it should be added to, optional
1965 * @param position The location on the screen where it was dropped, optional
1966 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001968 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001969 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001971 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001972 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001973 mPendingAddInfo.minSpanX = info.minSpanX;
1974 mPendingAddInfo.minSpanY = info.minSpanY;
1975
Adam Cohenfbba09b2011-07-18 21:43:05 -07001976 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001977 mPendingAddInfo.cellX = cell[0];
1978 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001979 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001980 if (span != null) {
1981 mPendingAddInfo.spanX = span[0];
1982 mPendingAddInfo.spanY = span[1];
1983 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001984
Adam Cohened66b2b2012-01-23 17:28:51 -08001985 AppWidgetHostView hostView = info.boundWidget;
1986 int appWidgetId;
1987 if (hostView != null) {
1988 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001989 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001990 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001991 // In this case, we either need to start an activity to get permission to bind
1992 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001993 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001994 Bundle options = info.bindOptions;
1995
1996 boolean success = false;
1997 if (options != null) {
1998 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1999 info.componentName, options);
2000 } else {
2001 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2002 info.componentName);
2003 }
2004 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002005 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002006 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002007 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002008 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2009 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2010 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002011 // TODO: we need to make sure that this accounts for the options bundle.
2012 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002013 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2014 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002015 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002016 }
2017
Joe Onoratodeb98af2010-02-19 14:59:39 -08002018 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002019 // Handle case where user selected "Applications"
2020 String applicationName = getResources().getString(R.string.group_applications);
2021 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002023 if (applicationName != null && applicationName.equals(shortcutName)) {
2024 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2025 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002026
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002027 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2028 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002029 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002030 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002031 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002032 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
Winson Chung24ab2f12010-09-16 14:10:47 -07002036 void processWallpaper(Intent intent) {
2037 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2038 }
2039
Adam Cohendcd297f2013-06-18 13:13:40 -07002040 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002041 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002042 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 folderInfo.title = getText(R.string.folder_name);
2044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002047 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002048 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049
2050 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002051 FolderIcon newFolder =
2052 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002053 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002054 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002055 // Force measure the new folder icon
2056 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2057 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002058 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
Romain Guycbb89e42009-06-08 15:52:54 -07002060
Joe Onorato9c1289c2009-08-17 11:03:03 -04002061 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002062 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063 }
2064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002066 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002068 pickWallpaper.setComponent(
2069 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2070 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002073 /**
2074 * Registers various content observers. The current implementation registers
2075 * only a favorites observer to keep track of the favorites applications.
2076 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002077 private void registerContentObservers() {
2078 ContentResolver resolver = getContentResolver();
2079 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2080 true, mWidgetObserver);
2081 }
2082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 @Override
2084 public boolean dispatchKeyEvent(KeyEvent event) {
2085 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2086 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002088 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002089 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002090 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002091 dumpState();
2092 return true;
2093 }
2094 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002095 }
2096 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2097 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002098 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 return true;
2100 }
2101 }
2102
2103 return super.dispatchKeyEvent(event);
2104 }
2105
Joe Onorato88ec0992009-11-19 13:16:06 -08002106 @Override
2107 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002108 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002109 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002110 } else if (mWorkspace.isInOverviewMode()) {
2111 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002112 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002113 Folder openFolder = mWorkspace.getOpenFolder();
2114 if (openFolder.isEditingName()) {
2115 openFolder.dismissEditingName();
2116 } else {
2117 closeFolder();
2118 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002119 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002120 mWorkspace.exitWidgetResizeMode();
2121
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002122 // Back button is a no-op here, but give at least some feedback for the button press
2123 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002124 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002125 }
2126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002128 * Re-listen when widgets are reset.
2129 */
2130 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002131 if (mAppWidgetHost != null) {
2132 mAppWidgetHost.startListening();
2133 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002134 }
2135
2136 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 * Launches the intent referred by the clicked shortcut.
2138 *
2139 * @param v The view representing the clicked shortcut.
2140 */
2141 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002142 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2143 // view has detached (it's possible for this to happen if the view is removed mid touch).
2144 if (v.getWindowToken() == null) {
2145 return;
2146 }
2147
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002148 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002149 return;
2150 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002151
Adam Cohenf358a4b2013-07-23 16:47:31 -07002152 if (v instanceof PageIndicator) {
2153 if (!mWorkspace.isInOverviewMode()) {
2154 mWorkspace.enterOverviewMode();
2155 }
2156 return;
2157 }
2158
2159 if (v instanceof CellLayout) {
2160 if (mWorkspace.isInOverviewMode()) {
2161 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2162 }
2163 }
2164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002166 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002168 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2169 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002170
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002171 // Check for special shortcuts
2172 if (intent.getComponent() != null) {
2173 final String shortcutClass = intent.getComponent().getClassName();
2174
2175 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2176 showAllApps(true);
2177 return;
2178 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2179 MemoryDumpActivity.startDump(this);
2180 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002181 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2182 toggleShowWeightWatcher();
2183 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002184 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002185 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002186
2187 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002188 int[] pos = new int[2];
2189 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002190 intent.setSourceBounds(new Rect(pos[0], pos[1],
2191 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002192
2193 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002194
Daniel Sandlerff02d492013-08-05 02:12:05 -04002195 mStats.recordLaunch(intent, shortcut);
2196
Michael Jurkaddd62e92011-02-16 17:49:14 -08002197 if (success && v instanceof BubbleTextView) {
2198 mWaitingForResume = (BubbleTextView) v;
2199 mWaitingForResume.setStayPressed(true);
2200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002202 if (v instanceof FolderIcon) {
2203 FolderIcon fi = (FolderIcon) v;
2204 handleFolderClick(fi);
2205 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002206 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002207 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002208 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002209 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002210 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
2213 }
2214
Michael Jurka0e260592010-06-30 17:07:39 -07002215 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002216 return false;
2217 }
2218
Michael Jurkaaf442092010-06-10 17:01:57 -07002219 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002220 * Event handler for the search button
2221 *
2222 * @param v The view that was clicked.
2223 */
2224 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002225 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2226
Amith Yamasania135ba82011-08-09 17:42:01 -07002227 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002228 }
2229
2230 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002231 * Event handler for the voice button
2232 *
2233 * @param v The view that was clicked.
2234 */
2235 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002236 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002237
Bjorn Bringertc459e522013-06-07 19:36:01 +01002238 startVoice();
2239 }
2240
2241 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002242 try {
2243 final SearchManager searchManager =
2244 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2245 ComponentName activityName = searchManager.getGlobalSearchActivity();
2246 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002247 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002248 if (activityName != null) {
2249 intent.setPackage(activityName.getPackageName());
2250 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002251 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002252 } catch (ActivityNotFoundException e) {
2253 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002254 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002255 startActivitySafely(null, intent, "onClickVoiceButton");
2256 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002257 }
2258
2259 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002260 * Event handler for the "grid" button that appears on the home screen, which
2261 * enters all apps mode.
2262 *
2263 * @param v The view that was clicked.
2264 */
2265 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002266 showAllApps(true);
2267 }
2268
2269 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002270 // Provide the same haptic feedback that the system offers for virtual keys.
2271 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002272 }
2273
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002274 public void onClickAppMarketButton(View v) {
2275 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002276 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002277 } else {
2278 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002279 }
2280 }
2281
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002282 void startApplicationDetailsActivity(ComponentName componentName) {
2283 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002284 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2285 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002286 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002287 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002288 }
2289
Michael Jurka1e2f4652013-07-08 18:03:46 -07002290 // returns true if the activity was started
2291 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2292 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002293 // System applications cannot be installed. For now, show a toast explaining that.
2294 // We may give them the option of disabling apps this way.
2295 int messageId = R.string.uninstall_system_app_text;
2296 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002297 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002298 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002299 String packageName = componentName.getPackageName();
2300 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002301 Intent intent = new Intent(
2302 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002303 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2304 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002305 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002306 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002307 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002308 }
2309
Michael Jurka86a720e2012-05-09 11:23:23 -07002310 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002314 // Only launch using the new animation if the shortcut has not opted out (this is a
2315 // private contract between launcher and may be ignored in the future).
2316 boolean useLaunchAnimation = (v != null) &&
2317 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2318 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002319 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2320 v.getMeasuredWidth(), v.getMeasuredHeight());
2321
2322 startActivity(intent, opts.toBundle());
2323 } else {
2324 startActivity(intent);
2325 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002326 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 } catch (SecurityException e) {
2328 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002329 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002331 "or use the exported attribute for this activity. "
2332 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002334 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002336
Michael Jurka86a720e2012-05-09 11:23:23 -07002337 boolean startActivitySafely(View v, Intent intent, Object tag) {
2338 boolean success = false;
2339 try {
2340 success = startActivity(v, intent, tag);
2341 } catch (ActivityNotFoundException e) {
2342 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2343 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2344 }
2345 return success;
2346 }
2347
Adam Cohena9cf38f2011-05-02 15:36:58 -07002348 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002349 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002350 Folder openFolder = mWorkspace.getFolderForTag(info);
2351
2352 // If the folder info reports that the associated folder is open, then verify that
2353 // it is actually opened. There have been a few instances where this gets out of sync.
2354 if (info.opened && openFolder == null) {
2355 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002357 info.opened = false;
2358 }
2359
Adam Cohenfb91f302012-06-11 15:45:18 -07002360 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 // Close any open folder
2362 closeFolder();
2363 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002364 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 } else {
2366 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 int folderScreen;
2368 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002369 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 // .. and close it
2371 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002372 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 // Close any folder open on the current screen
2374 closeFolder();
2375 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002376 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 }
2378 }
2379 }
2380 }
2381
Adam Cohen268c4752012-06-06 17:47:33 -07002382 /**
2383 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2384 * in the DragLayer in the exact absolute location of the original FolderIcon.
2385 */
2386 private void copyFolderIconToImage(FolderIcon fi) {
2387 final int width = fi.getMeasuredWidth();
2388 final int height = fi.getMeasuredHeight();
2389
2390 // Lazy load ImageView, Bitmap and Canvas
2391 if (mFolderIconImageView == null) {
2392 mFolderIconImageView = new ImageView(this);
2393 }
2394 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2395 mFolderIconBitmap.getHeight() != height) {
2396 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2397 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2398 }
2399
2400 DragLayer.LayoutParams lp;
2401 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2402 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2403 } else {
2404 lp = new DragLayer.LayoutParams(width, height);
2405 }
2406
Adam Cohen307fe232012-08-16 17:55:58 -07002407 // The layout from which the folder is being opened may be scaled, adjust the starting
2408 // view size by this scale factor.
2409 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002410 lp.customPosition = true;
2411 lp.x = mRectForFolderAnimation.left;
2412 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002413 lp.width = (int) (scale * width);
2414 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002415
2416 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2417 fi.draw(mFolderIconCanvas);
2418 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002419 if (fi.getFolder() != null) {
2420 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2421 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002422 }
Adam Cohen268c4752012-06-06 17:47:33 -07002423 // Just in case this image view is still in the drag layer from a previous animation,
2424 // we remove it and re-add it.
2425 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2426 mDragLayer.removeView(mFolderIconImageView);
2427 }
2428 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002429 if (fi.getFolder() != null) {
2430 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002431 }
Adam Cohen268c4752012-06-06 17:47:33 -07002432 }
2433
Adam Cohen2801caf2011-05-13 20:57:39 -07002434 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002435 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002436 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2437 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2438 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2439
Adam Cohenc51934b2011-07-26 21:07:43 -07002440 FolderInfo info = (FolderInfo) fi.getTag();
2441 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2442 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002443 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2444 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002445 }
2446
Adam Cohen268c4752012-06-06 17:47:33 -07002447 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2448 copyFolderIconToImage(fi);
2449 fi.setVisibility(View.INVISIBLE);
2450
Michael Jurka2ecf9952012-06-18 12:52:28 -07002451 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002452 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002453 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2454 oa.start();
2455 }
2456
Adam Cohen268c4752012-06-06 17:47:33 -07002457 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002458 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002459 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2460 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2461 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2462
Adam Cohen268c4752012-06-06 17:47:33 -07002463 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002464
Adam Cohen268c4752012-06-06 17:47:33 -07002465 // We remove and re-draw the FolderIcon in-case it has changed
2466 mDragLayer.removeView(mFolderIconImageView);
2467 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002468 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002469 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002470 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002471 oa.addListener(new AnimatorListenerAdapter() {
2472 @Override
2473 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002474 if (cl != null) {
2475 cl.clearFolderLeaveBehind();
2476 // Remove the ImageView copy of the FolderIcon and make the original visible.
2477 mDragLayer.removeView(mFolderIconImageView);
2478 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002479 }
2480 }
2481 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002482 oa.start();
2483 }
2484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002486 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 * is animated relative to the specified View. If the View is null, no animation
2488 * is played.
2489 *
2490 * @param folderInfo The FolderInfo describing the folder to open.
2491 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002492 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002493 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002494 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495
Adam Cohena9cf38f2011-05-02 15:36:58 -07002496 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497
Adam Cohen4554ee12011-08-03 16:13:21 -07002498 // Just verify that the folder hasn't already been added to the DragLayer.
2499 // There was a one-off crash where the folder had a parent already.
2500 if (folder.getParent() == null) {
2501 mDragLayer.addView(folder);
2502 mDragController.addDropTarget((DropTarget) folder);
2503 } else {
2504 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2505 folder.getParent() + ").");
2506 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002507 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002508 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002509
2510 // Notify the accessibility manager that this folder "window" has appeared and occluded
2511 // the workspace items
2512 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2513 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002514 }
2515
2516 public void closeFolder() {
2517 Folder folder = mWorkspace.getOpenFolder();
2518 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002519 if (folder.isEditingName()) {
2520 folder.dismissEditingName();
2521 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002522 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002523
2524 // Dismiss the folder cling
2525 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002526 }
2527 }
2528
2529 void closeFolder(Folder folder) {
2530 folder.getInfo().opened = false;
2531
2532 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2533 if (parent != null) {
2534 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2535 shrinkAndFadeInFolderIcon(fi);
2536 }
2537 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002538
2539 // Notify the accessibility manager that this folder "window" has disappeard and no
2540 // longer occludeds the workspace items
2541 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 }
2543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002545 if (!isDraggingEnabled()) return false;
2546 if (isWorkspaceLocked()) return false;
2547 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548
2549 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002550 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552
Michael Jurka0280c3b2010-09-17 15:00:07 -07002553 resetAddInfo();
2554 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002556 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 return true;
2558 }
2559
Winson Chung3d503fb2011-07-13 17:25:49 -07002560 // The hotseat touch handling does not go through Workspace, and we always allow long press
2561 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002562 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002563 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2564 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002565 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002566 if (mWorkspace.hasNonCustomEmptyScreens()) {
2567 // User long pressed on empty space
2568 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2569 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2570 // Disabling reordering until we sort out some issues.
2571 if (mWorkspace.isInOverviewMode()) {
2572 mWorkspace.startReordering(v);
2573 } else {
2574 mWorkspace.enterOverviewMode();
2575 }
Adam Cohendedbd962013-07-11 14:21:49 -07002576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002578 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002580 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 }
2582 }
2583 }
2584 return true;
2585 }
2586
Winson Chung3d503fb2011-07-13 17:25:49 -07002587 boolean isHotseatLayout(View layout) {
2588 return mHotseat != null && layout != null &&
2589 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2590 }
2591 Hotseat getHotseat() {
2592 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002593 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002594 View getOverviewPanel() {
2595 return mOverviewPanel;
2596 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002597 SearchDropTargetBar getSearchBar() {
2598 return mSearchDropTargetBar;
2599 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002600
Winson Chung3d503fb2011-07-13 17:25:49 -07002601 /**
2602 * Returns the CellLayout of the specified container at the specified screen.
2603 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002604 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002605 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2606 if (mHotseat != null) {
2607 return mHotseat.getLayout();
2608 } else {
2609 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002610 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002611 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002612 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002613 }
2614 }
2615
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002616 Workspace getWorkspace() {
2617 return mWorkspace;
2618 }
2619
Daniel Sandler843e8602010-06-07 14:59:01 -04002620 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002621 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002622 }
2623
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 /**
2625 * Helper method for the cameraZoomIn/cameraZoomOut animations
2626 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 * @param scaleFactor The scale factor used for the zoom
2628 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002629 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002631 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002632 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002633
Winson Chung18f41f82012-05-09 13:28:10 -07002634 void disableWallpaperIfInAllApps() {
2635 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002636 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002637 if (mAppsCustomizeTabHost != null &&
2638 !mAppsCustomizeTabHost.isTransitioning()) {
2639 updateWallpaperVisibility(false);
2640 }
2641 }
2642 }
2643
Craig Mautner360310b2012-10-26 15:13:08 -07002644 private void setWorkspaceBackground(boolean workspace) {
2645 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002646 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002647 }
2648
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002649 void updateWallpaperVisibility(boolean visible) {
2650 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2651 int curflags = getWindow().getAttributes().flags
2652 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2653 if (wpflags != curflags) {
2654 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2655 }
Craig Mautner360310b2012-10-26 15:13:08 -07002656 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002657 }
2658
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002659 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2660 if (v instanceof LauncherTransitionable) {
2661 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2662 }
2663 }
2664
Michael Jurkabed61d22012-02-14 22:51:29 -08002665 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2666 if (v instanceof LauncherTransitionable) {
2667 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2668 }
Winson Chung70442722012-02-10 15:43:22 -08002669
2670 // Update the workspace transition step as well
2671 dispatchOnLauncherTransitionStep(v, 0f);
2672 }
2673
2674 private void dispatchOnLauncherTransitionStep(View v, float t) {
2675 if (v instanceof LauncherTransitionable) {
2676 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2677 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002678 }
2679
2680 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2681 if (v instanceof LauncherTransitionable) {
2682 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2683 }
Winson Chung70442722012-02-10 15:43:22 -08002684
2685 // Update the workspace transition step as well
2686 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002687 }
2688
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002689 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002690 * Things to test when changing the following seven functions.
2691 * - Home from workspace
2692 * - from center screen
2693 * - from other screens
2694 * - Home from all apps
2695 * - from center screen
2696 * - from other screens
2697 * - Back from all apps
2698 * - from center screen
2699 * - from other screens
2700 * - Launch app from workspace and quit
2701 * - with back
2702 * - with home
2703 * - Launch app from all apps and quit
2704 * - with back
2705 * - with home
2706 * - Go to a screen that's not the default, then all
2707 * apps, and launch and app, and go back
2708 * - with back
2709 * -with home
2710 * - On workspace, long press power and go back
2711 * - with back
2712 * - with home
2713 * - On all apps, long press power and go back
2714 * - with back
2715 * - with home
2716 * - On workspace, power off
2717 * - On all apps, power off
2718 * - Launch an app and turn off the screen while in that app
2719 * - Go back with home key
2720 * - Go back with back key TODO: make this not go to workspace
2721 * - From all apps
2722 * - From workspace
2723 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2724 * - From all apps
2725 * - From the center workspace
2726 * - From another workspace
2727 */
2728
2729 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002730 * Zoom the camera out from the workspace to reveal 'toView'.
2731 * Assumes that the view to show is anchored at either the very top or very bottom
2732 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002733 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002734 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002735 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002736 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002737 mStateAnimation.cancel();
2738 mStateAnimation = null;
2739 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002740 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002741
Winson Chungf0ea4d32011-06-06 14:27:16 -07002742 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2743 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2744 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002745 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002746 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002747 final int startDelay =
2748 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002749
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002751
Michael Jurkad74c9842011-07-10 12:44:21 -07002752 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002753 Animator workspaceAnim =
2754 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755
2756 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002757 toView.setScaleX(scale);
2758 toView.setScaleY(scale);
2759 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2760 scaleAnim.
2761 scaleX(1f).scaleY(1f).
2762 setDuration(duration).
2763 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002764
Winson Chungf0ea4d32011-06-06 14:27:16 -07002765 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002766 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002767 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002768 .ofFloat(toView, "alpha", 0f, 1f)
2769 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002770 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002771 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2772 @Override
2773 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002774 if (animation == null) {
2775 throw new RuntimeException("animation is null");
2776 }
Winson Chung70442722012-02-10 15:43:22 -08002777 float t = (Float) animation.getAnimatedValue();
2778 dispatchOnLauncherTransitionStep(fromView, t);
2779 dispatchOnLauncherTransitionStep(toView, t);
2780 }
2781 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002782
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002783 // toView should appear right at the end of the workspace shrink
2784 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002785 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002786 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002787 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002788
2789 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002790 boolean animationCancelled = false;
2791
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002792 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002793 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002794 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002795 // Prepare the position
2796 toView.setTranslationX(0.0f);
2797 toView.setTranslationY(0.0f);
2798 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002799 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002801 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002802 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002803 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2804 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002805
Adam Cohenf4ddea32011-09-12 13:46:42 -07002806 if (!animationCancelled) {
2807 updateWallpaperVisibility(false);
2808 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002809
2810 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002811 if (mSearchDropTargetBar != null) {
2812 mSearchDropTargetBar.hideSearchBar(false);
2813 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002814 }
2815
Adam Cohencff6af82011-09-13 14:51:53 -07002816 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002817 public void onAnimationCancel(Animator animation) {
2818 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002819 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002820 });
2821
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002822 if (workspaceAnim != null) {
2823 mStateAnimation.play(workspaceAnim);
2824 }
Michael Jurka1899a362011-11-03 13:50:45 -07002825
2826 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002827
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002828 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2829 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002830
2831 // If any of the objects being animated haven't been measured/laid out
2832 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002833 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002834 (mWorkspace.getMeasuredWidth() == 0) ||
2835 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002836 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002837 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002838
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002839 final AnimatorSet stateAnimation = mStateAnimation;
2840 final Runnable startAnimRunnable = new Runnable() {
2841 public void run() {
2842 // Check that mStateAnimation hasn't changed while
2843 // we waited for a layout/draw pass
2844 if (mStateAnimation != stateAnimation)
2845 return;
2846 setPivotsForZoom(toView, scale);
2847 dispatchOnLauncherTransitionStart(fromView, animated, false);
2848 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002849 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002850 }
2851 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002852 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002853 final ViewTreeObserver observer = toView.getViewTreeObserver();
2854 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2855 public void onGlobalLayout() {
2856 startAnimRunnable.run();
2857 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2858 }
2859 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002860 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002861 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002862 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 } else {
2864 toView.setTranslationX(0.0f);
2865 toView.setTranslationY(0.0f);
2866 toView.setScaleX(1.0f);
2867 toView.setScaleY(1.0f);
2868 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002869 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002870
Daniel Sandlere4f98912013-06-25 15:13:26 -04002871 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002872 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002873 if (mSearchDropTargetBar != null) {
2874 mSearchDropTargetBar.hideSearchBar(false);
2875 }
Michael Jurkaabded662011-03-04 12:06:57 -08002876 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002877 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002878 dispatchOnLauncherTransitionStart(fromView, animated, false);
2879 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002880 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002881 dispatchOnLauncherTransitionStart(toView, animated, false);
2882 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002883 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002884 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002885 }
2886
2887 /**
2888 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002889 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002890 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002891 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002892 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2893 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002894
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002896 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002897 mStateAnimation.cancel();
2898 mStateAnimation = null;
2899 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002900 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002901
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002903 final int fadeOutDuration =
2904 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002905 final float scaleFactor = (float)
2906 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2907 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002908 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002909 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002910 if (toState == State.WORKSPACE) {
2911 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2912 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002913 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002914 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2915 workspaceAnim = mWorkspace.getChangeStateAnimation(
2916 Workspace.State.SPRING_LOADED, animated);
2917 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002918
Michael Jurkab3e22d92011-10-31 15:58:33 -07002919 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002920 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002921 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002922 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002923 final LauncherViewPropertyAnimator scaleAnim =
2924 new LauncherViewPropertyAnimator(fromView);
2925 scaleAnim.
2926 scaleX(scaleFactor).scaleY(scaleFactor).
2927 setDuration(duration).
2928 setInterpolator(new Workspace.ZoomInInterpolator());
2929
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002930 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002931 .ofFloat(fromView, "alpha", 1f, 0f)
2932 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002933 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002934 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2935 @Override
2936 public void onAnimationUpdate(ValueAnimator animation) {
2937 float t = 1f - (Float) animation.getAnimatedValue();
2938 dispatchOnLauncherTransitionStep(fromView, t);
2939 dispatchOnLauncherTransitionStep(toView, t);
2940 }
2941 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002942
Michael Jurka2ecf9952012-06-18 12:52:28 -07002943 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002944
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002945 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2946 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002947 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002948
2949 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002950 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002951 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002952 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002953 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002954 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2955 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002956 if (onCompleteRunnable != null) {
2957 onCompleteRunnable.run();
2958 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002959 mAppsCustomizeContent.updateCurrentPageScroll();
2960 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002961 }
2962 });
2963
Winson Chungf0ea4d32011-06-06 14:27:16 -07002964 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002965 if (workspaceAnim != null) {
2966 mStateAnimation.play(workspaceAnim);
2967 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002968 dispatchOnLauncherTransitionStart(fromView, animated, true);
2969 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002970 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002971 } else {
2972 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002973 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002974 dispatchOnLauncherTransitionStart(fromView, animated, true);
2975 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002976 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002977 dispatchOnLauncherTransitionStart(toView, animated, true);
2978 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002979 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002980 }
2981
Michael Jurkae326f182011-11-21 14:05:46 -08002982 @Override
2983 public void onTrimMemory(int level) {
2984 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002985 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002986 mAppsCustomizeTabHost.onTrimMemory();
2987 }
2988 }
2989
Winson Chung18f41f82012-05-09 13:28:10 -07002990 @Override
2991 public void onWindowFocusChanged(boolean hasFocus) {
2992 if (!hasFocus) {
2993 // When another window occludes launcher (like the notification shade, or recents),
2994 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2995 updateWallpaperVisibility(true);
2996 } else {
2997 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002998 mWorkspace.postDelayed(new Runnable() {
2999 @Override
3000 public void run() {
3001 disableWallpaperIfInAllApps();
3002 }
3003 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003004 }
3005 }
3006
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003007 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003008 showWorkspace(animated, null);
3009 }
3010
3011 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003012 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003013 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003014 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003015 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003016
Winson Chungc7d2b602012-05-16 17:02:20 -07003017 // Show the search bar (only animate if we were showing the drop target bar in spring
3018 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003019 if (mSearchDropTargetBar != null) {
3020 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3021 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003022
Michael Jurkab3e22d92011-10-31 15:58:33 -07003023 // Set focus to the AppsCustomize button
3024 if (mAllAppsButton != null) {
3025 mAllAppsButton.requestFocus();
3026 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003027 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003028
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003029 // Change the state *after* we've called all the transition code
3030 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003031
Winson Chung5fb63472011-02-02 17:03:37 -08003032 // Resume the auto-advance of widgets
3033 mUserPresent = true;
3034 updateRunning();
3035
alanv1d4fde62012-10-17 13:15:47 -07003036 // Send an accessibility event to announce the context change
3037 getWindow().getDecorView()
3038 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003039
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003040 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003041 }
3042
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003043 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003044 }
3045
Michael Jurkab3e22d92011-10-31 15:58:33 -07003046 void showAllApps(boolean animated) {
3047 if (mState != State.WORKSPACE) return;
3048
3049 showAppsCustomizeHelper(animated, false);
3050 mAppsCustomizeTabHost.requestFocus();
3051
Michael Jurkab3e22d92011-10-31 15:58:33 -07003052 // Change the state *after* we've called all the transition code
3053 mState = State.APPS_CUSTOMIZE;
3054
3055 // Pause the auto-advance of widgets until we are out of AllApps
3056 mUserPresent = false;
3057 updateRunning();
3058 closeFolder();
3059
3060 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003061 getWindow().getDecorView()
3062 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003063 }
3064
Adam Cohen7777d962011-08-18 18:58:38 -07003065 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003066 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003067 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003068 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003069 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003070 }
Adam Cohen7777d962011-08-18 18:58:38 -07003071
Adam Cohened66b2b2012-01-23 17:28:51 -08003072 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3073 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003074 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3075
Winson Chunge7a03942011-08-05 15:05:12 -07003076 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003077 @Override
3078 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003079 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003080 // Before we show workspace, hide all apps again because
3081 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3082 // clean up our state transition functions
3083 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003084 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003085 } else {
3086 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003087 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003088 }
3089 }, (extendedDelay ?
3090 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3091 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3092 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093
Michael Jurkad3ef3062010-11-23 16:23:58 -08003094 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003095 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003096 final boolean animated = true;
3097 final boolean springLoaded = true;
3098 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003099 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003100 }
3101 // Otherwise, we are not in spring loaded mode, so don't do anything.
3102 }
3103
Michael Jurkab3e22d92011-10-31 15:58:33 -07003104 void lockAllApps() {
3105 // TODO
3106 }
3107
3108 void unlockAllApps() {
3109 // TODO
3110 }
3111
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003113 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003114 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003115 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003116 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003117 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003118 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003119 int duration = 0;
3120 if (mSearchDropTargetBar != null) {
3121 duration = mSearchDropTargetBar.getTransitionInDuration();
3122 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003123 mHotseat.animate().alpha(1f).setDuration(duration);
3124 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003125 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003127 }
3128 }
3129 }
3130
3131 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003134 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003135 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003136 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003137 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003138 int duration = 0;
3139 if (mSearchDropTargetBar != null) {
3140 duration = mSearchDropTargetBar.getTransitionOutDuration();
3141 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003142 mHotseat.animate().alpha(0f).setDuration(duration);
3143 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003144 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003145 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003146 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003147 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003148 }
3149
Patrick Dubroy5f445422011-02-18 14:35:21 -08003150 /**
3151 * Add an item from all apps or customize onto the given workspace screen.
3152 * If layout is null, add to the current screen.
3153 */
3154 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003155 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003156 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003157 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003158 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003159
Winson Chungdff8ebb2011-09-08 17:25:31 -07003160 /** Maps the current orientation to an index for referencing orientation correct global icons */
3161 private int getCurrentOrientationIndexForGlobalIcons() {
3162 // default - 0, landscape - 1
3163 switch (getResources().getConfiguration().orientation) {
3164 case Configuration.ORIENTATION_LANDSCAPE:
3165 return 1;
3166 default:
3167 return 0;
3168 }
3169 }
3170
Michael Jurkaae65ee32012-04-30 11:45:54 -07003171 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003172 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003173 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003174 // Look for the toolbar icon specified in the activity meta-data
3175 Bundle metaData = packageManager.getActivityInfo(
3176 activityName, PackageManager.GET_META_DATA).metaData;
3177 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003178 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003179 if (iconResId != 0) {
3180 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003181 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003182 }
3183 }
3184 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003185 // This can happen if the activity defines an invalid drawable
3186 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3187 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003188 } catch (Resources.NotFoundException nfe) {
3189 // This can happen if the activity defines an invalid drawable
3190 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3191 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003192 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003193 return null;
3194 }
3195
3196 // if successful in getting icon, return it; otherwise, set button to use default drawable
3197 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003198 int buttonId, ComponentName activityName, int fallbackDrawableId,
3199 String toolbarResourceName) {
3200 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003201 Resources r = getResources();
3202 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3203 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003204
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003205 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003206 // If we were unable to find the icon via the meta-data, use a generic one
3207 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003208 toolbarIcon = r.getDrawable(fallbackDrawableId);
3209 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003210 if (button != null) {
3211 button.setCompoundDrawables(toolbarIcon, null, null, null);
3212 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003213 return null;
3214 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003215 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003216 if (button != null) {
3217 button.setCompoundDrawables(toolbarIcon, null, null, null);
3218 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003219 return toolbarIcon.getConstantState();
3220 }
3221 }
3222
3223 // if successful in getting icon, return it; otherwise, set button to use default drawable
3224 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003225 int buttonId, ComponentName activityName, int fallbackDrawableId,
3226 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003227 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003228 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003229
Michael Jurka19e0fc52011-07-22 18:00:21 -07003230 if (button != null) {
3231 // If we were unable to find the icon via the meta-data, use a
3232 // generic one
3233 if (toolbarIcon == null) {
3234 button.setImageResource(fallbackDrawableId);
3235 } else {
3236 button.setImageDrawable(toolbarIcon);
3237 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003238 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003239
3240 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3241
Michael Jurka0423dcf2010-10-05 14:56:18 -07003242 }
3243
Winson Chung1b884092012-06-08 17:13:02 -07003244 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003245 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003246 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003247 }
3248
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003249 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003250 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003251 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003252 }
3253
Winson Chungbb185bd2011-11-21 12:31:42 -08003254 private void invalidatePressedFocusedStates(View container, View button) {
3255 if (container instanceof HolographicLinearLayout) {
3256 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3257 layout.invalidatePressedFocusedStates();
3258 } else if (button instanceof HolographicImageView) {
3259 HolographicImageView view = (HolographicImageView) button;
3260 view.invalidatePressedFocusedStates();
3261 }
3262 }
3263
Cristina Stancu476493b2013-08-07 17:20:14 +01003264 public View getQsbBar() {
3265 if (mQsbBar == null) {
3266 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3267 }
3268 return mQsbBar;
3269 }
3270
3271 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003272 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003273 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003274 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003275 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003276
Winson Chung1cad91e2011-05-25 17:41:01 -07003277 final SearchManager searchManager =
3278 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3279 ComponentName activityName = searchManager.getGlobalSearchActivity();
3280 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003281 int coi = getCurrentOrientationIndexForGlobalIcons();
3282 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003283 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3284 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3285 if (sGlobalSearchIcon[coi] == null) {
3286 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3287 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3288 TOOLBAR_ICON_METADATA_NAME);
3289 }
3290
Winson Chungc51db6a2011-10-05 11:44:49 -07003291 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3292 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003293 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003294 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003295 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003296 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003297 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3298 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3299 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003300 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003301 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003302 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003303 }
3304 }
3305
Cristina Stancu476493b2013-08-07 17:20:14 +01003306 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003307 final View searchButtonContainer = findViewById(R.id.search_button_container);
3308 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003309 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003310 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003311 }
3312
Cristina Stancu476493b2013-08-07 17:20:14 +01003313 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003315 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003316
Winson Chungc51db6a2011-10-05 11:44:49 -07003317 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003318 final SearchManager searchManager =
3319 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3320 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3321
3322 ComponentName activityName = null;
3323 if (globalSearchActivity != null) {
3324 // Check if the global search activity handles voice search
3325 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3326 intent.setPackage(globalSearchActivity.getPackageName());
3327 activityName = intent.resolveActivity(getPackageManager());
3328 }
3329
3330 if (activityName == null) {
3331 // Fallback: check if an activity other than the global search activity
3332 // resolves this
3333 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3334 activityName = intent.resolveActivity(getPackageManager());
3335 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003336 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003337 int coi = getCurrentOrientationIndexForGlobalIcons();
3338 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003339 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3340 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3341 if (sVoiceSearchIcon[coi] == null) {
3342 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3343 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3344 TOOLBAR_ICON_METADATA_NAME);
3345 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003346 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003347 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003348 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003349 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003350 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003351 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003352 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003353 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003354 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003355 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003356 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003357 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003358
Cristina Stancu476493b2013-08-07 17:20:14 +01003359 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003360 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3361 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003362 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003363 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003364 }
3365
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003366 public void setVoiceButtonProxyVisible(boolean visible) {
3367 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3368 if (voiceButtonProxy != null) {
3369 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3370 }
3371 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003372 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003373 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003374 */
3375 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003376 final View marketButton = findViewById(R.id.market_button);
3377 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3378 // Find the app market activity by resolving an intent.
3379 // (If multiple app markets are installed, it will return the ResolverActivity.)
3380 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003381 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003382 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003383 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003384 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003385 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3386 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003387 marketButton.setVisibility(View.VISIBLE);
3388 } else {
3389 // We should hide and disable the view so that we don't try and restore the visibility
3390 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3391 marketButton.setVisibility(View.GONE);
3392 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003393 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003394 }
3395
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003396 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003397 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3398 Resources r = getResources();
3399 Drawable marketIconDrawable = d.newDrawable(r);
3400 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3401 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3402 marketIconDrawable.setBounds(0, 0, w, h);
3403
3404 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003405 }
3406
Michael Jurkad7c28052012-04-27 15:43:36 -07003407 @Override
3408 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003409 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003410 final List<CharSequence> text = event.getText();
3411 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003412 // Populate event with a fake title based on the current state.
3413 if (mState == State.APPS_CUSTOMIZE) {
3414 text.add(getString(R.string.all_apps_button_label));
3415 } else {
3416 text.add(getString(R.string.all_apps_home_button_label));
3417 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003418 return result;
3419 }
3420
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003421 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003422 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003423 */
3424 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3425 @Override
3426 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003427 closeSystemDialogs();
3428 }
3429 }
3430
3431 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003432 * Receives notifications whenever the appwidgets are reset.
3433 */
3434 private class AppWidgetResetObserver extends ContentObserver {
3435 public AppWidgetResetObserver() {
3436 super(new Handler());
3437 }
3438
3439 @Override
3440 public void onChange(boolean selfChange) {
3441 onAppWidgetReset();
3442 }
3443 }
3444
3445 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003446 * If the activity is currently paused, signal that we need to run the passed Runnable
3447 * in onResume.
3448 *
3449 * This needs to be called from incoming places where resources might have been loaded
3450 * while we are paused. That is becaues the Configuration might be wrong
3451 * when we're not running, and if it comes back to what it was when we
3452 * were paused, we are not restarted.
3453 *
3454 * Implementation of the method from LauncherModel.Callbacks.
3455 *
3456 * @return true if we are currently paused. The caller might be able to
3457 * skip some work in that case since we will come back again.
3458 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003459 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003460 if (mPaused) {
3461 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003462 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003463 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003464 }
3465 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003466 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003467 return true;
3468 } else {
3469 return false;
3470 }
3471 }
3472
Michael Jurkac402cd92013-05-20 15:49:32 +02003473 private boolean waitUntilResume(Runnable run) {
3474 return waitUntilResume(run, false);
3475 }
3476
Michael Jurka1e2f4652013-07-08 18:03:46 -07003477 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003478 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003479 }
3480
Michael Jurka7607c2f2013-04-03 14:33:19 -07003481 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003482 * If the activity is currently paused, signal that we need to re-run the loader
3483 * in onResume.
3484 *
3485 * This needs to be called from incoming places where resources might have been loaded
3486 * while we are paused. That is becaues the Configuration might be wrong
3487 * when we're not running, and if it comes back to what it was when we
3488 * were paused, we are not restarted.
3489 *
3490 * Implementation of the method from LauncherModel.Callbacks.
3491 *
3492 * @return true if we are currently paused. The caller might be able to
3493 * skip some work in that case since we will come back again.
3494 */
3495 public boolean setLoadOnResume() {
3496 if (mPaused) {
3497 Log.i(TAG, "setLoadOnResume");
3498 mOnResumeNeedsLoad = true;
3499 return true;
3500 } else {
3501 return false;
3502 }
3503 }
3504
3505 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003507 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003509 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003510 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003511 } else {
3512 return SCREEN_COUNT / 2;
3513 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003515
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 /**
3517 * Refreshes the shortcuts shown on the workspace.
3518 *
3519 * Implementation of the method from LauncherModel.Callbacks.
3520 */
3521 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003522 // If we're starting binding all over again, clear any bind calls we'd postponed in
3523 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3524 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003525 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003526
Winson Chungd64d1762013-08-20 14:37:16 -07003527 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003528 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003529 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003530
Michael Jurka05bf6442011-11-30 20:28:53 -08003531 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003532 if (mHotseat != null) {
3533 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003534 }
3535 }
3536
Adam Cohendcd297f2013-06-18 13:13:40 -07003537 @Override
3538 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003539 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003540
3541 // Create the new empty page
Winson Chung64359a52013-07-08 17:17:08 -07003542 mWorkspace.addExtraEmptyScreen();
Winson Chung0e6a7132013-08-23 12:55:10 -07003543
3544 // Create the custom content page (this call updates mDefaultScreen which calls
3545 // setCurrentPage() so ensure that all pages are added before calling this)
3546 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3547 mWorkspace.createCustomContentPage();
3548 }
Winson Chung64359a52013-07-08 17:17:08 -07003549 }
3550
3551 @Override
3552 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003553 int count = orderedScreenIds.size();
3554 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003555 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003556 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003557 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003558 }
3559
Adam Cohen39a06042013-07-19 14:30:12 -07003560 private boolean shouldShowWeightWatcher() {
3561 String spKey = LauncherAppState.getSharedPreferencesKey();
3562 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003563 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003564
3565 return show;
3566 }
3567
Adam Cohen4caf2982013-08-20 18:54:31 -07003568 private boolean emailSent() {
3569 String spKey = LauncherAppState.getSharedPreferencesKey();
3570 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3571 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3572 return show;
3573 }
3574
3575 private void setEmailSent(boolean sent) {
3576 String spKey = LauncherAppState.getSharedPreferencesKey();
3577 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3578
3579 SharedPreferences.Editor editor = sp.edit();
3580 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3581 editor.commit();
3582 }
3583
Adam Cohen39a06042013-07-19 14:30:12 -07003584 private void toggleShowWeightWatcher() {
3585 String spKey = LauncherAppState.getSharedPreferencesKey();
3586 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3587 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3588
3589 show = !show;
3590
3591 SharedPreferences.Editor editor = sp.edit();
3592 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3593 editor.commit();
3594
3595 if (mWeightWatcher != null) {
3596 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3597 }
3598 }
3599
Winson Chungd64d1762013-08-20 14:37:16 -07003600 public void bindAppsAdded(final ArrayList<Long> newScreens,
3601 final ArrayList<ItemInfo> addNotAnimated,
3602 final ArrayList<ItemInfo> addAnimated) {
3603 Runnable r = new Runnable() {
3604 public void run() {
3605 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3606 }
3607 };
3608 if (waitUntilResume(r)) {
3609 return;
3610 }
3611
Adam Cohen4caf2982013-08-20 18:54:31 -07003612 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003613
3614 // Add the new screens
3615 bindAddScreens(newScreens);
3616
3617 // We add the items without animation on non-visible pages, and with
3618 // animations on the new page (which we will try and snap to).
3619 if (!addNotAnimated.isEmpty()) {
3620 bindItems(addNotAnimated, 0,
3621 addNotAnimated.size(), false);
3622 }
3623 if (!addAnimated.isEmpty()) {
3624 bindItems(addAnimated, 0,
3625 addAnimated.size(), true);
3626 }
3627 }
3628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 * Bind the items start-end from the list.
3631 *
3632 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003633 */
Winson Chung64359a52013-07-08 17:17:08 -07003634 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3635 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003636 Runnable r = new Runnable() {
3637 public void run() {
3638 bindItems(shortcuts, start, end, forceAnimateIcons);
3639 }
3640 };
3641 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 return;
3643 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003644
Winson Chungf0c6ae02012-03-21 16:10:31 -07003645 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003646 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3647 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003648 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003649 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003650 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003651 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003653
3654 // Short circuit if we are loading dock items for a configuration which has no dock
3655 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3656 mHotseat == null) {
3657 continue;
3658 }
3659
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 switch (item.itemType) {
3661 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3662 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003663 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003664 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003665
Winson Chung64359a52013-07-08 17:17:08 -07003666 /*
3667 * TODO: FIX collision case
3668 */
Winson Chungce376632013-07-11 16:12:41 -07003669 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3670 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3671 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3672 throw new RuntimeException("OCCUPIED");
3673 }
Winson Chung64359a52013-07-08 17:17:08 -07003674 }
3675
Adam Cohendcd297f2013-06-18 13:13:40 -07003676 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3677 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003678 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003679 // Animate all the applications up now
3680 shortcut.setAlpha(0f);
3681 shortcut.setScaleX(0f);
3682 shortcut.setScaleY(0f);
3683 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003684 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003687 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003688 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003689 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003690 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003691 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3692 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003694 default:
3695 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003697 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003698
Winson Chung997a9232013-07-24 15:33:46 -07003699 if (animateIcons) {
3700 // Animate to the correct page
3701 if (newShortcutsScreenId > -1) {
3702 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3703 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3704 if (newShortcutsScreenId != currentScreenId) {
3705 mWorkspace.snapToPage(newScreenIndex);
3706 }
3707 }
3708
Winson Chung64359a52013-07-08 17:17:08 -07003709 // We post the animation slightly delayed to prevent slowdowns when we are loading
3710 // right after we return to launcher.
3711 mWorkspace.postDelayed(new Runnable() {
3712 public void run() {
3713 anim.playTogether(bounceAnims);
3714 anim.start();
3715 }
3716 }, NEW_APPS_ANIMATION_DELAY);
3717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003719 }
3720
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 /**
3722 * Implementation of the method from LauncherModel.Callbacks.
3723 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003724 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003725 Runnable r = new Runnable() {
3726 public void run() {
3727 bindFolders(folders);
3728 }
3729 };
3730 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003731 return;
3732 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003733 sFolders.clear();
3734 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736
3737 /**
3738 * Add the views for a widget to the workspace.
3739 *
3740 * Implementation of the method from LauncherModel.Callbacks.
3741 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003743 Runnable r = new Runnable() {
3744 public void run() {
3745 bindAppWidget(item);
3746 }
3747 };
3748 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003749 return;
3750 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003751
Daniel Sandler843e8602010-06-07 14:59:01 -04003752 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3753 if (DEBUG_WIDGETS) {
3754 Log.d(TAG, "bindAppWidget: " + item);
3755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003756 final Workspace workspace = mWorkspace;
3757
3758 final int appWidgetId = item.appWidgetId;
3759 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003760 if (DEBUG_WIDGETS) {
3761 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3762 }
3763
Joe Onorato9c1289c2009-08-17 11:03:03 -04003764 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3765
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003767 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003768
Adam Cohendcd297f2013-06-18 13:13:40 -07003769 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003771 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3772
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 workspace.requestLayout();
3774
Daniel Sandler843e8602010-06-07 14:59:01 -04003775 if (DEBUG_WIDGETS) {
3776 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3777 + (SystemClock.uptimeMillis()-start) + "ms");
3778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 }
3780
Adam Cohen1462de32012-07-24 22:34:36 -07003781 public void onPageBoundSynchronously(int page) {
3782 mSynchronouslyBoundPages.add(page);
3783 }
3784
Joe Onorato9c1289c2009-08-17 11:03:03 -04003785 /**
3786 * Callback saying that there aren't any more items to bind.
3787 *
3788 * Implementation of the method from LauncherModel.Callbacks.
3789 */
Adam Cohene25af792013-06-06 23:08:25 -07003790 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003791 Runnable r = new Runnable() {
3792 public void run() {
3793 finishBindingItems(upgradePath);
3794 }
3795 };
3796 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003797 return;
3798 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 if (mSavedState != null) {
3800 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003801 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 mSavedState = null;
3804 }
3805
Adam Cohen1462de32012-07-24 22:34:36 -07003806 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003807
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003808 // If we received the result of any pending adds while the loader was running (e.g. the
3809 // widget configuration forced an orientation change), process them now.
3810 for (int i = 0; i < sPendingAddList.size(); i++) {
3811 completeAdd(sPendingAddList.get(i));
3812 }
3813 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003814
Winson Chungc51db6a2011-10-05 11:44:49 -07003815 // Update the market app icon as necessary (the other icons will be managed in response to
3816 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003817 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003818
Winson Chungf0c6ae02012-03-21 16:10:31 -07003819 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003820 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003821 mWorkspace.getUniqueComponents(true, null);
3822 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003823 }
Adam Cohen99894d92013-06-14 11:22:59 -07003824
Adam Cohen66a01fd2013-06-11 12:48:00 -07003825 mWorkspace.post(new Runnable() {
3826 @Override
3827 public void run() {
3828 onFinishBindingItems();
3829 }
3830 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003831
3832 // Write all the logs to disk
3833 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3834 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003835 }
3836
Winson Chunga2413752012-04-03 14:22:34 -07003837 private boolean canRunNewAppsAnimation() {
3838 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3839 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3840 }
3841
Winson Chungc9168342013-06-26 14:54:55 -07003842 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3843 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3844 PropertyValuesHolder.ofFloat("alpha", 1f),
3845 PropertyValuesHolder.ofFloat("scaleX", 1f),
3846 PropertyValuesHolder.ofFloat("scaleY", 1f));
3847 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3848 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3849 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3850 return bounceAnim;
3851 }
3852
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003853 @Override
3854 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003855 boolean searchVisible = updateGlobalSearchIcon();
3856 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003857 if (mSearchDropTargetBar != null) {
3858 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3859 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003860 }
3861
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003862 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003863 * Add the icons for all apps.
3864 *
3865 * Implementation of the method from LauncherModel.Callbacks.
3866 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003867 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003868 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003869 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3870 getHotseat().addAllAppsFolder(mIconCache, apps,
3871 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3872 }
Winson Chung64359a52013-07-08 17:17:08 -07003873 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 }
3876
3877 /**
3878 * A package was updated.
3879 *
3880 * Implementation of the method from LauncherModel.Callbacks.
3881 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003882 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003883 Runnable r = new Runnable() {
3884 public void run() {
3885 bindAppsUpdated(apps);
3886 }
3887 };
3888 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003889 return;
3890 }
3891
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003892 if (mWorkspace != null) {
3893 mWorkspace.updateShortcuts(apps);
3894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 }
3896
3897 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003898 * A package was uninstalled. We take both the super set of packageNames
3899 * in addition to specific applications to remove, the reason being that
3900 * this can be called when a package is updated as well. In that scenario,
3901 * we only remove specific components from the workspace, where as
3902 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 *
3904 * Implementation of the method from LauncherModel.Callbacks.
3905 */
Winson Chung83892cc2013-05-01 16:53:33 -07003906 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3907 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003908 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003909 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003910 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003911 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003912 }
Winson Chungd64d1762013-08-20 14:37:16 -07003913 };
3914 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003915 return;
3916 }
3917
Winson Chung64359a52013-07-08 17:17:08 -07003918 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003919 mWorkspace.removeItemsByPackageName(packageNames);
3920 } else {
3921 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003922 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003923
Winson Chunga1820962011-10-03 16:31:06 -07003924 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003925 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003926 }
Joe Onoratobe386092009-11-17 17:32:16 -08003927
3928 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003929 * A number of packages were updated.
3930 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003931 private ArrayList<Object> mWidgetsAndShortcuts;
3932 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003933 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003934 bindPackagesUpdated(mWidgetsAndShortcuts);
3935 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003936 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003937 };
3938
3939 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3940 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3941 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003942 return;
3943 }
3944
Winson Chung64359a52013-07-08 17:17:08 -07003945 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003946 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003947 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003948 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003949 }
3950
Winson Chung400438b2011-01-16 17:53:48 -08003951 private int mapConfigurationOriActivityInfoOri(int configOri) {
3952 final Display d = getWindowManager().getDefaultDisplay();
3953 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3954 switch (d.getRotation()) {
3955 case Surface.ROTATION_0:
3956 case Surface.ROTATION_180:
3957 // We are currently in the same basic orientation as the natural orientation
3958 naturalOri = configOri;
3959 break;
3960 case Surface.ROTATION_90:
3961 case Surface.ROTATION_270:
3962 // We are currently in the other basic orientation to the natural orientation
3963 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3964 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3965 break;
3966 }
3967
3968 int[] oriMap = {
3969 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3970 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3971 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3972 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3973 };
3974 // Since the map starts at portrait, we need to offset if this device's natural orientation
3975 // is landscape.
3976 int indexOffset = 0;
3977 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3978 indexOffset = 1;
3979 }
3980 return oriMap[(d.getRotation() + indexOffset) % 4];
3981 }
Adam Cohen446e9402011-09-15 18:21:21 -07003982
Winson Chung4b919f82012-05-01 10:44:08 -07003983 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003984 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003985 getResources().getBoolean(R.bool.allow_rotation);
3986 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003987 }
Winson Chung4b919f82012-05-01 10:44:08 -07003988 public void lockScreenOrientation() {
3989 if (isRotationEnabled()) {
3990 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3991 .getConfiguration().orientation));
3992 }
3993 }
3994 public void unlockScreenOrientation(boolean immediate) {
3995 if (isRotationEnabled()) {
3996 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003997 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003998 } else {
3999 mHandler.postDelayed(new Runnable() {
4000 public void run() {
4001 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4002 }
4003 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004004 }
Winson Chung4b919f82012-05-01 10:44:08 -07004005 }
Winson Chung400438b2011-01-16 17:53:48 -08004006 }
4007
Winson Chung82f55532011-08-09 14:14:23 -07004008 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004009 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004010 if (DISABLE_CLINGS) {
4011 return false;
4012 }
4013
Brett Chabot2a9e2282011-08-23 15:03:13 -07004014 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004015 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004016
Michael Jurkae233a8b2013-03-19 13:49:20 +01004017 // Restricted secondary users (child mode) will potentially have very few apps
4018 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004019// boolean supportsLimitedUsers =
4020// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4021// Account[] accounts = AccountManager.get(this).getAccounts();
4022// if (supportsLimitedUsers && accounts.length == 0) {
4023// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4024// Bundle restrictions = um.getUserRestrictions();
4025// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4026// return false;
4027// }
4028// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004029 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004030 }
Michael Jurka22143132012-10-04 17:42:38 +02004031
Winson Chung7d7541e2011-09-16 20:14:36 -07004032 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004033 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004034 if (cling != null) {
4035 cling.init(this, positionData);
4036 cling.setVisibility(View.VISIBLE);
4037 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4038 if (animate) {
4039 cling.buildLayer();
4040 cling.setAlpha(0f);
4041 cling.animate()
4042 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004043 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004044 .setDuration(SHOW_CLING_DURATION)
4045 .setStartDelay(delay)
4046 .start();
4047 } else {
4048 cling.setAlpha(1f);
4049 }
Michael Jurka22143132012-10-04 17:42:38 +02004050 cling.setFocusableInTouchMode(true);
4051 cling.post(new Runnable() {
4052 public void run() {
4053 cling.setFocusable(true);
4054 cling.requestFocus();
4055 }
4056 });
4057 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4058 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 }
4060 return cling;
4061 }
Michael Jurka22143132012-10-04 17:42:38 +02004062
Winson Chung7d7541e2011-09-16 20:14:36 -07004063 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004064 // To catch cases where siblings of top-level views are made invisible, just check whether
4065 // the cling is directly set to GONE before dismissing it.
4066 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004067 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004069 anim.addListener(new AnimatorListenerAdapter() {
4070 public void onAnimationEnd(Animator animation) {
4071 cling.setVisibility(View.GONE);
4072 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004073 // We should update the shared preferences on a background thread
4074 new Thread("dismissClingThread") {
4075 public void run() {
4076 SharedPreferences.Editor editor = mSharedPrefs.edit();
4077 editor.putBoolean(flag, true);
4078 editor.commit();
4079 }
4080 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004081 };
4082 });
4083 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004084 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004085 }
4086 }
Michael Jurka22143132012-10-04 17:42:38 +02004087
Winson Chung9d9d74f2011-09-19 11:49:12 -07004088 private void removeCling(int id) {
4089 final View cling = findViewById(id);
4090 if (cling != null) {
4091 final ViewGroup parent = (ViewGroup) cling.getParent();
4092 parent.post(new Runnable() {
4093 @Override
4094 public void run() {
4095 parent.removeView(cling);
4096 }
4097 });
Michael Jurka22143132012-10-04 17:42:38 +02004098 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004099 }
4100 }
Michael Jurka974c3862012-05-22 22:00:31 -07004101
4102 private boolean skipCustomClingIfNoAccounts() {
4103 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4104 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4105 if (customCling) {
4106 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004107 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004108 Account[] accounts = am.getAccountsByType("com.google");
4109 return accounts.length == 0;
4110 }
4111 return false;
4112 }
4113
Winson Chung7d7541e2011-09-16 20:14:36 -07004114 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004116 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004117 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4118 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004119 // If we're not using the default workspace layout, replace workspace cling
4120 // with a custom workspace cling (usually specified in an overlay)
4121 // For now, only do this on tablets
4122 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004123 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004124 // Use a custom cling
4125 View cling = findViewById(R.id.workspace_cling);
4126 ViewGroup clingParent = (ViewGroup) cling.getParent();
4127 int clingIndex = clingParent.indexOfChild(cling);
4128 clingParent.removeViewAt(clingIndex);
4129 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4130 clingParent.addView(customCling, clingIndex);
4131 customCling.setId(R.id.workspace_cling);
4132 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004133 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004134 } else {
4135 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004136 }
4137 }
4138 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004139 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004140 if (isClingsEnabled() &&
4141 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004143 } else {
4144 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004145 }
4146 }
4147 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004148 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004149 if (isClingsEnabled() &&
4150 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4151 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004152 } else {
4153 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004154 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004155 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004156 }
Michael Jurka104c4562013-07-08 18:03:46 -07004157 protected SharedPreferences getSharedPrefs() {
4158 return mSharedPrefs;
4159 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004160 public boolean isFolderClingVisible() {
4161 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004162 if (cling != null) {
4163 return cling.getVisibility() == View.VISIBLE;
4164 }
4165 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004166 }
Winson Chung82f55532011-08-09 14:14:23 -07004167 public void dismissWorkspaceCling(View v) {
4168 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004169 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004170 }
4171 public void dismissAllAppsCling(View v) {
4172 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004173 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4174 }
4175 public void dismissFolderCling(View v) {
4176 Cling cling = (Cling) findViewById(R.id.folder_cling);
4177 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004178 }
4179
Winson Chung80baf5a2010-08-09 16:03:15 -07004180 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004181 * Prints out out state for debugging.
4182 */
4183 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004184 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004185 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004186 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4187 Log.d(TAG, "mRestoring=" + mRestoring);
4188 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4189 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004190 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004191 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004192
Winson Chung785d2eb2011-04-14 16:08:02 -07004193 if (mAppsCustomizeContent != null) {
4194 mAppsCustomizeContent.dumpState();
4195 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004196 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004197 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004198
4199 @Override
4200 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4201 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004202 synchronized (sDumpLogs) {
4203 writer.println(" ");
4204 writer.println("Debug logs: ");
4205 for (int i = 0; i < sDumpLogs.size(); i++) {
4206 writer.println(" " + sDumpLogs.get(i));
4207 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004208 }
4209 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004210
4211 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004212 synchronized (sDumpLogs) {
4213 Log.d(TAG, "");
4214 Log.d(TAG, "*********************");
4215 Log.d(TAG, "Launcher debug logs: ");
4216 for (int i = 0; i < sDumpLogs.size(); i++) {
4217 Log.d(TAG, " " + sDumpLogs.get(i));
4218 }
4219 Log.d(TAG, "*********************");
4220 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004221 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004222 }
4223
4224 public static void addDumpLog(String tag, String log, boolean debugLog) {
4225 if (debugLog) {
4226 Log.d(tag, log);
4227 }
4228 sDateStamp.setTime(System.currentTimeMillis());
4229 synchronized (sDumpLogs) {
4230 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4231 }
4232 }
4233
4234 public void dumpLogsToLocalData(final boolean email) {
4235 new Thread("DumpLogsToLocalData") {
4236 @Override
4237 public void run() {
4238 boolean success = false;
4239 sDateStamp.setTime(sRunStart);
4240 String FILENAME = sDateStamp.getMonth() + "-"
4241 + sDateStamp.getDay() + "_"
4242 + sDateStamp.getHours() + "-"
4243 + sDateStamp.getMinutes() + "_"
4244 + sDateStamp.getSeconds() + ".txt";
4245
4246 FileOutputStream fos = null;
4247 File outFile = null;
4248 try {
4249 outFile = new File(getFilesDir(), FILENAME);
4250 outFile.createNewFile();
4251 fos = new FileOutputStream(outFile);
4252 } catch (Exception e) {
4253 e.printStackTrace();
4254 }
4255 if (fos != null) {
4256 PrintWriter writer = new PrintWriter(fos);
4257
4258 writer.println(" ");
4259 writer.println("Debug logs: ");
4260 synchronized (sDumpLogs) {
4261 for (int i = 0; i < sDumpLogs.size(); i++) {
4262 writer.println(" " + sDumpLogs.get(i));
4263 }
4264 }
4265 writer.close();
4266 }
4267 try {
4268 if (fos != null) {
4269 fos.close();
4270 success = true;
4271 }
4272 } catch (IOException e) {
4273 e.printStackTrace();
4274 }
4275
4276 if (success && email) {
4277 if (!emailSent()) {
4278 emailFile(outFile);
4279 }
4280 }
4281 }
4282 }.start();
4283 }
4284
4285 private void emailFile(File file) {
4286 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4287 try {
4288 copyFile(file, publicCopy);
4289 } catch (IOException e) {
4290 e.printStackTrace();
4291 return;
4292 }
4293
4294 Intent intent = new Intent(Intent.ACTION_SEND);
4295 intent.setType("text/plain");
4296 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4297 "mikejurka@google"});
4298 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4299 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4300
4301 if (!file.exists() || !file.canRead()) {
4302 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4303 finish();
4304 return;
4305 }
4306
4307 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4308 Uri uri = Uri.fromFile(publicCopy);
4309 intent.putExtra(Intent.EXTRA_STREAM, uri);
4310 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4311
4312 setEmailSent(true);
4313 }
4314
4315 public void copyFile(File src, File dst) throws IOException {
4316 InputStream in = new FileInputStream(src);
4317 OutputStream out = new FileOutputStream(dst);
4318
4319 // Transfer bytes from in to out
4320 byte[] buf = new byte[1024];
4321 int len;
4322 while ((len = in.read(buf)) > 0) {
4323 out.write(buf, 0, len);
4324 }
4325 in.close();
4326 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004328}
Michael Jurka2763be32011-02-24 11:19:57 -08004329
Michael Jurkaabded662011-03-04 12:06:57 -08004330interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004331 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004332 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004333 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004334 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004335 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004336}