blob: 509560c740df3c0d4807e11b241bfcfc199a1a78 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
80import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070091import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.IOException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.InputStream;
111import java.io.OutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.util.Collections;
117import java.util.Comparator;
118import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700120import java.util.List;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122/**
123 * Default launcher application.
124 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100125public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700126 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700127 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800128 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700129 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Daniel Sandlerf061f822013-06-27 13:59:36 -0400131 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400132 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700133 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400134 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700135
Winson Chung70d72102011-08-12 11:24:35 -0700136 private static final int MENU_GROUP_WALLPAPER = 1;
137 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
138 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700139 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
140 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700143 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_PICK_APPLICATION = 6;
145 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Michael Jurka8b805b12012-04-18 14:23:14 -0700149 private static final int REQUEST_BIND_APPWIDGET = 11;
150
Mathew Inwood876a8462013-06-14 14:12:41 +0100151 /**
152 * IntentStarter uses request codes starting with this. This must be greater than all activity
153 * request codes used internally.
154 */
155 protected static final int REQUEST_LAST = 100;
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
158
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 static final int SCREEN_COUNT = 5;
160 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Romain Guy98d01652009-06-30 16:21:04 -0700162 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 // To turn on these properties, type
164 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
165 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
166 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100195 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700196 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100197 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700198 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100199 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700200
Adam Cohen39a06042013-07-19 14:30:12 -0700201 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700202 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700203
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700204 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700205 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700206 private State mState = State.WORKSPACE;
207 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700210 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
211 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700212 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700213 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800216 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Winson Chunga2413752012-04-03 14:22:34 -0700218 // How long to wait before the new-shortcut animation automatically pans the workspace
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700220 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private LayoutInflater mInflater;
227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800230 private DragLayer mDragLayer;
231 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700232 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700233
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 private AppWidgetManager mAppWidgetManager;
235 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700236
Michael Jurkac9d95c52011-08-29 14:03:34 -0700237 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700238 private AppWidgetProviderInfo mPendingAddWidgetInfo;
239
Michael Jurka0280c3b2010-09-17 15:00:07 -0700240 private int[] mTmpAddItemCellCoordinates = new int[2];
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private FolderInfo mFolderInfo;
243
Winson Chung3d503fb2011-07-13 17:25:49 -0700244 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700245 private View mOverviewPanel;
246
Michael Jurka838a4ca2011-02-07 13:33:06 -0800247 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700248
Winson Chungc51db6a2011-10-05 11:44:49 -0700249 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700250 private AppsCustomizeTabHost mAppsCustomizeTabHost;
251 private AppsCustomizePagedView mAppsCustomizeContent;
252 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100253 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700256 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
257 // scroll issues (because the workspace may not have been measured yet) and extra work.
258 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
259 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
261 private SpannableStringBuilder mDefaultKeySsb = null;
262
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 private boolean mWorkspaceLoading = true;
264
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800265 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private boolean mRestoring;
267 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700268 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Michael Jurka1e2f4652013-07-08 18:03:46 -0700270 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700271 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
272
Winson Chung4a2afa32012-07-19 14:53:05 -0700273 // Keep track of whether the user has left launcher
274 private static boolean sPausedFromUserAction = false;
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedInstanceState;
277
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800280 private boolean mUserPresent = true;
281 private boolean mVisible = false;
282 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700283 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700285 private static LocaleConfiguration sLocaleConfiguration = null;
286
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700287 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700289 private Intent mAppMarketIntent = null;
290
Adam Cohended9f8d2010-11-03 13:25:16 -0700291 // Related to the auto-advancing of widgets
292 private final int ADVANCE_MSG = 1;
293 private final int mAdvanceInterval = 20000;
294 private final int mAdvanceStagger = 250;
295 private long mAutoAdvanceSentTime;
296 private long mAutoAdvanceTimeLeft = -1;
297 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
298 new HashMap<View, AppWidgetProviderInfo>();
299
Winson Chung400438b2011-01-16 17:53:48 -0800300 // Determines how long to wait after a rotation before restoring the screen orientation to
301 // match the sensor state.
302 private final int mRestoreScreenOrientationDelay = 500;
303
Michael Jurka4ef207b2010-11-29 17:05:45 -0800304 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700305 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
306 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
307 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800308
Craig Mautner360310b2012-10-26 15:13:08 -0700309 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700310
Adam Cohen1462de32012-07-24 22:34:36 -0700311 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
312
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700313 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700314 static Date sDateStamp = new Date();
315 static DateFormat sDateFormat =
316 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
317 static long sRunStart = System.currentTimeMillis();
318 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700319
Winson Chung46353de2012-02-16 14:05:10 -0800320 // We only want to get the SharedPreferences once since it does an FS stat each time we get
321 // it from the context.
322 private SharedPreferences mSharedPrefs;
323
Adam Cohene25af792013-06-06 23:08:25 -0700324 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
325
Winson Chungf0c6ae02012-03-21 16:10:31 -0700326 // Holds the page that we need to animate to, and the icon views that we need to animate up
327 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700328 private ImageView mFolderIconImageView;
329 private Bitmap mFolderIconBitmap;
330 private Canvas mFolderIconCanvas;
331 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700332
Michael Jurkaddd62e92011-02-16 17:49:14 -0800333 private BubbleTextView mWaitingForResume;
334
Michael Jurka22143132012-10-04 17:42:38 +0200335 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
336 = new HideFromAccessibilityHelper();
337
Michael Jurkac1f5d262011-09-30 19:32:27 -0700338 private Runnable mBuildLayersRunnable = new Runnable() {
339 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700340 if (mWorkspace != null) {
341 mWorkspace.buildPageHardwareLayers();
342 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700343 }
344 };
345
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800346 private static ArrayList<PendingAddArguments> sPendingAddList
347 = new ArrayList<PendingAddArguments>();
348
Michael Jurka0a457bf2012-11-19 14:05:05 -0800349 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
350
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800351 private static class PendingAddArguments {
352 int requestCode;
353 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700354 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700355 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 int cellX;
357 int cellY;
358 }
359
Daniel Sandlerff02d492013-08-05 02:12:05 -0400360 private Stats mStats;
361
Michael Jurka0a457bf2012-11-19 14:05:05 -0800362 private static boolean isPropertyEnabled(String propertyName) {
363 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 @Override
367 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700368 if (DEBUG_STRICT_MODE) {
369 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
370 .detectDiskReads()
371 .detectDiskWrites()
372 .detectNetwork() // or .detectAll() for all detectable problems
373 .penaltyLog()
374 .build());
375 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
376 .detectLeakedSqlLiteObjects()
377 .detectLeakedClosableObjects()
378 .penaltyLog()
379 .penaltyDeath()
380 .build());
381 }
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400384
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400385 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400386 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700387
388 // Determine the dynamic grid properties
389 Point smallestSize = new Point();
390 Point largestSize = new Point();
391 Point realSize = new Point();
392 Display display = getWindowManager().getDefaultDisplay();
393 display.getCurrentSizeRange(smallestSize, largestSize);
394 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700395 DisplayMetrics dm = new DisplayMetrics();
396 display.getMetrics(dm);
Winson Chung5f8afe62013-08-12 16:19:28 -0700397 // Lazy-initialize the dynamic grid
398 DeviceProfile grid = app.initDynamicGrid(this,
399 Math.min(smallestSize.x, smallestSize.y),
400 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700401 realSize.x, realSize.y,
402 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700403
404 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400405 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700406 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800407 mModel = app.setLauncher(this);
408 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400409 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700411
Daniel Sandlerff02d492013-08-05 02:12:05 -0400412 mStats = new Stats(this);
413
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700414 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700415 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
416 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700418 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
419 // this also ensures that any synchronous binding below doesn't re-trigger another
420 // LauncherModel load.
421 mPaused = false;
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200424 android.os.Debug.startMethodTracing(
425 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
428 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 setContentView(R.layout.launcher);
430 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100431 grid.layout(this);
Winson Chung7d7541e2011-09-16 20:14:36 -0700432 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800434 registerContentObservers();
435
Joe Onorato7c312c12009-08-13 21:36:53 -0700436 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 mSavedState = savedInstanceState;
439 restoreState(mSavedState);
440
Winson Chunga12a2502010-12-20 14:41:35 -0800441 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700442 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200443 mAppsCustomizeContent.onPackagesUpdated(
444 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700445 }
Winson Chunga12a2502010-12-20 14:41:35 -0800446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (PROFILE_STARTUP) {
448 android.os.Debug.stopMethodTracing();
449 }
450
451 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700452 if (sPausedFromUserAction) {
453 // If the user leaves launcher, then we should just load items asynchronously when
454 // they return.
455 mModel.startLoader(true, -1);
456 } else {
457 // We only load the page synchronously if the user rotates (or triggers a
458 // configuration change) while launcher is in the foreground
459 mModel.startLoader(true, mWorkspace.getCurrentPage());
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462
463 // For handling default keys
464 mDefaultKeySsb = new SpannableStringBuilder();
465 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800466
467 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
468 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800469
Adam Cohenf9426d52012-06-04 17:26:21 -0700470 updateGlobalIcons();
471
472 // On large interfaces, we want the screen to auto-rotate based on the current orientation
473 unlockScreenOrientation(true);
474 }
475
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 protected void onUserLeaveHint() {
477 super.onUserLeaveHint();
478 sPausedFromUserAction = true;
479 }
480
Winson Chung98ca0f72013-07-29 12:58:51 -0700481 /** To be overriden by subclasses to hint to Launcher that we have custom content */
482 protected boolean hasCustomContentToLeft() {
483 return false;
484 }
485
Adam Cohenf9426d52012-06-04 17:26:21 -0700486 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700487 boolean searchVisible = false;
488 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800489 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700490 int coi = getCurrentOrientationIndexForGlobalIcons();
491 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
492 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700493 updateAppMarketIcon();
494 searchVisible = updateGlobalSearchIcon();
495 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700496 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700497 if (sGlobalSearchIcon[coi] != null) {
498 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700499 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700500 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700501 if (sVoiceSearchIcon[coi] != null) {
502 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700503 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700504 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700505 if (sAppMarketIcon[coi] != null) {
506 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800507 }
Winson Chungadf0c182012-08-23 14:59:07 -0700508 if (mSearchDropTargetBar != null) {
509 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 }
Romain Guycbb89e42009-06-08 15:52:54 -0700512
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700514 if (sLocaleConfiguration == null) {
515 new AsyncTask<Void, Void, LocaleConfiguration>() {
516 @Override
517 protected LocaleConfiguration doInBackground(Void... unused) {
518 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
519 readConfiguration(Launcher.this, localeConfiguration);
520 return localeConfiguration;
521 }
522
523 @Override
524 protected void onPostExecute(LocaleConfiguration result) {
525 sLocaleConfiguration = result;
526 checkForLocaleChange(); // recursive, but now with a locale configuration
527 }
528 }.execute();
529 return;
530 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700531
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 final Configuration configuration = getResources().getConfiguration();
533
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700534 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 final String locale = configuration.locale.toString();
536
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700537 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 final int mcc = configuration.mcc;
539
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700540 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 final int mnc = configuration.mnc;
542
Romain Guy84f296c2009-11-04 15:00:44 -0800543 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544
Romain Guy84f296c2009-11-04 15:00:44 -0800545 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700546 sLocaleConfiguration.locale = locale;
547 sLocaleConfiguration.mcc = mcc;
548 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700549
Joe Onorato0589f0f2010-02-08 13:44:00 -0800550 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700551
552 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
553 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700554 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700555 public void run() {
556 writeConfiguration(Launcher.this, localeConfiguration);
557 }
558 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700559 }
560 }
561
562 private static class LocaleConfiguration {
563 public String locale;
564 public int mcc = -1;
565 public int mnc = -1;
566 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700567
Romain Guy98d01652009-06-30 16:21:04 -0700568 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
569 DataInputStream in = null;
570 try {
571 in = new DataInputStream(context.openFileInput(PREFERENCES));
572 configuration.locale = in.readUTF();
573 configuration.mcc = in.readInt();
574 configuration.mnc = in.readInt();
575 } catch (FileNotFoundException e) {
576 // Ignore
577 } catch (IOException e) {
578 // Ignore
579 } finally {
580 if (in != null) {
581 try {
582 in.close();
583 } catch (IOException e) {
584 // Ignore
585 }
586 }
587 }
588 }
589
590 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
591 DataOutputStream out = null;
592 try {
593 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
594 out.writeUTF(configuration.locale);
595 out.writeInt(configuration.mcc);
596 out.writeInt(configuration.mnc);
597 out.flush();
598 } catch (FileNotFoundException e) {
599 // Ignore
600 } catch (IOException e) {
601 //noinspection ResultOfMethodCallIgnored
602 context.getFileStreamPath(PREFERENCES).delete();
603 } finally {
604 if (out != null) {
605 try {
606 out.close();
607 } catch (IOException e) {
608 // Ignore
609 }
610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612 }
613
Bjorn Bringertc459e522013-06-07 19:36:01 +0100614 public LayoutInflater getInflater() {
615 return mInflater;
616 }
617
Adam Cohen716b51e2011-06-30 12:09:54 -0700618 public DragLayer getDragLayer() {
619 return mDragLayer;
620 }
621
Winson Chung36a62fe2012-05-06 18:04:42 -0700622 boolean isDraggingEnabled() {
623 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
624 // that is subsequently removed from the workspace in startBinding().
625 return !mModel.isLoadingWorkspace();
626 }
627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 static int getScreen() {
629 synchronized (sLock) {
630 return sScreen;
631 }
632 }
633
634 static void setScreen(int screen) {
635 synchronized (sLock) {
636 sScreen = screen;
637 }
638 }
639
Winson Chung557d6ed2011-07-08 15:34:52 -0700640 /**
641 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
642 * a configuration step, this allows the proper animations to run after other transitions.
643 */
644 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700645 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800646 switch (args.requestCode) {
647 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800650 break;
651 case REQUEST_PICK_SHORTCUT:
652 processShortcut(args.intent);
653 break;
654 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700655 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700657 result = true;
658 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 case REQUEST_CREATE_APPWIDGET:
660 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700661 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700662 result = true;
663 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 case REQUEST_PICK_WALLPAPER:
665 // We just wanted the activity result here so we can clear mWaitingForResult
666 break;
667 }
Michael Jurka27614d22012-04-02 06:40:22 -0700668 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
669 // if you turned the screen off and then back while in All Apps, Launcher would not
670 // return to the workspace. Clearing mAddInfo.container here fixes this issue
671 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700672 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 }
674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 protected void onActivityResult(
677 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700678 // Reset the startActivity waiting flag
679 mWaitingForResult = false;
680
Michael Jurka8b805b12012-04-18 14:23:14 -0700681 if (requestCode == REQUEST_BIND_APPWIDGET) {
682 int appWidgetId = data != null ?
683 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
684 if (resultCode == RESULT_CANCELED) {
685 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
686 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700688 }
689 return;
690 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700691 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
693 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700694
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 // We have special handling for widgets
696 if (isWidgetDrop) {
697 int appWidgetId = data != null ?
698 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700699 if (appWidgetId < 0) {
700 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
701 "widget configuration activity.");
702 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
703 } else {
704 completeTwoStageWidgetDrop(resultCode, appWidgetId);
705 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800706 return;
707 }
708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 // The pattern used here is that a user PICKs a specific application,
710 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
713 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700714 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 final PendingAddArguments args = new PendingAddArguments();
716 args.requestCode = requestCode;
717 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700718 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700719 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700720 args.cellX = mPendingAddInfo.cellX;
721 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 if (isWorkspaceLocked()) {
723 sPendingAddList.add(args);
724 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700725 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700729 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800730 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
731 null);
732 }
733
734 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 Runnable onCompleteRunnable = null;
738 int animationType = 0;
739
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700740 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800741 if (resultCode == RESULT_OK) {
742 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
743 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700744 mPendingAddWidgetInfo);
745 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800746 onCompleteRunnable = new Runnable() {
747 @Override
748 public void run() {
749 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800751 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
752 null);
753 }
754 };
755 } else if (resultCode == RESULT_CANCELED) {
756 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
757 onCompleteRunnable = new Runnable() {
758 @Override
759 public void run() {
760 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
761 null);
762 }
763 };
764 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700765 if (mDragLayer.getAnimatedView() != null) {
766 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
767 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
768 animationType, boundWidget, true);
769 } else {
770 // The animated view may be null in the case of a rotation during widget configuration
771 onCompleteRunnable.run();
772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774
775 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700776 protected void onStop() {
777 super.onStop();
778 FirstFrameAnimatorHelper.setIsVisible(false);
779 }
780
781 @Override
782 protected void onStart() {
783 super.onStart();
784 FirstFrameAnimatorHelper.setIsVisible(true);
785 }
786
787 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200789 long startTime = 0;
790 if (DEBUG_RESUME_TIME) {
791 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400792 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700795
Winson Chung4a2afa32012-07-19 14:53:05 -0700796 // Restore the previous launcher state
797 if (mOnResumeState == State.WORKSPACE) {
798 showWorkspace(false);
799 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
800 showAllApps(false);
801 }
802 mOnResumeState = State.NONE;
803
Craig Mautner360310b2012-10-26 15:13:08 -0700804 // Background was set to gradient in onPause(), restore to black if in all apps.
805 setWorkspaceBackground(mState == State.WORKSPACE);
806
Winson Chungde0fb8f2012-05-08 14:37:08 -0700807 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700808 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700809
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800810 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700811 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700812 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700814 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400815 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700816 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700818 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200819 // We might have postponed some bind calls until onResume (see waitUntilResume) --
820 // execute them here
821 long startTimeCallbacks = 0;
822 if (DEBUG_RESUME_TIME) {
823 startTimeCallbacks = System.currentTimeMillis();
824 }
825
826 if (mAppsCustomizeContent != null) {
827 mAppsCustomizeContent.setBulkBind(true);
828 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700829 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
830 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200831 }
832 if (mAppsCustomizeContent != null) {
833 mAppsCustomizeContent.setBulkBind(false);
834 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700835 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200836 if (DEBUG_RESUME_TIME) {
837 Log.d(TAG, "Time spent processing callbacks in onResume: " +
838 (System.currentTimeMillis() - startTimeCallbacks));
839 }
Michael Jurka447bf842013-05-15 14:52:15 +0200840 }
Michael Jurka54554252013-08-01 12:52:23 +0200841 if (mOnResumeCallbacks.size() > 0) {
842 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
843 mOnResumeCallbacks.get(i).run();
844 }
845 mOnResumeCallbacks.clear();
846 }
Winson Chunge4e50662012-01-23 14:45:13 -0800847
848 // Reset the pressed state of icons that were locked in the press state while activities
849 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800850 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800851 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800852 mWaitingForResume.setStayPressed(false);
853 }
Winson Chunge4e50662012-01-23 14:45:13 -0800854 if (mAppsCustomizeContent != null) {
855 // Resets the previous all apps icon press state
856 mAppsCustomizeContent.resetDrawableState();
857 }
Adam Cohen06dff352012-06-01 17:17:08 -0700858 // It is possible that widgets can receive updates while launcher is not in the foreground.
859 // Consequently, the widgets will be inflated in the orientation of the foreground activity
860 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
861 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700862 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700863
864 // Again, as with the above scenario, it's possible that one or more of the global icons
865 // were updated in the wrong orientation.
866 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200867 if (DEBUG_RESUME_TIME) {
868 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
869 }
Adam Cohen4caf2982013-08-20 18:54:31 -0700870
871 // Write all the logs to disk
872 Launcher.addDumpLog(TAG, "10249126 - onResume() - dumping logs to disk", true);
873 dumpLogsToLocalData(false);
Adam Cohen06dff352012-06-01 17:17:08 -0700874 }
875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700877 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700878 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
879 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
880 // when Launcher resumes and we are still in AllApps.
881 updateWallpaperVisibility(true);
882
Winson Chung997a9232013-07-24 15:33:46 -0700883 // Ensure that items added to Launcher are queued until Launcher returns
884 InstallShortcutReceiver.enableInstallQueue();
885
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700886 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700887 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800888 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700889 mDragController.resetLastGestureUpTime();
Adam Cohen4caf2982013-08-20 18:54:31 -0700890
891 // Write all the logs to disk
892 Launcher.addDumpLog(TAG, "10249126 - onPause() - dumping logs to disk", true);
893 dumpLogsToLocalData(false);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 }
Romain Guycbb89e42009-06-08 15:52:54 -0700895
Adam Cohen66a01fd2013-06-11 12:48:00 -0700896 protected void onFinishBindingItems() {
897 }
898
Adam Cohenbffe7452013-07-22 18:21:45 -0700899 QSBScroller mQsbScroller = new QSBScroller() {
900 int scrollY = 0;
901
902 @Override
903 public void setScrollY(int scroll) {
904 scrollY = scroll;
905
906 if (mWorkspace.isOnOrMovingToCustomContent()) {
907 mSearchDropTargetBar.setTranslationY(- scrollY);
908 }
909 }
910 };
911
912 public void resetQSBScroll() {
913 mSearchDropTargetBar.animate().translationY(0).start();
914 }
915
916 public interface CustomContentCallbacks {
917 // Custom content is completely shown
918 public void onShow();
919
920 // Custom content is completely hidden
921 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -0700922
923 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
924 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -0700925 }
926
Adam Cohen30bacb22013-08-29 14:25:25 -0700927 protected void startSettings() {
928 }
929
Adam Cohenbffe7452013-07-22 18:21:45 -0700930 public interface QSBScroller {
931 public void setScrollY(int scrollY);
932 }
933
Adam Cohen66a01fd2013-06-11 12:48:00 -0700934 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700935 public QSBScroller addToCustomContentPage(View customContent) {
936 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700937 }
938
Winson Chung98ca0f72013-07-29 12:58:51 -0700939 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700940 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700941 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700942 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700943 }
944
Adam Cohenedb40762013-07-18 16:45:45 -0700945 // The custom content needs to offset its content to account for the QSB
946 public int getTopOffsetForCustomContent() {
947 return mWorkspace.getPaddingTop();
948 }
949
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700950 @Override
951 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400952 // Flag the loader to stop early before switching
953 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700954 if (mAppsCustomizeContent != null) {
955 mAppsCustomizeContent.surrender();
956 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800957 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700958 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700959
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800960 // We can't hide the IME if it was forced open. So don't bother
961 /*
962 @Override
963 public void onWindowFocusChanged(boolean hasFocus) {
964 super.onWindowFocusChanged(hasFocus);
965
966 if (hasFocus) {
967 final InputMethodManager inputManager = (InputMethodManager)
968 getSystemService(Context.INPUT_METHOD_SERVICE);
969 WindowManager.LayoutParams lp = getWindow().getAttributes();
970 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
971 android.os.Handler()) {
972 protected void onReceiveResult(int resultCode, Bundle resultData) {
973 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
974 }
975 });
976 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
977 }
978 }
979 */
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 private boolean acceptFilter() {
982 final InputMethodManager inputManager = (InputMethodManager)
983 getSystemService(Context.INPUT_METHOD_SERVICE);
984 return !inputManager.isFullscreenMode();
985 }
986
987 @Override
988 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700989 final int uniChar = event.getUnicodeChar();
990 final boolean handled = super.onKeyDown(keyCode, event);
991 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
992 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
994 keyCode, event);
995 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700996 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700997 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700998 // showSearchDialog()
999 // If there are multiple keystrokes before the search dialog takes focus,
1000 // onSearchRequested() will be called for every keystroke,
1001 // but it is idempotent, so it's fine.
1002 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004 }
1005
Joe Onorato8a9625e2010-01-28 15:55:35 -08001006 // Eat the long press event so the keyboard doesn't come up.
1007 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1008 return true;
1009 }
1010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 return handled;
1012 }
1013
Karl Rosaen138a0412009-04-23 19:00:21 -07001014 private String getTypedText() {
1015 return mDefaultKeySsb.toString();
1016 }
1017
1018 private void clearTypedText() {
1019 mDefaultKeySsb.clear();
1020 mDefaultKeySsb.clearSpans();
1021 Selection.setSelection(mDefaultKeySsb, 0);
1022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001025 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1026 * State
1027 */
1028 private static State intToState(int stateOrdinal) {
1029 State state = State.WORKSPACE;
1030 final State[] stateValues = State.values();
1031 for (int i = 0; i < stateValues.length; i++) {
1032 if (stateValues[i].ordinal() == stateOrdinal) {
1033 state = stateValues[i];
1034 break;
1035 }
1036 }
1037 return state;
1038 }
1039
1040 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 * Restores the previous state, if it exists.
1042 *
1043 * @param savedState The previous state.
1044 */
1045 private void restoreState(Bundle savedState) {
1046 if (savedState == null) {
1047 return;
1048 }
1049
Michael Jurka883f55b2010-10-21 15:47:14 -07001050 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001051 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001052 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001053 }
1054
Winson Chungf0c6ae02012-03-21 16:10:31 -07001055 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001057 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
1059
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001061 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1064 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001065 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001066 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1067 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001068 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1069 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1070 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001071 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 mRestoring = true;
1073 }
1074
1075 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1076 if (renameFolder) {
1077 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001078 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 mRestoring = true;
1080 }
Winson Chunga12a2502010-12-20 14:41:35 -08001081
Winson Chung785d2eb2011-04-14 16:08:02 -07001082 // Restore the AppsCustomize tab
1083 if (mAppsCustomizeTabHost != null) {
1084 String curTab = savedState.getString("apps_customize_currentTab");
1085 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001086 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001087 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001088 mAppsCustomizeContent.loadAssociatedPages(
1089 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 }
1091
Winson Chung5afbf7b2011-07-25 11:53:08 -07001092 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1093 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096
1097 /**
1098 * Finds all the views we need and configure them properly.
1099 */
1100 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001101 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001102
Craig Mautner360310b2012-10-26 15:13:08 -07001103 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001104 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1105 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001106
1107 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1108 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109
Winson Chung4c98d922011-05-31 16:50:48 -07001110 // Setup the drag layer
1111 mDragLayer.setup(this, dragController);
1112
Winson Chung3d503fb2011-07-13 17:25:49 -07001113 // Setup the hotseat
1114 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1115 if (mHotseat != null) {
1116 mHotseat.setup(this);
1117 }
1118
Adam Cohenf358a4b2013-07-23 16:47:31 -07001119 mOverviewPanel = findViewById(R.id.overview_panel);
1120 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1121 @Override
1122 public void onClick(View arg0) {
1123 showAllApps(true);
1124 }
1125 });
1126 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1127 @Override
1128 public void onClick(View arg0) {
1129 startWallpaper();
1130 }
1131 });
Adam Cohen30bacb22013-08-29 14:25:25 -07001132 findViewById(R.id.settings_button).setOnClickListener(new OnClickListener() {
1133 @Override
1134 public void onClick(View arg0) {
1135 startSettings();
1136 }
1137 });
Adam Cohenf358a4b2013-07-23 16:47:31 -07001138
Winson Chung4c98d922011-05-31 16:50:48 -07001139 // Setup the workspace
1140 mWorkspace.setHapticFeedbackEnabled(false);
1141 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001142 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001143 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001144
Winson Chungf0ea4d32011-06-06 14:27:16 -07001145 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001146 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001147
Winson Chungf0ea4d32011-06-06 14:27:16 -07001148 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001149 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001150 mAppsCustomizeContent = (AppsCustomizePagedView)
1151 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1152 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001153
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001155 dragController.setDragScoller(mWorkspace);
1156 dragController.setScrollView(mDragLayer);
1157 dragController.setMoveTarget(mWorkspace);
1158 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001159 if (mSearchDropTargetBar != null) {
1160 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001161 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001162
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001163 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001164 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001165 mWeightWatcher = new WeightWatcher(this);
1166 mWeightWatcher.setAlpha(0.5f);
1167 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001168 new FrameLayout.LayoutParams(
1169 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001170 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001171 Gravity.BOTTOM)
1172 );
Adam Cohen39a06042013-07-19 14:30:12 -07001173
1174 boolean show = shouldShowWeightWatcher();
1175 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
1178
1179 /**
1180 * Creates a view representing a shortcut.
1181 *
1182 * @param info The data structure describing the shortcut.
1183 *
1184 * @return A View inflated from R.layout.application.
1185 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001186 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001188 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190
1191 /**
1192 * Creates a view representing a shortcut inflated from the specified resource.
1193 *
1194 * @param layoutResId The id of the XML layout used to create the shortcut.
1195 * @param parent The group the shortcut belongs to.
1196 * @param info The data structure describing the shortcut.
1197 *
1198 * @return A View inflated from layoutResId.
1199 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001200 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001201 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1202 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 return favorite;
1205 }
1206
1207 /**
1208 * Add an application shortcut to the workspace.
1209 *
1210 * @param data The intent describing the application.
1211 * @param cellInfo The position on screen where to create the shortcut.
1212 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001213 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001215 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216
Adam Cohenfbba09b2011-07-18 21:43:05 -07001217 // First we check if we already know the exact location where we want to add this item.
1218 if (cellX >= 0 && cellY >= 0) {
1219 cellXY[0] = cellX;
1220 cellXY[1] = cellY;
1221 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001222 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 return;
1224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001226 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001227
Romain Guy73b979d2009-06-09 12:57:21 -07001228 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001229 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001231 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001232 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001233 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001234 } else {
1235 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237 }
Romain Guycbb89e42009-06-08 15:52:54 -07001238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 /**
1240 * Add a shortcut to the workspace.
1241 *
1242 * @param data The intent describing the shortcut.
1243 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001245 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001246 int cellY) {
1247 int[] cellXY = mTmpAddItemCellCoordinates;
1248 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001250
Michael Jurkad3ef3062010-11-23 16:23:58 -08001251 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001252
Adam Cohen558baaf2011-08-15 15:22:57 -07001253 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001254 if (info == null) {
1255 return;
1256 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001257 final View view = createShortcut(info);
1258
Adam Cohenfbba09b2011-07-18 21:43:05 -07001259 // First we check if we already know the exact location where we want to add this item.
1260 if (cellX >= 0 && cellY >= 0) {
1261 cellXY[0] = cellX;
1262 cellXY[1] = cellY;
1263 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001264
1265 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001266 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001267 true, null,null)) {
1268 return;
1269 }
1270 DragObject dragObject = new DragObject();
1271 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001272 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1273 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001274 return;
1275 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001276 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001277 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001278 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001279 foundCellSpan = (result != null);
1280 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001281 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001282 }
1283
1284 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001285 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001286 return;
1287 }
1288
Adam Cohendcd297f2013-06-18 13:13:40 -07001289 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
1291 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001292 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001293 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
1295 }
1296
Adam Cohen2f093b62012-04-30 18:59:53 -07001297 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1298 int minHeight) {
1299 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001300 // We want to account for the extra amount of padding that we are adding to the widget
1301 // to ensure that it gets the full amount of space that it has requested
1302 int requiredWidth = minWidth + padding.left + padding.right;
1303 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001304 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001305 }
1306
Adam Cohen2f093b62012-04-30 18:59:53 -07001307 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1308 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001309 }
1310
Adam Cohen2f093b62012-04-30 18:59:53 -07001311 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1312 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001313 }
1314
Adam Cohen2f093b62012-04-30 18:59:53 -07001315 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1316 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001317 }
1318
Adam Cohen2f093b62012-04-30 18:59:53 -07001319 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1320 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001321 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001322 }
1323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001325 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001327 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001328 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001330 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001331 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1332 if (appWidgetInfo == null) {
1333 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1334 }
Romain Guycbb89e42009-06-08 15:52:54 -07001335
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001336 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001337 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001338
Adam Cohen2f093b62012-04-30 18:59:53 -07001339 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1340 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001343 // We have saved the position to which the widget was dragged-- this really only matters
1344 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001345 int[] cellXY = mTmpAddItemCellCoordinates;
1346 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001347 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001348 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001349 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1350 cellXY[0] = mPendingAddInfo.cellX;
1351 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 spanXY[0] = mPendingAddInfo.spanX;
1353 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001354 foundCellSpan = true;
1355 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001358 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1359 spanXY[1], cellXY, finalSpan);
1360 spanXY[0] = finalSpan[0];
1361 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001362 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001363 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001364 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001365 }
1366
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001368 if (appWidgetId != -1) {
1369 // Deleting an app widget ID is a void call but writes to disk before returning
1370 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001371 new Thread("deleteAppWidgetId") {
1372 public void run() {
1373 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1374 }
1375 }.start();
1376 }
Winson Chung93eef082012-03-23 15:59:27 -07001377 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001378 return;
1379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001381 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001382 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1383 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384 launcherInfo.spanX = spanXY[0];
1385 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001386 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1387 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001390 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391
1392 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001393 if (hostView == null) {
1394 // Perform actual inflation because we're live
1395 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1396 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1397 } else {
1398 // The AppWidgetHostView has already been inflated and instantiated
1399 launcherInfo.hostView = hostView;
1400 }
Romain Guycbb89e42009-06-08 15:52:54 -07001401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001403 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001404 launcherInfo.notifyWidgetSizeChanged(this);
1405
Adam Cohendcd297f2013-06-18 13:13:40 -07001406 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001408
1409 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001411 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
Romain Guycbb89e42009-06-08 15:52:54 -07001413
Adam Cohended9f8d2010-11-03 13:25:16 -07001414 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1415 @Override
1416 public void onReceive(Context context, Intent intent) {
1417 final String action = intent.getAction();
1418 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1419 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001420 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001421 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001422
Winson Chungc100e8e2011-08-09 16:02:43 -07001423 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001424 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001425 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1426 mAppsCustomizeTabHost.reset();
1427 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001428 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001429 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1430 mUserPresent = true;
1431 updateRunning();
1432 }
1433 }
1434 };
1435
1436 @Override
1437 public void onAttachedToWindow() {
1438 super.onAttachedToWindow();
1439
1440 // Listen for broadcasts related to user-presence
1441 final IntentFilter filter = new IntentFilter();
1442 filter.addAction(Intent.ACTION_SCREEN_OFF);
1443 filter.addAction(Intent.ACTION_USER_PRESENT);
1444 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001445 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001446 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001447 mVisible = true;
1448 }
1449
1450 @Override
1451 public void onDetachedFromWindow() {
1452 super.onDetachedFromWindow();
1453 mVisible = false;
1454
Adam Cohend113e0c2010-11-11 10:48:05 -08001455 if (mAttached) {
1456 unregisterReceiver(mReceiver);
1457 mAttached = false;
1458 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001459 updateRunning();
1460 }
1461
1462 public void onWindowVisibilityChanged(int visibility) {
1463 mVisible = visibility == View.VISIBLE;
1464 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001465 // The following code used to be in onResume, but it turns out onResume is called when
1466 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1467 // is a more appropriate event to handle
1468 if (mVisible) {
1469 mAppsCustomizeTabHost.onWindowVisible();
1470 if (!mWorkspaceLoading) {
1471 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001472 // We want to let Launcher draw itself at least once before we force it to build
1473 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001474 // apps is nice and speedy.
1475 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001476 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001477 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001478 if (mStarted) return;
1479 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001480 // We delay the layer building a bit in order to give
1481 // other message processing a time to run. In particular
1482 // this avoids a delay in hiding the IME if it was
1483 // currently shown, because doing that may involve
1484 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001485 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001486 final ViewTreeObserver.OnDrawListener listener = this;
1487 mWorkspace.post(new Runnable() {
1488 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001489 if (mWorkspace != null &&
1490 mWorkspace.getViewTreeObserver() != null) {
1491 mWorkspace.getViewTreeObserver().
1492 removeOnDrawListener(listener);
1493 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001494 }
1495 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001496 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001497 }
1498 });
1499 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001500 // When Launcher comes back to foreground, a different Activity might be responsible for
1501 // the app market intent, so refresh the icon
1502 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001503 clearTypedText();
1504 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001505 }
1506
1507 private void sendAdvanceMessage(long delay) {
1508 mHandler.removeMessages(ADVANCE_MSG);
1509 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1510 mHandler.sendMessageDelayed(msg, delay);
1511 mAutoAdvanceSentTime = System.currentTimeMillis();
1512 }
1513
1514 private void updateRunning() {
1515 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1516 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1517 mAutoAdvanceRunning = autoAdvanceRunning;
1518 if (autoAdvanceRunning) {
1519 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1520 sendAdvanceMessage(delay);
1521 } else {
1522 if (!mWidgetsToAdvance.isEmpty()) {
1523 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1524 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1525 }
1526 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001527 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001528 }
1529 }
1530 }
1531
1532 private final Handler mHandler = new Handler() {
1533 @Override
1534 public void handleMessage(Message msg) {
1535 if (msg.what == ADVANCE_MSG) {
1536 int i = 0;
1537 for (View key: mWidgetsToAdvance.keySet()) {
1538 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1539 final int delay = mAdvanceStagger * i;
1540 if (v instanceof Advanceable) {
1541 postDelayed(new Runnable() {
1542 public void run() {
1543 ((Advanceable) v).advance();
1544 }
1545 }, delay);
1546 }
1547 i++;
1548 }
1549 sendAdvanceMessage(mAdvanceInterval);
1550 }
1551 }
1552 };
1553
1554 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001555 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001556 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1557 if (v instanceof Advanceable) {
1558 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001559 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001560 updateRunning();
1561 }
1562 }
1563
1564 void removeWidgetToAutoAdvance(View hostView) {
1565 if (mWidgetsToAdvance.containsKey(hostView)) {
1566 mWidgetsToAdvance.remove(hostView);
1567 updateRunning();
1568 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001569 }
1570
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001572 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001573 launcherInfo.hostView = null;
1574 }
1575
Winson Chung93eef082012-03-23 15:59:27 -07001576 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1577 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1578 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 }
1580
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001581 public LauncherAppWidgetHost getAppWidgetHost() {
1582 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Winson Chunga9abd0e2010-10-27 17:18:37 -07001585 public LauncherModel getModel() {
1586 return mModel;
1587 }
1588
Bjorn Bringertc459e522013-06-07 19:36:01 +01001589 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001590 getWindow().closeAllPanels();
1591
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001592 // Whatever we were doing is hereby canceled.
1593 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001594 }
1595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 @Override
1597 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001598 long startTime = 0;
1599 if (DEBUG_RESUME_TIME) {
1600 startTime = System.currentTimeMillis();
1601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 super.onNewIntent(intent);
1603
1604 // Close the menu
1605 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001606 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001607 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001608
Jamie Genniscb222e82012-10-23 15:44:26 -07001609 final boolean alreadyOnHome =
1610 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001611 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001612
Jamie Genniscb222e82012-10-23 15:44:26 -07001613 Runnable processIntent = new Runnable() {
1614 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001615 if (mWorkspace == null) {
1616 // Can be cases where mWorkspace is null, this prevents a NPE
1617 return;
1618 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001619 Folder openFolder = mWorkspace.getOpenFolder();
1620 // In all these cases, only animate if we're already on home
1621 mWorkspace.exitWidgetResizeMode();
1622 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1623 openFolder == null) {
1624 mWorkspace.moveToDefaultScreen(true);
1625 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001626
Jamie Genniscb222e82012-10-23 15:44:26 -07001627 closeFolder();
1628 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001629
Jamie Genniscb222e82012-10-23 15:44:26 -07001630 // If we are already on home, then just animate back to the workspace,
1631 // otherwise, just wait until onResume to set the state back to Workspace
1632 if (alreadyOnHome) {
1633 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001634 if (mWorkspace.isInOverviewMode()) {
1635 mWorkspace.exitOverviewMode();
1636 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001637 } else {
1638 mOnResumeState = State.WORKSPACE;
1639 }
1640
1641 final View v = getWindow().peekDecorView();
1642 if (v != null && v.getWindowToken() != null) {
1643 InputMethodManager imm = (InputMethodManager)getSystemService(
1644 INPUT_METHOD_SERVICE);
1645 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1646 }
1647
1648 // Reset AllApps to its initial state
1649 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1650 mAppsCustomizeTabHost.reset();
1651 }
1652 }
1653 };
1654
1655 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1656 // Delay processing of the intent to allow the status bar animation to finish
1657 // first in order to avoid janky animations.
1658 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001659 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001660 // Process the intent immediately.
1661 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001662 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
Michael Jurka447bf842013-05-15 14:52:15 +02001665 if (DEBUG_RESUME_TIME) {
1666 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 }
1669
1670 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001671 public void onRestoreInstanceState(Bundle state) {
1672 super.onRestoreInstanceState(state);
1673 for (int page: mSynchronouslyBoundPages) {
1674 mWorkspace.restoreInstanceStateForChild(page);
1675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
1677
1678 @Override
1679 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001680 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001681 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682
Michael Jurka883f55b2010-10-21 15:47:14 -07001683 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001684 // We close any open folder since it will not be re-opened, and we need to make sure
1685 // this state is reflected.
1686 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
Adam Cohendcd297f2013-06-18 13:13:40 -07001688 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001689 mWaitingForResult) {
1690 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001691 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1693 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001694 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1695 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1696 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
1698
1699 if (mFolderInfo != null && mWaitingForResult) {
1700 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1701 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1702 }
Michael Jurka946ad472010-07-09 18:05:18 -07001703
Winson Chung785d2eb2011-04-14 16:08:02 -07001704 // Save the current AppsCustomize tab
1705 if (mAppsCustomizeTabHost != null) {
1706 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1707 if (currentTabTag != null) {
1708 outState.putString("apps_customize_currentTab", currentTabTag);
1709 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001710 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1711 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714
1715 @Override
1716 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001718
Winson Chunge7a03942011-08-05 15:05:12 -07001719 // Remove all pending runnables
1720 mHandler.removeMessages(ADVANCE_MSG);
1721 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001722 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001723
Winson Chungcd2b0142011-06-08 16:02:26 -07001724 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001725 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001726 mModel.stopLoader();
1727 app.setLauncher(null);
1728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001730 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001732 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001734 mAppWidgetHost = null;
1735
1736 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737
1738 TextKeyListener.getInstance().release();
1739
Winson Chung81b52252012-08-27 15:34:29 -07001740 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1741 // to prevent leaking Launcher activities on orientation change.
1742 if (mModel != null) {
1743 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1744 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001745
1746 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001747 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001748
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001749 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001750 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1751 mWorkspace.removeAllViews();
1752 mWorkspace = null;
1753 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001754
Michael Jurka2ecf9952012-06-18 12:52:28 -07001755 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757
Adam Cohena9cf38f2011-05-02 15:36:58 -07001758 public DragController getDragController() {
1759 return mDragController;
1760 }
1761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 @Override
1763 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001764 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 super.startActivityForResult(intent, requestCode);
1766 }
1767
Winson Chung70d72102011-08-12 11:24:35 -07001768 /**
1769 * Indicates that we want global search for this activity by setting the globalSearch
1770 * argument for {@link #startSearch} to true.
1771 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001773 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001775
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001776 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001777
Karl Rosaen138a0412009-04-23 19:00:21 -07001778 if (initialQuery == null) {
1779 // Use any text typed in the launcher as the initial query
1780 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 if (appSearchData == null) {
1783 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001784 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
Winson Chungadf0c182012-08-23 14:59:07 -07001786 Rect sourceBounds = new Rect();
1787 if (mSearchDropTargetBar != null) {
1788 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1789 }
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Bjorn Bringertc459e522013-06-07 19:36:01 +01001791 startSearch(initialQuery, selectInitialQuery,
1792 appSearchData, sourceBounds);
1793 }
1794
1795 public void startSearch(String initialQuery,
1796 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001797 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001798 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001799 }
1800
1801 /**
1802 * Starts the global search activity. This code is a copied from SearchManager
1803 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001804 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001805 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001806 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001807 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1808 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1809 if (globalSearchActivity == null) {
1810 Log.w(TAG, "No global search activity found.");
1811 return;
1812 }
1813 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1814 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1815 intent.setComponent(globalSearchActivity);
1816 // Make sure that we have a Bundle to put source in
1817 if (appSearchData == null) {
1818 appSearchData = new Bundle();
1819 } else {
1820 appSearchData = new Bundle(appSearchData);
1821 }
1822 // Set source to package name of app that starts global search, if not set already.
1823 if (!appSearchData.containsKey("source")) {
1824 appSearchData.putString("source", getPackageName());
1825 }
1826 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1827 if (!TextUtils.isEmpty(initialQuery)) {
1828 intent.putExtra(SearchManager.QUERY, initialQuery);
1829 }
1830 if (selectInitialQuery) {
1831 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1832 }
1833 intent.setSourceBounds(sourceBounds);
1834 try {
1835 startActivity(intent);
1836 } catch (ActivityNotFoundException ex) {
1837 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840
Winson Chung70d72102011-08-12 11:24:35 -07001841 @Override
1842 public boolean onCreateOptionsMenu(Menu menu) {
1843 if (isWorkspaceLocked()) {
1844 return false;
1845 }
1846
1847 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001848
1849 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1850 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1851 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001852 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1853 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1854 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001855 String helpUrl = getString(R.string.help_url);
1856 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001857 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1858 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1859
Winson Chung70d72102011-08-12 11:24:35 -07001860 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1861 .setIcon(android.R.drawable.ic_menu_gallery)
1862 .setAlphabeticShortcut('W');
1863 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1864 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001865 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001866 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001867 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1868 .setIcon(android.R.drawable.ic_menu_preferences)
1869 .setIntent(settings)
1870 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001871 if (!helpUrl.isEmpty()) {
1872 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1873 .setIcon(android.R.drawable.ic_menu_help)
1874 .setIntent(help)
1875 .setAlphabeticShortcut('H');
1876 }
Winson Chung70d72102011-08-12 11:24:35 -07001877 return true;
1878 }
1879
1880 @Override
1881 public boolean onPrepareOptionsMenu(Menu menu) {
1882 super.onPrepareOptionsMenu(menu);
1883
1884 if (mAppsCustomizeTabHost.isTransitioning()) {
1885 return false;
1886 }
1887 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1888 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1889
1890 return true;
1891 }
1892
1893 @Override
1894 public boolean onOptionsItemSelected(MenuItem item) {
1895 switch (item.getItemId()) {
1896 case MENU_WALLPAPER_SETTINGS:
1897 startWallpaper();
1898 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001899 }
1900
1901 return super.onOptionsItemSelected(item);
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001904 @Override
1905 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001906 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001907 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001908 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001909 }
1910
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 public boolean isWorkspaceLocked() {
1912 return mWorkspaceLoading || mWaitingForResult;
1913 }
1914
Michael Jurka0280c3b2010-09-17 15:00:07 -07001915 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1919 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001920 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001922 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001923
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001924 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1925 AppWidgetProviderInfo appWidgetInfo) {
1926 if (appWidgetInfo.configure != null) {
1927 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001928
Bjorn Bringert7984c942009-12-09 15:38:25 +00001929 // Launch over to configure widget, if needed
1930 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001931 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001932 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02001933 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001935 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001936 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001937 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001938 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001939 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941 }
Romain Guycbb89e42009-06-08 15:52:54 -07001942
Adam Cohenfbba09b2011-07-18 21:43:05 -07001943 /**
1944 * Process a shortcut drop.
1945 *
1946 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001948 * @param cell The cell it should be added to, optional
1949 * @param position The location on the screen where it was dropped, optional
1950 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001951 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001953 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001954 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001956 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001957
1958 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001959 mPendingAddInfo.cellX = cell[0];
1960 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001961 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001962
1963 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1964 createShortcutIntent.setComponent(componentName);
1965 processShortcut(createShortcutIntent);
1966 }
1967
Adam Cohenfbba09b2011-07-18 21:43:05 -07001968 /**
1969 * Process a widget drop.
1970 *
1971 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001972 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001973 * @param cell The cell it should be added to, optional
1974 * @param position The location on the screen where it was dropped, optional
1975 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001976 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001977 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001978 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001981 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001982 mPendingAddInfo.minSpanX = info.minSpanX;
1983 mPendingAddInfo.minSpanY = info.minSpanY;
1984
Adam Cohenfbba09b2011-07-18 21:43:05 -07001985 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 mPendingAddInfo.cellX = cell[0];
1987 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001988 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001989 if (span != null) {
1990 mPendingAddInfo.spanX = span[0];
1991 mPendingAddInfo.spanY = span[1];
1992 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001993
Adam Cohened66b2b2012-01-23 17:28:51 -08001994 AppWidgetHostView hostView = info.boundWidget;
1995 int appWidgetId;
1996 if (hostView != null) {
1997 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001998 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001999 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002000 // In this case, we either need to start an activity to get permission to bind
2001 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002002 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002003 Bundle options = info.bindOptions;
2004
2005 boolean success = false;
2006 if (options != null) {
2007 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2008 info.componentName, options);
2009 } else {
2010 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2011 info.componentName);
2012 }
2013 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002014 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002015 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002016 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002017 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2018 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2019 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002020 // TODO: we need to make sure that this accounts for the options bundle.
2021 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002022 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2023 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002024 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002025 }
2026
Joe Onoratodeb98af2010-02-19 14:59:39 -08002027 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002028 // Handle case where user selected "Applications"
2029 String applicationName = getResources().getString(R.string.group_applications);
2030 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002031
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002032 if (applicationName != null && applicationName.equals(shortcutName)) {
2033 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2034 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002036 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2037 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002038 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002039 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002040 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002041 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
2044
Winson Chung24ab2f12010-09-16 14:10:47 -07002045 void processWallpaper(Intent intent) {
2046 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2047 }
2048
Adam Cohendcd297f2013-06-18 13:13:40 -07002049 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002050 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002051 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 folderInfo.title = getText(R.string.folder_name);
2053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002055 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002056 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002057 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058
2059 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002060 FolderIcon newFolder =
2061 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002062 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002063 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002064 // Force measure the new folder icon
2065 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2066 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002067 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
Romain Guycbb89e42009-06-08 15:52:54 -07002069
Joe Onorato9c1289c2009-08-17 11:03:03 -04002070 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002071 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002072 }
2073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002075 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002077 pickWallpaper.setComponent(
2078 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2079 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002082 /**
2083 * Registers various content observers. The current implementation registers
2084 * only a favorites observer to keep track of the favorites applications.
2085 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002086 private void registerContentObservers() {
2087 ContentResolver resolver = getContentResolver();
2088 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2089 true, mWidgetObserver);
2090 }
2091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 @Override
2093 public boolean dispatchKeyEvent(KeyEvent event) {
2094 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2095 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002097 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002098 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002099 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002100 dumpState();
2101 return true;
2102 }
2103 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002104 }
2105 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2106 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002107 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 return true;
2109 }
2110 }
2111
2112 return super.dispatchKeyEvent(event);
2113 }
2114
Joe Onorato88ec0992009-11-19 13:16:06 -08002115 @Override
2116 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002117 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002118 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002119 } else if (mWorkspace.isInOverviewMode()) {
2120 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002121 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002122 Folder openFolder = mWorkspace.getOpenFolder();
2123 if (openFolder.isEditingName()) {
2124 openFolder.dismissEditingName();
2125 } else {
2126 closeFolder();
2127 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002128 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002129 mWorkspace.exitWidgetResizeMode();
2130
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002131 // Back button is a no-op here, but give at least some feedback for the button press
2132 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002133 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002134 }
2135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002137 * Re-listen when widgets are reset.
2138 */
2139 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002140 if (mAppWidgetHost != null) {
2141 mAppWidgetHost.startListening();
2142 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002143 }
2144
2145 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 * Launches the intent referred by the clicked shortcut.
2147 *
2148 * @param v The view representing the clicked shortcut.
2149 */
2150 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002151 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2152 // view has detached (it's possible for this to happen if the view is removed mid touch).
2153 if (v.getWindowToken() == null) {
2154 return;
2155 }
2156
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002157 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002158 return;
2159 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002160
Adam Cohenf358a4b2013-07-23 16:47:31 -07002161 if (v instanceof PageIndicator) {
2162 if (!mWorkspace.isInOverviewMode()) {
2163 mWorkspace.enterOverviewMode();
2164 }
2165 return;
2166 }
2167
2168 if (v instanceof CellLayout) {
2169 if (mWorkspace.isInOverviewMode()) {
2170 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2171 }
2172 }
2173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002175 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002177 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2178 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002179
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002180 // Check for special shortcuts
2181 if (intent.getComponent() != null) {
2182 final String shortcutClass = intent.getComponent().getClassName();
2183
2184 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2185 showAllApps(true);
2186 return;
2187 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2188 MemoryDumpActivity.startDump(this);
2189 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002190 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2191 toggleShowWeightWatcher();
2192 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002193 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002194 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002195
2196 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002197 int[] pos = new int[2];
2198 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002199 intent.setSourceBounds(new Rect(pos[0], pos[1],
2200 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002201
2202 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002203
Daniel Sandlerff02d492013-08-05 02:12:05 -04002204 mStats.recordLaunch(intent, shortcut);
2205
Michael Jurkaddd62e92011-02-16 17:49:14 -08002206 if (success && v instanceof BubbleTextView) {
2207 mWaitingForResume = (BubbleTextView) v;
2208 mWaitingForResume.setStayPressed(true);
2209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002211 if (v instanceof FolderIcon) {
2212 FolderIcon fi = (FolderIcon) v;
2213 handleFolderClick(fi);
2214 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002215 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002216 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002217 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002218 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002219 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 }
2222 }
2223
Michael Jurka0e260592010-06-30 17:07:39 -07002224 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002225 return false;
2226 }
2227
Michael Jurkaaf442092010-06-10 17:01:57 -07002228 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002229 * Event handler for the search button
2230 *
2231 * @param v The view that was clicked.
2232 */
2233 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002234 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2235
Amith Yamasania135ba82011-08-09 17:42:01 -07002236 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002237 }
2238
2239 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002240 * Event handler for the voice button
2241 *
2242 * @param v The view that was clicked.
2243 */
2244 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002245 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002246
Bjorn Bringertc459e522013-06-07 19:36:01 +01002247 startVoice();
2248 }
2249
2250 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002251 try {
2252 final SearchManager searchManager =
2253 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2254 ComponentName activityName = searchManager.getGlobalSearchActivity();
2255 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002256 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002257 if (activityName != null) {
2258 intent.setPackage(activityName.getPackageName());
2259 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002260 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002261 } catch (ActivityNotFoundException e) {
2262 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002263 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002264 startActivitySafely(null, intent, "onClickVoiceButton");
2265 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002266 }
2267
2268 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002269 * Event handler for the "grid" button that appears on the home screen, which
2270 * enters all apps mode.
2271 *
2272 * @param v The view that was clicked.
2273 */
2274 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002275 showAllApps(true);
2276 }
2277
2278 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002279 // Provide the same haptic feedback that the system offers for virtual keys.
2280 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002281 }
2282
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002283 public void onClickAppMarketButton(View v) {
2284 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002285 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002286 } else {
2287 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002288 }
2289 }
2290
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002291 void startApplicationDetailsActivity(ComponentName componentName) {
2292 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002293 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2294 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002295 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002296 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002297 }
2298
Michael Jurka1e2f4652013-07-08 18:03:46 -07002299 // returns true if the activity was started
2300 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2301 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002302 // System applications cannot be installed. For now, show a toast explaining that.
2303 // We may give them the option of disabling apps this way.
2304 int messageId = R.string.uninstall_system_app_text;
2305 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002306 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002307 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002308 String packageName = componentName.getPackageName();
2309 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002310 Intent intent = new Intent(
2311 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002312 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2313 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002314 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002315 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002316 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002317 }
2318
Michael Jurka86a720e2012-05-09 11:23:23 -07002319 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002323 // Only launch using the new animation if the shortcut has not opted out (this is a
2324 // private contract between launcher and may be ignored in the future).
2325 boolean useLaunchAnimation = (v != null) &&
2326 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2327 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002328 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2329 v.getMeasuredWidth(), v.getMeasuredHeight());
2330
2331 startActivity(intent, opts.toBundle());
2332 } else {
2333 startActivity(intent);
2334 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002335 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 } catch (SecurityException e) {
2337 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002338 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002340 "or use the exported attribute for this activity. "
2341 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002343 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002345
Michael Jurka86a720e2012-05-09 11:23:23 -07002346 boolean startActivitySafely(View v, Intent intent, Object tag) {
2347 boolean success = false;
2348 try {
2349 success = startActivity(v, intent, tag);
2350 } catch (ActivityNotFoundException e) {
2351 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2352 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2353 }
2354 return success;
2355 }
2356
Adam Cohena9cf38f2011-05-02 15:36:58 -07002357 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002358 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002359 Folder openFolder = mWorkspace.getFolderForTag(info);
2360
2361 // If the folder info reports that the associated folder is open, then verify that
2362 // it is actually opened. There have been a few instances where this gets out of sync.
2363 if (info.opened && openFolder == null) {
2364 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002365 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002366 info.opened = false;
2367 }
2368
Adam Cohenfb91f302012-06-11 15:45:18 -07002369 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 // Close any open folder
2371 closeFolder();
2372 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002373 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 } else {
2375 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 int folderScreen;
2377 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002378 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 // .. and close it
2380 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002381 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 // Close any folder open on the current screen
2383 closeFolder();
2384 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002385 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 }
2387 }
2388 }
2389 }
2390
Adam Cohen268c4752012-06-06 17:47:33 -07002391 /**
2392 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2393 * in the DragLayer in the exact absolute location of the original FolderIcon.
2394 */
2395 private void copyFolderIconToImage(FolderIcon fi) {
2396 final int width = fi.getMeasuredWidth();
2397 final int height = fi.getMeasuredHeight();
2398
2399 // Lazy load ImageView, Bitmap and Canvas
2400 if (mFolderIconImageView == null) {
2401 mFolderIconImageView = new ImageView(this);
2402 }
2403 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2404 mFolderIconBitmap.getHeight() != height) {
2405 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2406 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2407 }
2408
2409 DragLayer.LayoutParams lp;
2410 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2411 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2412 } else {
2413 lp = new DragLayer.LayoutParams(width, height);
2414 }
2415
Adam Cohen307fe232012-08-16 17:55:58 -07002416 // The layout from which the folder is being opened may be scaled, adjust the starting
2417 // view size by this scale factor.
2418 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002419 lp.customPosition = true;
2420 lp.x = mRectForFolderAnimation.left;
2421 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002422 lp.width = (int) (scale * width);
2423 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002424
2425 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2426 fi.draw(mFolderIconCanvas);
2427 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002428 if (fi.getFolder() != null) {
2429 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2430 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002431 }
Adam Cohen268c4752012-06-06 17:47:33 -07002432 // Just in case this image view is still in the drag layer from a previous animation,
2433 // we remove it and re-add it.
2434 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2435 mDragLayer.removeView(mFolderIconImageView);
2436 }
2437 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002438 if (fi.getFolder() != null) {
2439 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002440 }
Adam Cohen268c4752012-06-06 17:47:33 -07002441 }
2442
Adam Cohen2801caf2011-05-13 20:57:39 -07002443 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002444 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002445 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2446 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2447 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2448
Adam Cohenc51934b2011-07-26 21:07:43 -07002449 FolderInfo info = (FolderInfo) fi.getTag();
2450 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2451 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002452 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2453 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002454 }
2455
Adam Cohen268c4752012-06-06 17:47:33 -07002456 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2457 copyFolderIconToImage(fi);
2458 fi.setVisibility(View.INVISIBLE);
2459
Michael Jurka2ecf9952012-06-18 12:52:28 -07002460 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002461 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002462 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2463 oa.start();
2464 }
2465
Adam Cohen268c4752012-06-06 17:47:33 -07002466 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002467 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002468 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2469 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2470 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2471
Adam Cohen268c4752012-06-06 17:47:33 -07002472 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002473
Adam Cohen268c4752012-06-06 17:47:33 -07002474 // We remove and re-draw the FolderIcon in-case it has changed
2475 mDragLayer.removeView(mFolderIconImageView);
2476 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002477 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002478 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002479 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002480 oa.addListener(new AnimatorListenerAdapter() {
2481 @Override
2482 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002483 if (cl != null) {
2484 cl.clearFolderLeaveBehind();
2485 // Remove the ImageView copy of the FolderIcon and make the original visible.
2486 mDragLayer.removeView(mFolderIconImageView);
2487 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002488 }
2489 }
2490 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002491 oa.start();
2492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002495 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 * is animated relative to the specified View. If the View is null, no animation
2497 * is played.
2498 *
2499 * @param folderInfo The FolderInfo describing the folder to open.
2500 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002501 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002502 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002503 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504
Adam Cohena9cf38f2011-05-02 15:36:58 -07002505 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506
Adam Cohen4554ee12011-08-03 16:13:21 -07002507 // Just verify that the folder hasn't already been added to the DragLayer.
2508 // There was a one-off crash where the folder had a parent already.
2509 if (folder.getParent() == null) {
2510 mDragLayer.addView(folder);
2511 mDragController.addDropTarget((DropTarget) folder);
2512 } else {
2513 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2514 folder.getParent() + ").");
2515 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002516 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002517 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002518
2519 // Notify the accessibility manager that this folder "window" has appeared and occluded
2520 // the workspace items
2521 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2522 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002523 }
2524
2525 public void closeFolder() {
2526 Folder folder = mWorkspace.getOpenFolder();
2527 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002528 if (folder.isEditingName()) {
2529 folder.dismissEditingName();
2530 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002531 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002532
2533 // Dismiss the folder cling
2534 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002535 }
2536 }
2537
2538 void closeFolder(Folder folder) {
2539 folder.getInfo().opened = false;
2540
2541 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2542 if (parent != null) {
2543 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2544 shrinkAndFadeInFolderIcon(fi);
2545 }
2546 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002547
2548 // Notify the accessibility manager that this folder "window" has disappeard and no
2549 // longer occludeds the workspace items
2550 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002554 if (!isDraggingEnabled()) return false;
2555 if (isWorkspaceLocked()) return false;
2556 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557
2558 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002559 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 }
2561
Michael Jurka0280c3b2010-09-17 15:00:07 -07002562 resetAddInfo();
2563 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002565 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 return true;
2567 }
2568
Winson Chung3d503fb2011-07-13 17:25:49 -07002569 // The hotseat touch handling does not go through Workspace, and we always allow long press
2570 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002571 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002572 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2573 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002574 if (itemUnderLongClick == null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07002575 if (mWorkspace.hasNonCustomEmptyScreens()) {
2576 // User long pressed on empty space
2577 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2578 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2579 // Disabling reordering until we sort out some issues.
2580 if (mWorkspace.isInOverviewMode()) {
2581 mWorkspace.startReordering(v);
2582 } else {
2583 mWorkspace.enterOverviewMode();
2584 }
Adam Cohendedbd962013-07-11 14:21:49 -07002585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002587 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002589 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 }
2591 }
2592 }
2593 return true;
2594 }
2595
Winson Chung3d503fb2011-07-13 17:25:49 -07002596 boolean isHotseatLayout(View layout) {
2597 return mHotseat != null && layout != null &&
2598 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2599 }
2600 Hotseat getHotseat() {
2601 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002602 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002603 View getOverviewPanel() {
2604 return mOverviewPanel;
2605 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002606 SearchDropTargetBar getSearchBar() {
2607 return mSearchDropTargetBar;
2608 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002609
Winson Chung3d503fb2011-07-13 17:25:49 -07002610 /**
2611 * Returns the CellLayout of the specified container at the specified screen.
2612 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002613 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002614 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2615 if (mHotseat != null) {
2616 return mHotseat.getLayout();
2617 } else {
2618 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002619 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002620 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002621 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002622 }
2623 }
2624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 Workspace getWorkspace() {
2626 return mWorkspace;
2627 }
2628
Daniel Sandler843e8602010-06-07 14:59:01 -04002629 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002630 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002631 }
2632
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 /**
2634 * Helper method for the cameraZoomIn/cameraZoomOut animations
2635 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636 * @param scaleFactor The scale factor used for the zoom
2637 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002638 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002640 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002641 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002642
Winson Chung18f41f82012-05-09 13:28:10 -07002643 void disableWallpaperIfInAllApps() {
2644 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002645 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002646 if (mAppsCustomizeTabHost != null &&
2647 !mAppsCustomizeTabHost.isTransitioning()) {
2648 updateWallpaperVisibility(false);
2649 }
2650 }
2651 }
2652
Craig Mautner360310b2012-10-26 15:13:08 -07002653 private void setWorkspaceBackground(boolean workspace) {
2654 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002655 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002656 }
2657
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002658 void updateWallpaperVisibility(boolean visible) {
2659 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2660 int curflags = getWindow().getAttributes().flags
2661 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2662 if (wpflags != curflags) {
2663 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2664 }
Craig Mautner360310b2012-10-26 15:13:08 -07002665 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002666 }
2667
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002668 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2669 if (v instanceof LauncherTransitionable) {
2670 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2671 }
2672 }
2673
Michael Jurkabed61d22012-02-14 22:51:29 -08002674 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2675 if (v instanceof LauncherTransitionable) {
2676 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2677 }
Winson Chung70442722012-02-10 15:43:22 -08002678
2679 // Update the workspace transition step as well
2680 dispatchOnLauncherTransitionStep(v, 0f);
2681 }
2682
2683 private void dispatchOnLauncherTransitionStep(View v, float t) {
2684 if (v instanceof LauncherTransitionable) {
2685 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2686 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002687 }
2688
2689 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2690 if (v instanceof LauncherTransitionable) {
2691 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2692 }
Winson Chung70442722012-02-10 15:43:22 -08002693
2694 // Update the workspace transition step as well
2695 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002696 }
2697
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002698 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002699 * Things to test when changing the following seven functions.
2700 * - Home from workspace
2701 * - from center screen
2702 * - from other screens
2703 * - Home from all apps
2704 * - from center screen
2705 * - from other screens
2706 * - Back from all apps
2707 * - from center screen
2708 * - from other screens
2709 * - Launch app from workspace and quit
2710 * - with back
2711 * - with home
2712 * - Launch app from all apps and quit
2713 * - with back
2714 * - with home
2715 * - Go to a screen that's not the default, then all
2716 * apps, and launch and app, and go back
2717 * - with back
2718 * -with home
2719 * - On workspace, long press power and go back
2720 * - with back
2721 * - with home
2722 * - On all apps, long press power and go back
2723 * - with back
2724 * - with home
2725 * - On workspace, power off
2726 * - On all apps, power off
2727 * - Launch an app and turn off the screen while in that app
2728 * - Go back with home key
2729 * - Go back with back key TODO: make this not go to workspace
2730 * - From all apps
2731 * - From workspace
2732 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2733 * - From all apps
2734 * - From the center workspace
2735 * - From another workspace
2736 */
2737
2738 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002739 * Zoom the camera out from the workspace to reveal 'toView'.
2740 * Assumes that the view to show is anchored at either the very top or very bottom
2741 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002742 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002743 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002744 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002745 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002746 mStateAnimation.cancel();
2747 mStateAnimation = null;
2748 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002749 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002750
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2752 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2753 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002754 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002755 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002756 final int startDelay =
2757 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002758
Michael Jurkab3e22d92011-10-31 15:58:33 -07002759 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002760
Michael Jurkad74c9842011-07-10 12:44:21 -07002761 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002762 Animator workspaceAnim =
2763 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002764
2765 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002766 toView.setScaleX(scale);
2767 toView.setScaleY(scale);
2768 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2769 scaleAnim.
2770 scaleX(1f).scaleY(1f).
2771 setDuration(duration).
2772 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002773
Winson Chungf0ea4d32011-06-06 14:27:16 -07002774 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002775 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002776 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002777 .ofFloat(toView, "alpha", 0f, 1f)
2778 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002780 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2781 @Override
2782 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002783 if (animation == null) {
2784 throw new RuntimeException("animation is null");
2785 }
Winson Chung70442722012-02-10 15:43:22 -08002786 float t = (Float) animation.getAnimatedValue();
2787 dispatchOnLauncherTransitionStep(fromView, t);
2788 dispatchOnLauncherTransitionStep(toView, t);
2789 }
2790 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002791
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002792 // toView should appear right at the end of the workspace shrink
2793 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002794 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002795 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002796 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002797
2798 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002799 boolean animationCancelled = false;
2800
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002801 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002802 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002803 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002804 // Prepare the position
2805 toView.setTranslationX(0.0f);
2806 toView.setTranslationY(0.0f);
2807 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002808 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002810 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002811 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002812 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2813 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002814
Adam Cohenf4ddea32011-09-12 13:46:42 -07002815 if (!animationCancelled) {
2816 updateWallpaperVisibility(false);
2817 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002818
2819 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002820 if (mSearchDropTargetBar != null) {
2821 mSearchDropTargetBar.hideSearchBar(false);
2822 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002823 }
2824
Adam Cohencff6af82011-09-13 14:51:53 -07002825 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002826 public void onAnimationCancel(Animator animation) {
2827 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002828 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 });
2830
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002831 if (workspaceAnim != null) {
2832 mStateAnimation.play(workspaceAnim);
2833 }
Michael Jurka1899a362011-11-03 13:50:45 -07002834
2835 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002836
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002837 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2838 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002839
2840 // If any of the objects being animated haven't been measured/laid out
2841 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002842 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002843 (mWorkspace.getMeasuredWidth() == 0) ||
2844 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002845 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002846 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002847
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002848 final AnimatorSet stateAnimation = mStateAnimation;
2849 final Runnable startAnimRunnable = new Runnable() {
2850 public void run() {
2851 // Check that mStateAnimation hasn't changed while
2852 // we waited for a layout/draw pass
2853 if (mStateAnimation != stateAnimation)
2854 return;
2855 setPivotsForZoom(toView, scale);
2856 dispatchOnLauncherTransitionStart(fromView, animated, false);
2857 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002858 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002859 }
2860 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002861 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002862 final ViewTreeObserver observer = toView.getViewTreeObserver();
2863 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2864 public void onGlobalLayout() {
2865 startAnimRunnable.run();
2866 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2867 }
2868 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002869 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002870 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002871 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872 } else {
2873 toView.setTranslationX(0.0f);
2874 toView.setTranslationY(0.0f);
2875 toView.setScaleX(1.0f);
2876 toView.setScaleY(1.0f);
2877 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002878 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002879
Daniel Sandlere4f98912013-06-25 15:13:26 -04002880 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002881 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002882 if (mSearchDropTargetBar != null) {
2883 mSearchDropTargetBar.hideSearchBar(false);
2884 }
Michael Jurkaabded662011-03-04 12:06:57 -08002885 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002886 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002887 dispatchOnLauncherTransitionStart(fromView, animated, false);
2888 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002889 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002890 dispatchOnLauncherTransitionStart(toView, animated, false);
2891 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002892 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002893 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002894 }
2895
2896 /**
2897 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002898 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002899 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002900 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002901 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2902 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002903
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002905 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002906 mStateAnimation.cancel();
2907 mStateAnimation = null;
2908 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002909 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002910
Winson Chungf0ea4d32011-06-06 14:27:16 -07002911 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002912 final int fadeOutDuration =
2913 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002914 final float scaleFactor = (float)
2915 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2916 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002917 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002918 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002919 if (toState == State.WORKSPACE) {
2920 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2921 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002922 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002923 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2924 workspaceAnim = mWorkspace.getChangeStateAnimation(
2925 Workspace.State.SPRING_LOADED, animated);
2926 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002927
Michael Jurkab3e22d92011-10-31 15:58:33 -07002928 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002929 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002930 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002931 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002932 final LauncherViewPropertyAnimator scaleAnim =
2933 new LauncherViewPropertyAnimator(fromView);
2934 scaleAnim.
2935 scaleX(scaleFactor).scaleY(scaleFactor).
2936 setDuration(duration).
2937 setInterpolator(new Workspace.ZoomInInterpolator());
2938
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002939 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002940 .ofFloat(fromView, "alpha", 1f, 0f)
2941 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002942 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002943 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2944 @Override
2945 public void onAnimationUpdate(ValueAnimator animation) {
2946 float t = 1f - (Float) animation.getAnimatedValue();
2947 dispatchOnLauncherTransitionStep(fromView, t);
2948 dispatchOnLauncherTransitionStep(toView, t);
2949 }
2950 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002951
Michael Jurka2ecf9952012-06-18 12:52:28 -07002952 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002953
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002954 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2955 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002956 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002957
2958 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002959 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002960 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002961 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002962 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002963 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2964 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002965 if (onCompleteRunnable != null) {
2966 onCompleteRunnable.run();
2967 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002968 mAppsCustomizeContent.updateCurrentPageScroll();
2969 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002970 }
2971 });
2972
Winson Chungf0ea4d32011-06-06 14:27:16 -07002973 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002974 if (workspaceAnim != null) {
2975 mStateAnimation.play(workspaceAnim);
2976 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002977 dispatchOnLauncherTransitionStart(fromView, animated, true);
2978 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002979 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002980 } else {
2981 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002982 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002983 dispatchOnLauncherTransitionStart(fromView, animated, true);
2984 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002985 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002986 dispatchOnLauncherTransitionStart(toView, animated, true);
2987 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002988 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002989 }
2990
Michael Jurkae326f182011-11-21 14:05:46 -08002991 @Override
2992 public void onTrimMemory(int level) {
2993 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002994 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002995 mAppsCustomizeTabHost.onTrimMemory();
2996 }
2997 }
2998
Winson Chung18f41f82012-05-09 13:28:10 -07002999 @Override
3000 public void onWindowFocusChanged(boolean hasFocus) {
3001 if (!hasFocus) {
3002 // When another window occludes launcher (like the notification shade, or recents),
3003 // ensure that we enable the wallpaper flag so that transitions are done correctly.
3004 updateWallpaperVisibility(true);
3005 } else {
3006 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07003007 mWorkspace.postDelayed(new Runnable() {
3008 @Override
3009 public void run() {
3010 disableWallpaperIfInAllApps();
3011 }
3012 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003013 }
3014 }
3015
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003016 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003017 showWorkspace(animated, null);
3018 }
3019
3020 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003021 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003022 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003023 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003024 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003025
Winson Chungc7d2b602012-05-16 17:02:20 -07003026 // Show the search bar (only animate if we were showing the drop target bar in spring
3027 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003028 if (mSearchDropTargetBar != null) {
3029 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3030 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003031
Michael Jurkab3e22d92011-10-31 15:58:33 -07003032 // Set focus to the AppsCustomize button
3033 if (mAllAppsButton != null) {
3034 mAllAppsButton.requestFocus();
3035 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003036 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003037
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003038 // Change the state *after* we've called all the transition code
3039 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003040
Winson Chung5fb63472011-02-02 17:03:37 -08003041 // Resume the auto-advance of widgets
3042 mUserPresent = true;
3043 updateRunning();
3044
alanv1d4fde62012-10-17 13:15:47 -07003045 // Send an accessibility event to announce the context change
3046 getWindow().getDecorView()
3047 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003048
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003049 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003050 }
3051
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003052 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003053 }
3054
Michael Jurkab3e22d92011-10-31 15:58:33 -07003055 void showAllApps(boolean animated) {
3056 if (mState != State.WORKSPACE) return;
3057
3058 showAppsCustomizeHelper(animated, false);
3059 mAppsCustomizeTabHost.requestFocus();
3060
Michael Jurkab3e22d92011-10-31 15:58:33 -07003061 // Change the state *after* we've called all the transition code
3062 mState = State.APPS_CUSTOMIZE;
3063
3064 // Pause the auto-advance of widgets until we are out of AllApps
3065 mUserPresent = false;
3066 updateRunning();
3067 closeFolder();
3068
3069 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003070 getWindow().getDecorView()
3071 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003072 }
3073
Adam Cohen7777d962011-08-18 18:58:38 -07003074 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003075 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003076 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003077 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003078 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003079 }
Adam Cohen7777d962011-08-18 18:58:38 -07003080
Adam Cohened66b2b2012-01-23 17:28:51 -08003081 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3082 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003083 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3084
Winson Chunge7a03942011-08-05 15:05:12 -07003085 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003086 @Override
3087 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003088 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003089 // Before we show workspace, hide all apps again because
3090 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3091 // clean up our state transition functions
3092 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003093 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003094 } else {
3095 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003096 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003097 }
3098 }, (extendedDelay ?
3099 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3100 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3101 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003102
Michael Jurkad3ef3062010-11-23 16:23:58 -08003103 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003104 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003105 final boolean animated = true;
3106 final boolean springLoaded = true;
3107 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003108 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003109 }
3110 // Otherwise, we are not in spring loaded mode, so don't do anything.
3111 }
3112
Michael Jurkab3e22d92011-10-31 15:58:33 -07003113 void lockAllApps() {
3114 // TODO
3115 }
3116
3117 void unlockAllApps() {
3118 // TODO
3119 }
3120
Winson Chungf0ea4d32011-06-06 14:27:16 -07003121 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003122 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003123 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003124 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003125 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003126 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003127 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003128 int duration = 0;
3129 if (mSearchDropTargetBar != null) {
3130 duration = mSearchDropTargetBar.getTransitionInDuration();
3131 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003132 mHotseat.animate().alpha(1f).setDuration(duration);
3133 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003134 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003135 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003136 }
3137 }
3138 }
3139
3140 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003141 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003142 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003144 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003145 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003146 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003147 int duration = 0;
3148 if (mSearchDropTargetBar != null) {
3149 duration = mSearchDropTargetBar.getTransitionOutDuration();
3150 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003151 mHotseat.animate().alpha(0f).setDuration(duration);
3152 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003153 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003155 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003156 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003157 }
3158
Patrick Dubroy5f445422011-02-18 14:35:21 -08003159 /**
3160 * Add an item from all apps or customize onto the given workspace screen.
3161 * If layout is null, add to the current screen.
3162 */
3163 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003164 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003165 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003166 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003167 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003168
Winson Chungdff8ebb2011-09-08 17:25:31 -07003169 /** Maps the current orientation to an index for referencing orientation correct global icons */
3170 private int getCurrentOrientationIndexForGlobalIcons() {
3171 // default - 0, landscape - 1
3172 switch (getResources().getConfiguration().orientation) {
3173 case Configuration.ORIENTATION_LANDSCAPE:
3174 return 1;
3175 default:
3176 return 0;
3177 }
3178 }
3179
Michael Jurkaae65ee32012-04-30 11:45:54 -07003180 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003181 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003182 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003183 // Look for the toolbar icon specified in the activity meta-data
3184 Bundle metaData = packageManager.getActivityInfo(
3185 activityName, PackageManager.GET_META_DATA).metaData;
3186 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003187 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003188 if (iconResId != 0) {
3189 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003190 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003191 }
3192 }
3193 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003194 // This can happen if the activity defines an invalid drawable
3195 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3196 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003197 } catch (Resources.NotFoundException nfe) {
3198 // This can happen if the activity defines an invalid drawable
3199 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3200 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003201 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003202 return null;
3203 }
3204
3205 // if successful in getting icon, return it; otherwise, set button to use default drawable
3206 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003207 int buttonId, ComponentName activityName, int fallbackDrawableId,
3208 String toolbarResourceName) {
3209 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003210 Resources r = getResources();
3211 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3212 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003213
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003214 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003215 // If we were unable to find the icon via the meta-data, use a generic one
3216 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003217 toolbarIcon = r.getDrawable(fallbackDrawableId);
3218 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003219 if (button != null) {
3220 button.setCompoundDrawables(toolbarIcon, null, null, null);
3221 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003222 return null;
3223 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003224 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003225 if (button != null) {
3226 button.setCompoundDrawables(toolbarIcon, null, null, null);
3227 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003228 return toolbarIcon.getConstantState();
3229 }
3230 }
3231
3232 // if successful in getting icon, return it; otherwise, set button to use default drawable
3233 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003234 int buttonId, ComponentName activityName, int fallbackDrawableId,
3235 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003236 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003237 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003238
Michael Jurka19e0fc52011-07-22 18:00:21 -07003239 if (button != null) {
3240 // If we were unable to find the icon via the meta-data, use a
3241 // generic one
3242 if (toolbarIcon == null) {
3243 button.setImageResource(fallbackDrawableId);
3244 } else {
3245 button.setImageDrawable(toolbarIcon);
3246 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003247 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003248
3249 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3250
Michael Jurka0423dcf2010-10-05 14:56:18 -07003251 }
3252
Winson Chung1b884092012-06-08 17:13:02 -07003253 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003254 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003255 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003256 }
3257
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003258 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003259 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003260 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003261 }
3262
Winson Chungbb185bd2011-11-21 12:31:42 -08003263 private void invalidatePressedFocusedStates(View container, View button) {
3264 if (container instanceof HolographicLinearLayout) {
3265 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3266 layout.invalidatePressedFocusedStates();
3267 } else if (button instanceof HolographicImageView) {
3268 HolographicImageView view = (HolographicImageView) button;
3269 view.invalidatePressedFocusedStates();
3270 }
3271 }
3272
Cristina Stancu476493b2013-08-07 17:20:14 +01003273 public View getQsbBar() {
3274 if (mQsbBar == null) {
3275 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3276 }
3277 return mQsbBar;
3278 }
3279
3280 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003282 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003283 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003284 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003285
Winson Chung1cad91e2011-05-25 17:41:01 -07003286 final SearchManager searchManager =
3287 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3288 ComponentName activityName = searchManager.getGlobalSearchActivity();
3289 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003290 int coi = getCurrentOrientationIndexForGlobalIcons();
3291 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003292 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3293 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3294 if (sGlobalSearchIcon[coi] == null) {
3295 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3296 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3297 TOOLBAR_ICON_METADATA_NAME);
3298 }
3299
Winson Chungc51db6a2011-10-05 11:44:49 -07003300 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3301 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003302 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003303 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003304 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003305 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003306 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3307 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3308 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003309 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003310 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003311 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003312 }
3313 }
3314
Cristina Stancu476493b2013-08-07 17:20:14 +01003315 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003316 final View searchButtonContainer = findViewById(R.id.search_button_container);
3317 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003318 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003319 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003320 }
3321
Cristina Stancu476493b2013-08-07 17:20:14 +01003322 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003324 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003325
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003327 final SearchManager searchManager =
3328 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3329 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3330
3331 ComponentName activityName = null;
3332 if (globalSearchActivity != null) {
3333 // Check if the global search activity handles voice search
3334 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3335 intent.setPackage(globalSearchActivity.getPackageName());
3336 activityName = intent.resolveActivity(getPackageManager());
3337 }
3338
3339 if (activityName == null) {
3340 // Fallback: check if an activity other than the global search activity
3341 // resolves this
3342 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3343 activityName = intent.resolveActivity(getPackageManager());
3344 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003345 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003346 int coi = getCurrentOrientationIndexForGlobalIcons();
3347 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003348 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3349 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3350 if (sVoiceSearchIcon[coi] == null) {
3351 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3352 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3353 TOOLBAR_ICON_METADATA_NAME);
3354 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003355 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003356 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003357 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003358 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003359 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003360 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003361 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003362 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003363 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003364 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003365 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003366 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003367
Cristina Stancu476493b2013-08-07 17:20:14 +01003368 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003369 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3370 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003371 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003372 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003373 }
3374
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003375 public void setVoiceButtonProxyVisible(boolean visible) {
3376 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3377 if (voiceButtonProxy != null) {
3378 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3379 }
3380 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003381 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003382 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003383 */
3384 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003385 final View marketButton = findViewById(R.id.market_button);
3386 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3387 // Find the app market activity by resolving an intent.
3388 // (If multiple app markets are installed, it will return the ResolverActivity.)
3389 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003390 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003391 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003392 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003393 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003394 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3395 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003396 marketButton.setVisibility(View.VISIBLE);
3397 } else {
3398 // We should hide and disable the view so that we don't try and restore the visibility
3399 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3400 marketButton.setVisibility(View.GONE);
3401 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003402 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003403 }
3404
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003405 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003406 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3407 Resources r = getResources();
3408 Drawable marketIconDrawable = d.newDrawable(r);
3409 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3410 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3411 marketIconDrawable.setBounds(0, 0, w, h);
3412
3413 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003414 }
3415
Michael Jurkad7c28052012-04-27 15:43:36 -07003416 @Override
3417 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003418 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003419 final List<CharSequence> text = event.getText();
3420 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003421 // Populate event with a fake title based on the current state.
3422 if (mState == State.APPS_CUSTOMIZE) {
3423 text.add(getString(R.string.all_apps_button_label));
3424 } else {
3425 text.add(getString(R.string.all_apps_home_button_label));
3426 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003427 return result;
3428 }
3429
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003430 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003431 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003432 */
3433 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3434 @Override
3435 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003436 closeSystemDialogs();
3437 }
3438 }
3439
3440 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003441 * Receives notifications whenever the appwidgets are reset.
3442 */
3443 private class AppWidgetResetObserver extends ContentObserver {
3444 public AppWidgetResetObserver() {
3445 super(new Handler());
3446 }
3447
3448 @Override
3449 public void onChange(boolean selfChange) {
3450 onAppWidgetReset();
3451 }
3452 }
3453
3454 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003455 * If the activity is currently paused, signal that we need to run the passed Runnable
3456 * in onResume.
3457 *
3458 * This needs to be called from incoming places where resources might have been loaded
3459 * while we are paused. That is becaues the Configuration might be wrong
3460 * when we're not running, and if it comes back to what it was when we
3461 * were paused, we are not restarted.
3462 *
3463 * Implementation of the method from LauncherModel.Callbacks.
3464 *
3465 * @return true if we are currently paused. The caller might be able to
3466 * skip some work in that case since we will come back again.
3467 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003468 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003469 if (mPaused) {
3470 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003471 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003472 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003473 }
3474 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003475 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003476 return true;
3477 } else {
3478 return false;
3479 }
3480 }
3481
Michael Jurkac402cd92013-05-20 15:49:32 +02003482 private boolean waitUntilResume(Runnable run) {
3483 return waitUntilResume(run, false);
3484 }
3485
Michael Jurka1e2f4652013-07-08 18:03:46 -07003486 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003487 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003488 }
3489
Michael Jurka7607c2f2013-04-03 14:33:19 -07003490 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003491 * If the activity is currently paused, signal that we need to re-run the loader
3492 * in onResume.
3493 *
3494 * This needs to be called from incoming places where resources might have been loaded
3495 * while we are paused. That is becaues the Configuration might be wrong
3496 * when we're not running, and if it comes back to what it was when we
3497 * were paused, we are not restarted.
3498 *
3499 * Implementation of the method from LauncherModel.Callbacks.
3500 *
3501 * @return true if we are currently paused. The caller might be able to
3502 * skip some work in that case since we will come back again.
3503 */
3504 public boolean setLoadOnResume() {
3505 if (mPaused) {
3506 Log.i(TAG, "setLoadOnResume");
3507 mOnResumeNeedsLoad = true;
3508 return true;
3509 } else {
3510 return false;
3511 }
3512 }
3513
3514 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003516 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003517 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003518 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003519 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003520 } else {
3521 return SCREEN_COUNT / 2;
3522 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003524
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 /**
3526 * Refreshes the shortcuts shown on the workspace.
3527 *
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 */
3530 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003531 // If we're starting binding all over again, clear any bind calls we'd postponed in
3532 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3533 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003534 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003535
Winson Chungd64d1762013-08-20 14:37:16 -07003536 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003537 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003538 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003539
Michael Jurka05bf6442011-11-30 20:28:53 -08003540 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003541 if (mHotseat != null) {
3542 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 }
3544 }
3545
Adam Cohendcd297f2013-06-18 13:13:40 -07003546 @Override
3547 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003548 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003549
3550 // Create the new empty page
Winson Chung64359a52013-07-08 17:17:08 -07003551 mWorkspace.addExtraEmptyScreen();
Winson Chung0e6a7132013-08-23 12:55:10 -07003552
3553 // Create the custom content page (this call updates mDefaultScreen which calls
3554 // setCurrentPage() so ensure that all pages are added before calling this)
3555 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3556 mWorkspace.createCustomContentPage();
3557 }
Winson Chung64359a52013-07-08 17:17:08 -07003558 }
3559
3560 @Override
3561 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003562 int count = orderedScreenIds.size();
3563 for (int i = 0; i < count; i++) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003564 Launcher.addDumpLog(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")", true);
Adam Cohen89bddfa2013-08-20 11:57:13 -07003565 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003566 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003567 }
3568
Adam Cohen39a06042013-07-19 14:30:12 -07003569 private boolean shouldShowWeightWatcher() {
3570 String spKey = LauncherAppState.getSharedPreferencesKey();
3571 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003572 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003573
3574 return show;
3575 }
3576
Adam Cohen4caf2982013-08-20 18:54:31 -07003577 private boolean emailSent() {
3578 String spKey = LauncherAppState.getSharedPreferencesKey();
3579 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3580 boolean show = sp.getBoolean(CORRUPTION_EMAIL_SENT_KEY, false);
3581 return show;
3582 }
3583
3584 private void setEmailSent(boolean sent) {
3585 String spKey = LauncherAppState.getSharedPreferencesKey();
3586 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3587
3588 SharedPreferences.Editor editor = sp.edit();
3589 editor.putBoolean(CORRUPTION_EMAIL_SENT_KEY, sent);
3590 editor.commit();
3591 }
3592
Adam Cohen39a06042013-07-19 14:30:12 -07003593 private void toggleShowWeightWatcher() {
3594 String spKey = LauncherAppState.getSharedPreferencesKey();
3595 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3596 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3597
3598 show = !show;
3599
3600 SharedPreferences.Editor editor = sp.edit();
3601 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3602 editor.commit();
3603
3604 if (mWeightWatcher != null) {
3605 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3606 }
3607 }
3608
Winson Chungd64d1762013-08-20 14:37:16 -07003609 public void bindAppsAdded(final ArrayList<Long> newScreens,
3610 final ArrayList<ItemInfo> addNotAnimated,
3611 final ArrayList<ItemInfo> addAnimated) {
3612 Runnable r = new Runnable() {
3613 public void run() {
3614 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3615 }
3616 };
3617 if (waitUntilResume(r)) {
3618 return;
3619 }
3620
Adam Cohen4caf2982013-08-20 18:54:31 -07003621 Launcher.addDumpLog(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")", true);
Winson Chungd64d1762013-08-20 14:37:16 -07003622
3623 // Add the new screens
3624 bindAddScreens(newScreens);
3625
3626 // We add the items without animation on non-visible pages, and with
3627 // animations on the new page (which we will try and snap to).
3628 if (!addNotAnimated.isEmpty()) {
3629 bindItems(addNotAnimated, 0,
3630 addNotAnimated.size(), false);
3631 }
3632 if (!addAnimated.isEmpty()) {
3633 bindItems(addAnimated, 0,
3634 addAnimated.size(), true);
3635 }
3636 }
3637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003638 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 * Bind the items start-end from the list.
3640 *
3641 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003642 */
Winson Chung64359a52013-07-08 17:17:08 -07003643 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3644 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003645 Runnable r = new Runnable() {
3646 public void run() {
3647 bindItems(shortcuts, start, end, forceAnimateIcons);
3648 }
3649 };
3650 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003651 return;
3652 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003653
Winson Chungf0c6ae02012-03-21 16:10:31 -07003654 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003655 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3656 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003657 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003658 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003659 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003660 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003662
3663 // Short circuit if we are loading dock items for a configuration which has no dock
3664 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3665 mHotseat == null) {
3666 continue;
3667 }
3668
Joe Onorato9c1289c2009-08-17 11:03:03 -04003669 switch (item.itemType) {
3670 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3671 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003672 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003673 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003674
Winson Chung64359a52013-07-08 17:17:08 -07003675 /*
3676 * TODO: FIX collision case
3677 */
Winson Chungce376632013-07-11 16:12:41 -07003678 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3679 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3680 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3681 throw new RuntimeException("OCCUPIED");
3682 }
Winson Chung64359a52013-07-08 17:17:08 -07003683 }
3684
Adam Cohendcd297f2013-06-18 13:13:40 -07003685 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3686 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003687 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003688 // Animate all the applications up now
3689 shortcut.setAlpha(0f);
3690 shortcut.setScaleX(0f);
3691 shortcut.setScaleY(0f);
3692 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003693 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003696 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003697 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003698 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003699 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003700 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3701 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003703 default:
3704 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003706 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707
Winson Chung997a9232013-07-24 15:33:46 -07003708 if (animateIcons) {
3709 // Animate to the correct page
3710 if (newShortcutsScreenId > -1) {
3711 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3712 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3713 if (newShortcutsScreenId != currentScreenId) {
3714 mWorkspace.snapToPage(newScreenIndex);
3715 }
3716 }
3717
Winson Chung64359a52013-07-08 17:17:08 -07003718 // We post the animation slightly delayed to prevent slowdowns when we are loading
3719 // right after we return to launcher.
3720 mWorkspace.postDelayed(new Runnable() {
3721 public void run() {
3722 anim.playTogether(bounceAnims);
3723 anim.start();
3724 }
3725 }, NEW_APPS_ANIMATION_DELAY);
3726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 }
3729
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 /**
3731 * Implementation of the method from LauncherModel.Callbacks.
3732 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003733 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003734 Runnable r = new Runnable() {
3735 public void run() {
3736 bindFolders(folders);
3737 }
3738 };
3739 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003740 return;
3741 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003742 sFolders.clear();
3743 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003744 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003745
3746 /**
3747 * Add the views for a widget to the workspace.
3748 *
3749 * Implementation of the method from LauncherModel.Callbacks.
3750 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003751 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003752 Runnable r = new Runnable() {
3753 public void run() {
3754 bindAppWidget(item);
3755 }
3756 };
3757 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003758 return;
3759 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003760
Daniel Sandler843e8602010-06-07 14:59:01 -04003761 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3762 if (DEBUG_WIDGETS) {
3763 Log.d(TAG, "bindAppWidget: " + item);
3764 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 final Workspace workspace = mWorkspace;
3766
3767 final int appWidgetId = item.appWidgetId;
3768 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003769 if (DEBUG_WIDGETS) {
3770 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3771 }
3772
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3774
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003776 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003777
Adam Cohendcd297f2013-06-18 13:13:40 -07003778 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003780 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3781
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 workspace.requestLayout();
3783
Daniel Sandler843e8602010-06-07 14:59:01 -04003784 if (DEBUG_WIDGETS) {
3785 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3786 + (SystemClock.uptimeMillis()-start) + "ms");
3787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 }
3789
Adam Cohen1462de32012-07-24 22:34:36 -07003790 public void onPageBoundSynchronously(int page) {
3791 mSynchronouslyBoundPages.add(page);
3792 }
3793
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 /**
3795 * Callback saying that there aren't any more items to bind.
3796 *
3797 * Implementation of the method from LauncherModel.Callbacks.
3798 */
Adam Cohene25af792013-06-06 23:08:25 -07003799 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003800 Runnable r = new Runnable() {
3801 public void run() {
3802 finishBindingItems(upgradePath);
3803 }
3804 };
3805 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003806 return;
3807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 if (mSavedState != null) {
3809 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003810 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003811 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812 mSavedState = null;
3813 }
3814
Adam Cohen1462de32012-07-24 22:34:36 -07003815 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003817 // If we received the result of any pending adds while the loader was running (e.g. the
3818 // widget configuration forced an orientation change), process them now.
3819 for (int i = 0; i < sPendingAddList.size(); i++) {
3820 completeAdd(sPendingAddList.get(i));
3821 }
3822 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823
Winson Chungc51db6a2011-10-05 11:44:49 -07003824 // Update the market app icon as necessary (the other icons will be managed in response to
3825 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003826 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003827
Winson Chungf0c6ae02012-03-21 16:10:31 -07003828 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003829 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07003830 mWorkspace.getUniqueComponents(true, null);
3831 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07003832 }
Adam Cohen99894d92013-06-14 11:22:59 -07003833
Adam Cohen66a01fd2013-06-11 12:48:00 -07003834 mWorkspace.post(new Runnable() {
3835 @Override
3836 public void run() {
3837 onFinishBindingItems();
3838 }
3839 });
Adam Cohen4caf2982013-08-20 18:54:31 -07003840
3841 // Write all the logs to disk
3842 Launcher.addDumpLog(TAG, "10249126 - finishBindingItems() - dumping logs to disk", true);
3843 dumpLogsToLocalData(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003844 }
3845
Winson Chunga2413752012-04-03 14:22:34 -07003846 private boolean canRunNewAppsAnimation() {
3847 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3848 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3849 }
3850
Winson Chungc9168342013-06-26 14:54:55 -07003851 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3852 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3853 PropertyValuesHolder.ofFloat("alpha", 1f),
3854 PropertyValuesHolder.ofFloat("scaleX", 1f),
3855 PropertyValuesHolder.ofFloat("scaleY", 1f));
3856 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3857 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3858 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3859 return bounceAnim;
3860 }
3861
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003862 @Override
3863 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003864 boolean searchVisible = updateGlobalSearchIcon();
3865 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003866 if (mSearchDropTargetBar != null) {
3867 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3868 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003869 }
3870
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003871 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 * Add the icons for all apps.
3873 *
3874 * Implementation of the method from LauncherModel.Callbacks.
3875 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003876 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003877 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003878 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3879 getHotseat().addAllAppsFolder(mIconCache, apps,
3880 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3881 }
Winson Chung64359a52013-07-08 17:17:08 -07003882 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
3885
3886 /**
3887 * A package was updated.
3888 *
3889 * Implementation of the method from LauncherModel.Callbacks.
3890 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003891 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003892 Runnable r = new Runnable() {
3893 public void run() {
3894 bindAppsUpdated(apps);
3895 }
3896 };
3897 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003898 return;
3899 }
3900
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003901 if (mWorkspace != null) {
3902 mWorkspace.updateShortcuts(apps);
3903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003904 }
3905
3906 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003907 * A package was uninstalled. We take both the super set of packageNames
3908 * in addition to specific applications to remove, the reason being that
3909 * this can be called when a package is updated as well. In that scenario,
3910 * we only remove specific components from the workspace, where as
3911 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912 *
3913 * Implementation of the method from LauncherModel.Callbacks.
3914 */
Winson Chung83892cc2013-05-01 16:53:33 -07003915 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3916 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003917 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003918 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003919 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003920 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003921 }
Winson Chungd64d1762013-08-20 14:37:16 -07003922 };
3923 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003924 return;
3925 }
3926
Winson Chung64359a52013-07-08 17:17:08 -07003927 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003928 mWorkspace.removeItemsByPackageName(packageNames);
3929 } else {
3930 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003931 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003932
Winson Chunga1820962011-10-03 16:31:06 -07003933 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003934 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 }
Joe Onoratobe386092009-11-17 17:32:16 -08003936
3937 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003938 * A number of packages were updated.
3939 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003940 private ArrayList<Object> mWidgetsAndShortcuts;
3941 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003942 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003943 bindPackagesUpdated(mWidgetsAndShortcuts);
3944 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003945 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003946 };
3947
3948 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3949 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3950 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003951 return;
3952 }
3953
Winson Chung64359a52013-07-08 17:17:08 -07003954 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003955 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003956 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003957 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003958 }
3959
Winson Chung400438b2011-01-16 17:53:48 -08003960 private int mapConfigurationOriActivityInfoOri(int configOri) {
3961 final Display d = getWindowManager().getDefaultDisplay();
3962 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3963 switch (d.getRotation()) {
3964 case Surface.ROTATION_0:
3965 case Surface.ROTATION_180:
3966 // We are currently in the same basic orientation as the natural orientation
3967 naturalOri = configOri;
3968 break;
3969 case Surface.ROTATION_90:
3970 case Surface.ROTATION_270:
3971 // We are currently in the other basic orientation to the natural orientation
3972 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3973 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3974 break;
3975 }
3976
3977 int[] oriMap = {
3978 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3979 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3980 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3981 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3982 };
3983 // Since the map starts at portrait, we need to offset if this device's natural orientation
3984 // is landscape.
3985 int indexOffset = 0;
3986 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3987 indexOffset = 1;
3988 }
3989 return oriMap[(d.getRotation() + indexOffset) % 4];
3990 }
Adam Cohen446e9402011-09-15 18:21:21 -07003991
Winson Chung4b919f82012-05-01 10:44:08 -07003992 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003993 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003994 getResources().getBoolean(R.bool.allow_rotation);
3995 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003996 }
Winson Chung4b919f82012-05-01 10:44:08 -07003997 public void lockScreenOrientation() {
3998 if (isRotationEnabled()) {
3999 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4000 .getConfiguration().orientation));
4001 }
4002 }
4003 public void unlockScreenOrientation(boolean immediate) {
4004 if (isRotationEnabled()) {
4005 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004006 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004007 } else {
4008 mHandler.postDelayed(new Runnable() {
4009 public void run() {
4010 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4011 }
4012 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004013 }
Winson Chung4b919f82012-05-01 10:44:08 -07004014 }
Winson Chung400438b2011-01-16 17:53:48 -08004015 }
4016
Winson Chung82f55532011-08-09 14:14:23 -07004017 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004018 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004019 if (DISABLE_CLINGS) {
4020 return false;
4021 }
4022
Brett Chabot2a9e2282011-08-23 15:03:13 -07004023 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004024 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004025
Michael Jurkae233a8b2013-03-19 13:49:20 +01004026 // Restricted secondary users (child mode) will potentially have very few apps
4027 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004028// boolean supportsLimitedUsers =
4029// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4030// Account[] accounts = AccountManager.get(this).getAccounts();
4031// if (supportsLimitedUsers && accounts.length == 0) {
4032// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4033// Bundle restrictions = um.getUserRestrictions();
4034// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4035// return false;
4036// }
4037// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004038 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004039 }
Michael Jurka22143132012-10-04 17:42:38 +02004040
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004042 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004043 if (cling != null) {
4044 cling.init(this, positionData);
4045 cling.setVisibility(View.VISIBLE);
4046 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4047 if (animate) {
4048 cling.buildLayer();
4049 cling.setAlpha(0f);
4050 cling.animate()
4051 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004052 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004053 .setDuration(SHOW_CLING_DURATION)
4054 .setStartDelay(delay)
4055 .start();
4056 } else {
4057 cling.setAlpha(1f);
4058 }
Michael Jurka22143132012-10-04 17:42:38 +02004059 cling.setFocusableInTouchMode(true);
4060 cling.post(new Runnable() {
4061 public void run() {
4062 cling.setFocusable(true);
4063 cling.requestFocus();
4064 }
4065 });
4066 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4067 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 }
4069 return cling;
4070 }
Michael Jurka22143132012-10-04 17:42:38 +02004071
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004073 // To catch cases where siblings of top-level views are made invisible, just check whether
4074 // the cling is directly set to GONE before dismissing it.
4075 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004076 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004077 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004078 anim.addListener(new AnimatorListenerAdapter() {
4079 public void onAnimationEnd(Animator animation) {
4080 cling.setVisibility(View.GONE);
4081 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004082 // We should update the shared preferences on a background thread
4083 new Thread("dismissClingThread") {
4084 public void run() {
4085 SharedPreferences.Editor editor = mSharedPrefs.edit();
4086 editor.putBoolean(flag, true);
4087 editor.commit();
4088 }
4089 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004090 };
4091 });
4092 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004093 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004094 }
4095 }
Michael Jurka22143132012-10-04 17:42:38 +02004096
Winson Chung9d9d74f2011-09-19 11:49:12 -07004097 private void removeCling(int id) {
4098 final View cling = findViewById(id);
4099 if (cling != null) {
4100 final ViewGroup parent = (ViewGroup) cling.getParent();
4101 parent.post(new Runnable() {
4102 @Override
4103 public void run() {
4104 parent.removeView(cling);
4105 }
4106 });
Michael Jurka22143132012-10-04 17:42:38 +02004107 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004108 }
4109 }
Michael Jurka974c3862012-05-22 22:00:31 -07004110
4111 private boolean skipCustomClingIfNoAccounts() {
4112 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4113 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4114 if (customCling) {
4115 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004116 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004117 Account[] accounts = am.getAccountsByType("com.google");
4118 return accounts.length == 0;
4119 }
4120 return false;
4121 }
4122
Winson Chung7d7541e2011-09-16 20:14:36 -07004123 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004125 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004126 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4127 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004128 // If we're not using the default workspace layout, replace workspace cling
4129 // with a custom workspace cling (usually specified in an overlay)
4130 // For now, only do this on tablets
4131 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004132 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004133 // Use a custom cling
4134 View cling = findViewById(R.id.workspace_cling);
4135 ViewGroup clingParent = (ViewGroup) cling.getParent();
4136 int clingIndex = clingParent.indexOfChild(cling);
4137 clingParent.removeViewAt(clingIndex);
4138 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4139 clingParent.addView(customCling, clingIndex);
4140 customCling.setId(R.id.workspace_cling);
4141 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004143 } else {
4144 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004145 }
4146 }
4147 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004148 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004149 if (isClingsEnabled() &&
4150 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004151 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004152 } else {
4153 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004154 }
4155 }
4156 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004158 if (isClingsEnabled() &&
4159 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4160 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004161 } else {
4162 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004163 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004165 }
Michael Jurka104c4562013-07-08 18:03:46 -07004166 protected SharedPreferences getSharedPrefs() {
4167 return mSharedPrefs;
4168 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004169 public boolean isFolderClingVisible() {
4170 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004171 if (cling != null) {
4172 return cling.getVisibility() == View.VISIBLE;
4173 }
4174 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004175 }
Winson Chung82f55532011-08-09 14:14:23 -07004176 public void dismissWorkspaceCling(View v) {
4177 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004178 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004179 }
4180 public void dismissAllAppsCling(View v) {
4181 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004182 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4183 }
4184 public void dismissFolderCling(View v) {
4185 Cling cling = (Cling) findViewById(R.id.folder_cling);
4186 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004187 }
4188
Winson Chung80baf5a2010-08-09 16:03:15 -07004189 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004190 * Prints out out state for debugging.
4191 */
4192 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004193 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004194 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004195 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4196 Log.d(TAG, "mRestoring=" + mRestoring);
4197 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4198 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004199 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004200 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004201
Winson Chung785d2eb2011-04-14 16:08:02 -07004202 if (mAppsCustomizeContent != null) {
4203 mAppsCustomizeContent.dumpState();
4204 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004205 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004206 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004207
4208 @Override
4209 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4210 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004211 synchronized (sDumpLogs) {
4212 writer.println(" ");
4213 writer.println("Debug logs: ");
4214 for (int i = 0; i < sDumpLogs.size(); i++) {
4215 writer.println(" " + sDumpLogs.get(i));
4216 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004217 }
4218 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004219
4220 public static void dumpDebugLogsToConsole() {
Adam Cohen4caf2982013-08-20 18:54:31 -07004221 synchronized (sDumpLogs) {
4222 Log.d(TAG, "");
4223 Log.d(TAG, "*********************");
4224 Log.d(TAG, "Launcher debug logs: ");
4225 for (int i = 0; i < sDumpLogs.size(); i++) {
4226 Log.d(TAG, " " + sDumpLogs.get(i));
4227 }
4228 Log.d(TAG, "*********************");
4229 Log.d(TAG, "");
Adam Cohen487f7dd2012-06-28 18:12:10 -07004230 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004231 }
4232
4233 public static void addDumpLog(String tag, String log, boolean debugLog) {
4234 if (debugLog) {
4235 Log.d(tag, log);
4236 }
4237 sDateStamp.setTime(System.currentTimeMillis());
4238 synchronized (sDumpLogs) {
4239 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log);
4240 }
4241 }
4242
4243 public void dumpLogsToLocalData(final boolean email) {
4244 new Thread("DumpLogsToLocalData") {
4245 @Override
4246 public void run() {
4247 boolean success = false;
4248 sDateStamp.setTime(sRunStart);
4249 String FILENAME = sDateStamp.getMonth() + "-"
4250 + sDateStamp.getDay() + "_"
4251 + sDateStamp.getHours() + "-"
4252 + sDateStamp.getMinutes() + "_"
4253 + sDateStamp.getSeconds() + ".txt";
4254
4255 FileOutputStream fos = null;
4256 File outFile = null;
4257 try {
4258 outFile = new File(getFilesDir(), FILENAME);
4259 outFile.createNewFile();
4260 fos = new FileOutputStream(outFile);
4261 } catch (Exception e) {
4262 e.printStackTrace();
4263 }
4264 if (fos != null) {
4265 PrintWriter writer = new PrintWriter(fos);
4266
4267 writer.println(" ");
4268 writer.println("Debug logs: ");
4269 synchronized (sDumpLogs) {
4270 for (int i = 0; i < sDumpLogs.size(); i++) {
4271 writer.println(" " + sDumpLogs.get(i));
4272 }
4273 }
4274 writer.close();
4275 }
4276 try {
4277 if (fos != null) {
4278 fos.close();
4279 success = true;
4280 }
4281 } catch (IOException e) {
4282 e.printStackTrace();
4283 }
4284
4285 if (success && email) {
4286 if (!emailSent()) {
4287 emailFile(outFile);
4288 }
4289 }
4290 }
4291 }.start();
4292 }
4293
4294 private void emailFile(File file) {
4295 File publicCopy = new File(Environment.getExternalStorageDirectory(), file.getName());
4296 try {
4297 copyFile(file, publicCopy);
4298 } catch (IOException e) {
4299 e.printStackTrace();
4300 return;
4301 }
4302
4303 Intent intent = new Intent(Intent.ACTION_SEND);
4304 intent.setType("text/plain");
4305 intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"adamcohen@google.com, winsonc@google.com," +
4306 "mikejurka@google"});
4307 intent.putExtra(Intent.EXTRA_SUBJECT, "Data corruption " + file.getName());
4308 intent.putExtra(Intent.EXTRA_TEXT, "Data corruption has occurred, logs attached");
4309
4310 if (!file.exists() || !file.canRead()) {
4311 Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
4312 finish();
4313 return;
4314 }
4315
4316 Toast.makeText(this, "Data corruption has occurred, please send e-mail", Toast.LENGTH_LONG);
4317 Uri uri = Uri.fromFile(publicCopy);
4318 intent.putExtra(Intent.EXTRA_STREAM, uri);
4319 startActivity(Intent.createChooser(intent, "Please send logs, consider clearing data"));
4320
4321 setEmailSent(true);
4322 }
4323
4324 public void copyFile(File src, File dst) throws IOException {
4325 InputStream in = new FileInputStream(src);
4326 OutputStream out = new FileOutputStream(dst);
4327
4328 // Transfer bytes from in to out
4329 byte[] buf = new byte[1024];
4330 int len;
4331 while ((len = in.read(buf)) > 0) {
4332 out.write(buf, 0, len);
4333 }
4334 in.close();
4335 out.close();
Adam Cohen487f7dd2012-06-28 18:12:10 -07004336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004337}
Michael Jurka2763be32011-02-24 11:19:57 -08004338
Michael Jurkaabded662011-03-04 12:06:57 -08004339interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004340 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004341 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004342 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004343 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004344 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004345}