blob: 35e166fc23497bfe892ebd24dadd113f9d78e698 [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();
922 }
923
924 public interface QSBScroller {
925 public void setScrollY(int scrollY);
926 }
927
Adam Cohen66a01fd2013-06-11 12:48:00 -0700928 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700929 public QSBScroller addToCustomContentPage(View customContent) {
930 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700931 }
932
Winson Chung98ca0f72013-07-29 12:58:51 -0700933 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700934 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700935 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700936 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700937 }
938
Adam Cohenedb40762013-07-18 16:45:45 -0700939 // The custom content needs to offset its content to account for the QSB
940 public int getTopOffsetForCustomContent() {
941 return mWorkspace.getPaddingTop();
942 }
943
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700944 @Override
945 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 // Flag the loader to stop early before switching
947 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700948 if (mAppsCustomizeContent != null) {
949 mAppsCustomizeContent.surrender();
950 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800951 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700952 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700953
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800954 // We can't hide the IME if it was forced open. So don't bother
955 /*
956 @Override
957 public void onWindowFocusChanged(boolean hasFocus) {
958 super.onWindowFocusChanged(hasFocus);
959
960 if (hasFocus) {
961 final InputMethodManager inputManager = (InputMethodManager)
962 getSystemService(Context.INPUT_METHOD_SERVICE);
963 WindowManager.LayoutParams lp = getWindow().getAttributes();
964 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
965 android.os.Handler()) {
966 protected void onReceiveResult(int resultCode, Bundle resultData) {
967 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
968 }
969 });
970 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
971 }
972 }
973 */
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 private boolean acceptFilter() {
976 final InputMethodManager inputManager = (InputMethodManager)
977 getSystemService(Context.INPUT_METHOD_SERVICE);
978 return !inputManager.isFullscreenMode();
979 }
980
981 @Override
982 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700983 final int uniChar = event.getUnicodeChar();
984 final boolean handled = super.onKeyDown(keyCode, event);
985 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
986 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
988 keyCode, event);
989 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700990 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700991 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700992 // showSearchDialog()
993 // If there are multiple keystrokes before the search dialog takes focus,
994 // onSearchRequested() will be called for every keystroke,
995 // but it is idempotent, so it's fine.
996 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 }
998 }
999
Joe Onorato8a9625e2010-01-28 15:55:35 -08001000 // Eat the long press event so the keyboard doesn't come up.
1001 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1002 return true;
1003 }
1004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 return handled;
1006 }
1007
Karl Rosaen138a0412009-04-23 19:00:21 -07001008 private String getTypedText() {
1009 return mDefaultKeySsb.toString();
1010 }
1011
1012 private void clearTypedText() {
1013 mDefaultKeySsb.clear();
1014 mDefaultKeySsb.clearSpans();
1015 Selection.setSelection(mDefaultKeySsb, 0);
1016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001019 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1020 * State
1021 */
1022 private static State intToState(int stateOrdinal) {
1023 State state = State.WORKSPACE;
1024 final State[] stateValues = State.values();
1025 for (int i = 0; i < stateValues.length; i++) {
1026 if (stateValues[i].ordinal() == stateOrdinal) {
1027 state = stateValues[i];
1028 break;
1029 }
1030 }
1031 return state;
1032 }
1033
1034 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 * Restores the previous state, if it exists.
1036 *
1037 * @param savedState The previous state.
1038 */
1039 private void restoreState(Bundle savedState) {
1040 if (savedState == null) {
1041 return;
1042 }
1043
Michael Jurka883f55b2010-10-21 15:47:14 -07001044 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001045 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001046 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001047 }
1048
Winson Chungf0c6ae02012-03-21 16:10:31 -07001049 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001051 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053
Winson Chung3d503fb2011-07-13 17:25:49 -07001054 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001055 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1058 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001059 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1061 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001062 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1063 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1064 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001065 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 mRestoring = true;
1067 }
1068
1069 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1070 if (renameFolder) {
1071 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001072 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 mRestoring = true;
1074 }
Winson Chunga12a2502010-12-20 14:41:35 -08001075
Winson Chung785d2eb2011-04-14 16:08:02 -07001076 // Restore the AppsCustomize tab
1077 if (mAppsCustomizeTabHost != null) {
1078 String curTab = savedState.getString("apps_customize_currentTab");
1079 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001080 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001081 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001082 mAppsCustomizeContent.loadAssociatedPages(
1083 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001084 }
1085
Winson Chung5afbf7b2011-07-25 11:53:08 -07001086 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1087 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
1090
1091 /**
1092 * Finds all the views we need and configure them properly.
1093 */
1094 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001095 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001096
Craig Mautner360310b2012-10-26 15:13:08 -07001097 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001098 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1099 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001100
1101 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1102 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103
Winson Chung4c98d922011-05-31 16:50:48 -07001104 // Setup the drag layer
1105 mDragLayer.setup(this, dragController);
1106
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 // Setup the hotseat
1108 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1109 if (mHotseat != null) {
1110 mHotseat.setup(this);
1111 }
1112
Adam Cohenf358a4b2013-07-23 16:47:31 -07001113 mOverviewPanel = findViewById(R.id.overview_panel);
1114 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1115 @Override
1116 public void onClick(View arg0) {
1117 showAllApps(true);
1118 }
1119 });
1120 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1121 @Override
1122 public void onClick(View arg0) {
1123 startWallpaper();
1124 }
1125 });
1126
Winson Chung4c98d922011-05-31 16:50:48 -07001127 // Setup the workspace
1128 mWorkspace.setHapticFeedbackEnabled(false);
1129 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001130 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001131 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001132
Winson Chungf0ea4d32011-06-06 14:27:16 -07001133 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001134 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001135
Winson Chungf0ea4d32011-06-06 14:27:16 -07001136 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001137 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001138 mAppsCustomizeContent = (AppsCustomizePagedView)
1139 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1140 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001141
Winson Chung3d503fb2011-07-13 17:25:49 -07001142 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001143 dragController.setDragScoller(mWorkspace);
1144 dragController.setScrollView(mDragLayer);
1145 dragController.setMoveTarget(mWorkspace);
1146 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001147 if (mSearchDropTargetBar != null) {
1148 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001149 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001150
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001151 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001152 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001153 mWeightWatcher = new WeightWatcher(this);
1154 mWeightWatcher.setAlpha(0.5f);
1155 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001156 new FrameLayout.LayoutParams(
1157 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001158 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001159 Gravity.BOTTOM)
1160 );
Adam Cohen39a06042013-07-19 14:30:12 -07001161
1162 boolean show = shouldShowWeightWatcher();
1163 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 }
1166
1167 /**
1168 * Creates a view representing a shortcut.
1169 *
1170 * @param info The data structure describing the shortcut.
1171 *
1172 * @return A View inflated from R.layout.application.
1173 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001174 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001176 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
1178
1179 /**
1180 * Creates a view representing a shortcut inflated from the specified resource.
1181 *
1182 * @param layoutResId The id of the XML layout used to create the shortcut.
1183 * @param parent The group the shortcut belongs to.
1184 * @param info The data structure describing the shortcut.
1185 *
1186 * @return A View inflated from layoutResId.
1187 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001188 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001189 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1190 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 return favorite;
1193 }
1194
1195 /**
1196 * Add an application shortcut to the workspace.
1197 *
1198 * @param data The intent describing the application.
1199 * @param cellInfo The position on screen where to create the shortcut.
1200 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001201 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001203 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204
Adam Cohenfbba09b2011-07-18 21:43:05 -07001205 // First we check if we already know the exact location where we want to add this item.
1206 if (cellX >= 0 && cellY >= 0) {
1207 cellXY[0] = cellX;
1208 cellXY[1] = cellY;
1209 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001210 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 return;
1212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001214 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001215
Romain Guy73b979d2009-06-09 12:57:21 -07001216 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001217 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001219 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001220 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001221 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001222 } else {
1223 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225 }
Romain Guycbb89e42009-06-08 15:52:54 -07001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 /**
1228 * Add a shortcut to the workspace.
1229 *
1230 * @param data The intent describing the shortcut.
1231 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001233 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001234 int cellY) {
1235 int[] cellXY = mTmpAddItemCellCoordinates;
1236 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001237 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001238
Michael Jurkad3ef3062010-11-23 16:23:58 -08001239 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001240
Adam Cohen558baaf2011-08-15 15:22:57 -07001241 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001242 if (info == null) {
1243 return;
1244 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001245 final View view = createShortcut(info);
1246
Adam Cohenfbba09b2011-07-18 21:43:05 -07001247 // First we check if we already know the exact location where we want to add this item.
1248 if (cellX >= 0 && cellY >= 0) {
1249 cellXY[0] = cellX;
1250 cellXY[1] = cellY;
1251 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001252
1253 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001254 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001255 true, null,null)) {
1256 return;
1257 }
1258 DragObject dragObject = new DragObject();
1259 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001260 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1261 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001262 return;
1263 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001264 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001265 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001266 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001267 foundCellSpan = (result != null);
1268 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001269 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001270 }
1271
1272 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001273 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 return;
1275 }
1276
Adam Cohendcd297f2013-06-18 13:13:40 -07001277 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278
1279 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001280 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001281 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
1283 }
1284
Adam Cohen2f093b62012-04-30 18:59:53 -07001285 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1286 int minHeight) {
1287 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001288 // We want to account for the extra amount of padding that we are adding to the widget
1289 // to ensure that it gets the full amount of space that it has requested
1290 int requiredWidth = minWidth + padding.left + padding.right;
1291 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001292 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001293 }
1294
Adam Cohen2f093b62012-04-30 18:59:53 -07001295 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1296 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001297 }
1298
Adam Cohen2f093b62012-04-30 18:59:53 -07001299 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1300 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001301 }
1302
Adam Cohen2f093b62012-04-30 18:59:53 -07001303 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1304 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001305 }
1306
Adam Cohen2f093b62012-04-30 18:59:53 -07001307 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1308 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001309 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001310 }
1311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001313 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001315 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001316 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001318 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001319 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1320 if (appWidgetInfo == null) {
1321 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1322 }
Romain Guycbb89e42009-06-08 15:52:54 -07001323
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001324 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001325 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001326
Adam Cohen2f093b62012-04-30 18:59:53 -07001327 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1328 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001331 // We have saved the position to which the widget was dragged-- this really only matters
1332 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 int[] cellXY = mTmpAddItemCellCoordinates;
1334 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001335 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001336 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1338 cellXY[0] = mPendingAddInfo.cellX;
1339 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 spanXY[0] = mPendingAddInfo.spanX;
1341 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001342 foundCellSpan = true;
1343 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001344 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001345 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001346 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1347 spanXY[1], cellXY, finalSpan);
1348 spanXY[0] = finalSpan[0];
1349 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001350 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001353 }
1354
Michael Jurka0280c3b2010-09-17 15:00:07 -07001355 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001356 if (appWidgetId != -1) {
1357 // Deleting an app widget ID is a void call but writes to disk before returning
1358 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001359 new Thread("deleteAppWidgetId") {
1360 public void run() {
1361 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1362 }
1363 }.start();
1364 }
Winson Chung93eef082012-03-23 15:59:27 -07001365 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001366 return;
1367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001369 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001370 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1371 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001372 launcherInfo.spanX = spanXY[0];
1373 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001374 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1375 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001378 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379
1380 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001381 if (hostView == null) {
1382 // Perform actual inflation because we're live
1383 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1384 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1385 } else {
1386 // The AppWidgetHostView has already been inflated and instantiated
1387 launcherInfo.hostView = hostView;
1388 }
Romain Guycbb89e42009-06-08 15:52:54 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001391 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001392 launcherInfo.notifyWidgetSizeChanged(this);
1393
Adam Cohendcd297f2013-06-18 13:13:40 -07001394 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001396
1397 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001399 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
Romain Guycbb89e42009-06-08 15:52:54 -07001401
Adam Cohended9f8d2010-11-03 13:25:16 -07001402 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1403 @Override
1404 public void onReceive(Context context, Intent intent) {
1405 final String action = intent.getAction();
1406 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1407 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001408 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001409 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001410
Winson Chungc100e8e2011-08-09 16:02:43 -07001411 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001412 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001413 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1414 mAppsCustomizeTabHost.reset();
1415 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001416 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001417 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1418 mUserPresent = true;
1419 updateRunning();
1420 }
1421 }
1422 };
1423
1424 @Override
1425 public void onAttachedToWindow() {
1426 super.onAttachedToWindow();
1427
1428 // Listen for broadcasts related to user-presence
1429 final IntentFilter filter = new IntentFilter();
1430 filter.addAction(Intent.ACTION_SCREEN_OFF);
1431 filter.addAction(Intent.ACTION_USER_PRESENT);
1432 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001433 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001434 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001435 mVisible = true;
1436 }
1437
1438 @Override
1439 public void onDetachedFromWindow() {
1440 super.onDetachedFromWindow();
1441 mVisible = false;
1442
Adam Cohend113e0c2010-11-11 10:48:05 -08001443 if (mAttached) {
1444 unregisterReceiver(mReceiver);
1445 mAttached = false;
1446 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001447 updateRunning();
1448 }
1449
1450 public void onWindowVisibilityChanged(int visibility) {
1451 mVisible = visibility == View.VISIBLE;
1452 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001453 // The following code used to be in onResume, but it turns out onResume is called when
1454 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1455 // is a more appropriate event to handle
1456 if (mVisible) {
1457 mAppsCustomizeTabHost.onWindowVisible();
1458 if (!mWorkspaceLoading) {
1459 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001460 // We want to let Launcher draw itself at least once before we force it to build
1461 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001462 // apps is nice and speedy.
1463 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001464 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001465 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001466 if (mStarted) return;
1467 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001468 // We delay the layer building a bit in order to give
1469 // other message processing a time to run. In particular
1470 // this avoids a delay in hiding the IME if it was
1471 // currently shown, because doing that may involve
1472 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001473 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001474 final ViewTreeObserver.OnDrawListener listener = this;
1475 mWorkspace.post(new Runnable() {
1476 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001477 if (mWorkspace != null &&
1478 mWorkspace.getViewTreeObserver() != null) {
1479 mWorkspace.getViewTreeObserver().
1480 removeOnDrawListener(listener);
1481 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001482 }
1483 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001484 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001485 }
1486 });
1487 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001488 // When Launcher comes back to foreground, a different Activity might be responsible for
1489 // the app market intent, so refresh the icon
1490 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001491 clearTypedText();
1492 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001493 }
1494
1495 private void sendAdvanceMessage(long delay) {
1496 mHandler.removeMessages(ADVANCE_MSG);
1497 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1498 mHandler.sendMessageDelayed(msg, delay);
1499 mAutoAdvanceSentTime = System.currentTimeMillis();
1500 }
1501
1502 private void updateRunning() {
1503 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1504 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1505 mAutoAdvanceRunning = autoAdvanceRunning;
1506 if (autoAdvanceRunning) {
1507 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1508 sendAdvanceMessage(delay);
1509 } else {
1510 if (!mWidgetsToAdvance.isEmpty()) {
1511 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1512 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1513 }
1514 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001515 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001516 }
1517 }
1518 }
1519
1520 private final Handler mHandler = new Handler() {
1521 @Override
1522 public void handleMessage(Message msg) {
1523 if (msg.what == ADVANCE_MSG) {
1524 int i = 0;
1525 for (View key: mWidgetsToAdvance.keySet()) {
1526 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1527 final int delay = mAdvanceStagger * i;
1528 if (v instanceof Advanceable) {
1529 postDelayed(new Runnable() {
1530 public void run() {
1531 ((Advanceable) v).advance();
1532 }
1533 }, delay);
1534 }
1535 i++;
1536 }
1537 sendAdvanceMessage(mAdvanceInterval);
1538 }
1539 }
1540 };
1541
1542 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001543 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001544 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1545 if (v instanceof Advanceable) {
1546 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001547 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001548 updateRunning();
1549 }
1550 }
1551
1552 void removeWidgetToAutoAdvance(View hostView) {
1553 if (mWidgetsToAdvance.containsKey(hostView)) {
1554 mWidgetsToAdvance.remove(hostView);
1555 updateRunning();
1556 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 }
1558
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001560 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 launcherInfo.hostView = null;
1562 }
1563
Winson Chung93eef082012-03-23 15:59:27 -07001564 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1565 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1566 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001567 }
1568
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001569 public LauncherAppWidgetHost getAppWidgetHost() {
1570 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Winson Chunga9abd0e2010-10-27 17:18:37 -07001573 public LauncherModel getModel() {
1574 return mModel;
1575 }
1576
Bjorn Bringertc459e522013-06-07 19:36:01 +01001577 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001578 getWindow().closeAllPanels();
1579
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001580 // Whatever we were doing is hereby canceled.
1581 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001582 }
1583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 @Override
1585 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001586 long startTime = 0;
1587 if (DEBUG_RESUME_TIME) {
1588 startTime = System.currentTimeMillis();
1589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 super.onNewIntent(intent);
1591
1592 // Close the menu
1593 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001594 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001595 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001596
Jamie Genniscb222e82012-10-23 15:44:26 -07001597 final boolean alreadyOnHome =
1598 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001599 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001600
Jamie Genniscb222e82012-10-23 15:44:26 -07001601 Runnable processIntent = new Runnable() {
1602 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001603 if (mWorkspace == null) {
1604 // Can be cases where mWorkspace is null, this prevents a NPE
1605 return;
1606 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001607 Folder openFolder = mWorkspace.getOpenFolder();
1608 // In all these cases, only animate if we're already on home
1609 mWorkspace.exitWidgetResizeMode();
1610 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1611 openFolder == null) {
1612 mWorkspace.moveToDefaultScreen(true);
1613 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001614
Jamie Genniscb222e82012-10-23 15:44:26 -07001615 closeFolder();
1616 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001617
Jamie Genniscb222e82012-10-23 15:44:26 -07001618 // If we are already on home, then just animate back to the workspace,
1619 // otherwise, just wait until onResume to set the state back to Workspace
1620 if (alreadyOnHome) {
1621 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001622 if (mWorkspace.isInOverviewMode()) {
1623 mWorkspace.exitOverviewMode();
1624 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001625 } else {
1626 mOnResumeState = State.WORKSPACE;
1627 }
1628
1629 final View v = getWindow().peekDecorView();
1630 if (v != null && v.getWindowToken() != null) {
1631 InputMethodManager imm = (InputMethodManager)getSystemService(
1632 INPUT_METHOD_SERVICE);
1633 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1634 }
1635
1636 // Reset AllApps to its initial state
1637 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1638 mAppsCustomizeTabHost.reset();
1639 }
1640 }
1641 };
1642
1643 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1644 // Delay processing of the intent to allow the status bar animation to finish
1645 // first in order to avoid janky animations.
1646 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001647 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001648 // Process the intent immediately.
1649 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001650 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
Michael Jurka447bf842013-05-15 14:52:15 +02001653 if (DEBUG_RESUME_TIME) {
1654 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
1658 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001659 public void onRestoreInstanceState(Bundle state) {
1660 super.onRestoreInstanceState(state);
1661 for (int page: mSynchronouslyBoundPages) {
1662 mWorkspace.restoreInstanceStateForChild(page);
1663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665
1666 @Override
1667 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001668 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001669 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670
Michael Jurka883f55b2010-10-21 15:47:14 -07001671 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001672 // We close any open folder since it will not be re-opened, and we need to make sure
1673 // this state is reflected.
1674 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675
Adam Cohendcd297f2013-06-18 13:13:40 -07001676 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001677 mWaitingForResult) {
1678 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001679 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1681 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001682 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1683 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1684 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 }
1686
1687 if (mFolderInfo != null && mWaitingForResult) {
1688 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1689 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1690 }
Michael Jurka946ad472010-07-09 18:05:18 -07001691
Winson Chung785d2eb2011-04-14 16:08:02 -07001692 // Save the current AppsCustomize tab
1693 if (mAppsCustomizeTabHost != null) {
1694 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1695 if (currentTabTag != null) {
1696 outState.putString("apps_customize_currentTab", currentTabTag);
1697 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001698 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1699 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 }
1702
1703 @Override
1704 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001706
Winson Chunge7a03942011-08-05 15:05:12 -07001707 // Remove all pending runnables
1708 mHandler.removeMessages(ADVANCE_MSG);
1709 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001710 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001711
Winson Chungcd2b0142011-06-08 16:02:26 -07001712 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001713 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001714 mModel.stopLoader();
1715 app.setLauncher(null);
1716
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001718 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001720 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001722 mAppWidgetHost = null;
1723
1724 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725
1726 TextKeyListener.getInstance().release();
1727
Winson Chung81b52252012-08-27 15:34:29 -07001728 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1729 // to prevent leaking Launcher activities on orientation change.
1730 if (mModel != null) {
1731 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1732 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001733
1734 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001735 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001736
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001737 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001738 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1739 mWorkspace.removeAllViews();
1740 mWorkspace = null;
1741 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001742
Michael Jurka2ecf9952012-06-18 12:52:28 -07001743 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 }
1745
Adam Cohena9cf38f2011-05-02 15:36:58 -07001746 public DragController getDragController() {
1747 return mDragController;
1748 }
1749
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 @Override
1751 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001752 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 super.startActivityForResult(intent, requestCode);
1754 }
1755
Winson Chung70d72102011-08-12 11:24:35 -07001756 /**
1757 * Indicates that we want global search for this activity by setting the globalSearch
1758 * argument for {@link #startSearch} to true.
1759 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001761 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001763
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001764 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001765
Karl Rosaen138a0412009-04-23 19:00:21 -07001766 if (initialQuery == null) {
1767 // Use any text typed in the launcher as the initial query
1768 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 if (appSearchData == null) {
1771 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001772 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
Winson Chungadf0c182012-08-23 14:59:07 -07001774 Rect sourceBounds = new Rect();
1775 if (mSearchDropTargetBar != null) {
1776 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1777 }
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Bjorn Bringertc459e522013-06-07 19:36:01 +01001779 startSearch(initialQuery, selectInitialQuery,
1780 appSearchData, sourceBounds);
1781 }
1782
1783 public void startSearch(String initialQuery,
1784 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001785 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001786 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001787 }
1788
1789 /**
1790 * Starts the global search activity. This code is a copied from SearchManager
1791 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001792 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001793 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001794 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001795 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1796 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1797 if (globalSearchActivity == null) {
1798 Log.w(TAG, "No global search activity found.");
1799 return;
1800 }
1801 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1802 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1803 intent.setComponent(globalSearchActivity);
1804 // Make sure that we have a Bundle to put source in
1805 if (appSearchData == null) {
1806 appSearchData = new Bundle();
1807 } else {
1808 appSearchData = new Bundle(appSearchData);
1809 }
1810 // Set source to package name of app that starts global search, if not set already.
1811 if (!appSearchData.containsKey("source")) {
1812 appSearchData.putString("source", getPackageName());
1813 }
1814 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1815 if (!TextUtils.isEmpty(initialQuery)) {
1816 intent.putExtra(SearchManager.QUERY, initialQuery);
1817 }
1818 if (selectInitialQuery) {
1819 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1820 }
1821 intent.setSourceBounds(sourceBounds);
1822 try {
1823 startActivity(intent);
1824 } catch (ActivityNotFoundException ex) {
1825 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
1828
Winson Chung70d72102011-08-12 11:24:35 -07001829 @Override
1830 public boolean onCreateOptionsMenu(Menu menu) {
1831 if (isWorkspaceLocked()) {
1832 return false;
1833 }
1834
1835 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001836
1837 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1838 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1839 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001840 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1841 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1842 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001843 String helpUrl = getString(R.string.help_url);
1844 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001845 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1846 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1847
Winson Chung70d72102011-08-12 11:24:35 -07001848 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1849 .setIcon(android.R.drawable.ic_menu_gallery)
1850 .setAlphabeticShortcut('W');
1851 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1852 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001853 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001854 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001855 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1856 .setIcon(android.R.drawable.ic_menu_preferences)
1857 .setIntent(settings)
1858 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001859 if (!helpUrl.isEmpty()) {
1860 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1861 .setIcon(android.R.drawable.ic_menu_help)
1862 .setIntent(help)
1863 .setAlphabeticShortcut('H');
1864 }
Winson Chung70d72102011-08-12 11:24:35 -07001865 return true;
1866 }
1867
1868 @Override
1869 public boolean onPrepareOptionsMenu(Menu menu) {
1870 super.onPrepareOptionsMenu(menu);
1871
1872 if (mAppsCustomizeTabHost.isTransitioning()) {
1873 return false;
1874 }
1875 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1876 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1877
1878 return true;
1879 }
1880
1881 @Override
1882 public boolean onOptionsItemSelected(MenuItem item) {
1883 switch (item.getItemId()) {
1884 case MENU_WALLPAPER_SETTINGS:
1885 startWallpaper();
1886 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001887 }
1888
1889 return super.onOptionsItemSelected(item);
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001892 @Override
1893 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001894 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001895 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001896 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001897 }
1898
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 public boolean isWorkspaceLocked() {
1900 return mWorkspaceLoading || mWaitingForResult;
1901 }
1902
Michael Jurka0280c3b2010-09-17 15:00:07 -07001903 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001904 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001905 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001906 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1907 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001908 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001910 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001911
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001912 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1913 AppWidgetProviderInfo appWidgetInfo) {
1914 if (appWidgetInfo.configure != null) {
1915 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001916
Bjorn Bringert7984c942009-12-09 15:38:25 +00001917 // Launch over to configure widget, if needed
1918 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001919 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001920 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001921 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001923 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001924 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001925 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001926 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001927 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
1929 }
Romain Guycbb89e42009-06-08 15:52:54 -07001930
Adam Cohenfbba09b2011-07-18 21:43:05 -07001931 /**
1932 * Process a shortcut drop.
1933 *
1934 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001936 * @param cell The cell it should be added to, optional
1937 * @param position The location on the screen where it was dropped, optional
1938 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001939 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001940 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001941 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001942 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001943 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001944 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001945
1946 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 mPendingAddInfo.cellX = cell[0];
1948 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001949 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001950
1951 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1952 createShortcutIntent.setComponent(componentName);
1953 processShortcut(createShortcutIntent);
1954 }
1955
Adam Cohenfbba09b2011-07-18 21:43:05 -07001956 /**
1957 * Process a widget drop.
1958 *
1959 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001961 * @param cell The cell it should be added to, optional
1962 * @param position The location on the screen where it was dropped, optional
1963 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001965 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001966 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001968 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001969 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001970 mPendingAddInfo.minSpanX = info.minSpanX;
1971 mPendingAddInfo.minSpanY = info.minSpanY;
1972
Adam Cohenfbba09b2011-07-18 21:43:05 -07001973 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001974 mPendingAddInfo.cellX = cell[0];
1975 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001976 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001977 if (span != null) {
1978 mPendingAddInfo.spanX = span[0];
1979 mPendingAddInfo.spanY = span[1];
1980 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001981
Adam Cohened66b2b2012-01-23 17:28:51 -08001982 AppWidgetHostView hostView = info.boundWidget;
1983 int appWidgetId;
1984 if (hostView != null) {
1985 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001986 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001987 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001988 // In this case, we either need to start an activity to get permission to bind
1989 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001990 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001991 Bundle options = info.bindOptions;
1992
1993 boolean success = false;
1994 if (options != null) {
1995 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1996 info.componentName, options);
1997 } else {
1998 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1999 info.componentName);
2000 }
2001 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002002 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002003 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002004 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002005 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2006 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2007 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002008 // TODO: we need to make sure that this accounts for the options bundle.
2009 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002010 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2011 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002012 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002013 }
2014
Joe Onoratodeb98af2010-02-19 14:59:39 -08002015 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002016 // Handle case where user selected "Applications"
2017 String applicationName = getResources().getString(R.string.group_applications);
2018 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002019
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002020 if (applicationName != null && applicationName.equals(shortcutName)) {
2021 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2022 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002023
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002024 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2025 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002026 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002027 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002028 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002029 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 }
2032
Winson Chung24ab2f12010-09-16 14:10:47 -07002033 void processWallpaper(Intent intent) {
2034 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2035 }
2036
Adam Cohendcd297f2013-06-18 13:13:40 -07002037 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002038 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002039 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 folderInfo.title = getText(R.string.folder_name);
2041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002043 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002044 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002045 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046
2047 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002048 FolderIcon newFolder =
2049 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002050 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002051 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002052 // Force measure the new folder icon
2053 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2054 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002055 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
Romain Guycbb89e42009-06-08 15:52:54 -07002057
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002059 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002060 }
2061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002063 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002065 pickWallpaper.setComponent(
2066 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2067 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
2069
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002070 /**
2071 * Registers various content observers. The current implementation registers
2072 * only a favorites observer to keep track of the favorites applications.
2073 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002074 private void registerContentObservers() {
2075 ContentResolver resolver = getContentResolver();
2076 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2077 true, mWidgetObserver);
2078 }
2079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
2081 public boolean dispatchKeyEvent(KeyEvent event) {
2082 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2083 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002085 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002086 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002087 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002088 dumpState();
2089 return true;
2090 }
2091 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002092 }
2093 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2094 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002095 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 return true;
2097 }
2098 }
2099
2100 return super.dispatchKeyEvent(event);
2101 }
2102
Joe Onorato88ec0992009-11-19 13:16:06 -08002103 @Override
2104 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002105 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002106 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002107 } else if (mWorkspace.isInOverviewMode()) {
2108 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002109 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002110 Folder openFolder = mWorkspace.getOpenFolder();
2111 if (openFolder.isEditingName()) {
2112 openFolder.dismissEditingName();
2113 } else {
2114 closeFolder();
2115 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002116 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002117 mWorkspace.exitWidgetResizeMode();
2118
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002119 // Back button is a no-op here, but give at least some feedback for the button press
2120 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002121 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002122 }
2123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002125 * Re-listen when widgets are reset.
2126 */
2127 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002128 if (mAppWidgetHost != null) {
2129 mAppWidgetHost.startListening();
2130 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002131 }
2132
2133 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 * Launches the intent referred by the clicked shortcut.
2135 *
2136 * @param v The view representing the clicked shortcut.
2137 */
2138 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002139 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2140 // view has detached (it's possible for this to happen if the view is removed mid touch).
2141 if (v.getWindowToken() == null) {
2142 return;
2143 }
2144
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002145 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002146 return;
2147 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002148
Adam Cohenf358a4b2013-07-23 16:47:31 -07002149 if (v instanceof PageIndicator) {
2150 if (!mWorkspace.isInOverviewMode()) {
2151 mWorkspace.enterOverviewMode();
2152 }
2153 return;
2154 }
2155
2156 if (v instanceof CellLayout) {
2157 if (mWorkspace.isInOverviewMode()) {
2158 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2159 }
2160 }
2161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002163 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002165 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2166 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002167
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002168 // Check for special shortcuts
2169 if (intent.getComponent() != null) {
2170 final String shortcutClass = intent.getComponent().getClassName();
2171
2172 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2173 showAllApps(true);
2174 return;
2175 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2176 MemoryDumpActivity.startDump(this);
2177 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002178 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2179 toggleShowWeightWatcher();
2180 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002181 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002182 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002183
2184 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002185 int[] pos = new int[2];
2186 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002187 intent.setSourceBounds(new Rect(pos[0], pos[1],
2188 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002189
2190 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002191
Daniel Sandlerff02d492013-08-05 02:12:05 -04002192 mStats.recordLaunch(intent, shortcut);
2193
Michael Jurkaddd62e92011-02-16 17:49:14 -08002194 if (success && v instanceof BubbleTextView) {
2195 mWaitingForResume = (BubbleTextView) v;
2196 mWaitingForResume.setStayPressed(true);
2197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002199 if (v instanceof FolderIcon) {
2200 FolderIcon fi = (FolderIcon) v;
2201 handleFolderClick(fi);
2202 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002203 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002204 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002205 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002206 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002207 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
2210 }
2211
Michael Jurka0e260592010-06-30 17:07:39 -07002212 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002213 return false;
2214 }
2215
Michael Jurkaaf442092010-06-10 17:01:57 -07002216 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002217 * Event handler for the search button
2218 *
2219 * @param v The view that was clicked.
2220 */
2221 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002222 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2223
Amith Yamasania135ba82011-08-09 17:42:01 -07002224 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002225 }
2226
2227 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002228 * Event handler for the voice button
2229 *
2230 * @param v The view that was clicked.
2231 */
2232 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002233 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002234
Bjorn Bringertc459e522013-06-07 19:36:01 +01002235 startVoice();
2236 }
2237
2238 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002239 try {
2240 final SearchManager searchManager =
2241 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2242 ComponentName activityName = searchManager.getGlobalSearchActivity();
2243 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002244 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002245 if (activityName != null) {
2246 intent.setPackage(activityName.getPackageName());
2247 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002248 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002249 } catch (ActivityNotFoundException e) {
2250 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002251 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002252 startActivitySafely(null, intent, "onClickVoiceButton");
2253 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002254 }
2255
2256 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002257 * Event handler for the "grid" button that appears on the home screen, which
2258 * enters all apps mode.
2259 *
2260 * @param v The view that was clicked.
2261 */
2262 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002263 showAllApps(true);
2264 }
2265
2266 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002267 // Provide the same haptic feedback that the system offers for virtual keys.
2268 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002269 }
2270
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002271 public void onClickAppMarketButton(View v) {
2272 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002273 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002274 } else {
2275 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002276 }
2277 }
2278
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002279 void startApplicationDetailsActivity(ComponentName componentName) {
2280 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002281 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2282 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002283 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002284 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002285 }
2286
Michael Jurka1e2f4652013-07-08 18:03:46 -07002287 // returns true if the activity was started
2288 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2289 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002290 // System applications cannot be installed. For now, show a toast explaining that.
2291 // We may give them the option of disabling apps this way.
2292 int messageId = R.string.uninstall_system_app_text;
2293 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002294 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002295 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002296 String packageName = componentName.getPackageName();
2297 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002298 Intent intent = new Intent(
2299 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002300 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2301 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002302 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002303 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002304 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002305 }
2306
Michael Jurka86a720e2012-05-09 11:23:23 -07002307 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002311 // Only launch using the new animation if the shortcut has not opted out (this is a
2312 // private contract between launcher and may be ignored in the future).
2313 boolean useLaunchAnimation = (v != null) &&
2314 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2315 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002316 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2317 v.getMeasuredWidth(), v.getMeasuredHeight());
2318
2319 startActivity(intent, opts.toBundle());
2320 } else {
2321 startActivity(intent);
2322 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002323 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 } catch (SecurityException e) {
2325 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002326 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002328 "or use the exported attribute for this activity. "
2329 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002331 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002333
Michael Jurka86a720e2012-05-09 11:23:23 -07002334 boolean startActivitySafely(View v, Intent intent, Object tag) {
2335 boolean success = false;
2336 try {
2337 success = startActivity(v, intent, tag);
2338 } catch (ActivityNotFoundException e) {
2339 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2340 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2341 }
2342 return success;
2343 }
2344
Adam Cohena9cf38f2011-05-02 15:36:58 -07002345 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002346 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002347 Folder openFolder = mWorkspace.getFolderForTag(info);
2348
2349 // If the folder info reports that the associated folder is open, then verify that
2350 // it is actually opened. There have been a few instances where this gets out of sync.
2351 if (info.opened && openFolder == null) {
2352 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002354 info.opened = false;
2355 }
2356
Adam Cohenfb91f302012-06-11 15:45:18 -07002357 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 // Close any open folder
2359 closeFolder();
2360 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002361 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 } else {
2363 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 int folderScreen;
2365 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002366 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 // .. and close it
2368 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002369 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 // Close any folder open on the current screen
2371 closeFolder();
2372 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002373 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 }
2375 }
2376 }
2377 }
2378
Adam Cohen268c4752012-06-06 17:47:33 -07002379 /**
2380 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2381 * in the DragLayer in the exact absolute location of the original FolderIcon.
2382 */
2383 private void copyFolderIconToImage(FolderIcon fi) {
2384 final int width = fi.getMeasuredWidth();
2385 final int height = fi.getMeasuredHeight();
2386
2387 // Lazy load ImageView, Bitmap and Canvas
2388 if (mFolderIconImageView == null) {
2389 mFolderIconImageView = new ImageView(this);
2390 }
2391 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2392 mFolderIconBitmap.getHeight() != height) {
2393 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2394 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2395 }
2396
2397 DragLayer.LayoutParams lp;
2398 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2399 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2400 } else {
2401 lp = new DragLayer.LayoutParams(width, height);
2402 }
2403
Adam Cohen307fe232012-08-16 17:55:58 -07002404 // The layout from which the folder is being opened may be scaled, adjust the starting
2405 // view size by this scale factor.
2406 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002407 lp.customPosition = true;
2408 lp.x = mRectForFolderAnimation.left;
2409 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002410 lp.width = (int) (scale * width);
2411 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002412
2413 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2414 fi.draw(mFolderIconCanvas);
2415 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002416 if (fi.getFolder() != null) {
2417 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2418 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002419 }
Adam Cohen268c4752012-06-06 17:47:33 -07002420 // Just in case this image view is still in the drag layer from a previous animation,
2421 // we remove it and re-add it.
2422 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2423 mDragLayer.removeView(mFolderIconImageView);
2424 }
2425 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002426 if (fi.getFolder() != null) {
2427 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002428 }
Adam Cohen268c4752012-06-06 17:47:33 -07002429 }
2430
Adam Cohen2801caf2011-05-13 20:57:39 -07002431 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002432 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002433 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2434 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2435 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2436
Adam Cohenc51934b2011-07-26 21:07:43 -07002437 FolderInfo info = (FolderInfo) fi.getTag();
2438 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2439 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002440 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2441 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002442 }
2443
Adam Cohen268c4752012-06-06 17:47:33 -07002444 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2445 copyFolderIconToImage(fi);
2446 fi.setVisibility(View.INVISIBLE);
2447
Michael Jurka2ecf9952012-06-18 12:52:28 -07002448 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002449 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002450 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2451 oa.start();
2452 }
2453
Adam Cohen268c4752012-06-06 17:47:33 -07002454 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002455 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002456 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2457 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2458 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2459
Adam Cohen268c4752012-06-06 17:47:33 -07002460 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002461
Adam Cohen268c4752012-06-06 17:47:33 -07002462 // We remove and re-draw the FolderIcon in-case it has changed
2463 mDragLayer.removeView(mFolderIconImageView);
2464 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002465 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002466 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002467 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002468 oa.addListener(new AnimatorListenerAdapter() {
2469 @Override
2470 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002471 if (cl != null) {
2472 cl.clearFolderLeaveBehind();
2473 // Remove the ImageView copy of the FolderIcon and make the original visible.
2474 mDragLayer.removeView(mFolderIconImageView);
2475 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002476 }
2477 }
2478 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002479 oa.start();
2480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002483 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 * is animated relative to the specified View. If the View is null, no animation
2485 * is played.
2486 *
2487 * @param folderInfo The FolderInfo describing the folder to open.
2488 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002489 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002490 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002491 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492
Adam Cohena9cf38f2011-05-02 15:36:58 -07002493 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494
Adam Cohen4554ee12011-08-03 16:13:21 -07002495 // Just verify that the folder hasn't already been added to the DragLayer.
2496 // There was a one-off crash where the folder had a parent already.
2497 if (folder.getParent() == null) {
2498 mDragLayer.addView(folder);
2499 mDragController.addDropTarget((DropTarget) folder);
2500 } else {
2501 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2502 folder.getParent() + ").");
2503 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002504 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002505 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002506
2507 // Notify the accessibility manager that this folder "window" has appeared and occluded
2508 // the workspace items
2509 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2510 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002511 }
2512
2513 public void closeFolder() {
2514 Folder folder = mWorkspace.getOpenFolder();
2515 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002516 if (folder.isEditingName()) {
2517 folder.dismissEditingName();
2518 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002519 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002520
2521 // Dismiss the folder cling
2522 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002523 }
2524 }
2525
2526 void closeFolder(Folder folder) {
2527 folder.getInfo().opened = false;
2528
2529 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2530 if (parent != null) {
2531 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2532 shrinkAndFadeInFolderIcon(fi);
2533 }
2534 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002535
2536 // Notify the accessibility manager that this folder "window" has disappeard and no
2537 // longer occludeds the workspace items
2538 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 }
2540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002542 if (!isDraggingEnabled()) return false;
2543 if (isWorkspaceLocked()) return false;
2544 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545
2546 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002547 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 }
2549
Michael Jurka0280c3b2010-09-17 15:00:07 -07002550 resetAddInfo();
2551 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002553 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 return true;
2555 }
2556
Winson Chung3d503fb2011-07-13 17:25:49 -07002557 // The hotseat touch handling does not go through Workspace, and we always allow long press
2558 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002559 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002560 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2561 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002562 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002563 if (mWorkspace.hasNonCustomEmptyScreens()) {
2564 // User long pressed on empty space
2565 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2566 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2567 // Disabling reordering until we sort out some issues.
2568 if (mWorkspace.isInOverviewMode()) {
2569 mWorkspace.startReordering(v);
2570 } else {
2571 mWorkspace.enterOverviewMode();
2572 }
Adam Cohendedbd962013-07-11 14:21:49 -07002573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002575 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002577 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 }
2579 }
2580 }
2581 return true;
2582 }
2583
Winson Chung3d503fb2011-07-13 17:25:49 -07002584 boolean isHotseatLayout(View layout) {
2585 return mHotseat != null && layout != null &&
2586 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2587 }
2588 Hotseat getHotseat() {
2589 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002590 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002591 View getOverviewPanel() {
2592 return mOverviewPanel;
2593 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002594 SearchDropTargetBar getSearchBar() {
2595 return mSearchDropTargetBar;
2596 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002597
Winson Chung3d503fb2011-07-13 17:25:49 -07002598 /**
2599 * Returns the CellLayout of the specified container at the specified screen.
2600 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002601 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002602 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2603 if (mHotseat != null) {
2604 return mHotseat.getLayout();
2605 } else {
2606 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002607 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002608 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002609 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002610 }
2611 }
2612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 Workspace getWorkspace() {
2614 return mWorkspace;
2615 }
2616
Daniel Sandler843e8602010-06-07 14:59:01 -04002617 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002618 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002619 }
2620
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002621 /**
2622 * Helper method for the cameraZoomIn/cameraZoomOut animations
2623 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 * @param scaleFactor The scale factor used for the zoom
2625 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002626 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002628 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002630
Winson Chung18f41f82012-05-09 13:28:10 -07002631 void disableWallpaperIfInAllApps() {
2632 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002633 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002634 if (mAppsCustomizeTabHost != null &&
2635 !mAppsCustomizeTabHost.isTransitioning()) {
2636 updateWallpaperVisibility(false);
2637 }
2638 }
2639 }
2640
Craig Mautner360310b2012-10-26 15:13:08 -07002641 private void setWorkspaceBackground(boolean workspace) {
2642 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002643 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002644 }
2645
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002646 void updateWallpaperVisibility(boolean visible) {
2647 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2648 int curflags = getWindow().getAttributes().flags
2649 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2650 if (wpflags != curflags) {
2651 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2652 }
Craig Mautner360310b2012-10-26 15:13:08 -07002653 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002654 }
2655
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002656 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2657 if (v instanceof LauncherTransitionable) {
2658 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2659 }
2660 }
2661
Michael Jurkabed61d22012-02-14 22:51:29 -08002662 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2663 if (v instanceof LauncherTransitionable) {
2664 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2665 }
Winson Chung70442722012-02-10 15:43:22 -08002666
2667 // Update the workspace transition step as well
2668 dispatchOnLauncherTransitionStep(v, 0f);
2669 }
2670
2671 private void dispatchOnLauncherTransitionStep(View v, float t) {
2672 if (v instanceof LauncherTransitionable) {
2673 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2674 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002675 }
2676
2677 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2678 if (v instanceof LauncherTransitionable) {
2679 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2680 }
Winson Chung70442722012-02-10 15:43:22 -08002681
2682 // Update the workspace transition step as well
2683 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002684 }
2685
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002686 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002687 * Things to test when changing the following seven functions.
2688 * - Home from workspace
2689 * - from center screen
2690 * - from other screens
2691 * - Home from all apps
2692 * - from center screen
2693 * - from other screens
2694 * - Back from all apps
2695 * - from center screen
2696 * - from other screens
2697 * - Launch app from workspace and quit
2698 * - with back
2699 * - with home
2700 * - Launch app from all apps and quit
2701 * - with back
2702 * - with home
2703 * - Go to a screen that's not the default, then all
2704 * apps, and launch and app, and go back
2705 * - with back
2706 * -with home
2707 * - On workspace, long press power and go back
2708 * - with back
2709 * - with home
2710 * - On all apps, long press power and go back
2711 * - with back
2712 * - with home
2713 * - On workspace, power off
2714 * - On all apps, power off
2715 * - Launch an app and turn off the screen while in that app
2716 * - Go back with home key
2717 * - Go back with back key TODO: make this not go to workspace
2718 * - From all apps
2719 * - From workspace
2720 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2721 * - From all apps
2722 * - From the center workspace
2723 * - From another workspace
2724 */
2725
2726 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002727 * Zoom the camera out from the workspace to reveal 'toView'.
2728 * Assumes that the view to show is anchored at either the very top or very bottom
2729 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002730 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002731 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002732 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002733 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002734 mStateAnimation.cancel();
2735 mStateAnimation = null;
2736 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002737 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002738
Winson Chungf0ea4d32011-06-06 14:27:16 -07002739 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2740 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2741 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002742 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002743 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002744 final int startDelay =
2745 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002746
Michael Jurkab3e22d92011-10-31 15:58:33 -07002747 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002748
Michael Jurkad74c9842011-07-10 12:44:21 -07002749 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002750 Animator workspaceAnim =
2751 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752
2753 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002754 toView.setScaleX(scale);
2755 toView.setScaleY(scale);
2756 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2757 scaleAnim.
2758 scaleX(1f).scaleY(1f).
2759 setDuration(duration).
2760 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002761
Winson Chungf0ea4d32011-06-06 14:27:16 -07002762 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002763 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002764 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002765 .ofFloat(toView, "alpha", 0f, 1f)
2766 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002767 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002768 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2769 @Override
2770 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002771 if (animation == null) {
2772 throw new RuntimeException("animation is null");
2773 }
Winson Chung70442722012-02-10 15:43:22 -08002774 float t = (Float) animation.getAnimatedValue();
2775 dispatchOnLauncherTransitionStep(fromView, t);
2776 dispatchOnLauncherTransitionStep(toView, t);
2777 }
2778 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002779
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002780 // toView should appear right at the end of the workspace shrink
2781 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002782 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002783 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002784 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002785
2786 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002787 boolean animationCancelled = false;
2788
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002789 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002790 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002791 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792 // Prepare the position
2793 toView.setTranslationX(0.0f);
2794 toView.setTranslationY(0.0f);
2795 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002796 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002798 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002799 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002800 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2801 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002802
Adam Cohenf4ddea32011-09-12 13:46:42 -07002803 if (!animationCancelled) {
2804 updateWallpaperVisibility(false);
2805 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002806
2807 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002808 if (mSearchDropTargetBar != null) {
2809 mSearchDropTargetBar.hideSearchBar(false);
2810 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002811 }
2812
Adam Cohencff6af82011-09-13 14:51:53 -07002813 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002814 public void onAnimationCancel(Animator animation) {
2815 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002816 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002817 });
2818
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002819 if (workspaceAnim != null) {
2820 mStateAnimation.play(workspaceAnim);
2821 }
Michael Jurka1899a362011-11-03 13:50:45 -07002822
2823 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002824
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002825 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2826 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002827
2828 // If any of the objects being animated haven't been measured/laid out
2829 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002830 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002831 (mWorkspace.getMeasuredWidth() == 0) ||
2832 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002833 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002834 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002835
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002836 final AnimatorSet stateAnimation = mStateAnimation;
2837 final Runnable startAnimRunnable = new Runnable() {
2838 public void run() {
2839 // Check that mStateAnimation hasn't changed while
2840 // we waited for a layout/draw pass
2841 if (mStateAnimation != stateAnimation)
2842 return;
2843 setPivotsForZoom(toView, scale);
2844 dispatchOnLauncherTransitionStart(fromView, animated, false);
2845 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002846 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002847 }
2848 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002849 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002850 final ViewTreeObserver observer = toView.getViewTreeObserver();
2851 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2852 public void onGlobalLayout() {
2853 startAnimRunnable.run();
2854 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2855 }
2856 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002857 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002858 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002859 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 } else {
2861 toView.setTranslationX(0.0f);
2862 toView.setTranslationY(0.0f);
2863 toView.setScaleX(1.0f);
2864 toView.setScaleY(1.0f);
2865 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002866 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002867
Daniel Sandlere4f98912013-06-25 15:13:26 -04002868 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002869 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002870 if (mSearchDropTargetBar != null) {
2871 mSearchDropTargetBar.hideSearchBar(false);
2872 }
Michael Jurkaabded662011-03-04 12:06:57 -08002873 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002874 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002875 dispatchOnLauncherTransitionStart(fromView, animated, false);
2876 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002877 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002878 dispatchOnLauncherTransitionStart(toView, animated, false);
2879 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002880 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002881 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002882 }
2883
2884 /**
2885 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002886 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002887 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002888 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002889 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2890 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002891
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002893 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002894 mStateAnimation.cancel();
2895 mStateAnimation = null;
2896 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002897 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002898
Winson Chungf0ea4d32011-06-06 14:27:16 -07002899 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002900 final int fadeOutDuration =
2901 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 final float scaleFactor = (float)
2903 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2904 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002905 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002906 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002907 if (toState == State.WORKSPACE) {
2908 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2909 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002910 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002911 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2912 workspaceAnim = mWorkspace.getChangeStateAnimation(
2913 Workspace.State.SPRING_LOADED, animated);
2914 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002915
Michael Jurkab3e22d92011-10-31 15:58:33 -07002916 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002917 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002918 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002919 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002920 final LauncherViewPropertyAnimator scaleAnim =
2921 new LauncherViewPropertyAnimator(fromView);
2922 scaleAnim.
2923 scaleX(scaleFactor).scaleY(scaleFactor).
2924 setDuration(duration).
2925 setInterpolator(new Workspace.ZoomInInterpolator());
2926
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002927 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002928 .ofFloat(fromView, "alpha", 1f, 0f)
2929 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002930 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002931 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2932 @Override
2933 public void onAnimationUpdate(ValueAnimator animation) {
2934 float t = 1f - (Float) animation.getAnimatedValue();
2935 dispatchOnLauncherTransitionStep(fromView, t);
2936 dispatchOnLauncherTransitionStep(toView, t);
2937 }
2938 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002939
Michael Jurka2ecf9952012-06-18 12:52:28 -07002940 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002941
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002942 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2943 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002944 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002945
2946 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002947 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002948 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002949 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002950 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002951 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2952 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002953 if (onCompleteRunnable != null) {
2954 onCompleteRunnable.run();
2955 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002956 mAppsCustomizeContent.updateCurrentPageScroll();
2957 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002958 }
2959 });
2960
Winson Chungf0ea4d32011-06-06 14:27:16 -07002961 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002962 if (workspaceAnim != null) {
2963 mStateAnimation.play(workspaceAnim);
2964 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002965 dispatchOnLauncherTransitionStart(fromView, animated, true);
2966 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002967 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002968 } else {
2969 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002970 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002971 dispatchOnLauncherTransitionStart(fromView, animated, true);
2972 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002973 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002974 dispatchOnLauncherTransitionStart(toView, animated, true);
2975 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002976 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002977 }
2978
Michael Jurkae326f182011-11-21 14:05:46 -08002979 @Override
2980 public void onTrimMemory(int level) {
2981 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002982 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002983 mAppsCustomizeTabHost.onTrimMemory();
2984 }
2985 }
2986
Winson Chung18f41f82012-05-09 13:28:10 -07002987 @Override
2988 public void onWindowFocusChanged(boolean hasFocus) {
2989 if (!hasFocus) {
2990 // When another window occludes launcher (like the notification shade, or recents),
2991 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2992 updateWallpaperVisibility(true);
2993 } else {
2994 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002995 mWorkspace.postDelayed(new Runnable() {
2996 @Override
2997 public void run() {
2998 disableWallpaperIfInAllApps();
2999 }
3000 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003001 }
3002 }
3003
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003004 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003005 showWorkspace(animated, null);
3006 }
3007
3008 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003009 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003010 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003012 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003013
Winson Chungc7d2b602012-05-16 17:02:20 -07003014 // Show the search bar (only animate if we were showing the drop target bar in spring
3015 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003016 if (mSearchDropTargetBar != null) {
3017 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3018 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003019
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 // Set focus to the AppsCustomize button
3021 if (mAllAppsButton != null) {
3022 mAllAppsButton.requestFocus();
3023 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003024 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003025
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003026 // Change the state *after* we've called all the transition code
3027 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003028
Winson Chung5fb63472011-02-02 17:03:37 -08003029 // Resume the auto-advance of widgets
3030 mUserPresent = true;
3031 updateRunning();
3032
alanv1d4fde62012-10-17 13:15:47 -07003033 // Send an accessibility event to announce the context change
3034 getWindow().getDecorView()
3035 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003036
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003037 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003038 }
3039
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003040 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003041 }
3042
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 void showAllApps(boolean animated) {
3044 if (mState != State.WORKSPACE) return;
3045
3046 showAppsCustomizeHelper(animated, false);
3047 mAppsCustomizeTabHost.requestFocus();
3048
Michael Jurkab3e22d92011-10-31 15:58:33 -07003049 // Change the state *after* we've called all the transition code
3050 mState = State.APPS_CUSTOMIZE;
3051
3052 // Pause the auto-advance of widgets until we are out of AllApps
3053 mUserPresent = false;
3054 updateRunning();
3055 closeFolder();
3056
3057 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003058 getWindow().getDecorView()
3059 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003060 }
3061
Adam Cohen7777d962011-08-18 18:58:38 -07003062 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003063 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003064 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003065 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003066 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003067 }
Adam Cohen7777d962011-08-18 18:58:38 -07003068
Adam Cohened66b2b2012-01-23 17:28:51 -08003069 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3070 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003071 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3072
Winson Chunge7a03942011-08-05 15:05:12 -07003073 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003074 @Override
3075 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003076 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003077 // Before we show workspace, hide all apps again because
3078 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3079 // clean up our state transition functions
3080 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003081 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003082 } else {
3083 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003084 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003085 }
3086 }, (extendedDelay ?
3087 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3088 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3089 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003090
Michael Jurkad3ef3062010-11-23 16:23:58 -08003091 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003092 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093 final boolean animated = true;
3094 final boolean springLoaded = true;
3095 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003096 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003097 }
3098 // Otherwise, we are not in spring loaded mode, so don't do anything.
3099 }
3100
Michael Jurkab3e22d92011-10-31 15:58:33 -07003101 void lockAllApps() {
3102 // TODO
3103 }
3104
3105 void unlockAllApps() {
3106 // TODO
3107 }
3108
Winson Chungf0ea4d32011-06-06 14:27:16 -07003109 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003110 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003111 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003112 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003113 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003114 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003115 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003116 int duration = 0;
3117 if (mSearchDropTargetBar != null) {
3118 duration = mSearchDropTargetBar.getTransitionInDuration();
3119 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003120 mHotseat.animate().alpha(1f).setDuration(duration);
3121 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003122 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003123 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003124 }
3125 }
3126 }
3127
3128 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003130 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003131 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003132 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003134 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003135 int duration = 0;
3136 if (mSearchDropTargetBar != null) {
3137 duration = mSearchDropTargetBar.getTransitionOutDuration();
3138 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003139 mHotseat.animate().alpha(0f).setDuration(duration);
3140 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003141 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003142 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003143 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003144 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003145 }
3146
Patrick Dubroy5f445422011-02-18 14:35:21 -08003147 /**
3148 * Add an item from all apps or customize onto the given workspace screen.
3149 * If layout is null, add to the current screen.
3150 */
3151 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003152 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003153 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003154 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003155 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003156
Winson Chungdff8ebb2011-09-08 17:25:31 -07003157 /** Maps the current orientation to an index for referencing orientation correct global icons */
3158 private int getCurrentOrientationIndexForGlobalIcons() {
3159 // default - 0, landscape - 1
3160 switch (getResources().getConfiguration().orientation) {
3161 case Configuration.ORIENTATION_LANDSCAPE:
3162 return 1;
3163 default:
3164 return 0;
3165 }
3166 }
3167
Michael Jurkaae65ee32012-04-30 11:45:54 -07003168 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003170 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003171 // Look for the toolbar icon specified in the activity meta-data
3172 Bundle metaData = packageManager.getActivityInfo(
3173 activityName, PackageManager.GET_META_DATA).metaData;
3174 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003175 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003176 if (iconResId != 0) {
3177 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003178 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003179 }
3180 }
3181 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003182 // This can happen if the activity defines an invalid drawable
3183 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3184 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003185 } catch (Resources.NotFoundException nfe) {
3186 // This can happen if the activity defines an invalid drawable
3187 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3188 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003189 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003190 return null;
3191 }
3192
3193 // if successful in getting icon, return it; otherwise, set button to use default drawable
3194 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003195 int buttonId, ComponentName activityName, int fallbackDrawableId,
3196 String toolbarResourceName) {
3197 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003198 Resources r = getResources();
3199 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3200 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003201
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003202 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003203 // If we were unable to find the icon via the meta-data, use a generic one
3204 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003205 toolbarIcon = r.getDrawable(fallbackDrawableId);
3206 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003207 if (button != null) {
3208 button.setCompoundDrawables(toolbarIcon, null, null, null);
3209 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003210 return null;
3211 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003212 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003213 if (button != null) {
3214 button.setCompoundDrawables(toolbarIcon, null, null, null);
3215 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003216 return toolbarIcon.getConstantState();
3217 }
3218 }
3219
3220 // if successful in getting icon, return it; otherwise, set button to use default drawable
3221 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003222 int buttonId, ComponentName activityName, int fallbackDrawableId,
3223 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003224 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003225 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003226
Michael Jurka19e0fc52011-07-22 18:00:21 -07003227 if (button != null) {
3228 // If we were unable to find the icon via the meta-data, use a
3229 // generic one
3230 if (toolbarIcon == null) {
3231 button.setImageResource(fallbackDrawableId);
3232 } else {
3233 button.setImageDrawable(toolbarIcon);
3234 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003235 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003236
3237 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3238
Michael Jurka0423dcf2010-10-05 14:56:18 -07003239 }
3240
Winson Chung1b884092012-06-08 17:13:02 -07003241 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003242 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003243 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003244 }
3245
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003246 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003247 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003248 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003249 }
3250
Winson Chungbb185bd2011-11-21 12:31:42 -08003251 private void invalidatePressedFocusedStates(View container, View button) {
3252 if (container instanceof HolographicLinearLayout) {
3253 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3254 layout.invalidatePressedFocusedStates();
3255 } else if (button instanceof HolographicImageView) {
3256 HolographicImageView view = (HolographicImageView) button;
3257 view.invalidatePressedFocusedStates();
3258 }
3259 }
3260
Cristina Stancu476493b2013-08-07 17:20:14 +01003261 public View getQsbBar() {
3262 if (mQsbBar == null) {
3263 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3264 }
3265 return mQsbBar;
3266 }
3267
3268 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003270 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003272 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003273
Winson Chung1cad91e2011-05-25 17:41:01 -07003274 final SearchManager searchManager =
3275 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3276 ComponentName activityName = searchManager.getGlobalSearchActivity();
3277 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003278 int coi = getCurrentOrientationIndexForGlobalIcons();
3279 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003280 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3281 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3282 if (sGlobalSearchIcon[coi] == null) {
3283 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3284 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3285 TOOLBAR_ICON_METADATA_NAME);
3286 }
3287
Winson Chungc51db6a2011-10-05 11:44:49 -07003288 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3289 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003290 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003291 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003292 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003293 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003294 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3295 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3296 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003297 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003298 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003299 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003300 }
3301 }
3302
Cristina Stancu476493b2013-08-07 17:20:14 +01003303 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003304 final View searchButtonContainer = findViewById(R.id.search_button_container);
3305 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003306 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003307 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003308 }
3309
Cristina Stancu476493b2013-08-07 17:20:14 +01003310 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003311 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003312 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003313
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003315 final SearchManager searchManager =
3316 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3317 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3318
3319 ComponentName activityName = null;
3320 if (globalSearchActivity != null) {
3321 // Check if the global search activity handles voice search
3322 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3323 intent.setPackage(globalSearchActivity.getPackageName());
3324 activityName = intent.resolveActivity(getPackageManager());
3325 }
3326
3327 if (activityName == null) {
3328 // Fallback: check if an activity other than the global search activity
3329 // resolves this
3330 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3331 activityName = intent.resolveActivity(getPackageManager());
3332 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003333 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003334 int coi = getCurrentOrientationIndexForGlobalIcons();
3335 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003336 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3337 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3338 if (sVoiceSearchIcon[coi] == null) {
3339 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3340 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3341 TOOLBAR_ICON_METADATA_NAME);
3342 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003343 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003344 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003345 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003346 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003347 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003348 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003349 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003350 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003351 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003352 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003353 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003354 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003355
Cristina Stancu476493b2013-08-07 17:20:14 +01003356 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003357 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3358 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003359 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003360 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003361 }
3362
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003363 public void setVoiceButtonProxyVisible(boolean visible) {
3364 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3365 if (voiceButtonProxy != null) {
3366 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3367 }
3368 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003369 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003370 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003371 */
3372 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003373 final View marketButton = findViewById(R.id.market_button);
3374 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3375 // Find the app market activity by resolving an intent.
3376 // (If multiple app markets are installed, it will return the ResolverActivity.)
3377 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003378 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003379 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003380 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003381 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003382 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3383 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003384 marketButton.setVisibility(View.VISIBLE);
3385 } else {
3386 // We should hide and disable the view so that we don't try and restore the visibility
3387 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3388 marketButton.setVisibility(View.GONE);
3389 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003390 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003391 }
3392
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003393 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003394 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3395 Resources r = getResources();
3396 Drawable marketIconDrawable = d.newDrawable(r);
3397 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3398 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3399 marketIconDrawable.setBounds(0, 0, w, h);
3400
3401 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003402 }
3403
Michael Jurkad7c28052012-04-27 15:43:36 -07003404 @Override
3405 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003406 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003407 final List<CharSequence> text = event.getText();
3408 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003409 // Populate event with a fake title based on the current state.
3410 if (mState == State.APPS_CUSTOMIZE) {
3411 text.add(getString(R.string.all_apps_button_label));
3412 } else {
3413 text.add(getString(R.string.all_apps_home_button_label));
3414 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003415 return result;
3416 }
3417
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003418 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003419 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003420 */
3421 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3422 @Override
3423 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003424 closeSystemDialogs();
3425 }
3426 }
3427
3428 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003429 * Receives notifications whenever the appwidgets are reset.
3430 */
3431 private class AppWidgetResetObserver extends ContentObserver {
3432 public AppWidgetResetObserver() {
3433 super(new Handler());
3434 }
3435
3436 @Override
3437 public void onChange(boolean selfChange) {
3438 onAppWidgetReset();
3439 }
3440 }
3441
3442 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003443 * If the activity is currently paused, signal that we need to run the passed Runnable
3444 * in onResume.
3445 *
3446 * This needs to be called from incoming places where resources might have been loaded
3447 * while we are paused. That is becaues the Configuration might be wrong
3448 * when we're not running, and if it comes back to what it was when we
3449 * were paused, we are not restarted.
3450 *
3451 * Implementation of the method from LauncherModel.Callbacks.
3452 *
3453 * @return true if we are currently paused. The caller might be able to
3454 * skip some work in that case since we will come back again.
3455 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003456 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003457 if (mPaused) {
3458 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003459 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003460 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003461 }
3462 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003463 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003464 return true;
3465 } else {
3466 return false;
3467 }
3468 }
3469
Michael Jurkac402cd92013-05-20 15:49:32 +02003470 private boolean waitUntilResume(Runnable run) {
3471 return waitUntilResume(run, false);
3472 }
3473
Michael Jurka1e2f4652013-07-08 18:03:46 -07003474 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003475 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003476 }
3477
Michael Jurka7607c2f2013-04-03 14:33:19 -07003478 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003479 * If the activity is currently paused, signal that we need to re-run the loader
3480 * in onResume.
3481 *
3482 * This needs to be called from incoming places where resources might have been loaded
3483 * while we are paused. That is becaues the Configuration might be wrong
3484 * when we're not running, and if it comes back to what it was when we
3485 * were paused, we are not restarted.
3486 *
3487 * Implementation of the method from LauncherModel.Callbacks.
3488 *
3489 * @return true if we are currently paused. The caller might be able to
3490 * skip some work in that case since we will come back again.
3491 */
3492 public boolean setLoadOnResume() {
3493 if (mPaused) {
3494 Log.i(TAG, "setLoadOnResume");
3495 mOnResumeNeedsLoad = true;
3496 return true;
3497 } else {
3498 return false;
3499 }
3500 }
3501
3502 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003504 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003506 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003507 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003508 } else {
3509 return SCREEN_COUNT / 2;
3510 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003512
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 /**
3514 * Refreshes the shortcuts shown on the workspace.
3515 *
3516 * Implementation of the method from LauncherModel.Callbacks.
3517 */
3518 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003519 // If we're starting binding all over again, clear any bind calls we'd postponed in
3520 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3521 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003522 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003523
Winson Chungd64d1762013-08-20 14:37:16 -07003524 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003525 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003526 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003527
Michael Jurka05bf6442011-11-30 20:28:53 -08003528 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003529 if (mHotseat != null) {
3530 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003531 }
3532 }
3533
Adam Cohendcd297f2013-06-18 13:13:40 -07003534 @Override
3535 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003536 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003537
3538 // Create the new empty page
Winson Chung64359a52013-07-08 17:17:08 -07003539 mWorkspace.addExtraEmptyScreen();
Winson Chung0e6a7132013-08-23 12:55:10 -07003540
3541 // Create the custom content page (this call updates mDefaultScreen which calls
3542 // setCurrentPage() so ensure that all pages are added before calling this)
3543 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3544 mWorkspace.createCustomContentPage();
3545 }
Winson Chung64359a52013-07-08 17:17:08 -07003546 }
3547
3548 @Override
3549 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003550 int count = orderedScreenIds.size();
3551 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003552 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003553 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003554 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003555 }
3556
Adam Cohen39a06042013-07-19 14:30:12 -07003557 private boolean shouldShowWeightWatcher() {
3558 String spKey = LauncherAppState.getSharedPreferencesKey();
3559 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003560 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003561
3562 return show;
3563 }
3564
Adam Cohen4caf2982013-08-20 18:54:31 -07003565 private boolean emailSent() {
3566 String spKey = LauncherAppState.getSharedPreferencesKey();
3567 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3568 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3569 return show;
3570 }
3571
3572 private void setEmailSent(boolean sent) {
3573 String spKey = LauncherAppState.getSharedPreferencesKey();
3574 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3575
3576 SharedPreferences.Editor editor = sp.edit();
3577 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3578 editor.commit();
3579 }
3580
Adam Cohen39a06042013-07-19 14:30:12 -07003581 private void toggleShowWeightWatcher() {
3582 String spKey = LauncherAppState.getSharedPreferencesKey();
3583 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3584 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3585
3586 show = !show;
3587
3588 SharedPreferences.Editor editor = sp.edit();
3589 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3590 editor.commit();
3591
3592 if (mWeightWatcher != null) {
3593 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3594 }
3595 }
3596
Winson Chungd64d1762013-08-20 14:37:16 -07003597 public void bindAppsAdded(final ArrayList<Long> newScreens,
3598 final ArrayList<ItemInfo> addNotAnimated,
3599 final ArrayList<ItemInfo> addAnimated) {
3600 Runnable r = new Runnable() {
3601 public void run() {
3602 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3603 }
3604 };
3605 if (waitUntilResume(r)) {
3606 return;
3607 }
3608
Adam Cohen4caf2982013-08-20 18:54:31 -07003609 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003610
3611 // Add the new screens
3612 bindAddScreens(newScreens);
3613
3614 // We add the items without animation on non-visible pages, and with
3615 // animations on the new page (which we will try and snap to).
3616 if (!addNotAnimated.isEmpty()) {
3617 bindItems(addNotAnimated, 0,
3618 addNotAnimated.size(), false);
3619 }
3620 if (!addAnimated.isEmpty()) {
3621 bindItems(addAnimated, 0,
3622 addAnimated.size(), true);
3623 }
3624 }
3625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003626 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 * Bind the items start-end from the list.
3628 *
3629 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003630 */
Winson Chung64359a52013-07-08 17:17:08 -07003631 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3632 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003633 Runnable r = new Runnable() {
3634 public void run() {
3635 bindItems(shortcuts, start, end, forceAnimateIcons);
3636 }
3637 };
3638 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003639 return;
3640 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003641
Winson Chungf0c6ae02012-03-21 16:10:31 -07003642 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003643 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3644 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003645 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003646 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003647 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003648 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003650
3651 // Short circuit if we are loading dock items for a configuration which has no dock
3652 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3653 mHotseat == null) {
3654 continue;
3655 }
3656
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 switch (item.itemType) {
3658 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3659 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003660 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003661 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003662
Winson Chung64359a52013-07-08 17:17:08 -07003663 /*
3664 * TODO: FIX collision case
3665 */
Winson Chungce376632013-07-11 16:12:41 -07003666 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3667 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3668 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3669 throw new RuntimeException("OCCUPIED");
3670 }
Winson Chung64359a52013-07-08 17:17:08 -07003671 }
3672
Adam Cohendcd297f2013-06-18 13:13:40 -07003673 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3674 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003675 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003676 // Animate all the applications up now
3677 shortcut.setAlpha(0f);
3678 shortcut.setScaleX(0f);
3679 shortcut.setScaleY(0f);
3680 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003681 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003682 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003683 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003684 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003685 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003686 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003687 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003688 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3689 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003690 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003691 default:
3692 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003695
Winson Chung997a9232013-07-24 15:33:46 -07003696 if (animateIcons) {
3697 // Animate to the correct page
3698 if (newShortcutsScreenId > -1) {
3699 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3700 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3701 if (newShortcutsScreenId != currentScreenId) {
3702 mWorkspace.snapToPage(newScreenIndex);
3703 }
3704 }
3705
Winson Chung64359a52013-07-08 17:17:08 -07003706 // We post the animation slightly delayed to prevent slowdowns when we are loading
3707 // right after we return to launcher.
3708 mWorkspace.postDelayed(new Runnable() {
3709 public void run() {
3710 anim.playTogether(bounceAnims);
3711 anim.start();
3712 }
3713 }, NEW_APPS_ANIMATION_DELAY);
3714 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003716 }
3717
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 /**
3719 * Implementation of the method from LauncherModel.Callbacks.
3720 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003721 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003722 Runnable r = new Runnable() {
3723 public void run() {
3724 bindFolders(folders);
3725 }
3726 };
3727 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003728 return;
3729 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003730 sFolders.clear();
3731 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733
3734 /**
3735 * Add the views for a widget to the workspace.
3736 *
3737 * Implementation of the method from LauncherModel.Callbacks.
3738 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003739 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003740 Runnable r = new Runnable() {
3741 public void run() {
3742 bindAppWidget(item);
3743 }
3744 };
3745 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 return;
3747 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003748
Daniel Sandler843e8602010-06-07 14:59:01 -04003749 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3750 if (DEBUG_WIDGETS) {
3751 Log.d(TAG, "bindAppWidget: " + item);
3752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003753 final Workspace workspace = mWorkspace;
3754
3755 final int appWidgetId = item.appWidgetId;
3756 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003757 if (DEBUG_WIDGETS) {
3758 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3759 }
3760
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3762
Joe Onorato9c1289c2009-08-17 11:03:03 -04003763 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003764 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765
Adam Cohendcd297f2013-06-18 13:13:40 -07003766 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003767 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003768 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3769
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 workspace.requestLayout();
3771
Daniel Sandler843e8602010-06-07 14:59:01 -04003772 if (DEBUG_WIDGETS) {
3773 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3774 + (SystemClock.uptimeMillis()-start) + "ms");
3775 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 }
3777
Adam Cohen1462de32012-07-24 22:34:36 -07003778 public void onPageBoundSynchronously(int page) {
3779 mSynchronouslyBoundPages.add(page);
3780 }
3781
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 /**
3783 * Callback saying that there aren't any more items to bind.
3784 *
3785 * Implementation of the method from LauncherModel.Callbacks.
3786 */
Adam Cohene25af792013-06-06 23:08:25 -07003787 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003788 Runnable r = new Runnable() {
3789 public void run() {
3790 finishBindingItems(upgradePath);
3791 }
3792 };
3793 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003794 return;
3795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 if (mSavedState != null) {
3797 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003798 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 mSavedState = null;
3801 }
3802
Adam Cohen1462de32012-07-24 22:34:36 -07003803 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003805 // If we received the result of any pending adds while the loader was running (e.g. the
3806 // widget configuration forced an orientation change), process them now.
3807 for (int i = 0; i < sPendingAddList.size(); i++) {
3808 completeAdd(sPendingAddList.get(i));
3809 }
3810 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811
Winson Chungc51db6a2011-10-05 11:44:49 -07003812 // Update the market app icon as necessary (the other icons will be managed in response to
3813 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003814 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003815
Winson Chungf0c6ae02012-03-21 16:10:31 -07003816 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003817 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003818 mWorkspace.getUniqueComponents(true, null);
3819 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003820 }
Adam Cohen99894d92013-06-14 11:22:59 -07003821
Adam Cohen66a01fd2013-06-11 12:48:00 -07003822 mWorkspace.post(new Runnable() {
3823 @Override
3824 public void run() {
3825 onFinishBindingItems();
3826 }
3827 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003828
3829 // Write all the logs to disk
3830 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3831 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003832 }
3833
Winson Chunga2413752012-04-03 14:22:34 -07003834 private boolean canRunNewAppsAnimation() {
3835 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3836 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3837 }
3838
Winson Chungc9168342013-06-26 14:54:55 -07003839 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3840 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3841 PropertyValuesHolder.ofFloat("alpha", 1f),
3842 PropertyValuesHolder.ofFloat("scaleX", 1f),
3843 PropertyValuesHolder.ofFloat("scaleY", 1f));
3844 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3845 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3846 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3847 return bounceAnim;
3848 }
3849
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003850 @Override
3851 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003852 boolean searchVisible = updateGlobalSearchIcon();
3853 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003854 if (mSearchDropTargetBar != null) {
3855 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3856 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003857 }
3858
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003859 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003860 * Add the icons for all apps.
3861 *
3862 * Implementation of the method from LauncherModel.Callbacks.
3863 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003864 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003865 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003866 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3867 getHotseat().addAllAppsFolder(mIconCache, apps,
3868 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3869 }
Winson Chung64359a52013-07-08 17:17:08 -07003870 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 }
3873
3874 /**
3875 * A package was updated.
3876 *
3877 * Implementation of the method from LauncherModel.Callbacks.
3878 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003879 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003880 Runnable r = new Runnable() {
3881 public void run() {
3882 bindAppsUpdated(apps);
3883 }
3884 };
3885 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003886 return;
3887 }
3888
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003889 if (mWorkspace != null) {
3890 mWorkspace.updateShortcuts(apps);
3891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003892 }
3893
3894 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003895 * A package was uninstalled. We take both the super set of packageNames
3896 * in addition to specific applications to remove, the reason being that
3897 * this can be called when a package is updated as well. In that scenario,
3898 * we only remove specific components from the workspace, where as
3899 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003900 *
3901 * Implementation of the method from LauncherModel.Callbacks.
3902 */
Winson Chung83892cc2013-05-01 16:53:33 -07003903 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3904 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003905 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003906 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003907 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003908 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003909 }
Winson Chungd64d1762013-08-20 14:37:16 -07003910 };
3911 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003912 return;
3913 }
3914
Winson Chung64359a52013-07-08 17:17:08 -07003915 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003916 mWorkspace.removeItemsByPackageName(packageNames);
3917 } else {
3918 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003919 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003920
Winson Chunga1820962011-10-03 16:31:06 -07003921 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003922 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 }
Joe Onoratobe386092009-11-17 17:32:16 -08003924
3925 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003926 * A number of packages were updated.
3927 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003928 private ArrayList<Object> mWidgetsAndShortcuts;
3929 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003930 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003931 bindPackagesUpdated(mWidgetsAndShortcuts);
3932 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003933 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003934 };
3935
3936 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3937 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3938 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003939 return;
3940 }
3941
Winson Chung64359a52013-07-08 17:17:08 -07003942 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003943 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003944 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003945 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003946 }
3947
Winson Chung400438b2011-01-16 17:53:48 -08003948 private int mapConfigurationOriActivityInfoOri(int configOri) {
3949 final Display d = getWindowManager().getDefaultDisplay();
3950 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3951 switch (d.getRotation()) {
3952 case Surface.ROTATION_0:
3953 case Surface.ROTATION_180:
3954 // We are currently in the same basic orientation as the natural orientation
3955 naturalOri = configOri;
3956 break;
3957 case Surface.ROTATION_90:
3958 case Surface.ROTATION_270:
3959 // We are currently in the other basic orientation to the natural orientation
3960 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3961 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3962 break;
3963 }
3964
3965 int[] oriMap = {
3966 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3967 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3968 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3969 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3970 };
3971 // Since the map starts at portrait, we need to offset if this device's natural orientation
3972 // is landscape.
3973 int indexOffset = 0;
3974 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3975 indexOffset = 1;
3976 }
3977 return oriMap[(d.getRotation() + indexOffset) % 4];
3978 }
Adam Cohen446e9402011-09-15 18:21:21 -07003979
Winson Chung4b919f82012-05-01 10:44:08 -07003980 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003981 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003982 getResources().getBoolean(R.bool.allow_rotation);
3983 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003984 }
Winson Chung4b919f82012-05-01 10:44:08 -07003985 public void lockScreenOrientation() {
3986 if (isRotationEnabled()) {
3987 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3988 .getConfiguration().orientation));
3989 }
3990 }
3991 public void unlockScreenOrientation(boolean immediate) {
3992 if (isRotationEnabled()) {
3993 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003994 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003995 } else {
3996 mHandler.postDelayed(new Runnable() {
3997 public void run() {
3998 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3999 }
4000 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004001 }
Winson Chung4b919f82012-05-01 10:44:08 -07004002 }
Winson Chung400438b2011-01-16 17:53:48 -08004003 }
4004
Winson Chung82f55532011-08-09 14:14:23 -07004005 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004006 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004007 if (DISABLE_CLINGS) {
4008 return false;
4009 }
4010
Brett Chabot2a9e2282011-08-23 15:03:13 -07004011 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004012 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004013
Michael Jurkae233a8b2013-03-19 13:49:20 +01004014 // Restricted secondary users (child mode) will potentially have very few apps
4015 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004016// boolean supportsLimitedUsers =
4017// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4018// Account[] accounts = AccountManager.get(this).getAccounts();
4019// if (supportsLimitedUsers && accounts.length == 0) {
4020// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4021// Bundle restrictions = um.getUserRestrictions();
4022// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4023// return false;
4024// }
4025// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004027 }
Michael Jurka22143132012-10-04 17:42:38 +02004028
Winson Chung7d7541e2011-09-16 20:14:36 -07004029 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004030 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004031 if (cling != null) {
4032 cling.init(this, positionData);
4033 cling.setVisibility(View.VISIBLE);
4034 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4035 if (animate) {
4036 cling.buildLayer();
4037 cling.setAlpha(0f);
4038 cling.animate()
4039 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004040 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 .setDuration(SHOW_CLING_DURATION)
4042 .setStartDelay(delay)
4043 .start();
4044 } else {
4045 cling.setAlpha(1f);
4046 }
Michael Jurka22143132012-10-04 17:42:38 +02004047 cling.setFocusableInTouchMode(true);
4048 cling.post(new Runnable() {
4049 public void run() {
4050 cling.setFocusable(true);
4051 cling.requestFocus();
4052 }
4053 });
4054 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4055 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 }
4057 return cling;
4058 }
Michael Jurka22143132012-10-04 17:42:38 +02004059
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004061 // To catch cases where siblings of top-level views are made invisible, just check whether
4062 // the cling is directly set to GONE before dismissing it.
4063 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004064 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004066 anim.addListener(new AnimatorListenerAdapter() {
4067 public void onAnimationEnd(Animator animation) {
4068 cling.setVisibility(View.GONE);
4069 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004070 // We should update the shared preferences on a background thread
4071 new Thread("dismissClingThread") {
4072 public void run() {
4073 SharedPreferences.Editor editor = mSharedPrefs.edit();
4074 editor.putBoolean(flag, true);
4075 editor.commit();
4076 }
4077 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004078 };
4079 });
4080 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004081 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004082 }
4083 }
Michael Jurka22143132012-10-04 17:42:38 +02004084
Winson Chung9d9d74f2011-09-19 11:49:12 -07004085 private void removeCling(int id) {
4086 final View cling = findViewById(id);
4087 if (cling != null) {
4088 final ViewGroup parent = (ViewGroup) cling.getParent();
4089 parent.post(new Runnable() {
4090 @Override
4091 public void run() {
4092 parent.removeView(cling);
4093 }
4094 });
Michael Jurka22143132012-10-04 17:42:38 +02004095 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004096 }
4097 }
Michael Jurka974c3862012-05-22 22:00:31 -07004098
4099 private boolean skipCustomClingIfNoAccounts() {
4100 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4101 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4102 if (customCling) {
4103 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004104 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004105 Account[] accounts = am.getAccountsByType("com.google");
4106 return accounts.length == 0;
4107 }
4108 return false;
4109 }
4110
Winson Chung7d7541e2011-09-16 20:14:36 -07004111 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004112 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004113 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004114 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4115 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004116 // If we're not using the default workspace layout, replace workspace cling
4117 // with a custom workspace cling (usually specified in an overlay)
4118 // For now, only do this on tablets
4119 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004120 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004121 // Use a custom cling
4122 View cling = findViewById(R.id.workspace_cling);
4123 ViewGroup clingParent = (ViewGroup) cling.getParent();
4124 int clingIndex = clingParent.indexOfChild(cling);
4125 clingParent.removeViewAt(clingIndex);
4126 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4127 clingParent.addView(customCling, clingIndex);
4128 customCling.setId(R.id.workspace_cling);
4129 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004130 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004131 } else {
4132 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004133 }
4134 }
4135 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004136 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004137 if (isClingsEnabled() &&
4138 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004139 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004140 } else {
4141 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 }
4143 }
4144 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004145 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004146 if (isClingsEnabled() &&
4147 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4148 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004149 } else {
4150 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004151 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004152 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004153 }
Michael Jurka104c4562013-07-08 18:03:46 -07004154 protected SharedPreferences getSharedPrefs() {
4155 return mSharedPrefs;
4156 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 public boolean isFolderClingVisible() {
4158 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004159 if (cling != null) {
4160 return cling.getVisibility() == View.VISIBLE;
4161 }
4162 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004163 }
Winson Chung82f55532011-08-09 14:14:23 -07004164 public void dismissWorkspaceCling(View v) {
4165 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004166 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004167 }
4168 public void dismissAllAppsCling(View v) {
4169 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004170 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4171 }
4172 public void dismissFolderCling(View v) {
4173 Cling cling = (Cling) findViewById(R.id.folder_cling);
4174 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004175 }
4176
Winson Chung80baf5a2010-08-09 16:03:15 -07004177 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004178 * Prints out out state for debugging.
4179 */
4180 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004181 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004182 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004183 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4184 Log.d(TAG, "mRestoring=" + mRestoring);
4185 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4186 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004187 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004188 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004189
Winson Chung785d2eb2011-04-14 16:08:02 -07004190 if (mAppsCustomizeContent != null) {
4191 mAppsCustomizeContent.dumpState();
4192 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004193 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004194 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004195
4196 @Override
4197 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4198 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004199 synchronized (sDumpLogs) {
4200 writer.println(" ");
4201 writer.println("Debug logs: ");
4202 for (int i = 0; i < sDumpLogs.size(); i++) {
4203 writer.println(" " + sDumpLogs.get(i));
4204 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004205 }
4206 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004207
4208 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004209 synchronized (sDumpLogs) {
4210 Log.d(TAG, "");
4211 Log.d(TAG, "*********************");
4212 Log.d(TAG, "Launcher debug logs: ");
4213 for (int i = 0; i < sDumpLogs.size(); i++) {
4214 Log.d(TAG, " " + sDumpLogs.get(i));
4215 }
4216 Log.d(TAG, "*********************");
4217 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004218 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004219 }
4220
4221 public static void addDumpLog(String tag, String log, boolean debugLog) {
4222 if (debugLog) {
4223 Log.d(tag, log);
4224 }
4225 sDateStamp.setTime(System.currentTimeMillis());
4226 synchronized (sDumpLogs) {
4227 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4228 }
4229 }
4230
4231 public void dumpLogsToLocalData(final boolean email) {
4232 new Thread("DumpLogsToLocalData") {
4233 @Override
4234 public void run() {
4235 boolean success = false;
4236 sDateStamp.setTime(sRunStart);
4237 String FILENAME = sDateStamp.getMonth() + "-"
4238 + sDateStamp.getDay() + "_"
4239 + sDateStamp.getHours() + "-"
4240 + sDateStamp.getMinutes() + "_"
4241 + sDateStamp.getSeconds() + ".txt";
4242
4243 FileOutputStream fos = null;
4244 File outFile = null;
4245 try {
4246 outFile = new File(getFilesDir(), FILENAME);
4247 outFile.createNewFile();
4248 fos = new FileOutputStream(outFile);
4249 } catch (Exception e) {
4250 e.printStackTrace();
4251 }
4252 if (fos != null) {
4253 PrintWriter writer = new PrintWriter(fos);
4254
4255 writer.println(" ");
4256 writer.println("Debug logs: ");
4257 synchronized (sDumpLogs) {
4258 for (int i = 0; i < sDumpLogs.size(); i++) {
4259 writer.println(" " + sDumpLogs.get(i));
4260 }
4261 }
4262 writer.close();
4263 }
4264 try {
4265 if (fos != null) {
4266 fos.close();
4267 success = true;
4268 }
4269 } catch (IOException e) {
4270 e.printStackTrace();
4271 }
4272
4273 if (success && email) {
4274 if (!emailSent()) {
4275 emailFile(outFile);
4276 }
4277 }
4278 }
4279 }.start();
4280 }
4281
4282 private void emailFile(File file) {
4283 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4284 try {
4285 copyFile(file, publicCopy);
4286 } catch (IOException e) {
4287 e.printStackTrace();
4288 return;
4289 }
4290
4291 Intent intent = new Intent(Intent.ACTION_SEND);
4292 intent.setType("text/plain");
4293 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4294 "mikejurka@google"});
4295 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4296 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4297
4298 if (!file.exists() || !file.canRead()) {
4299 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4300 finish();
4301 return;
4302 }
4303
4304 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4305 Uri uri = Uri.fromFile(publicCopy);
4306 intent.putExtra(Intent.EXTRA_STREAM, uri);
4307 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4308
4309 setEmailSent(true);
4310 }
4311
4312 public void copyFile(File src, File dst) throws IOException {
4313 InputStream in = new FileInputStream(src);
4314 OutputStream out = new FileOutputStream(dst);
4315
4316 // Transfer bytes from in to out
4317 byte[] buf = new byte[1024];
4318 int len;
4319 while ((len = in.read(buf)) > 0) {
4320 out.write(buf, 0, len);
4321 }
4322 in.close();
4323 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004325}
Michael Jurka2763be32011-02-24 11:19:57 -08004326
Michael Jurkaabded662011-03-04 12:06:57 -08004327interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004328 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004329 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004330 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004331 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004332 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004333}