blob: 7970b71bef116ead65a06113b71a2801e4d148e9 [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 Sandler6053b8002013-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
Adam Cohen30bacb22013-08-29 14:25:25 -0700924 protected void startSettings() {
925 }
926
Adam Cohenbffe7452013-07-22 18:21:45 -0700927 public interface QSBScroller {
928 public void setScrollY(int scrollY);
929 }
930
Adam Cohen66a01fd2013-06-11 12:48:00 -0700931 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700932 public QSBScroller addToCustomContentPage(View customContent) {
933 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700934 }
935
Winson Chung98ca0f72013-07-29 12:58:51 -0700936 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700937 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700938 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700939 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700940 }
941
Adam Cohenedb40762013-07-18 16:45:45 -0700942 // The custom content needs to offset its content to account for the QSB
943 public int getTopOffsetForCustomContent() {
944 return mWorkspace.getPaddingTop();
945 }
946
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700947 @Override
948 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 // Flag the loader to stop early before switching
950 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700951 if (mAppsCustomizeContent != null) {
952 mAppsCustomizeContent.surrender();
953 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800954 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700955 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800957 // We can't hide the IME if it was forced open. So don't bother
958 /*
959 @Override
960 public void onWindowFocusChanged(boolean hasFocus) {
961 super.onWindowFocusChanged(hasFocus);
962
963 if (hasFocus) {
964 final InputMethodManager inputManager = (InputMethodManager)
965 getSystemService(Context.INPUT_METHOD_SERVICE);
966 WindowManager.LayoutParams lp = getWindow().getAttributes();
967 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
968 android.os.Handler()) {
969 protected void onReceiveResult(int resultCode, Bundle resultData) {
970 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
971 }
972 });
973 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
974 }
975 }
976 */
977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 private boolean acceptFilter() {
979 final InputMethodManager inputManager = (InputMethodManager)
980 getSystemService(Context.INPUT_METHOD_SERVICE);
981 return !inputManager.isFullscreenMode();
982 }
983
984 @Override
985 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700986 final int uniChar = event.getUnicodeChar();
987 final boolean handled = super.onKeyDown(keyCode, event);
988 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
989 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
991 keyCode, event);
992 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700993 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700994 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700995 // showSearchDialog()
996 // If there are multiple keystrokes before the search dialog takes focus,
997 // onSearchRequested() will be called for every keystroke,
998 // but it is idempotent, so it's fine.
999 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001 }
1002
Joe Onorato8a9625e2010-01-28 15:55:35 -08001003 // Eat the long press event so the keyboard doesn't come up.
1004 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1005 return true;
1006 }
1007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 return handled;
1009 }
1010
Karl Rosaen138a0412009-04-23 19:00:21 -07001011 private String getTypedText() {
1012 return mDefaultKeySsb.toString();
1013 }
1014
1015 private void clearTypedText() {
1016 mDefaultKeySsb.clear();
1017 mDefaultKeySsb.clearSpans();
1018 Selection.setSelection(mDefaultKeySsb, 0);
1019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001022 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1023 * State
1024 */
1025 private static State intToState(int stateOrdinal) {
1026 State state = State.WORKSPACE;
1027 final State[] stateValues = State.values();
1028 for (int i = 0; i < stateValues.length; i++) {
1029 if (stateValues[i].ordinal() == stateOrdinal) {
1030 state = stateValues[i];
1031 break;
1032 }
1033 }
1034 return state;
1035 }
1036
1037 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 * Restores the previous state, if it exists.
1039 *
1040 * @param savedState The previous state.
1041 */
1042 private void restoreState(Bundle savedState) {
1043 if (savedState == null) {
1044 return;
1045 }
1046
Michael Jurka883f55b2010-10-21 15:47:14 -07001047 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001048 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001049 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001050 }
1051
Winson Chungf0c6ae02012-03-21 16:10:31 -07001052 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001054 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001058 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001059
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1061 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001062 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1064 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001065 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1066 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1067 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001068 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 mRestoring = true;
1070 }
1071
1072 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1073 if (renameFolder) {
1074 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001075 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 mRestoring = true;
1077 }
Winson Chunga12a2502010-12-20 14:41:35 -08001078
Winson Chung785d2eb2011-04-14 16:08:02 -07001079 // Restore the AppsCustomize tab
1080 if (mAppsCustomizeTabHost != null) {
1081 String curTab = savedState.getString("apps_customize_currentTab");
1082 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001083 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001084 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001085 mAppsCustomizeContent.loadAssociatedPages(
1086 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001087 }
1088
Winson Chung5afbf7b2011-07-25 11:53:08 -07001089 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1090 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
1094 /**
1095 * Finds all the views we need and configure them properly.
1096 */
1097 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001098 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001099
Craig Mautner360310b2012-10-26 15:13:08 -07001100 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001101 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1102 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001103
1104 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1105 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106
Winson Chung4c98d922011-05-31 16:50:48 -07001107 // Setup the drag layer
1108 mDragLayer.setup(this, dragController);
1109
Winson Chung3d503fb2011-07-13 17:25:49 -07001110 // Setup the hotseat
1111 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1112 if (mHotseat != null) {
1113 mHotseat.setup(this);
1114 }
1115
Adam Cohenf358a4b2013-07-23 16:47:31 -07001116 mOverviewPanel = findViewById(R.id.overview_panel);
1117 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1118 @Override
1119 public void onClick(View arg0) {
1120 showAllApps(true);
1121 }
1122 });
1123 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1124 @Override
1125 public void onClick(View arg0) {
1126 startWallpaper();
1127 }
1128 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001129 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1130 @Override
1131 public void onClick(View arg0) {
1132 startSettings();
1133 }
1134 });
Adam Cohenf358a4b2013-07-23 16:47:31 -07001135
Winson Chung4c98d922011-05-31 16:50:48 -07001136 // Setup the workspace
1137 mWorkspace.setHapticFeedbackEnabled(false);
1138 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001139 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001140 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001141
Winson Chungf0ea4d32011-06-06 14:27:16 -07001142 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001143 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001144
Winson Chungf0ea4d32011-06-06 14:27:16 -07001145 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001146 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001147 mAppsCustomizeContent = (AppsCustomizePagedView)
1148 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1149 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001150
Winson Chung3d503fb2011-07-13 17:25:49 -07001151 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001152 dragController.setDragScoller(mWorkspace);
1153 dragController.setScrollView(mDragLayer);
1154 dragController.setMoveTarget(mWorkspace);
1155 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001156 if (mSearchDropTargetBar != null) {
1157 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001158 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001159
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001160 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001161 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001162 mWeightWatcher = new WeightWatcher(this);
1163 mWeightWatcher.setAlpha(0.5f);
1164 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001165 new FrameLayout.LayoutParams(
1166 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001167 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001168 Gravity.BOTTOM)
1169 );
Adam Cohen39a06042013-07-19 14:30:12 -07001170
1171 boolean show = shouldShowWeightWatcher();
1172 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175
1176 /**
1177 * Creates a view representing a shortcut.
1178 *
1179 * @param info The data structure describing the shortcut.
1180 *
1181 * @return A View inflated from R.layout.application.
1182 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001183 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001185 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
1187
1188 /**
1189 * Creates a view representing a shortcut inflated from the specified resource.
1190 *
1191 * @param layoutResId The id of the XML layout used to create the shortcut.
1192 * @param parent The group the shortcut belongs to.
1193 * @param info The data structure describing the shortcut.
1194 *
1195 * @return A View inflated from layoutResId.
1196 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001197 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001198 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1199 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 return favorite;
1202 }
1203
1204 /**
1205 * Add an application shortcut to the workspace.
1206 *
1207 * @param data The intent describing the application.
1208 * @param cellInfo The position on screen where to create the shortcut.
1209 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001210 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001212 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213
Adam Cohenfbba09b2011-07-18 21:43:05 -07001214 // First we check if we already know the exact location where we want to add this item.
1215 if (cellX >= 0 && cellY >= 0) {
1216 cellXY[0] = cellX;
1217 cellXY[1] = cellY;
1218 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001219 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 return;
1221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001223 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001224
Romain Guy73b979d2009-06-09 12:57:21 -07001225 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001226 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001228 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001229 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001230 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001231 } else {
1232 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234 }
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 /**
1237 * Add a shortcut to the workspace.
1238 *
1239 * @param data The intent describing the shortcut.
1240 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001242 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001243 int cellY) {
1244 int[] cellXY = mTmpAddItemCellCoordinates;
1245 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001246 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001247
Michael Jurkad3ef3062010-11-23 16:23:58 -08001248 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001249
Adam Cohen558baaf2011-08-15 15:22:57 -07001250 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001251 if (info == null) {
1252 return;
1253 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001254 final View view = createShortcut(info);
1255
Adam Cohenfbba09b2011-07-18 21:43:05 -07001256 // First we check if we already know the exact location where we want to add this item.
1257 if (cellX >= 0 && cellY >= 0) {
1258 cellXY[0] = cellX;
1259 cellXY[1] = cellY;
1260 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001261
1262 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001263 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001264 true, null,null)) {
1265 return;
1266 }
1267 DragObject dragObject = new DragObject();
1268 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001269 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1270 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001271 return;
1272 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001273 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001274 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001276 foundCellSpan = (result != null);
1277 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001278 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001279 }
1280
1281 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001282 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001283 return;
1284 }
1285
Adam Cohendcd297f2013-06-18 13:13:40 -07001286 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287
1288 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001289 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001290 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292 }
1293
Adam Cohen2f093b62012-04-30 18:59:53 -07001294 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1295 int minHeight) {
1296 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001297 // We want to account for the extra amount of padding that we are adding to the widget
1298 // to ensure that it gets the full amount of space that it has requested
1299 int requiredWidth = minWidth + padding.left + padding.right;
1300 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001301 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001302 }
1303
Adam Cohen2f093b62012-04-30 18:59:53 -07001304 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1305 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001306 }
1307
Adam Cohen2f093b62012-04-30 18:59:53 -07001308 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1309 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001310 }
1311
Adam Cohen2f093b62012-04-30 18:59:53 -07001312 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1313 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001314 }
1315
Adam Cohen2f093b62012-04-30 18:59:53 -07001316 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1317 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001318 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001319 }
1320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001322 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001324 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001325 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001327 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001328 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1329 if (appWidgetInfo == null) {
1330 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1331 }
Romain Guycbb89e42009-06-08 15:52:54 -07001332
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001333 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001334 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001335
Adam Cohen2f093b62012-04-30 18:59:53 -07001336 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1337 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001340 // We have saved the position to which the widget was dragged-- this really only matters
1341 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 int[] cellXY = mTmpAddItemCellCoordinates;
1343 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001344 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1347 cellXY[0] = mPendingAddInfo.cellX;
1348 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001349 spanXY[0] = mPendingAddInfo.spanX;
1350 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001351 foundCellSpan = true;
1352 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001353 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001355 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1356 spanXY[1], cellXY, finalSpan);
1357 spanXY[0] = finalSpan[0];
1358 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001359 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001360 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001362 }
1363
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001365 if (appWidgetId != -1) {
1366 // Deleting an app widget ID is a void call but writes to disk before returning
1367 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001368 new Thread("deleteAppWidgetId") {
1369 public void run() {
1370 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1371 }
1372 }.start();
1373 }
Winson Chung93eef082012-03-23 15:59:27 -07001374 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001375 return;
1376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001378 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001379 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1380 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001381 launcherInfo.spanX = spanXY[0];
1382 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001383 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1384 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001387 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388
1389 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001390 if (hostView == null) {
1391 // Perform actual inflation because we're live
1392 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1393 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1394 } else {
1395 // The AppWidgetHostView has already been inflated and instantiated
1396 launcherInfo.hostView = hostView;
1397 }
Romain Guycbb89e42009-06-08 15:52:54 -07001398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001400 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001401 launcherInfo.notifyWidgetSizeChanged(this);
1402
Adam Cohendcd297f2013-06-18 13:13:40 -07001403 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001405
1406 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001408 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 }
Romain Guycbb89e42009-06-08 15:52:54 -07001410
Adam Cohended9f8d2010-11-03 13:25:16 -07001411 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1412 @Override
1413 public void onReceive(Context context, Intent intent) {
1414 final String action = intent.getAction();
1415 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1416 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001417 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001418 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001419
Winson Chungc100e8e2011-08-09 16:02:43 -07001420 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001421 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001422 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1423 mAppsCustomizeTabHost.reset();
1424 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001425 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001426 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1427 mUserPresent = true;
1428 updateRunning();
1429 }
1430 }
1431 };
1432
1433 @Override
1434 public void onAttachedToWindow() {
1435 super.onAttachedToWindow();
1436
1437 // Listen for broadcasts related to user-presence
1438 final IntentFilter filter = new IntentFilter();
1439 filter.addAction(Intent.ACTION_SCREEN_OFF);
1440 filter.addAction(Intent.ACTION_USER_PRESENT);
1441 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001442 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001443 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001444 mVisible = true;
1445 }
1446
1447 @Override
1448 public void onDetachedFromWindow() {
1449 super.onDetachedFromWindow();
1450 mVisible = false;
1451
Adam Cohend113e0c2010-11-11 10:48:05 -08001452 if (mAttached) {
1453 unregisterReceiver(mReceiver);
1454 mAttached = false;
1455 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001456 updateRunning();
1457 }
1458
1459 public void onWindowVisibilityChanged(int visibility) {
1460 mVisible = visibility == View.VISIBLE;
1461 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001462 // The following code used to be in onResume, but it turns out onResume is called when
1463 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1464 // is a more appropriate event to handle
1465 if (mVisible) {
1466 mAppsCustomizeTabHost.onWindowVisible();
1467 if (!mWorkspaceLoading) {
1468 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001469 // We want to let Launcher draw itself at least once before we force it to build
1470 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001471 // apps is nice and speedy.
1472 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001473 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001474 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001475 if (mStarted) return;
1476 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001477 // We delay the layer building a bit in order to give
1478 // other message processing a time to run. In particular
1479 // this avoids a delay in hiding the IME if it was
1480 // currently shown, because doing that may involve
1481 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001482 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001483 final ViewTreeObserver.OnDrawListener listener = this;
1484 mWorkspace.post(new Runnable() {
1485 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001486 if (mWorkspace != null &&
1487 mWorkspace.getViewTreeObserver() != null) {
1488 mWorkspace.getViewTreeObserver().
1489 removeOnDrawListener(listener);
1490 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001491 }
1492 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001493 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001494 }
1495 });
1496 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001497 // When Launcher comes back to foreground, a different Activity might be responsible for
1498 // the app market intent, so refresh the icon
1499 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001500 clearTypedText();
1501 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001502 }
1503
1504 private void sendAdvanceMessage(long delay) {
1505 mHandler.removeMessages(ADVANCE_MSG);
1506 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1507 mHandler.sendMessageDelayed(msg, delay);
1508 mAutoAdvanceSentTime = System.currentTimeMillis();
1509 }
1510
1511 private void updateRunning() {
1512 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1513 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1514 mAutoAdvanceRunning = autoAdvanceRunning;
1515 if (autoAdvanceRunning) {
1516 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1517 sendAdvanceMessage(delay);
1518 } else {
1519 if (!mWidgetsToAdvance.isEmpty()) {
1520 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1521 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1522 }
1523 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001524 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001525 }
1526 }
1527 }
1528
1529 private final Handler mHandler = new Handler() {
1530 @Override
1531 public void handleMessage(Message msg) {
1532 if (msg.what == ADVANCE_MSG) {
1533 int i = 0;
1534 for (View key: mWidgetsToAdvance.keySet()) {
1535 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1536 final int delay = mAdvanceStagger * i;
1537 if (v instanceof Advanceable) {
1538 postDelayed(new Runnable() {
1539 public void run() {
1540 ((Advanceable) v).advance();
1541 }
1542 }, delay);
1543 }
1544 i++;
1545 }
1546 sendAdvanceMessage(mAdvanceInterval);
1547 }
1548 }
1549 };
1550
1551 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001552 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001553 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1554 if (v instanceof Advanceable) {
1555 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001556 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001557 updateRunning();
1558 }
1559 }
1560
1561 void removeWidgetToAutoAdvance(View hostView) {
1562 if (mWidgetsToAdvance.containsKey(hostView)) {
1563 mWidgetsToAdvance.remove(hostView);
1564 updateRunning();
1565 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 }
1567
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001569 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 launcherInfo.hostView = null;
1571 }
1572
Winson Chung93eef082012-03-23 15:59:27 -07001573 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1574 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1575 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001576 }
1577
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001578 public LauncherAppWidgetHost getAppWidgetHost() {
1579 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Winson Chunga9abd0e2010-10-27 17:18:37 -07001582 public LauncherModel getModel() {
1583 return mModel;
1584 }
1585
Bjorn Bringertc459e522013-06-07 19:36:01 +01001586 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001587 getWindow().closeAllPanels();
1588
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001589 // Whatever we were doing is hereby canceled.
1590 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001591 }
1592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 @Override
1594 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001595 long startTime = 0;
1596 if (DEBUG_RESUME_TIME) {
1597 startTime = System.currentTimeMillis();
1598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 super.onNewIntent(intent);
1600
1601 // Close the menu
1602 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001603 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001604 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001605
Jamie Genniscb222e82012-10-23 15:44:26 -07001606 final boolean alreadyOnHome =
1607 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001608 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001609
Jamie Genniscb222e82012-10-23 15:44:26 -07001610 Runnable processIntent = new Runnable() {
1611 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001612 if (mWorkspace == null) {
1613 // Can be cases where mWorkspace is null, this prevents a NPE
1614 return;
1615 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001616 Folder openFolder = mWorkspace.getOpenFolder();
1617 // In all these cases, only animate if we're already on home
1618 mWorkspace.exitWidgetResizeMode();
1619 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1620 openFolder == null) {
1621 mWorkspace.moveToDefaultScreen(true);
1622 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001623
Jamie Genniscb222e82012-10-23 15:44:26 -07001624 closeFolder();
1625 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001626
Jamie Genniscb222e82012-10-23 15:44:26 -07001627 // If we are already on home, then just animate back to the workspace,
1628 // otherwise, just wait until onResume to set the state back to Workspace
1629 if (alreadyOnHome) {
1630 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001631 if (mWorkspace.isInOverviewMode()) {
1632 mWorkspace.exitOverviewMode();
1633 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001634 } else {
1635 mOnResumeState = State.WORKSPACE;
1636 }
1637
1638 final View v = getWindow().peekDecorView();
1639 if (v != null && v.getWindowToken() != null) {
1640 InputMethodManager imm = (InputMethodManager)getSystemService(
1641 INPUT_METHOD_SERVICE);
1642 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1643 }
1644
1645 // Reset AllApps to its initial state
1646 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1647 mAppsCustomizeTabHost.reset();
1648 }
1649 }
1650 };
1651
1652 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1653 // Delay processing of the intent to allow the status bar animation to finish
1654 // first in order to avoid janky animations.
1655 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001656 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001657 // Process the intent immediately.
1658 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001659 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
Michael Jurka447bf842013-05-15 14:52:15 +02001662 if (DEBUG_RESUME_TIME) {
1663 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
1666
1667 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001668 public void onRestoreInstanceState(Bundle state) {
1669 super.onRestoreInstanceState(state);
1670 for (int page: mSynchronouslyBoundPages) {
1671 mWorkspace.restoreInstanceStateForChild(page);
1672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674
1675 @Override
1676 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001677 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001678 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679
Michael Jurka883f55b2010-10-21 15:47:14 -07001680 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001681 // We close any open folder since it will not be re-opened, and we need to make sure
1682 // this state is reflected.
1683 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684
Adam Cohendcd297f2013-06-18 13:13:40 -07001685 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001686 mWaitingForResult) {
1687 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001688 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001689 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1690 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001691 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1692 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1693 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
1695
1696 if (mFolderInfo != null && mWaitingForResult) {
1697 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1698 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1699 }
Michael Jurka946ad472010-07-09 18:05:18 -07001700
Winson Chung785d2eb2011-04-14 16:08:02 -07001701 // Save the current AppsCustomize tab
1702 if (mAppsCustomizeTabHost != null) {
1703 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1704 if (currentTabTag != null) {
1705 outState.putString("apps_customize_currentTab", currentTabTag);
1706 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001707 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1708 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 }
1711
1712 @Override
1713 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Winson Chunge7a03942011-08-05 15:05:12 -07001716 // Remove all pending runnables
1717 mHandler.removeMessages(ADVANCE_MSG);
1718 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001719 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001720
Winson Chungcd2b0142011-06-08 16:02:26 -07001721 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001722 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001723 mModel.stopLoader();
1724 app.setLauncher(null);
1725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001727 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001729 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001731 mAppWidgetHost = null;
1732
1733 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734
1735 TextKeyListener.getInstance().release();
1736
Winson Chung81b52252012-08-27 15:34:29 -07001737 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1738 // to prevent leaking Launcher activities on orientation change.
1739 if (mModel != null) {
1740 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1741 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001742
1743 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001744 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001745
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001746 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001747 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1748 mWorkspace.removeAllViews();
1749 mWorkspace = null;
1750 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001751
Michael Jurka2ecf9952012-06-18 12:52:28 -07001752 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754
Adam Cohena9cf38f2011-05-02 15:36:58 -07001755 public DragController getDragController() {
1756 return mDragController;
1757 }
1758
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 @Override
1760 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001761 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 super.startActivityForResult(intent, requestCode);
1763 }
1764
Winson Chung70d72102011-08-12 11:24:35 -07001765 /**
1766 * Indicates that we want global search for this activity by setting the globalSearch
1767 * argument for {@link #startSearch} to true.
1768 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001770 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001772
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001773 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001774
Karl Rosaen138a0412009-04-23 19:00:21 -07001775 if (initialQuery == null) {
1776 // Use any text typed in the launcher as the initial query
1777 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 if (appSearchData == null) {
1780 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001781 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
Winson Chungadf0c182012-08-23 14:59:07 -07001783 Rect sourceBounds = new Rect();
1784 if (mSearchDropTargetBar != null) {
1785 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1786 }
Romain Guycbb89e42009-06-08 15:52:54 -07001787
Bjorn Bringertc459e522013-06-07 19:36:01 +01001788 startSearch(initialQuery, selectInitialQuery,
1789 appSearchData, sourceBounds);
1790 }
1791
1792 public void startSearch(String initialQuery,
1793 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001794 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001795 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001796 }
1797
1798 /**
1799 * Starts the global search activity. This code is a copied from SearchManager
1800 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001801 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001802 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001803 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001804 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1805 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1806 if (globalSearchActivity == null) {
1807 Log.w(TAG, "No global search activity found.");
1808 return;
1809 }
1810 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1811 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1812 intent.setComponent(globalSearchActivity);
1813 // Make sure that we have a Bundle to put source in
1814 if (appSearchData == null) {
1815 appSearchData = new Bundle();
1816 } else {
1817 appSearchData = new Bundle(appSearchData);
1818 }
1819 // Set source to package name of app that starts global search, if not set already.
1820 if (!appSearchData.containsKey("source")) {
1821 appSearchData.putString("source", getPackageName());
1822 }
1823 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1824 if (!TextUtils.isEmpty(initialQuery)) {
1825 intent.putExtra(SearchManager.QUERY, initialQuery);
1826 }
1827 if (selectInitialQuery) {
1828 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1829 }
1830 intent.setSourceBounds(sourceBounds);
1831 try {
1832 startActivity(intent);
1833 } catch (ActivityNotFoundException ex) {
1834 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837
Winson Chung70d72102011-08-12 11:24:35 -07001838 @Override
1839 public boolean onCreateOptionsMenu(Menu menu) {
1840 if (isWorkspaceLocked()) {
1841 return false;
1842 }
1843
1844 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001845
1846 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1847 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1848 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001849 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1850 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1851 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001852 String helpUrl = getString(R.string.help_url);
1853 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001854 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1855 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1856
Winson Chung70d72102011-08-12 11:24:35 -07001857 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1858 .setIcon(android.R.drawable.ic_menu_gallery)
1859 .setAlphabeticShortcut('W');
1860 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1861 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001862 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001863 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001864 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1865 .setIcon(android.R.drawable.ic_menu_preferences)
1866 .setIntent(settings)
1867 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001868 if (!helpUrl.isEmpty()) {
1869 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1870 .setIcon(android.R.drawable.ic_menu_help)
1871 .setIntent(help)
1872 .setAlphabeticShortcut('H');
1873 }
Winson Chung70d72102011-08-12 11:24:35 -07001874 return true;
1875 }
1876
1877 @Override
1878 public boolean onPrepareOptionsMenu(Menu menu) {
1879 super.onPrepareOptionsMenu(menu);
1880
1881 if (mAppsCustomizeTabHost.isTransitioning()) {
1882 return false;
1883 }
1884 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1885 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1886
1887 return true;
1888 }
1889
1890 @Override
1891 public boolean onOptionsItemSelected(MenuItem item) {
1892 switch (item.getItemId()) {
1893 case MENU_WALLPAPER_SETTINGS:
1894 startWallpaper();
1895 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001896 }
1897
1898 return super.onOptionsItemSelected(item);
1899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001901 @Override
1902 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001903 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001904 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001905 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001906 }
1907
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908 public boolean isWorkspaceLocked() {
1909 return mWorkspaceLoading || mWaitingForResult;
1910 }
1911
Michael Jurka0280c3b2010-09-17 15:00:07 -07001912 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001913 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001914 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001915 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1916 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001917 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001919 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001920
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001921 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1922 AppWidgetProviderInfo appWidgetInfo) {
1923 if (appWidgetInfo.configure != null) {
1924 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001925
Bjorn Bringert7984c942009-12-09 15:38:25 +00001926 // Launch over to configure widget, if needed
1927 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001928 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001929 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001930 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001932 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001934 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001935 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001936 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
1938 }
Romain Guycbb89e42009-06-08 15:52:54 -07001939
Adam Cohenfbba09b2011-07-18 21:43:05 -07001940 /**
1941 * Process a shortcut drop.
1942 *
1943 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001945 * @param cell The cell it should be added to, optional
1946 * @param position The location on the screen where it was dropped, optional
1947 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001948 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001949 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001950 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001952 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001953 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001954
1955 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001956 mPendingAddInfo.cellX = cell[0];
1957 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001958 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001959
1960 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1961 createShortcutIntent.setComponent(componentName);
1962 processShortcut(createShortcutIntent);
1963 }
1964
Adam Cohenfbba09b2011-07-18 21:43:05 -07001965 /**
1966 * Process a widget drop.
1967 *
1968 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001969 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001970 * @param cell The cell it should be added to, optional
1971 * @param position The location on the screen where it was dropped, optional
1972 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001973 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001974 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001975 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001976 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001978 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001979 mPendingAddInfo.minSpanX = info.minSpanX;
1980 mPendingAddInfo.minSpanY = info.minSpanY;
1981
Adam Cohenfbba09b2011-07-18 21:43:05 -07001982 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001983 mPendingAddInfo.cellX = cell[0];
1984 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001985 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001986 if (span != null) {
1987 mPendingAddInfo.spanX = span[0];
1988 mPendingAddInfo.spanY = span[1];
1989 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001990
Adam Cohened66b2b2012-01-23 17:28:51 -08001991 AppWidgetHostView hostView = info.boundWidget;
1992 int appWidgetId;
1993 if (hostView != null) {
1994 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001995 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001996 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001997 // In this case, we either need to start an activity to get permission to bind
1998 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001999 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002000 Bundle options = info.bindOptions;
2001
2002 boolean success = false;
2003 if (options != null) {
2004 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2005 info.componentName, options);
2006 } else {
2007 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2008 info.componentName);
2009 }
2010 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002011 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002012 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002013 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002014 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2015 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2016 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002017 // TODO: we need to make sure that this accounts for the options bundle.
2018 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002019 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2020 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002021 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002022 }
2023
Joe Onoratodeb98af2010-02-19 14:59:39 -08002024 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002025 // Handle case where user selected "Applications"
2026 String applicationName = getResources().getString(R.string.group_applications);
2027 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002028
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002029 if (applicationName != null && applicationName.equals(shortcutName)) {
2030 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2031 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002032
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002033 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2034 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002035 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002036 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002037 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002038 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041
Winson Chung24ab2f12010-09-16 14:10:47 -07002042 void processWallpaper(Intent intent) {
2043 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2044 }
2045
Adam Cohendcd297f2013-06-18 13:13:40 -07002046 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002047 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002048 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 folderInfo.title = getText(R.string.folder_name);
2050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002052 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002053 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002054 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055
2056 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002057 FolderIcon newFolder =
2058 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002059 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002060 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002061 // Force measure the new folder icon
2062 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2063 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002064 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
Romain Guycbb89e42009-06-08 15:52:54 -07002066
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002068 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002069 }
2070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002072 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002074 pickWallpaper.setComponent(
2075 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2076 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002079 /**
2080 * Registers various content observers. The current implementation registers
2081 * only a favorites observer to keep track of the favorites applications.
2082 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002083 private void registerContentObservers() {
2084 ContentResolver resolver = getContentResolver();
2085 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2086 true, mWidgetObserver);
2087 }
2088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 @Override
2090 public boolean dispatchKeyEvent(KeyEvent event) {
2091 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2092 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002094 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002095 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002096 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002097 dumpState();
2098 return true;
2099 }
2100 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002101 }
2102 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2103 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002104 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 return true;
2106 }
2107 }
2108
2109 return super.dispatchKeyEvent(event);
2110 }
2111
Joe Onorato88ec0992009-11-19 13:16:06 -08002112 @Override
2113 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002114 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002115 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002116 } else if (mWorkspace.isInOverviewMode()) {
2117 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002118 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002119 Folder openFolder = mWorkspace.getOpenFolder();
2120 if (openFolder.isEditingName()) {
2121 openFolder.dismissEditingName();
2122 } else {
2123 closeFolder();
2124 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002125 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002126 mWorkspace.exitWidgetResizeMode();
2127
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002128 // Back button is a no-op here, but give at least some feedback for the button press
2129 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002130 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002131 }
2132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002134 * Re-listen when widgets are reset.
2135 */
2136 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002137 if (mAppWidgetHost != null) {
2138 mAppWidgetHost.startListening();
2139 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002140 }
2141
2142 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 * Launches the intent referred by the clicked shortcut.
2144 *
2145 * @param v The view representing the clicked shortcut.
2146 */
2147 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002148 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2149 // view has detached (it's possible for this to happen if the view is removed mid touch).
2150 if (v.getWindowToken() == null) {
2151 return;
2152 }
2153
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002154 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002155 return;
2156 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002157
Adam Cohenf358a4b2013-07-23 16:47:31 -07002158 if (v instanceof PageIndicator) {
2159 if (!mWorkspace.isInOverviewMode()) {
2160 mWorkspace.enterOverviewMode();
2161 }
2162 return;
2163 }
2164
2165 if (v instanceof CellLayout) {
2166 if (mWorkspace.isInOverviewMode()) {
2167 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2168 }
2169 }
2170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002172 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002174 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2175 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002176
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002177 // Check for special shortcuts
2178 if (intent.getComponent() != null) {
2179 final String shortcutClass = intent.getComponent().getClassName();
2180
2181 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2182 showAllApps(true);
2183 return;
2184 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2185 MemoryDumpActivity.startDump(this);
2186 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002187 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2188 toggleShowWeightWatcher();
2189 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002190 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002191 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002192
2193 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002194 int[] pos = new int[2];
2195 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002196 intent.setSourceBounds(new Rect(pos[0], pos[1],
2197 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002198
2199 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002200
Daniel Sandlerff02d492013-08-05 02:12:05 -04002201 mStats.recordLaunch(intent, shortcut);
2202
Michael Jurkaddd62e92011-02-16 17:49:14 -08002203 if (success && v instanceof BubbleTextView) {
2204 mWaitingForResume = (BubbleTextView) v;
2205 mWaitingForResume.setStayPressed(true);
2206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002208 if (v instanceof FolderIcon) {
2209 FolderIcon fi = (FolderIcon) v;
2210 handleFolderClick(fi);
2211 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002212 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002213 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002214 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002215 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002216 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219 }
2220
Michael Jurka0e260592010-06-30 17:07:39 -07002221 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002222 return false;
2223 }
2224
Michael Jurkaaf442092010-06-10 17:01:57 -07002225 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002226 * Event handler for the search button
2227 *
2228 * @param v The view that was clicked.
2229 */
2230 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002231 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2232
Amith Yamasania135ba82011-08-09 17:42:01 -07002233 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002234 }
2235
2236 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002237 * Event handler for the voice button
2238 *
2239 * @param v The view that was clicked.
2240 */
2241 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002242 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002243
Bjorn Bringertc459e522013-06-07 19:36:01 +01002244 startVoice();
2245 }
2246
2247 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002248 try {
2249 final SearchManager searchManager =
2250 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2251 ComponentName activityName = searchManager.getGlobalSearchActivity();
2252 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002253 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002254 if (activityName != null) {
2255 intent.setPackage(activityName.getPackageName());
2256 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002257 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002258 } catch (ActivityNotFoundException e) {
2259 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002260 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002261 startActivitySafely(null, intent, "onClickVoiceButton");
2262 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002263 }
2264
2265 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002266 * Event handler for the "grid" button that appears on the home screen, which
2267 * enters all apps mode.
2268 *
2269 * @param v The view that was clicked.
2270 */
2271 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002272 showAllApps(true);
2273 }
2274
2275 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002276 // Provide the same haptic feedback that the system offers for virtual keys.
2277 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002278 }
2279
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002280 public void onClickAppMarketButton(View v) {
2281 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002282 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002283 } else {
2284 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002285 }
2286 }
2287
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002288 void startApplicationDetailsActivity(ComponentName componentName) {
2289 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002290 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2291 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002292 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002293 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002294 }
2295
Michael Jurka1e2f4652013-07-08 18:03:46 -07002296 // returns true if the activity was started
2297 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2298 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002299 // System applications cannot be installed. For now, show a toast explaining that.
2300 // We may give them the option of disabling apps this way.
2301 int messageId = R.string.uninstall_system_app_text;
2302 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002303 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002304 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002305 String packageName = componentName.getPackageName();
2306 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002307 Intent intent = new Intent(
2308 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002309 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2310 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002311 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002312 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002313 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002314 }
2315
Michael Jurka86a720e2012-05-09 11:23:23 -07002316 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002320 // Only launch using the new animation if the shortcut has not opted out (this is a
2321 // private contract between launcher and may be ignored in the future).
2322 boolean useLaunchAnimation = (v != null) &&
2323 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2324 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002325 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2326 v.getMeasuredWidth(), v.getMeasuredHeight());
2327
2328 startActivity(intent, opts.toBundle());
2329 } else {
2330 startActivity(intent);
2331 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002332 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 } catch (SecurityException e) {
2334 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002335 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002337 "or use the exported attribute for this activity. "
2338 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002340 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002342
Michael Jurka86a720e2012-05-09 11:23:23 -07002343 boolean startActivitySafely(View v, Intent intent, Object tag) {
2344 boolean success = false;
2345 try {
2346 success = startActivity(v, intent, tag);
2347 } catch (ActivityNotFoundException e) {
2348 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2349 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2350 }
2351 return success;
2352 }
2353
Adam Cohena9cf38f2011-05-02 15:36:58 -07002354 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002355 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002356 Folder openFolder = mWorkspace.getFolderForTag(info);
2357
2358 // If the folder info reports that the associated folder is open, then verify that
2359 // it is actually opened. There have been a few instances where this gets out of sync.
2360 if (info.opened && openFolder == null) {
2361 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002362 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002363 info.opened = false;
2364 }
2365
Adam Cohenfb91f302012-06-11 15:45:18 -07002366 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 // Close any open folder
2368 closeFolder();
2369 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002370 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 } else {
2372 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 int folderScreen;
2374 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002375 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 // .. and close it
2377 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002378 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 // Close any folder open on the current screen
2380 closeFolder();
2381 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002382 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
2384 }
2385 }
2386 }
2387
Adam Cohen268c4752012-06-06 17:47:33 -07002388 /**
2389 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2390 * in the DragLayer in the exact absolute location of the original FolderIcon.
2391 */
2392 private void copyFolderIconToImage(FolderIcon fi) {
2393 final int width = fi.getMeasuredWidth();
2394 final int height = fi.getMeasuredHeight();
2395
2396 // Lazy load ImageView, Bitmap and Canvas
2397 if (mFolderIconImageView == null) {
2398 mFolderIconImageView = new ImageView(this);
2399 }
2400 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2401 mFolderIconBitmap.getHeight() != height) {
2402 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2403 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2404 }
2405
2406 DragLayer.LayoutParams lp;
2407 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2408 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2409 } else {
2410 lp = new DragLayer.LayoutParams(width, height);
2411 }
2412
Adam Cohen307fe232012-08-16 17:55:58 -07002413 // The layout from which the folder is being opened may be scaled, adjust the starting
2414 // view size by this scale factor.
2415 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002416 lp.customPosition = true;
2417 lp.x = mRectForFolderAnimation.left;
2418 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002419 lp.width = (int) (scale * width);
2420 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002421
2422 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2423 fi.draw(mFolderIconCanvas);
2424 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002425 if (fi.getFolder() != null) {
2426 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2427 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002428 }
Adam Cohen268c4752012-06-06 17:47:33 -07002429 // Just in case this image view is still in the drag layer from a previous animation,
2430 // we remove it and re-add it.
2431 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2432 mDragLayer.removeView(mFolderIconImageView);
2433 }
2434 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002435 if (fi.getFolder() != null) {
2436 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002437 }
Adam Cohen268c4752012-06-06 17:47:33 -07002438 }
2439
Adam Cohen2801caf2011-05-13 20:57:39 -07002440 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002441 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002442 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2443 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2444 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2445
Adam Cohenc51934b2011-07-26 21:07:43 -07002446 FolderInfo info = (FolderInfo) fi.getTag();
2447 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2448 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002449 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2450 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002451 }
2452
Adam Cohen268c4752012-06-06 17:47:33 -07002453 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2454 copyFolderIconToImage(fi);
2455 fi.setVisibility(View.INVISIBLE);
2456
Michael Jurka2ecf9952012-06-18 12:52:28 -07002457 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002458 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002459 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2460 oa.start();
2461 }
2462
Adam Cohen268c4752012-06-06 17:47:33 -07002463 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002464 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002465 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2466 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2467 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2468
Adam Cohen268c4752012-06-06 17:47:33 -07002469 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002470
Adam Cohen268c4752012-06-06 17:47:33 -07002471 // We remove and re-draw the FolderIcon in-case it has changed
2472 mDragLayer.removeView(mFolderIconImageView);
2473 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002474 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002475 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002476 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002477 oa.addListener(new AnimatorListenerAdapter() {
2478 @Override
2479 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002480 if (cl != null) {
2481 cl.clearFolderLeaveBehind();
2482 // Remove the ImageView copy of the FolderIcon and make the original visible.
2483 mDragLayer.removeView(mFolderIconImageView);
2484 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002485 }
2486 }
2487 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002488 oa.start();
2489 }
2490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002492 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 * is animated relative to the specified View. If the View is null, no animation
2494 * is played.
2495 *
2496 * @param folderInfo The FolderInfo describing the folder to open.
2497 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002498 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002499 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503
Adam Cohen4554ee12011-08-03 16:13:21 -07002504 // Just verify that the folder hasn't already been added to the DragLayer.
2505 // There was a one-off crash where the folder had a parent already.
2506 if (folder.getParent() == null) {
2507 mDragLayer.addView(folder);
2508 mDragController.addDropTarget((DropTarget) folder);
2509 } else {
2510 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2511 folder.getParent() + ").");
2512 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002513 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002514 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002515
2516 // Notify the accessibility manager that this folder "window" has appeared and occluded
2517 // the workspace items
2518 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2519 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002520 }
2521
2522 public void closeFolder() {
2523 Folder folder = mWorkspace.getOpenFolder();
2524 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002525 if (folder.isEditingName()) {
2526 folder.dismissEditingName();
2527 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002528 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002529
2530 // Dismiss the folder cling
2531 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002532 }
2533 }
2534
2535 void closeFolder(Folder folder) {
2536 folder.getInfo().opened = false;
2537
2538 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2539 if (parent != null) {
2540 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2541 shrinkAndFadeInFolderIcon(fi);
2542 }
2543 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002544
2545 // Notify the accessibility manager that this folder "window" has disappeard and no
2546 // longer occludeds the workspace items
2547 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 }
2549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002551 if (!isDraggingEnabled()) return false;
2552 if (isWorkspaceLocked()) return false;
2553 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554
2555 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002556 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
2558
Michael Jurka0280c3b2010-09-17 15:00:07 -07002559 resetAddInfo();
2560 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002562 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 return true;
2564 }
2565
Winson Chung3d503fb2011-07-13 17:25:49 -07002566 // The hotseat touch handling does not go through Workspace, and we always allow long press
2567 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002568 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002569 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2570 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002571 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002572 if (mWorkspace.hasNonCustomEmptyScreens()) {
2573 // User long pressed on empty space
2574 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2575 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2576 // Disabling reordering until we sort out some issues.
2577 if (mWorkspace.isInOverviewMode()) {
2578 mWorkspace.startReordering(v);
2579 } else {
2580 mWorkspace.enterOverviewMode();
2581 }
Adam Cohendedbd962013-07-11 14:21:49 -07002582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002584 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002586 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588 }
2589 }
2590 return true;
2591 }
2592
Winson Chung3d503fb2011-07-13 17:25:49 -07002593 boolean isHotseatLayout(View layout) {
2594 return mHotseat != null && layout != null &&
2595 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2596 }
2597 Hotseat getHotseat() {
2598 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002599 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002600 View getOverviewPanel() {
2601 return mOverviewPanel;
2602 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002603 SearchDropTargetBar getSearchBar() {
2604 return mSearchDropTargetBar;
2605 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002606
Winson Chung3d503fb2011-07-13 17:25:49 -07002607 /**
2608 * Returns the CellLayout of the specified container at the specified screen.
2609 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002610 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002611 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2612 if (mHotseat != null) {
2613 return mHotseat.getLayout();
2614 } else {
2615 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002616 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002617 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002618 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002619 }
2620 }
2621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 Workspace getWorkspace() {
2623 return mWorkspace;
2624 }
2625
Daniel Sandler843e8602010-06-07 14:59:01 -04002626 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002627 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002628 }
2629
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 /**
2631 * Helper method for the cameraZoomIn/cameraZoomOut animations
2632 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 * @param scaleFactor The scale factor used for the zoom
2634 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002635 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002637 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002639
Winson Chung18f41f82012-05-09 13:28:10 -07002640 void disableWallpaperIfInAllApps() {
2641 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002642 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002643 if (mAppsCustomizeTabHost != null &&
2644 !mAppsCustomizeTabHost.isTransitioning()) {
2645 updateWallpaperVisibility(false);
2646 }
2647 }
2648 }
2649
Craig Mautner360310b2012-10-26 15:13:08 -07002650 private void setWorkspaceBackground(boolean workspace) {
2651 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002652 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002653 }
2654
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002655 void updateWallpaperVisibility(boolean visible) {
2656 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2657 int curflags = getWindow().getAttributes().flags
2658 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2659 if (wpflags != curflags) {
2660 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2661 }
Craig Mautner360310b2012-10-26 15:13:08 -07002662 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002663 }
2664
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002665 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2666 if (v instanceof LauncherTransitionable) {
2667 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2668 }
2669 }
2670
Michael Jurkabed61d22012-02-14 22:51:29 -08002671 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2672 if (v instanceof LauncherTransitionable) {
2673 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2674 }
Winson Chung70442722012-02-10 15:43:22 -08002675
2676 // Update the workspace transition step as well
2677 dispatchOnLauncherTransitionStep(v, 0f);
2678 }
2679
2680 private void dispatchOnLauncherTransitionStep(View v, float t) {
2681 if (v instanceof LauncherTransitionable) {
2682 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2683 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002684 }
2685
2686 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2687 if (v instanceof LauncherTransitionable) {
2688 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2689 }
Winson Chung70442722012-02-10 15:43:22 -08002690
2691 // Update the workspace transition step as well
2692 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002693 }
2694
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002695 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002696 * Things to test when changing the following seven functions.
2697 * - Home from workspace
2698 * - from center screen
2699 * - from other screens
2700 * - Home from all apps
2701 * - from center screen
2702 * - from other screens
2703 * - Back from all apps
2704 * - from center screen
2705 * - from other screens
2706 * - Launch app from workspace and quit
2707 * - with back
2708 * - with home
2709 * - Launch app from all apps and quit
2710 * - with back
2711 * - with home
2712 * - Go to a screen that's not the default, then all
2713 * apps, and launch and app, and go back
2714 * - with back
2715 * -with home
2716 * - On workspace, long press power and go back
2717 * - with back
2718 * - with home
2719 * - On all apps, long press power and go back
2720 * - with back
2721 * - with home
2722 * - On workspace, power off
2723 * - On all apps, power off
2724 * - Launch an app and turn off the screen while in that app
2725 * - Go back with home key
2726 * - Go back with back key TODO: make this not go to workspace
2727 * - From all apps
2728 * - From workspace
2729 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2730 * - From all apps
2731 * - From the center workspace
2732 * - From another workspace
2733 */
2734
2735 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002736 * Zoom the camera out from the workspace to reveal 'toView'.
2737 * Assumes that the view to show is anchored at either the very top or very bottom
2738 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002739 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002740 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002741 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002742 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002743 mStateAnimation.cancel();
2744 mStateAnimation = null;
2745 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002746 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002747
Winson Chungf0ea4d32011-06-06 14:27:16 -07002748 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2749 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2750 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002751 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002752 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002753 final int startDelay =
2754 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002755
Michael Jurkab3e22d92011-10-31 15:58:33 -07002756 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002757
Michael Jurkad74c9842011-07-10 12:44:21 -07002758 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002759 Animator workspaceAnim =
2760 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002761
2762 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002763 toView.setScaleX(scale);
2764 toView.setScaleY(scale);
2765 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2766 scaleAnim.
2767 scaleX(1f).scaleY(1f).
2768 setDuration(duration).
2769 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002770
Winson Chungf0ea4d32011-06-06 14:27:16 -07002771 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002772 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002773 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002774 .ofFloat(toView, "alpha", 0f, 1f)
2775 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002776 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002777 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2778 @Override
2779 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002780 if (animation == null) {
2781 throw new RuntimeException("animation is null");
2782 }
Winson Chung70442722012-02-10 15:43:22 -08002783 float t = (Float) animation.getAnimatedValue();
2784 dispatchOnLauncherTransitionStep(fromView, t);
2785 dispatchOnLauncherTransitionStep(toView, t);
2786 }
2787 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002788
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002789 // toView should appear right at the end of the workspace shrink
2790 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002791 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002792 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002793 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002794
2795 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002796 boolean animationCancelled = false;
2797
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002798 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002799 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002800 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002801 // Prepare the position
2802 toView.setTranslationX(0.0f);
2803 toView.setTranslationY(0.0f);
2804 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002805 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002807 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002808 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002809 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2810 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002811
Adam Cohenf4ddea32011-09-12 13:46:42 -07002812 if (!animationCancelled) {
2813 updateWallpaperVisibility(false);
2814 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002815
2816 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002817 if (mSearchDropTargetBar != null) {
2818 mSearchDropTargetBar.hideSearchBar(false);
2819 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002820 }
2821
Adam Cohencff6af82011-09-13 14:51:53 -07002822 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002823 public void onAnimationCancel(Animator animation) {
2824 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002825 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 });
2827
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002828 if (workspaceAnim != null) {
2829 mStateAnimation.play(workspaceAnim);
2830 }
Michael Jurka1899a362011-11-03 13:50:45 -07002831
2832 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002833
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002834 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2835 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002836
2837 // If any of the objects being animated haven't been measured/laid out
2838 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002839 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002840 (mWorkspace.getMeasuredWidth() == 0) ||
2841 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002842 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002843 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002844
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002845 final AnimatorSet stateAnimation = mStateAnimation;
2846 final Runnable startAnimRunnable = new Runnable() {
2847 public void run() {
2848 // Check that mStateAnimation hasn't changed while
2849 // we waited for a layout/draw pass
2850 if (mStateAnimation != stateAnimation)
2851 return;
2852 setPivotsForZoom(toView, scale);
2853 dispatchOnLauncherTransitionStart(fromView, animated, false);
2854 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002855 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002856 }
2857 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002858 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002859 final ViewTreeObserver observer = toView.getViewTreeObserver();
2860 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2861 public void onGlobalLayout() {
2862 startAnimRunnable.run();
2863 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2864 }
2865 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002866 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002867 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002868 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869 } else {
2870 toView.setTranslationX(0.0f);
2871 toView.setTranslationY(0.0f);
2872 toView.setScaleX(1.0f);
2873 toView.setScaleY(1.0f);
2874 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002875 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002876
Daniel Sandlere4f98912013-06-25 15:13:26 -04002877 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002878 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002879 if (mSearchDropTargetBar != null) {
2880 mSearchDropTargetBar.hideSearchBar(false);
2881 }
Michael Jurkaabded662011-03-04 12:06:57 -08002882 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002883 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002884 dispatchOnLauncherTransitionStart(fromView, animated, false);
2885 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002886 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002887 dispatchOnLauncherTransitionStart(toView, animated, false);
2888 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002889 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002891 }
2892
2893 /**
2894 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002896 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002897 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002898 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2899 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002900
Michael Jurkab3e22d92011-10-31 15:58:33 -07002901 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002902 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002903 mStateAnimation.cancel();
2904 mStateAnimation = null;
2905 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002906 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002907
Winson Chungf0ea4d32011-06-06 14:27:16 -07002908 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002909 final int fadeOutDuration =
2910 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002911 final float scaleFactor = (float)
2912 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2913 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002914 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002915 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002916 if (toState == State.WORKSPACE) {
2917 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2918 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002919 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002920 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2921 workspaceAnim = mWorkspace.getChangeStateAnimation(
2922 Workspace.State.SPRING_LOADED, animated);
2923 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002924
Michael Jurkab3e22d92011-10-31 15:58:33 -07002925 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002926 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002927 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002928 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002929 final LauncherViewPropertyAnimator scaleAnim =
2930 new LauncherViewPropertyAnimator(fromView);
2931 scaleAnim.
2932 scaleX(scaleFactor).scaleY(scaleFactor).
2933 setDuration(duration).
2934 setInterpolator(new Workspace.ZoomInInterpolator());
2935
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002936 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002937 .ofFloat(fromView, "alpha", 1f, 0f)
2938 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002939 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002940 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2941 @Override
2942 public void onAnimationUpdate(ValueAnimator animation) {
2943 float t = 1f - (Float) animation.getAnimatedValue();
2944 dispatchOnLauncherTransitionStep(fromView, t);
2945 dispatchOnLauncherTransitionStep(toView, t);
2946 }
2947 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002948
Michael Jurka2ecf9952012-06-18 12:52:28 -07002949 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002950
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002951 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2952 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002953 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002954
2955 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002956 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002957 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002958 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002959 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002960 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2961 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002962 if (onCompleteRunnable != null) {
2963 onCompleteRunnable.run();
2964 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002965 mAppsCustomizeContent.updateCurrentPageScroll();
2966 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002967 }
2968 });
2969
Winson Chungf0ea4d32011-06-06 14:27:16 -07002970 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002971 if (workspaceAnim != null) {
2972 mStateAnimation.play(workspaceAnim);
2973 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002974 dispatchOnLauncherTransitionStart(fromView, animated, true);
2975 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002976 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002977 } else {
2978 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002979 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002980 dispatchOnLauncherTransitionStart(fromView, animated, true);
2981 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002982 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002983 dispatchOnLauncherTransitionStart(toView, animated, true);
2984 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002985 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002986 }
2987
Michael Jurkae326f182011-11-21 14:05:46 -08002988 @Override
2989 public void onTrimMemory(int level) {
2990 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002991 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002992 mAppsCustomizeTabHost.onTrimMemory();
2993 }
2994 }
2995
Winson Chung18f41f82012-05-09 13:28:10 -07002996 @Override
2997 public void onWindowFocusChanged(boolean hasFocus) {
2998 if (!hasFocus) {
2999 // When another window occludes launcher (like the notification shade, or recents),
3000 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3001 updateWallpaperVisibility(true);
3002 } else {
3003 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003004 mWorkspace.postDelayed(new Runnable() {
3005 @Override
3006 public void run() {
3007 disableWallpaperIfInAllApps();
3008 }
3009 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003010 }
3011 }
3012
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003013 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003014 showWorkspace(animated, null);
3015 }
3016
3017 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003018 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003019 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003021 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003022
Winson Chungc7d2b602012-05-16 17:02:20 -07003023 // Show the search bar (only animate if we were showing the drop target bar in spring
3024 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003025 if (mSearchDropTargetBar != null) {
3026 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3027 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003028
Michael Jurkab3e22d92011-10-31 15:58:33 -07003029 // Set focus to the AppsCustomize button
3030 if (mAllAppsButton != null) {
3031 mAllAppsButton.requestFocus();
3032 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003033 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003034
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003035 // Change the state *after* we've called all the transition code
3036 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003037
Winson Chung5fb63472011-02-02 17:03:37 -08003038 // Resume the auto-advance of widgets
3039 mUserPresent = true;
3040 updateRunning();
3041
alanv1d4fde62012-10-17 13:15:47 -07003042 // Send an accessibility event to announce the context change
3043 getWindow().getDecorView()
3044 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003045
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003046 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003047 }
3048
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003049 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003050 }
3051
Michael Jurkab3e22d92011-10-31 15:58:33 -07003052 void showAllApps(boolean animated) {
3053 if (mState != State.WORKSPACE) return;
3054
3055 showAppsCustomizeHelper(animated, false);
3056 mAppsCustomizeTabHost.requestFocus();
3057
Michael Jurkab3e22d92011-10-31 15:58:33 -07003058 // Change the state *after* we've called all the transition code
3059 mState = State.APPS_CUSTOMIZE;
3060
3061 // Pause the auto-advance of widgets until we are out of AllApps
3062 mUserPresent = false;
3063 updateRunning();
3064 closeFolder();
3065
3066 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003067 getWindow().getDecorView()
3068 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003069 }
3070
Adam Cohen7777d962011-08-18 18:58:38 -07003071 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003072 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003073 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003074 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003075 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003076 }
Adam Cohen7777d962011-08-18 18:58:38 -07003077
Adam Cohened66b2b2012-01-23 17:28:51 -08003078 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3079 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003080 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3081
Winson Chunge7a03942011-08-05 15:05:12 -07003082 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003083 @Override
3084 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003085 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003086 // Before we show workspace, hide all apps again because
3087 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3088 // clean up our state transition functions
3089 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003090 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003091 } else {
3092 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003093 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003094 }
3095 }, (extendedDelay ?
3096 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3097 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3098 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003099
Michael Jurkad3ef3062010-11-23 16:23:58 -08003100 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003101 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003102 final boolean animated = true;
3103 final boolean springLoaded = true;
3104 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003105 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003106 }
3107 // Otherwise, we are not in spring loaded mode, so don't do anything.
3108 }
3109
Michael Jurkab3e22d92011-10-31 15:58:33 -07003110 void lockAllApps() {
3111 // TODO
3112 }
3113
3114 void unlockAllApps() {
3115 // TODO
3116 }
3117
Winson Chungf0ea4d32011-06-06 14:27:16 -07003118 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003122 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003123 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003124 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003125 int duration = 0;
3126 if (mSearchDropTargetBar != null) {
3127 duration = mSearchDropTargetBar.getTransitionInDuration();
3128 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003129 mHotseat.animate().alpha(1f).setDuration(duration);
3130 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 }
3134 }
3135 }
3136
3137 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003138 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003139 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003140 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003141 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003142 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003143 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003144 int duration = 0;
3145 if (mSearchDropTargetBar != null) {
3146 duration = mSearchDropTargetBar.getTransitionOutDuration();
3147 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003148 mHotseat.animate().alpha(0f).setDuration(duration);
3149 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003150 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003152 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003153 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003154 }
3155
Patrick Dubroy5f445422011-02-18 14:35:21 -08003156 /**
3157 * Add an item from all apps or customize onto the given workspace screen.
3158 * If layout is null, add to the current screen.
3159 */
3160 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003161 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003162 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003163 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003164 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003165
Winson Chungdff8ebb2011-09-08 17:25:31 -07003166 /** Maps the current orientation to an index for referencing orientation correct global icons */
3167 private int getCurrentOrientationIndexForGlobalIcons() {
3168 // default - 0, landscape - 1
3169 switch (getResources().getConfiguration().orientation) {
3170 case Configuration.ORIENTATION_LANDSCAPE:
3171 return 1;
3172 default:
3173 return 0;
3174 }
3175 }
3176
Michael Jurkaae65ee32012-04-30 11:45:54 -07003177 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003178 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003179 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003180 // Look for the toolbar icon specified in the activity meta-data
3181 Bundle metaData = packageManager.getActivityInfo(
3182 activityName, PackageManager.GET_META_DATA).metaData;
3183 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003184 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003185 if (iconResId != 0) {
3186 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003187 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003188 }
3189 }
3190 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003191 // This can happen if the activity defines an invalid drawable
3192 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3193 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003194 } catch (Resources.NotFoundException nfe) {
3195 // This can happen if the activity defines an invalid drawable
3196 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3197 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003198 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003199 return null;
3200 }
3201
3202 // if successful in getting icon, return it; otherwise, set button to use default drawable
3203 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003204 int buttonId, ComponentName activityName, int fallbackDrawableId,
3205 String toolbarResourceName) {
3206 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003207 Resources r = getResources();
3208 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3209 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003210
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003211 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003212 // If we were unable to find the icon via the meta-data, use a generic one
3213 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003214 toolbarIcon = r.getDrawable(fallbackDrawableId);
3215 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003216 if (button != null) {
3217 button.setCompoundDrawables(toolbarIcon, null, null, null);
3218 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003219 return null;
3220 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003221 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003222 if (button != null) {
3223 button.setCompoundDrawables(toolbarIcon, null, null, null);
3224 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003225 return toolbarIcon.getConstantState();
3226 }
3227 }
3228
3229 // if successful in getting icon, return it; otherwise, set button to use default drawable
3230 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003231 int buttonId, ComponentName activityName, int fallbackDrawableId,
3232 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003233 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003234 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003235
Michael Jurka19e0fc52011-07-22 18:00:21 -07003236 if (button != null) {
3237 // If we were unable to find the icon via the meta-data, use a
3238 // generic one
3239 if (toolbarIcon == null) {
3240 button.setImageResource(fallbackDrawableId);
3241 } else {
3242 button.setImageDrawable(toolbarIcon);
3243 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003244 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003245
3246 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3247
Michael Jurka0423dcf2010-10-05 14:56:18 -07003248 }
3249
Winson Chung1b884092012-06-08 17:13:02 -07003250 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003251 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003252 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003253 }
3254
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003255 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003256 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003257 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003258 }
3259
Winson Chungbb185bd2011-11-21 12:31:42 -08003260 private void invalidatePressedFocusedStates(View container, View button) {
3261 if (container instanceof HolographicLinearLayout) {
3262 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3263 layout.invalidatePressedFocusedStates();
3264 } else if (button instanceof HolographicImageView) {
3265 HolographicImageView view = (HolographicImageView) button;
3266 view.invalidatePressedFocusedStates();
3267 }
3268 }
3269
Cristina Stancu476493b2013-08-07 17:20:14 +01003270 public View getQsbBar() {
3271 if (mQsbBar == null) {
3272 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3273 }
3274 return mQsbBar;
3275 }
3276
3277 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003278 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003279 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003280 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003281 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003282
Winson Chung1cad91e2011-05-25 17:41:01 -07003283 final SearchManager searchManager =
3284 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3285 ComponentName activityName = searchManager.getGlobalSearchActivity();
3286 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003287 int coi = getCurrentOrientationIndexForGlobalIcons();
3288 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003289 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3290 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3291 if (sGlobalSearchIcon[coi] == null) {
3292 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3293 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3294 TOOLBAR_ICON_METADATA_NAME);
3295 }
3296
Winson Chungc51db6a2011-10-05 11:44:49 -07003297 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3298 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003299 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003300 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003301 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003302 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003303 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3304 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3305 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003306 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003307 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003308 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003309 }
3310 }
3311
Cristina Stancu476493b2013-08-07 17:20:14 +01003312 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003313 final View searchButtonContainer = findViewById(R.id.search_button_container);
3314 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003315 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003316 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003317 }
3318
Cristina Stancu476493b2013-08-07 17:20:14 +01003319 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003320 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003321 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003322
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003324 final SearchManager searchManager =
3325 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3326 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3327
3328 ComponentName activityName = null;
3329 if (globalSearchActivity != null) {
3330 // Check if the global search activity handles voice search
3331 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3332 intent.setPackage(globalSearchActivity.getPackageName());
3333 activityName = intent.resolveActivity(getPackageManager());
3334 }
3335
3336 if (activityName == null) {
3337 // Fallback: check if an activity other than the global search activity
3338 // resolves this
3339 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3340 activityName = intent.resolveActivity(getPackageManager());
3341 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003342 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003343 int coi = getCurrentOrientationIndexForGlobalIcons();
3344 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003345 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3346 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3347 if (sVoiceSearchIcon[coi] == null) {
3348 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3349 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3350 TOOLBAR_ICON_METADATA_NAME);
3351 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003352 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003353 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003354 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003355 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003356 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003357 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003358 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003359 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003360 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003361 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003362 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003363 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003364
Cristina Stancu476493b2013-08-07 17:20:14 +01003365 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003366 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3367 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003368 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003369 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003370 }
3371
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003372 public void setVoiceButtonProxyVisible(boolean visible) {
3373 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3374 if (voiceButtonProxy != null) {
3375 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3376 }
3377 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003378 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003379 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003380 */
3381 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003382 final View marketButton = findViewById(R.id.market_button);
3383 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3384 // Find the app market activity by resolving an intent.
3385 // (If multiple app markets are installed, it will return the ResolverActivity.)
3386 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003387 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003388 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003389 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003390 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003391 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3392 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003393 marketButton.setVisibility(View.VISIBLE);
3394 } else {
3395 // We should hide and disable the view so that we don't try and restore the visibility
3396 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3397 marketButton.setVisibility(View.GONE);
3398 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003399 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003400 }
3401
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003402 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003403 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3404 Resources r = getResources();
3405 Drawable marketIconDrawable = d.newDrawable(r);
3406 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3407 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3408 marketIconDrawable.setBounds(0, 0, w, h);
3409
3410 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003411 }
3412
Michael Jurkad7c28052012-04-27 15:43:36 -07003413 @Override
3414 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003415 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003416 final List<CharSequence> text = event.getText();
3417 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003418 // Populate event with a fake title based on the current state.
3419 if (mState == State.APPS_CUSTOMIZE) {
3420 text.add(getString(R.string.all_apps_button_label));
3421 } else {
3422 text.add(getString(R.string.all_apps_home_button_label));
3423 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003424 return result;
3425 }
3426
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003427 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003428 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003429 */
3430 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3431 @Override
3432 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003433 closeSystemDialogs();
3434 }
3435 }
3436
3437 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003438 * Receives notifications whenever the appwidgets are reset.
3439 */
3440 private class AppWidgetResetObserver extends ContentObserver {
3441 public AppWidgetResetObserver() {
3442 super(new Handler());
3443 }
3444
3445 @Override
3446 public void onChange(boolean selfChange) {
3447 onAppWidgetReset();
3448 }
3449 }
3450
3451 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003452 * If the activity is currently paused, signal that we need to run the passed Runnable
3453 * in onResume.
3454 *
3455 * This needs to be called from incoming places where resources might have been loaded
3456 * while we are paused. That is becaues the Configuration might be wrong
3457 * when we're not running, and if it comes back to what it was when we
3458 * were paused, we are not restarted.
3459 *
3460 * Implementation of the method from LauncherModel.Callbacks.
3461 *
3462 * @return true if we are currently paused. The caller might be able to
3463 * skip some work in that case since we will come back again.
3464 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003465 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003466 if (mPaused) {
3467 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003468 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003469 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003470 }
3471 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003472 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003473 return true;
3474 } else {
3475 return false;
3476 }
3477 }
3478
Michael Jurkac402cd92013-05-20 15:49:32 +02003479 private boolean waitUntilResume(Runnable run) {
3480 return waitUntilResume(run, false);
3481 }
3482
Michael Jurka1e2f4652013-07-08 18:03:46 -07003483 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003484 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003485 }
3486
Michael Jurka7607c2f2013-04-03 14:33:19 -07003487 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003488 * If the activity is currently paused, signal that we need to re-run the loader
3489 * in onResume.
3490 *
3491 * This needs to be called from incoming places where resources might have been loaded
3492 * while we are paused. That is becaues the Configuration might be wrong
3493 * when we're not running, and if it comes back to what it was when we
3494 * were paused, we are not restarted.
3495 *
3496 * Implementation of the method from LauncherModel.Callbacks.
3497 *
3498 * @return true if we are currently paused. The caller might be able to
3499 * skip some work in that case since we will come back again.
3500 */
3501 public boolean setLoadOnResume() {
3502 if (mPaused) {
3503 Log.i(TAG, "setLoadOnResume");
3504 mOnResumeNeedsLoad = true;
3505 return true;
3506 } else {
3507 return false;
3508 }
3509 }
3510
3511 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003513 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003515 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003516 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003517 } else {
3518 return SCREEN_COUNT / 2;
3519 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003521
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 /**
3523 * Refreshes the shortcuts shown on the workspace.
3524 *
3525 * Implementation of the method from LauncherModel.Callbacks.
3526 */
3527 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003528 // If we're starting binding all over again, clear any bind calls we'd postponed in
3529 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3530 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003531 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003532
Winson Chungd64d1762013-08-20 14:37:16 -07003533 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003534 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003535 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003536
Michael Jurka05bf644e2011-11-30 20:28:53 -08003537 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003538 if (mHotseat != null) {
3539 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
3541 }
3542
Adam Cohendcd297f2013-06-18 13:13:40 -07003543 @Override
3544 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003545 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003546
3547 // Create the new empty page
Winson Chung64359a52013-07-08 17:17:08 -07003548 mWorkspace.addExtraEmptyScreen();
Winson Chung0e6a7132013-08-23 12:55:10 -07003549
3550 // Create the custom content page (this call updates mDefaultScreen which calls
3551 // setCurrentPage() so ensure that all pages are added before calling this)
3552 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3553 mWorkspace.createCustomContentPage();
3554 }
Winson Chung64359a52013-07-08 17:17:08 -07003555 }
3556
3557 @Override
3558 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003559 int count = orderedScreenIds.size();
3560 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003561 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003562 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003563 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003564 }
3565
Adam Cohen39a06042013-07-19 14:30:12 -07003566 private boolean shouldShowWeightWatcher() {
3567 String spKey = LauncherAppState.getSharedPreferencesKey();
3568 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003569 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003570
3571 return show;
3572 }
3573
Adam Cohen4caf2982013-08-20 18:54:31 -07003574 private boolean emailSent() {
3575 String spKey = LauncherAppState.getSharedPreferencesKey();
3576 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3577 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3578 return show;
3579 }
3580
3581 private void setEmailSent(boolean sent) {
3582 String spKey = LauncherAppState.getSharedPreferencesKey();
3583 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3584
3585 SharedPreferences.Editor editor = sp.edit();
3586 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3587 editor.commit();
3588 }
3589
Adam Cohen39a06042013-07-19 14:30:12 -07003590 private void toggleShowWeightWatcher() {
3591 String spKey = LauncherAppState.getSharedPreferencesKey();
3592 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3593 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3594
3595 show = !show;
3596
3597 SharedPreferences.Editor editor = sp.edit();
3598 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3599 editor.commit();
3600
3601 if (mWeightWatcher != null) {
3602 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3603 }
3604 }
3605
Winson Chungd64d1762013-08-20 14:37:16 -07003606 public void bindAppsAdded(final ArrayList<Long> newScreens,
3607 final ArrayList<ItemInfo> addNotAnimated,
3608 final ArrayList<ItemInfo> addAnimated) {
3609 Runnable r = new Runnable() {
3610 public void run() {
3611 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3612 }
3613 };
3614 if (waitUntilResume(r)) {
3615 return;
3616 }
3617
Adam Cohen4caf2982013-08-20 18:54:31 -07003618 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003619
3620 // Add the new screens
3621 bindAddScreens(newScreens);
3622
3623 // We add the items without animation on non-visible pages, and with
3624 // animations on the new page (which we will try and snap to).
3625 if (!addNotAnimated.isEmpty()) {
3626 bindItems(addNotAnimated, 0,
3627 addNotAnimated.size(), false);
3628 }
3629 if (!addAnimated.isEmpty()) {
3630 bindItems(addAnimated, 0,
3631 addAnimated.size(), true);
3632 }
3633 }
3634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003635 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003636 * Bind the items start-end from the list.
3637 *
3638 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003639 */
Winson Chung64359a52013-07-08 17:17:08 -07003640 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3641 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003642 Runnable r = new Runnable() {
3643 public void run() {
3644 bindItems(shortcuts, start, end, forceAnimateIcons);
3645 }
3646 };
3647 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003648 return;
3649 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003650
Winson Chungf0c6ae02012-03-21 16:10:31 -07003651 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003652 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3653 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003654 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003655 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003656 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003657 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003658 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003659
3660 // Short circuit if we are loading dock items for a configuration which has no dock
3661 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3662 mHotseat == null) {
3663 continue;
3664 }
3665
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 switch (item.itemType) {
3667 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3668 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003669 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003670 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003671
Winson Chung64359a52013-07-08 17:17:08 -07003672 /*
3673 * TODO: FIX collision case
3674 */
Winson Chungce376632013-07-11 16:12:41 -07003675 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3676 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3677 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3678 throw new RuntimeException("OCCUPIED");
3679 }
Winson Chung64359a52013-07-08 17:17:08 -07003680 }
3681
Adam Cohendcd297f2013-06-18 13:13:40 -07003682 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3683 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003684 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003685 // Animate all the applications up now
3686 shortcut.setAlpha(0f);
3687 shortcut.setScaleX(0f);
3688 shortcut.setScaleY(0f);
3689 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003690 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003692 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003693 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003694 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003695 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003696 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003697 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3698 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003700 default:
3701 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003703 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704
Winson Chung997a9232013-07-24 15:33:46 -07003705 if (animateIcons) {
3706 // Animate to the correct page
3707 if (newShortcutsScreenId > -1) {
3708 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3709 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3710 if (newShortcutsScreenId != currentScreenId) {
3711 mWorkspace.snapToPage(newScreenIndex);
3712 }
3713 }
3714
Winson Chung64359a52013-07-08 17:17:08 -07003715 // We post the animation slightly delayed to prevent slowdowns when we are loading
3716 // right after we return to launcher.
3717 mWorkspace.postDelayed(new Runnable() {
3718 public void run() {
3719 anim.playTogether(bounceAnims);
3720 anim.start();
3721 }
3722 }, NEW_APPS_ANIMATION_DELAY);
3723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003725 }
3726
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 /**
3728 * Implementation of the method from LauncherModel.Callbacks.
3729 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003730 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003731 Runnable r = new Runnable() {
3732 public void run() {
3733 bindFolders(folders);
3734 }
3735 };
3736 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003737 return;
3738 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003739 sFolders.clear();
3740 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003742
3743 /**
3744 * Add the views for a widget to the workspace.
3745 *
3746 * Implementation of the method from LauncherModel.Callbacks.
3747 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003748 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003749 Runnable r = new Runnable() {
3750 public void run() {
3751 bindAppWidget(item);
3752 }
3753 };
3754 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003755 return;
3756 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003757
Daniel Sandler843e8602010-06-07 14:59:01 -04003758 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3759 if (DEBUG_WIDGETS) {
3760 Log.d(TAG, "bindAppWidget: " + item);
3761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 final Workspace workspace = mWorkspace;
3763
3764 final int appWidgetId = item.appWidgetId;
3765 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003766 if (DEBUG_WIDGETS) {
3767 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3768 }
3769
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3771
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003773 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003774
Adam Cohendcd297f2013-06-18 13:13:40 -07003775 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003776 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003777 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3778
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 workspace.requestLayout();
3780
Daniel Sandler843e8602010-06-07 14:59:01 -04003781 if (DEBUG_WIDGETS) {
3782 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3783 + (SystemClock.uptimeMillis()-start) + "ms");
3784 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003785 }
3786
Adam Cohen1462de32012-07-24 22:34:36 -07003787 public void onPageBoundSynchronously(int page) {
3788 mSynchronouslyBoundPages.add(page);
3789 }
3790
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 /**
3792 * Callback saying that there aren't any more items to bind.
3793 *
3794 * Implementation of the method from LauncherModel.Callbacks.
3795 */
Adam Cohene25af792013-06-06 23:08:25 -07003796 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003797 Runnable r = new Runnable() {
3798 public void run() {
3799 finishBindingItems(upgradePath);
3800 }
3801 };
3802 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003803 return;
3804 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003805 if (mSavedState != null) {
3806 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003807 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 mSavedState = null;
3810 }
3811
Adam Cohen1462de32012-07-24 22:34:36 -07003812 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003814 // If we received the result of any pending adds while the loader was running (e.g. the
3815 // widget configuration forced an orientation change), process them now.
3816 for (int i = 0; i < sPendingAddList.size(); i++) {
3817 completeAdd(sPendingAddList.get(i));
3818 }
3819 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820
Winson Chungc51db6a2011-10-05 11:44:49 -07003821 // Update the market app icon as necessary (the other icons will be managed in response to
3822 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003823 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003824
Winson Chungf0c6ae02012-03-21 16:10:31 -07003825 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003826 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003827 mWorkspace.getUniqueComponents(true, null);
3828 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003829 }
Adam Cohen99894d92013-06-14 11:22:59 -07003830
Adam Cohen66a01fd2013-06-11 12:48:00 -07003831 mWorkspace.post(new Runnable() {
3832 @Override
3833 public void run() {
3834 onFinishBindingItems();
3835 }
3836 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003837
3838 // Write all the logs to disk
3839 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3840 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003841 }
3842
Winson Chunga2413752012-04-03 14:22:34 -07003843 private boolean canRunNewAppsAnimation() {
3844 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3845 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3846 }
3847
Winson Chungc9168342013-06-26 14:54:55 -07003848 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3849 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3850 PropertyValuesHolder.ofFloat("alpha", 1f),
3851 PropertyValuesHolder.ofFloat("scaleX", 1f),
3852 PropertyValuesHolder.ofFloat("scaleY", 1f));
3853 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3854 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3855 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3856 return bounceAnim;
3857 }
3858
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003859 @Override
3860 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003861 boolean searchVisible = updateGlobalSearchIcon();
3862 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003863 if (mSearchDropTargetBar != null) {
3864 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3865 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003866 }
3867
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003868 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 * Add the icons for all apps.
3870 *
3871 * Implementation of the method from LauncherModel.Callbacks.
3872 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003873 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003874 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003875 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3876 getHotseat().addAllAppsFolder(mIconCache, apps,
3877 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3878 }
Winson Chung64359a52013-07-08 17:17:08 -07003879 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 }
3882
3883 /**
3884 * A package was updated.
3885 *
3886 * Implementation of the method from LauncherModel.Callbacks.
3887 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003888 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003889 Runnable r = new Runnable() {
3890 public void run() {
3891 bindAppsUpdated(apps);
3892 }
3893 };
3894 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003895 return;
3896 }
3897
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003898 if (mWorkspace != null) {
3899 mWorkspace.updateShortcuts(apps);
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 }
3902
3903 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003904 * A package was uninstalled. We take both the super set of packageNames
3905 * in addition to specific applications to remove, the reason being that
3906 * this can be called when a package is updated as well. In that scenario,
3907 * we only remove specific components from the workspace, where as
3908 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909 *
3910 * Implementation of the method from LauncherModel.Callbacks.
3911 */
Winson Chung83892cc2013-05-01 16:53:33 -07003912 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3913 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003914 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003915 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003916 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003917 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003918 }
Winson Chungd64d1762013-08-20 14:37:16 -07003919 };
3920 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003921 return;
3922 }
3923
Winson Chung64359a52013-07-08 17:17:08 -07003924 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003925 mWorkspace.removeItemsByPackageName(packageNames);
3926 } else {
3927 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003928 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003929
Winson Chunga1820962011-10-03 16:31:06 -07003930 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003931 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003932 }
Joe Onoratobe386092009-11-17 17:32:16 -08003933
3934 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003935 * A number of packages were updated.
3936 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003937 private ArrayList<Object> mWidgetsAndShortcuts;
3938 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003939 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003940 bindPackagesUpdated(mWidgetsAndShortcuts);
3941 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003942 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003943 };
3944
3945 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3946 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3947 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003948 return;
3949 }
3950
Winson Chung64359a52013-07-08 17:17:08 -07003951 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003952 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003953 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003954 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003955 }
3956
Winson Chung400438b2011-01-16 17:53:48 -08003957 private int mapConfigurationOriActivityInfoOri(int configOri) {
3958 final Display d = getWindowManager().getDefaultDisplay();
3959 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3960 switch (d.getRotation()) {
3961 case Surface.ROTATION_0:
3962 case Surface.ROTATION_180:
3963 // We are currently in the same basic orientation as the natural orientation
3964 naturalOri = configOri;
3965 break;
3966 case Surface.ROTATION_90:
3967 case Surface.ROTATION_270:
3968 // We are currently in the other basic orientation to the natural orientation
3969 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3970 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3971 break;
3972 }
3973
3974 int[] oriMap = {
3975 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3976 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3977 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3978 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3979 };
3980 // Since the map starts at portrait, we need to offset if this device's natural orientation
3981 // is landscape.
3982 int indexOffset = 0;
3983 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3984 indexOffset = 1;
3985 }
3986 return oriMap[(d.getRotation() + indexOffset) % 4];
3987 }
Adam Cohen446e9402011-09-15 18:21:21 -07003988
Winson Chung4b919f82012-05-01 10:44:08 -07003989 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003990 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003991 getResources().getBoolean(R.bool.allow_rotation);
3992 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003993 }
Winson Chung4b919f82012-05-01 10:44:08 -07003994 public void lockScreenOrientation() {
3995 if (isRotationEnabled()) {
3996 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3997 .getConfiguration().orientation));
3998 }
3999 }
4000 public void unlockScreenOrientation(boolean immediate) {
4001 if (isRotationEnabled()) {
4002 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004003 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004004 } else {
4005 mHandler.postDelayed(new Runnable() {
4006 public void run() {
4007 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4008 }
4009 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004010 }
Winson Chung4b919f82012-05-01 10:44:08 -07004011 }
Winson Chung400438b2011-01-16 17:53:48 -08004012 }
4013
Winson Chung82f55532011-08-09 14:14:23 -07004014 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004015 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004016 if (DISABLE_CLINGS) {
4017 return false;
4018 }
4019
Brett Chabot2a9e2282011-08-23 15:03:13 -07004020 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004022
Michael Jurkae233a8b2013-03-19 13:49:20 +01004023 // Restricted secondary users (child mode) will potentially have very few apps
4024 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004025// boolean supportsLimitedUsers =
4026// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4027// Account[] accounts = AccountManager.get(this).getAccounts();
4028// if (supportsLimitedUsers && accounts.length == 0) {
4029// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4030// Bundle restrictions = um.getUserRestrictions();
4031// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4032// return false;
4033// }
4034// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004035 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004036 }
Michael Jurka22143132012-10-04 17:42:38 +02004037
Winson Chung7d7541e2011-09-16 20:14:36 -07004038 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004039 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 if (cling != null) {
4041 cling.init(this, positionData);
4042 cling.setVisibility(View.VISIBLE);
4043 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4044 if (animate) {
4045 cling.buildLayer();
4046 cling.setAlpha(0f);
4047 cling.animate()
4048 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004049 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004050 .setDuration(SHOW_CLING_DURATION)
4051 .setStartDelay(delay)
4052 .start();
4053 } else {
4054 cling.setAlpha(1f);
4055 }
Michael Jurka22143132012-10-04 17:42:38 +02004056 cling.setFocusableInTouchMode(true);
4057 cling.post(new Runnable() {
4058 public void run() {
4059 cling.setFocusable(true);
4060 cling.requestFocus();
4061 }
4062 });
4063 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4064 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 }
4066 return cling;
4067 }
Michael Jurka22143132012-10-04 17:42:38 +02004068
Winson Chung7d7541e2011-09-16 20:14:36 -07004069 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004070 // To catch cases where siblings of top-level views are made invisible, just check whether
4071 // the cling is directly set to GONE before dismissing it.
4072 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004073 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004074 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004075 anim.addListener(new AnimatorListenerAdapter() {
4076 public void onAnimationEnd(Animator animation) {
4077 cling.setVisibility(View.GONE);
4078 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004079 // We should update the shared preferences on a background thread
4080 new Thread("dismissClingThread") {
4081 public void run() {
4082 SharedPreferences.Editor editor = mSharedPrefs.edit();
4083 editor.putBoolean(flag, true);
4084 editor.commit();
4085 }
4086 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004087 };
4088 });
4089 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004090 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004091 }
4092 }
Michael Jurka22143132012-10-04 17:42:38 +02004093
Winson Chung9d9d74f2011-09-19 11:49:12 -07004094 private void removeCling(int id) {
4095 final View cling = findViewById(id);
4096 if (cling != null) {
4097 final ViewGroup parent = (ViewGroup) cling.getParent();
4098 parent.post(new Runnable() {
4099 @Override
4100 public void run() {
4101 parent.removeView(cling);
4102 }
4103 });
Michael Jurka22143132012-10-04 17:42:38 +02004104 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004105 }
4106 }
Michael Jurka974c3862012-05-22 22:00:31 -07004107
4108 private boolean skipCustomClingIfNoAccounts() {
4109 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4110 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4111 if (customCling) {
4112 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004113 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004114 Account[] accounts = am.getAccountsByType("com.google");
4115 return accounts.length == 0;
4116 }
4117 return false;
4118 }
4119
Winson Chung7d7541e2011-09-16 20:14:36 -07004120 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004121 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004122 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004123 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4124 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004125 // If we're not using the default workspace layout, replace workspace cling
4126 // with a custom workspace cling (usually specified in an overlay)
4127 // For now, only do this on tablets
4128 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004129 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004130 // Use a custom cling
4131 View cling = findViewById(R.id.workspace_cling);
4132 ViewGroup clingParent = (ViewGroup) cling.getParent();
4133 int clingIndex = clingParent.indexOfChild(cling);
4134 clingParent.removeViewAt(clingIndex);
4135 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4136 clingParent.addView(customCling, clingIndex);
4137 customCling.setId(R.id.workspace_cling);
4138 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004139 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004140 } else {
4141 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 }
4143 }
4144 public void showFirstRunAllAppsCling(int[] position) {
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.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004148 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004149 } else {
4150 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004151 }
4152 }
4153 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004154 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004155 if (isClingsEnabled() &&
4156 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4157 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004158 } else {
4159 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004160 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004161 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004162 }
Michael Jurka104c4562013-07-08 18:03:46 -07004163 protected SharedPreferences getSharedPrefs() {
4164 return mSharedPrefs;
4165 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004166 public boolean isFolderClingVisible() {
4167 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004168 if (cling != null) {
4169 return cling.getVisibility() == View.VISIBLE;
4170 }
4171 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004172 }
Winson Chung82f55532011-08-09 14:14:23 -07004173 public void dismissWorkspaceCling(View v) {
4174 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004175 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004176 }
4177 public void dismissAllAppsCling(View v) {
4178 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004179 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4180 }
4181 public void dismissFolderCling(View v) {
4182 Cling cling = (Cling) findViewById(R.id.folder_cling);
4183 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004184 }
4185
Winson Chung80baf5a2010-08-09 16:03:15 -07004186 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004187 * Prints out out state for debugging.
4188 */
4189 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004190 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004191 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004192 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4193 Log.d(TAG, "mRestoring=" + mRestoring);
4194 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4195 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004196 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004197 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004198
Winson Chung785d2eb2011-04-14 16:08:02 -07004199 if (mAppsCustomizeContent != null) {
4200 mAppsCustomizeContent.dumpState();
4201 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004202 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004203 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004204
4205 @Override
4206 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4207 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004208 synchronized (sDumpLogs) {
4209 writer.println(" ");
4210 writer.println("Debug logs: ");
4211 for (int i = 0; i < sDumpLogs.size(); i++) {
4212 writer.println(" " + sDumpLogs.get(i));
4213 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004214 }
4215 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004216
4217 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004218 synchronized (sDumpLogs) {
4219 Log.d(TAG, "");
4220 Log.d(TAG, "*********************");
4221 Log.d(TAG, "Launcher debug logs: ");
4222 for (int i = 0; i < sDumpLogs.size(); i++) {
4223 Log.d(TAG, " " + sDumpLogs.get(i));
4224 }
4225 Log.d(TAG, "*********************");
4226 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004227 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004228 }
4229
4230 public static void addDumpLog(String tag, String log, boolean debugLog) {
4231 if (debugLog) {
4232 Log.d(tag, log);
4233 }
4234 sDateStamp.setTime(System.currentTimeMillis());
4235 synchronized (sDumpLogs) {
4236 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4237 }
4238 }
4239
4240 public void dumpLogsToLocalData(final boolean email) {
4241 new Thread("DumpLogsToLocalData") {
4242 @Override
4243 public void run() {
4244 boolean success = false;
4245 sDateStamp.setTime(sRunStart);
4246 String FILENAME = sDateStamp.getMonth() + "-"
4247 + sDateStamp.getDay() + "_"
4248 + sDateStamp.getHours() + "-"
4249 + sDateStamp.getMinutes() + "_"
4250 + sDateStamp.getSeconds() + ".txt";
4251
4252 FileOutputStream fos = null;
4253 File outFile = null;
4254 try {
4255 outFile = new File(getFilesDir(), FILENAME);
4256 outFile.createNewFile();
4257 fos = new FileOutputStream(outFile);
4258 } catch (Exception e) {
4259 e.printStackTrace();
4260 }
4261 if (fos != null) {
4262 PrintWriter writer = new PrintWriter(fos);
4263
4264 writer.println(" ");
4265 writer.println("Debug logs: ");
4266 synchronized (sDumpLogs) {
4267 for (int i = 0; i < sDumpLogs.size(); i++) {
4268 writer.println(" " + sDumpLogs.get(i));
4269 }
4270 }
4271 writer.close();
4272 }
4273 try {
4274 if (fos != null) {
4275 fos.close();
4276 success = true;
4277 }
4278 } catch (IOException e) {
4279 e.printStackTrace();
4280 }
4281
4282 if (success && email) {
4283 if (!emailSent()) {
4284 emailFile(outFile);
4285 }
4286 }
4287 }
4288 }.start();
4289 }
4290
4291 private void emailFile(File file) {
4292 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4293 try {
4294 copyFile(file, publicCopy);
4295 } catch (IOException e) {
4296 e.printStackTrace();
4297 return;
4298 }
4299
4300 Intent intent = new Intent(Intent.ACTION_SEND);
4301 intent.setType("text/plain");
4302 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4303 "mikejurka@google"});
4304 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4305 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4306
4307 if (!file.exists() || !file.canRead()) {
4308 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4309 finish();
4310 return;
4311 }
4312
4313 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4314 Uri uri = Uri.fromFile(publicCopy);
4315 intent.putExtra(Intent.EXTRA_STREAM, uri);
4316 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4317
4318 setEmailSent(true);
4319 }
4320
4321 public void copyFile(File src, File dst) throws IOException {
4322 InputStream in = new FileInputStream(src);
4323 OutputStream out = new FileOutputStream(dst);
4324
4325 // Transfer bytes from in to out
4326 byte[] buf = new byte[1024];
4327 int len;
4328 while ((len = in.read(buf)) > 0) {
4329 out.write(buf, 0, len);
4330 }
4331 in.close();
4332 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004334}
Michael Jurka2763be32011-02-24 11:19:57 -08004335
Michael Jurkaabded662011-03-04 12:06:57 -08004336interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004337 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004338 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004339 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004340 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004341 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004342}