blob: 305f249d9251bae11b3a51abf8062d87401c7a11 [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 Cohen99894d92013-06-14 11:22:59 -070047import android.content.pm.ResolveInfo;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Craig Mautner360310b2012-10-26 15:13:08 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Craig Mautner360310b2012-10-26 15:13:08 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080068import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Daniel Sandler924b9932013-06-12 00:38:25 -040074import android.view.*;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080075import android.view.View.OnLongClickListener;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080076import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080077import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070078import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070079import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080080import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Daniel Sandler924b9932013-06-12 00:38:25 -040082import android.widget.*;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070083
Daniel Sandler325dc232013-06-05 22:57:57 -040084import com.android.launcher3.R;
85import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080086
Adam Cohenc0dcf592011-06-01 15:30:43 -070087import java.io.DataInputStream;
88import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070089import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070090import java.io.FileNotFoundException;
91import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070092import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -070093import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070094import java.util.Collection;
95import java.util.Collections;
96import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070097import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -070098import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -070099import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700100import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102/**
103 * Default launcher application.
104 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100105public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700106 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700107 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800108 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700109 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Daniel Sandler924b9932013-06-12 00:38:25 -0400111 static final boolean PROFILE_STARTUP = true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400112 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700113 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandler924b9932013-06-12 00:38:25 -0400114 static final boolean DEBUG_RESUME_TIME = true;
115 static final boolean DEBUG_MEMORY = true;
Romain Guy6fefcf12009-06-11 13:07:43 -0700116
Winson Chung70d72102011-08-12 11:24:35 -0700117 private static final int MENU_GROUP_WALLPAPER = 1;
118 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
119 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700120 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
121 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int REQUEST_PICK_APPLICATION = 6;
126 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700128 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Michael Jurka8b805b12012-04-18 14:23:14 -0700130 private static final int REQUEST_BIND_APPWIDGET = 11;
131
Mathew Inwood876a8462013-06-14 14:12:41 +0100132 /**
133 * IntentStarter uses request codes starting with this. This must be greater than all activity
134 * request codes used internally.
135 */
136 protected static final int REQUEST_LAST = 100;
137
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
139
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800140 static final int SCREEN_COUNT = 5;
141 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Romain Guy98d01652009-06-30 16:21:04 -0700143 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800144 // To turn on these properties, type
145 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
146 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
147 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Winson Chung2672ff92012-05-04 16:22:30 -0700149 // The Intent extra that defines whether to ignore the launch animation
150 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400151 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
154 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700155 // Type: int
156 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
159 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
161 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
171 // Type: int
172 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
173 // Type: parcelable
174 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100176 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700177 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100178 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700179 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100180 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700181
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700183 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700184 private State mState = State.WORKSPACE;
185 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800186 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700189 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
190 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700191 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700192 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800195 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
Winson Chunga2413752012-04-03 14:22:34 -0700197 // How long to wait before the new-shortcut animation automatically pans the workspace
198 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
199
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800200 private final BroadcastReceiver mCloseSystemDialogsReceiver
201 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800202 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private LayoutInflater mInflater;
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800207 private View mQsbDivider;
208 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700209 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800210 private DragLayer mDragLayer;
211 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private AppWidgetManager mAppWidgetManager;
214 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Michael Jurkac9d95c52011-08-29 14:03:34 -0700216 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700217 private AppWidgetProviderInfo mPendingAddWidgetInfo;
218
Michael Jurka0280c3b2010-09-17 15:00:07 -0700219 private int[] mTmpAddItemCellCoordinates = new int[2];
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private FolderInfo mFolderInfo;
222
Winson Chung3d503fb2011-07-13 17:25:49 -0700223 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800224 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700225
Winson Chungc51db6a2011-10-05 11:44:49 -0700226 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700227 private AppsCustomizeTabHost mAppsCustomizeTabHost;
228 private AppsCustomizePagedView mAppsCustomizeContent;
229 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700232 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
233 // scroll issues (because the workspace may not have been measured yet) and extra work.
234 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
235 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
237 private SpannableStringBuilder mDefaultKeySsb = null;
238
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239 private boolean mWorkspaceLoading = true;
240
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800241 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private boolean mRestoring;
243 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700244 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
Michael Jurka7607c2f2013-04-03 14:33:19 -0700246 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
247
Winson Chung4a2afa32012-07-19 14:53:05 -0700248 // Keep track of whether the user has left launcher
249 private static boolean sPausedFromUserAction = false;
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private Bundle mSavedInstanceState;
252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800255 private boolean mUserPresent = true;
256 private boolean mVisible = false;
257 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700258 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700260 private static LocaleConfiguration sLocaleConfiguration = null;
261
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700262 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700264 private Intent mAppMarketIntent = null;
265
Adam Cohended9f8d2010-11-03 13:25:16 -0700266 // Related to the auto-advancing of widgets
267 private final int ADVANCE_MSG = 1;
268 private final int mAdvanceInterval = 20000;
269 private final int mAdvanceStagger = 250;
270 private long mAutoAdvanceSentTime;
271 private long mAutoAdvanceTimeLeft = -1;
272 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
273 new HashMap<View, AppWidgetProviderInfo>();
274
Winson Chung400438b2011-01-16 17:53:48 -0800275 // Determines how long to wait after a rotation before restoring the screen orientation to
276 // match the sensor state.
277 private final int mRestoreScreenOrientationDelay = 500;
278
Michael Jurka4ef207b2010-11-29 17:05:45 -0800279 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700280 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
282 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800283
Craig Mautner360310b2012-10-26 15:13:08 -0700284 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700285
Adam Cohen1462de32012-07-24 22:34:36 -0700286 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
287
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700288 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
289
Winson Chung46353de2012-02-16 14:05:10 -0800290 // We only want to get the SharedPreferences once since it does an FS stat each time we get
291 // it from the context.
292 private SharedPreferences mSharedPrefs;
293
Adam Cohene25af792013-06-06 23:08:25 -0700294 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
295
Winson Chungf0c6ae02012-03-21 16:10:31 -0700296 // Holds the page that we need to animate to, and the icon views that we need to animate up
297 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700298 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700299 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700300 private ImageView mFolderIconImageView;
301 private Bitmap mFolderIconBitmap;
302 private Canvas mFolderIconCanvas;
303 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700304
Michael Jurkaddd62e92011-02-16 17:49:14 -0800305 private BubbleTextView mWaitingForResume;
306
Michael Jurka22143132012-10-04 17:42:38 +0200307 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
308 = new HideFromAccessibilityHelper();
309
Michael Jurkac1f5d262011-09-30 19:32:27 -0700310 private Runnable mBuildLayersRunnable = new Runnable() {
311 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700312 if (mWorkspace != null) {
313 mWorkspace.buildPageHardwareLayers();
314 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700315 }
316 };
317
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800318 private static ArrayList<PendingAddArguments> sPendingAddList
319 = new ArrayList<PendingAddArguments>();
320
Michael Jurka0a457bf2012-11-19 14:05:05 -0800321 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
322
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800323 private static class PendingAddArguments {
324 int requestCode;
325 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700326 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700327 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800328 int cellX;
329 int cellY;
330 }
331
Michael Jurka0a457bf2012-11-19 14:05:05 -0800332 private static boolean isPropertyEnabled(String propertyName) {
333 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700334 }
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 @Override
337 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700338 if (DEBUG_STRICT_MODE) {
339 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
340 .detectDiskReads()
341 .detectDiskWrites()
342 .detectNetwork() // or .detectAll() for all detectable problems
343 .penaltyLog()
344 .build());
345 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
346 .detectLeakedSqlLiteObjects()
347 .detectLeakedClosableObjects()
348 .penaltyLog()
349 .penaltyDeath()
350 .build());
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400354 LauncherAppState app = LauncherAppState.getInstance();
355 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700356 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 mModel = app.setLauncher(this);
358 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400359 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
364 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700366 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
367 // this also ensures that any synchronous binding below doesn't re-trigger another
368 // LauncherModel load.
369 mPaused = false;
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200372 android.os.Debug.startMethodTracing(
373 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 }
375
376 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 setContentView(R.layout.launcher);
378 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700379 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800381 registerContentObservers();
382
Joe Onorato7c312c12009-08-13 21:36:53 -0700383 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mSavedState = savedInstanceState;
386 restoreState(mSavedState);
387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700389 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200390 mAppsCustomizeContent.onPackagesUpdated(
391 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700392 }
Winson Chunga12a2502010-12-20 14:41:35 -0800393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 if (PROFILE_STARTUP) {
395 android.os.Debug.stopMethodTracing();
396 }
397
398 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700399 if (sPausedFromUserAction) {
400 // If the user leaves launcher, then we should just load items asynchronously when
401 // they return.
402 mModel.startLoader(true, -1);
403 } else {
404 // We only load the page synchronously if the user rotates (or triggers a
405 // configuration change) while launcher is in the foreground
406 mModel.startLoader(true, mWorkspace.getCurrentPage());
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
Michael Jurkac57b7a82011-08-09 22:02:20 -0700410 if (!mModel.isAllAppsLoaded()) {
411 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
412 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 // For handling default keys
416 mDefaultKeySsb = new SpannableStringBuilder();
417 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800418
419 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
420 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800421
Adam Cohenf9426d52012-06-04 17:26:21 -0700422 updateGlobalIcons();
423
424 // On large interfaces, we want the screen to auto-rotate based on the current orientation
425 unlockScreenOrientation(true);
426 }
427
Winson Chung4a2afa32012-07-19 14:53:05 -0700428 protected void onUserLeaveHint() {
429 super.onUserLeaveHint();
430 sPausedFromUserAction = true;
431 }
432
Adam Cohenf9426d52012-06-04 17:26:21 -0700433 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700434 boolean searchVisible = false;
435 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800436 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700437 int coi = getCurrentOrientationIndexForGlobalIcons();
438 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
439 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700440 updateAppMarketIcon();
441 searchVisible = updateGlobalSearchIcon();
442 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700444 if (sGlobalSearchIcon[coi] != null) {
445 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700447 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 if (sVoiceSearchIcon[coi] != null) {
449 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700450 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700452 if (sAppMarketIcon[coi] != null) {
453 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800454 }
Winson Chungadf0c182012-08-23 14:59:07 -0700455 if (mSearchDropTargetBar != null) {
456 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
Romain Guycbb89e42009-06-08 15:52:54 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700461 if (sLocaleConfiguration == null) {
462 new AsyncTask<Void, Void, LocaleConfiguration>() {
463 @Override
464 protected LocaleConfiguration doInBackground(Void... unused) {
465 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
466 readConfiguration(Launcher.this, localeConfiguration);
467 return localeConfiguration;
468 }
469
470 @Override
471 protected void onPostExecute(LocaleConfiguration result) {
472 sLocaleConfiguration = result;
473 checkForLocaleChange(); // recursive, but now with a locale configuration
474 }
475 }.execute();
476 return;
477 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final Configuration configuration = getResources().getConfiguration();
480
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final String locale = configuration.locale.toString();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final int mcc = configuration.mcc;
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mnc = configuration.mnc;
489
Romain Guy84f296c2009-11-04 15:00:44 -0800490 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493 sLocaleConfiguration.locale = locale;
494 sLocaleConfiguration.mcc = mcc;
495 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700496
Joe Onorato0589f0f2010-02-08 13:44:00 -0800497 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498
499 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
500 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700501 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700502 public void run() {
503 writeConfiguration(Launcher.this, localeConfiguration);
504 }
505 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700506 }
507 }
508
509 private static class LocaleConfiguration {
510 public String locale;
511 public int mcc = -1;
512 public int mnc = -1;
513 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700514
Romain Guy98d01652009-06-30 16:21:04 -0700515 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
516 DataInputStream in = null;
517 try {
518 in = new DataInputStream(context.openFileInput(PREFERENCES));
519 configuration.locale = in.readUTF();
520 configuration.mcc = in.readInt();
521 configuration.mnc = in.readInt();
522 } catch (FileNotFoundException e) {
523 // Ignore
524 } catch (IOException e) {
525 // Ignore
526 } finally {
527 if (in != null) {
528 try {
529 in.close();
530 } catch (IOException e) {
531 // Ignore
532 }
533 }
534 }
535 }
536
537 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
538 DataOutputStream out = null;
539 try {
540 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
541 out.writeUTF(configuration.locale);
542 out.writeInt(configuration.mcc);
543 out.writeInt(configuration.mnc);
544 out.flush();
545 } catch (FileNotFoundException e) {
546 // Ignore
547 } catch (IOException e) {
548 //noinspection ResultOfMethodCallIgnored
549 context.getFileStreamPath(PREFERENCES).delete();
550 } finally {
551 if (out != null) {
552 try {
553 out.close();
554 } catch (IOException e) {
555 // Ignore
556 }
557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 }
559 }
560
Bjorn Bringertc459e522013-06-07 19:36:01 +0100561 public LayoutInflater getInflater() {
562 return mInflater;
563 }
564
Adam Cohen716b51e2011-06-30 12:09:54 -0700565 public DragLayer getDragLayer() {
566 return mDragLayer;
567 }
568
Winson Chung36a62fe2012-05-06 18:04:42 -0700569 boolean isDraggingEnabled() {
570 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
571 // that is subsequently removed from the workspace in startBinding().
572 return !mModel.isLoadingWorkspace();
573 }
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 static int getScreen() {
576 synchronized (sLock) {
577 return sScreen;
578 }
579 }
580
581 static void setScreen(int screen) {
582 synchronized (sLock) {
583 sScreen = screen;
584 }
585 }
586
Winson Chung557d6ed2011-07-08 15:34:52 -0700587 /**
588 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
589 * a configuration step, this allows the proper animations to run after other transitions.
590 */
591 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700592 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 switch (args.requestCode) {
594 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700595 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700596 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 break;
598 case REQUEST_PICK_SHORTCUT:
599 processShortcut(args.intent);
600 break;
601 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700602 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700603 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700604 result = true;
605 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 case REQUEST_CREATE_APPWIDGET:
607 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700608 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700609 result = true;
610 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 case REQUEST_PICK_WALLPAPER:
612 // We just wanted the activity result here so we can clear mWaitingForResult
613 break;
614 }
Michael Jurka27614d22012-04-02 06:40:22 -0700615 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
616 // if you turned the screen off and then back while in All Apps, Launcher would not
617 // return to the workspace. Clearing mAddInfo.container here fixes this issue
618 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700619 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700623 protected void onActivityResult(
624 final int requestCode, final int resultCode, final Intent data) {
625 if (requestCode == REQUEST_BIND_APPWIDGET) {
626 int appWidgetId = data != null ?
627 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
628 if (resultCode == RESULT_CANCELED) {
629 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
630 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700631 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700632 }
633 return;
634 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700635 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
637 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700638 mWaitingForResult = false;
639
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 // We have special handling for widgets
641 if (isWidgetDrop) {
642 int appWidgetId = data != null ?
643 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700644 if (appWidgetId < 0) {
645 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
646 "widget configuration activity.");
647 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
648 } else {
649 completeTwoStageWidgetDrop(resultCode, appWidgetId);
650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 return;
652 }
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // The pattern used here is that a user PICKs a specific application,
655 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
658 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700659 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 final PendingAddArguments args = new PendingAddArguments();
661 args.requestCode = requestCode;
662 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700664 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 args.cellX = mPendingAddInfo.cellX;
666 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 if (isWorkspaceLocked()) {
668 sPendingAddList.add(args);
669 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
676 null);
677 }
678
679 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 Runnable onCompleteRunnable = null;
683 int animationType = 0;
684
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700685 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 if (resultCode == RESULT_OK) {
687 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
688 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700689 mPendingAddWidgetInfo);
690 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 onCompleteRunnable = new Runnable() {
692 @Override
693 public void run() {
694 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
697 null);
698 }
699 };
700 } else if (resultCode == RESULT_CANCELED) {
701 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
702 onCompleteRunnable = new Runnable() {
703 @Override
704 public void run() {
705 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
706 null);
707 }
708 };
709 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 if (mDragLayer.getAnimatedView() != null) {
711 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
712 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
713 animationType, boundWidget, true);
714 } else {
715 // The animated view may be null in the case of a rotation during widget configuration
716 onCompleteRunnable.run();
717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 }
719
720 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700721 protected void onStop() {
722 super.onStop();
723 FirstFrameAnimatorHelper.setIsVisible(false);
724 }
725
726 @Override
727 protected void onStart() {
728 super.onStart();
729 FirstFrameAnimatorHelper.setIsVisible(true);
730 }
731
732 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200734 long startTime = 0;
735 if (DEBUG_RESUME_TIME) {
736 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400737 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700740
Winson Chung4a2afa32012-07-19 14:53:05 -0700741 // Restore the previous launcher state
742 if (mOnResumeState == State.WORKSPACE) {
743 showWorkspace(false);
744 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
745 showAllApps(false);
746 }
747 mOnResumeState = State.NONE;
748
Craig Mautner360310b2012-10-26 15:13:08 -0700749 // Background was set to gradient in onPause(), restore to black if in all apps.
750 setWorkspaceBackground(mState == State.WORKSPACE);
751
Winson Chungde0fb8f2012-05-08 14:37:08 -0700752 // Process any items that were added while Launcher was away
753 InstallShortcutReceiver.flushInstallQueue(this);
754
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800755 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700756 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700757 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700759 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700761 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200763 if (mOnResumeCallbacks.size() > 0) {
764 // We might have postponed some bind calls until onResume (see waitUntilResume) --
765 // execute them here
766 long startTimeCallbacks = 0;
767 if (DEBUG_RESUME_TIME) {
768 startTimeCallbacks = System.currentTimeMillis();
769 }
770
771 if (mAppsCustomizeContent != null) {
772 mAppsCustomizeContent.setBulkBind(true);
773 }
774 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
775 mOnResumeCallbacks.get(i).run();
776 }
777 if (mAppsCustomizeContent != null) {
778 mAppsCustomizeContent.setBulkBind(false);
779 }
780 mOnResumeCallbacks.clear();
781 if (DEBUG_RESUME_TIME) {
782 Log.d(TAG, "Time spent processing callbacks in onResume: " +
783 (System.currentTimeMillis() - startTimeCallbacks));
784 }
Michael Jurka447bf842013-05-15 14:52:15 +0200785 }
Winson Chunge4e50662012-01-23 14:45:13 -0800786
787 // Reset the pressed state of icons that were locked in the press state while activities
788 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800789 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800790 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800791 mWaitingForResume.setStayPressed(false);
792 }
Winson Chunge4e50662012-01-23 14:45:13 -0800793 if (mAppsCustomizeContent != null) {
794 // Resets the previous all apps icon press state
795 mAppsCustomizeContent.resetDrawableState();
796 }
Adam Cohen06dff352012-06-01 17:17:08 -0700797 // It is possible that widgets can receive updates while launcher is not in the foreground.
798 // Consequently, the widgets will be inflated in the orientation of the foreground activity
799 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
800 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700801 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700802
803 // Again, as with the above scenario, it's possible that one or more of the global icons
804 // were updated in the wrong orientation.
805 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200806 if (DEBUG_RESUME_TIME) {
807 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
808 }
Adam Cohen06dff352012-06-01 17:17:08 -0700809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700812 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700813 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
814 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
815 // when Launcher resumes and we are still in AllApps.
816 updateWallpaperVisibility(true);
817
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700819 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800820 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700821 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 }
Romain Guycbb89e42009-06-08 15:52:54 -0700823
Adam Cohen66a01fd2013-06-11 12:48:00 -0700824 protected void onFinishBindingItems() {
825 }
826
827 // Add a fullscreen unpadded view to the workspace to the left all other screens.
828 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700829 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700830 }
831
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700832 @Override
833 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 // Flag the loader to stop early before switching
835 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700836 if (mAppsCustomizeContent != null) {
837 mAppsCustomizeContent.surrender();
838 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800839 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700840 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700841
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800842 // We can't hide the IME if it was forced open. So don't bother
843 /*
844 @Override
845 public void onWindowFocusChanged(boolean hasFocus) {
846 super.onWindowFocusChanged(hasFocus);
847
848 if (hasFocus) {
849 final InputMethodManager inputManager = (InputMethodManager)
850 getSystemService(Context.INPUT_METHOD_SERVICE);
851 WindowManager.LayoutParams lp = getWindow().getAttributes();
852 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
853 android.os.Handler()) {
854 protected void onReceiveResult(int resultCode, Bundle resultData) {
855 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
856 }
857 });
858 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
859 }
860 }
861 */
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 private boolean acceptFilter() {
864 final InputMethodManager inputManager = (InputMethodManager)
865 getSystemService(Context.INPUT_METHOD_SERVICE);
866 return !inputManager.isFullscreenMode();
867 }
868
869 @Override
870 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700871 final int uniChar = event.getUnicodeChar();
872 final boolean handled = super.onKeyDown(keyCode, event);
873 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
874 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
876 keyCode, event);
877 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700878 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700879 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700880 // showSearchDialog()
881 // If there are multiple keystrokes before the search dialog takes focus,
882 // onSearchRequested() will be called for every keystroke,
883 // but it is idempotent, so it's fine.
884 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
887
Joe Onorato8a9625e2010-01-28 15:55:35 -0800888 // Eat the long press event so the keyboard doesn't come up.
889 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
890 return true;
891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 return handled;
894 }
895
Karl Rosaen138a0412009-04-23 19:00:21 -0700896 private String getTypedText() {
897 return mDefaultKeySsb.toString();
898 }
899
900 private void clearTypedText() {
901 mDefaultKeySsb.clear();
902 mDefaultKeySsb.clearSpans();
903 Selection.setSelection(mDefaultKeySsb, 0);
904 }
905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700907 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
908 * State
909 */
910 private static State intToState(int stateOrdinal) {
911 State state = State.WORKSPACE;
912 final State[] stateValues = State.values();
913 for (int i = 0; i < stateValues.length; i++) {
914 if (stateValues[i].ordinal() == stateOrdinal) {
915 state = stateValues[i];
916 break;
917 }
918 }
919 return state;
920 }
921
922 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 * Restores the previous state, if it exists.
924 *
925 * @param savedState The previous state.
926 */
927 private void restoreState(Bundle savedState) {
928 if (savedState == null) {
929 return;
930 }
931
Michael Jurka883f55b2010-10-21 15:47:14 -0700932 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700933 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700934 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800935 }
936
Winson Chungf0c6ae02012-03-21 16:10:31 -0700937 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700939 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
946 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
949 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
951 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
952 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700953 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 mRestoring = true;
955 }
956
957 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
958 if (renameFolder) {
959 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700960 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 mRestoring = true;
962 }
Winson Chunga12a2502010-12-20 14:41:35 -0800963
Winson Chung785d2eb2011-04-14 16:08:02 -0700964 // Restore the AppsCustomize tab
965 if (mAppsCustomizeTabHost != null) {
966 String curTab = savedState.getString("apps_customize_currentTab");
967 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700968 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700970 mAppsCustomizeContent.loadAssociatedPages(
971 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 }
973
Winson Chung5afbf7b2011-07-25 11:53:08 -0700974 int currentIndex = savedState.getInt("apps_customize_currentIndex");
975 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Finds all the views we need and configure them properly.
981 */
982 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700983 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400984
Craig Mautner360310b2012-10-26 15:13:08 -0700985 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700986 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
987 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700988 mQsbDivider = findViewById(R.id.qsb_divider);
989 mDockDivider = findViewById(R.id.dock_divider);
990
991 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
992 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Winson Chung4c98d922011-05-31 16:50:48 -0700994 // Setup the drag layer
995 mDragLayer.setup(this, dragController);
996
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 // Setup the hotseat
998 mHotseat = (Hotseat) findViewById(R.id.hotseat);
999 if (mHotseat != null) {
1000 mHotseat.setup(this);
1001 }
1002
Winson Chung4c98d922011-05-31 16:50:48 -07001003 // Setup the workspace
1004 mWorkspace.setHapticFeedbackEnabled(false);
1005 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001006 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001007 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001008
Winson Chungf0ea4d32011-06-06 14:27:16 -07001009 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001010 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001011
Winson Chungf0ea4d32011-06-06 14:27:16 -07001012 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001013 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001014 mAppsCustomizeContent = (AppsCustomizePagedView)
1015 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1016 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001017
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001019 dragController.setDragScoller(mWorkspace);
1020 dragController.setScrollView(mDragLayer);
1021 dragController.setMoveTarget(mWorkspace);
1022 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001023 if (mSearchDropTargetBar != null) {
1024 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001025 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001026
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001027 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001028 Log.v(TAG, "adding WeightWatcher");
1029 ((FrameLayout) mLauncherView).addView(new WeightWatcher(this),
1030 new FrameLayout.LayoutParams(
1031 FrameLayout.LayoutParams.MATCH_PARENT,
1032 44,
1033 Gravity.BOTTOM)
1034 );
1035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 /**
1039 * Creates a view representing a shortcut.
1040 *
1041 * @param info The data structure describing the shortcut.
1042 *
1043 * @return A View inflated from R.layout.application.
1044 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001045 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001047 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 }
1049
1050 /**
1051 * Creates a view representing a shortcut inflated from the specified resource.
1052 *
1053 * @param layoutResId The id of the XML layout used to create the shortcut.
1054 * @param parent The group the shortcut belongs to.
1055 * @param info The data structure describing the shortcut.
1056 *
1057 * @return A View inflated from layoutResId.
1058 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001059 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001060 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1061 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 return favorite;
1064 }
1065
1066 /**
1067 * Add an application shortcut to the workspace.
1068 *
1069 * @param data The intent describing the application.
1070 * @param cellInfo The position on screen where to create the shortcut.
1071 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001072 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001074 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075
Adam Cohenfbba09b2011-07-18 21:43:05 -07001076 // First we check if we already know the exact location where we want to add this item.
1077 if (cellX >= 0 && cellY >= 0) {
1078 cellXY[0] = cellX;
1079 cellXY[1] = cellY;
1080 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001081 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 return;
1083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001085 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086
Romain Guy73b979d2009-06-09 12:57:21 -07001087 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001092 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001093 } else {
1094 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
1096 }
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 /**
1099 * Add a shortcut to the workspace.
1100 *
1101 * @param data The intent describing the shortcut.
1102 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001105 int cellY) {
1106 int[] cellXY = mTmpAddItemCellCoordinates;
1107 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001108 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Michael Jurkad3ef3062010-11-23 16:23:58 -08001110 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001111
Adam Cohen558baaf2011-08-15 15:22:57 -07001112 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001113 if (info == null) {
1114 return;
1115 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001116 final View view = createShortcut(info);
1117
Adam Cohenfbba09b2011-07-18 21:43:05 -07001118 // First we check if we already know the exact location where we want to add this item.
1119 if (cellX >= 0 && cellY >= 0) {
1120 cellXY[0] = cellX;
1121 cellXY[1] = cellY;
1122 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001123
1124 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001125 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001126 true, null,null)) {
1127 return;
1128 }
1129 DragObject dragObject = new DragObject();
1130 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001131 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1132 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001133 return;
1134 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001135 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001136 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001138 foundCellSpan = (result != null);
1139 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001140 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001141 }
1142
1143 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001144 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145 return;
1146 }
1147
Adam Cohendcd297f2013-06-18 13:13:40 -07001148 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
1150 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001151 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001152 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154 }
1155
Adam Cohen2f093b62012-04-30 18:59:53 -07001156 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1157 int minHeight) {
1158 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001159 // We want to account for the extra amount of padding that we are adding to the widget
1160 // to ensure that it gets the full amount of space that it has requested
1161 int requiredWidth = minWidth + padding.left + padding.right;
1162 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001163 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001164 }
1165
Adam Cohen2f093b62012-04-30 18:59:53 -07001166 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1167 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001168 }
1169
Adam Cohen2f093b62012-04-30 18:59:53 -07001170 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1171 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001172 }
1173
Adam Cohen2f093b62012-04-30 18:59:53 -07001174 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1175 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001176 }
1177
Adam Cohen2f093b62012-04-30 18:59:53 -07001178 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1179 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001180 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001181 }
1182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001186 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001187 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001189 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001190 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1191 if (appWidgetInfo == null) {
1192 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001196 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001197
Adam Cohen2f093b62012-04-30 18:59:53 -07001198 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1199 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001202 // We have saved the position to which the widget was dragged-- this really only matters
1203 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001204 int[] cellXY = mTmpAddItemCellCoordinates;
1205 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001208 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1209 cellXY[0] = mPendingAddInfo.cellX;
1210 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001211 spanXY[0] = mPendingAddInfo.spanX;
1212 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001213 foundCellSpan = true;
1214 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001216 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1218 spanXY[1], cellXY, finalSpan);
1219 spanXY[0] = finalSpan[0];
1220 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001221 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001224 }
1225
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001227 if (appWidgetId != -1) {
1228 // Deleting an app widget ID is a void call but writes to disk before returning
1229 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001230 new Thread("deleteAppWidgetId") {
1231 public void run() {
1232 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1233 }
1234 }.start();
1235 }
Winson Chung93eef082012-03-23 15:59:27 -07001236 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001237 return;
1238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001240 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001241 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1242 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001243 launcherInfo.spanX = spanXY[0];
1244 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1246 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001249 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250
1251 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001252 if (hostView == null) {
1253 // Perform actual inflation because we're live
1254 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1255 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1256 } else {
1257 // The AppWidgetHostView has already been inflated and instantiated
1258 launcherInfo.hostView = hostView;
1259 }
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001262 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001263 launcherInfo.notifyWidgetSizeChanged(this);
1264
Adam Cohendcd297f2013-06-18 13:13:40 -07001265 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001267
1268 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001270 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
Romain Guycbb89e42009-06-08 15:52:54 -07001272
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1274 @Override
1275 public void onReceive(Context context, Intent intent) {
1276 final String action = intent.getAction();
1277 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1278 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001279 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001280 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001281
Winson Chungc100e8e2011-08-09 16:02:43 -07001282 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001283 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001284 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1285 mAppsCustomizeTabHost.reset();
1286 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001287 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1289 mUserPresent = true;
1290 updateRunning();
1291 }
1292 }
1293 };
1294
1295 @Override
1296 public void onAttachedToWindow() {
1297 super.onAttachedToWindow();
1298
1299 // Listen for broadcasts related to user-presence
1300 final IntentFilter filter = new IntentFilter();
1301 filter.addAction(Intent.ACTION_SCREEN_OFF);
1302 filter.addAction(Intent.ACTION_USER_PRESENT);
1303 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001304 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001305 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001306 mVisible = true;
1307 }
1308
1309 @Override
1310 public void onDetachedFromWindow() {
1311 super.onDetachedFromWindow();
1312 mVisible = false;
1313
Adam Cohend113e0c2010-11-11 10:48:05 -08001314 if (mAttached) {
1315 unregisterReceiver(mReceiver);
1316 mAttached = false;
1317 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001318 updateRunning();
1319 }
1320
1321 public void onWindowVisibilityChanged(int visibility) {
1322 mVisible = visibility == View.VISIBLE;
1323 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001324 // The following code used to be in onResume, but it turns out onResume is called when
1325 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1326 // is a more appropriate event to handle
1327 if (mVisible) {
1328 mAppsCustomizeTabHost.onWindowVisible();
1329 if (!mWorkspaceLoading) {
1330 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001331 // We want to let Launcher draw itself at least once before we force it to build
1332 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001333 // apps is nice and speedy.
1334 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001335 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001336 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001337 if (mStarted) return;
1338 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001339 // We delay the layer building a bit in order to give
1340 // other message processing a time to run. In particular
1341 // this avoids a delay in hiding the IME if it was
1342 // currently shown, because doing that may involve
1343 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001344 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001345 final ViewTreeObserver.OnDrawListener listener = this;
1346 mWorkspace.post(new Runnable() {
1347 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001348 if (mWorkspace != null &&
1349 mWorkspace.getViewTreeObserver() != null) {
1350 mWorkspace.getViewTreeObserver().
1351 removeOnDrawListener(listener);
1352 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001353 }
1354 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001355 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001356 }
1357 });
1358 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001359 // When Launcher comes back to foreground, a different Activity might be responsible for
1360 // the app market intent, so refresh the icon
1361 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001362 clearTypedText();
1363 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 }
1365
1366 private void sendAdvanceMessage(long delay) {
1367 mHandler.removeMessages(ADVANCE_MSG);
1368 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1369 mHandler.sendMessageDelayed(msg, delay);
1370 mAutoAdvanceSentTime = System.currentTimeMillis();
1371 }
1372
1373 private void updateRunning() {
1374 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1375 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1376 mAutoAdvanceRunning = autoAdvanceRunning;
1377 if (autoAdvanceRunning) {
1378 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1379 sendAdvanceMessage(delay);
1380 } else {
1381 if (!mWidgetsToAdvance.isEmpty()) {
1382 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1383 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1384 }
1385 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001386 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001387 }
1388 }
1389 }
1390
1391 private final Handler mHandler = new Handler() {
1392 @Override
1393 public void handleMessage(Message msg) {
1394 if (msg.what == ADVANCE_MSG) {
1395 int i = 0;
1396 for (View key: mWidgetsToAdvance.keySet()) {
1397 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1398 final int delay = mAdvanceStagger * i;
1399 if (v instanceof Advanceable) {
1400 postDelayed(new Runnable() {
1401 public void run() {
1402 ((Advanceable) v).advance();
1403 }
1404 }, delay);
1405 }
1406 i++;
1407 }
1408 sendAdvanceMessage(mAdvanceInterval);
1409 }
1410 }
1411 };
1412
1413 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001414 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001415 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1416 if (v instanceof Advanceable) {
1417 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001418 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001419 updateRunning();
1420 }
1421 }
1422
1423 void removeWidgetToAutoAdvance(View hostView) {
1424 if (mWidgetsToAdvance.containsKey(hostView)) {
1425 mWidgetsToAdvance.remove(hostView);
1426 updateRunning();
1427 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001428 }
1429
Joe Onorato9c1289c2009-08-17 11:03:03 -04001430 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001431 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 launcherInfo.hostView = null;
1433 }
1434
Winson Chung93eef082012-03-23 15:59:27 -07001435 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1436 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1437 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001438 }
1439
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001440 public LauncherAppWidgetHost getAppWidgetHost() {
1441 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Winson Chunga9abd0e2010-10-27 17:18:37 -07001444 public LauncherModel getModel() {
1445 return mModel;
1446 }
1447
Bjorn Bringertc459e522013-06-07 19:36:01 +01001448 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001449 getWindow().closeAllPanels();
1450
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001451 // Whatever we were doing is hereby canceled.
1452 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001453 }
1454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 @Override
1456 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001457 long startTime = 0;
1458 if (DEBUG_RESUME_TIME) {
1459 startTime = System.currentTimeMillis();
1460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 super.onNewIntent(intent);
1462
1463 // Close the menu
1464 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001465 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001466 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001467
Jamie Genniscb222e82012-10-23 15:44:26 -07001468 final boolean alreadyOnHome =
1469 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001470 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001471
Jamie Genniscb222e82012-10-23 15:44:26 -07001472 Runnable processIntent = new Runnable() {
1473 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001474 if (mWorkspace == null) {
1475 // Can be cases where mWorkspace is null, this prevents a NPE
1476 return;
1477 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001478 Folder openFolder = mWorkspace.getOpenFolder();
1479 // In all these cases, only animate if we're already on home
1480 mWorkspace.exitWidgetResizeMode();
1481 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1482 openFolder == null) {
1483 mWorkspace.moveToDefaultScreen(true);
1484 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001485
Jamie Genniscb222e82012-10-23 15:44:26 -07001486 closeFolder();
1487 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001488
Jamie Genniscb222e82012-10-23 15:44:26 -07001489 // If we are already on home, then just animate back to the workspace,
1490 // otherwise, just wait until onResume to set the state back to Workspace
1491 if (alreadyOnHome) {
1492 showWorkspace(true);
1493 } else {
1494 mOnResumeState = State.WORKSPACE;
1495 }
1496
1497 final View v = getWindow().peekDecorView();
1498 if (v != null && v.getWindowToken() != null) {
1499 InputMethodManager imm = (InputMethodManager)getSystemService(
1500 INPUT_METHOD_SERVICE);
1501 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1502 }
1503
1504 // Reset AllApps to its initial state
1505 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1506 mAppsCustomizeTabHost.reset();
1507 }
1508 }
1509 };
1510
1511 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1512 // Delay processing of the intent to allow the status bar animation to finish
1513 // first in order to avoid janky animations.
1514 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001515 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001516 // Process the intent immediately.
1517 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001518 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
Michael Jurka447bf842013-05-15 14:52:15 +02001521 if (DEBUG_RESUME_TIME) {
1522 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
1525
1526 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001527 public void onRestoreInstanceState(Bundle state) {
1528 super.onRestoreInstanceState(state);
1529 for (int page: mSynchronouslyBoundPages) {
1530 mWorkspace.restoreInstanceStateForChild(page);
1531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533
1534 @Override
1535 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001536 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001537 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
Michael Jurka883f55b2010-10-21 15:47:14 -07001539 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001540 // We close any open folder since it will not be re-opened, and we need to make sure
1541 // this state is reflected.
1542 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 mWaitingForResult) {
1546 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1549 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001550 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1551 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1552 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554
1555 if (mFolderInfo != null && mWaitingForResult) {
1556 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1557 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1558 }
Michael Jurka946ad472010-07-09 18:05:18 -07001559
Winson Chung785d2eb2011-04-14 16:08:02 -07001560 // Save the current AppsCustomize tab
1561 if (mAppsCustomizeTabHost != null) {
1562 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1563 if (currentTabTag != null) {
1564 outState.putString("apps_customize_currentTab", currentTabTag);
1565 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001566 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1567 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
1571 @Override
1572 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Winson Chunge7a03942011-08-05 15:05:12 -07001575 // Remove all pending runnables
1576 mHandler.removeMessages(ADVANCE_MSG);
1577 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001578 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001579
Winson Chungcd2b0142011-06-08 16:02:26 -07001580 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001581 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001582 mModel.stopLoader();
1583 app.setLauncher(null);
1584
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001586 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001588 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001590 mAppWidgetHost = null;
1591
1592 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 TextKeyListener.getInstance().release();
1595
Winson Chung81b52252012-08-27 15:34:29 -07001596 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1597 // to prevent leaking Launcher activities on orientation change.
1598 if (mModel != null) {
1599 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1600 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001601
1602 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001603 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001604
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001605 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001606 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1607 mWorkspace.removeAllViews();
1608 mWorkspace = null;
1609 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001610
Michael Jurka2ecf9952012-06-18 12:52:28 -07001611 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 }
1613
Adam Cohena9cf38f2011-05-02 15:36:58 -07001614 public DragController getDragController() {
1615 return mDragController;
1616 }
1617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 @Override
1619 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001620 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 super.startActivityForResult(intent, requestCode);
1622 }
1623
Winson Chung70d72102011-08-12 11:24:35 -07001624 /**
1625 * Indicates that we want global search for this activity by setting the globalSearch
1626 * argument for {@link #startSearch} to true.
1627 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001629 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001631
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001632 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Karl Rosaen138a0412009-04-23 19:00:21 -07001634 if (initialQuery == null) {
1635 // Use any text typed in the launcher as the initial query
1636 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 if (appSearchData == null) {
1639 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001640 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
Winson Chungadf0c182012-08-23 14:59:07 -07001642 Rect sourceBounds = new Rect();
1643 if (mSearchDropTargetBar != null) {
1644 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1645 }
Romain Guycbb89e42009-06-08 15:52:54 -07001646
Bjorn Bringertc459e522013-06-07 19:36:01 +01001647 startSearch(initialQuery, selectInitialQuery,
1648 appSearchData, sourceBounds);
1649 }
1650
1651 public void startSearch(String initialQuery,
1652 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001653 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001654 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001655 }
1656
1657 /**
1658 * Starts the global search activity. This code is a copied from SearchManager
1659 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001660 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001661 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001662 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001663 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1664 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1665 if (globalSearchActivity == null) {
1666 Log.w(TAG, "No global search activity found.");
1667 return;
1668 }
1669 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1670 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1671 intent.setComponent(globalSearchActivity);
1672 // Make sure that we have a Bundle to put source in
1673 if (appSearchData == null) {
1674 appSearchData = new Bundle();
1675 } else {
1676 appSearchData = new Bundle(appSearchData);
1677 }
1678 // Set source to package name of app that starts global search, if not set already.
1679 if (!appSearchData.containsKey("source")) {
1680 appSearchData.putString("source", getPackageName());
1681 }
1682 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1683 if (!TextUtils.isEmpty(initialQuery)) {
1684 intent.putExtra(SearchManager.QUERY, initialQuery);
1685 }
1686 if (selectInitialQuery) {
1687 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1688 }
1689 intent.setSourceBounds(sourceBounds);
1690 try {
1691 startActivity(intent);
1692 } catch (ActivityNotFoundException ex) {
1693 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696
Winson Chung70d72102011-08-12 11:24:35 -07001697 @Override
1698 public boolean onCreateOptionsMenu(Menu menu) {
1699 if (isWorkspaceLocked()) {
1700 return false;
1701 }
1702
1703 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001704
1705 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1706 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1707 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001708 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1709 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1710 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001711 String helpUrl = getString(R.string.help_url);
1712 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001713 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1714 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1715
Winson Chung70d72102011-08-12 11:24:35 -07001716 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1717 .setIcon(android.R.drawable.ic_menu_gallery)
1718 .setAlphabeticShortcut('W');
1719 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1720 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001721 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001722 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001723 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1724 .setIcon(android.R.drawable.ic_menu_preferences)
1725 .setIntent(settings)
1726 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001727 if (!helpUrl.isEmpty()) {
1728 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1729 .setIcon(android.R.drawable.ic_menu_help)
1730 .setIntent(help)
1731 .setAlphabeticShortcut('H');
1732 }
Winson Chung70d72102011-08-12 11:24:35 -07001733 return true;
1734 }
1735
1736 @Override
1737 public boolean onPrepareOptionsMenu(Menu menu) {
1738 super.onPrepareOptionsMenu(menu);
1739
1740 if (mAppsCustomizeTabHost.isTransitioning()) {
1741 return false;
1742 }
1743 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1744 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1745
1746 return true;
1747 }
1748
1749 @Override
1750 public boolean onOptionsItemSelected(MenuItem item) {
1751 switch (item.getItemId()) {
1752 case MENU_WALLPAPER_SETTINGS:
1753 startWallpaper();
1754 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001755 }
1756
1757 return super.onOptionsItemSelected(item);
1758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001760 @Override
1761 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001762 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001763 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001764 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001765 }
1766
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767 public boolean isWorkspaceLocked() {
1768 return mWorkspaceLoading || mWaitingForResult;
1769 }
1770
Michael Jurka0280c3b2010-09-17 15:00:07 -07001771 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001772 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001773 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001774 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1775 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001776 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001777 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001778 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001779
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001780 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1781 AppWidgetProviderInfo appWidgetInfo) {
1782 if (appWidgetInfo.configure != null) {
1783 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001784
Bjorn Bringert7984c942009-12-09 15:38:25 +00001785 // Launch over to configure widget, if needed
1786 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001787 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001788 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001789 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001791 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001792 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001793 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001794 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001795 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Adam Cohenfbba09b2011-07-18 21:43:05 -07001799 /**
1800 * Process a shortcut drop.
1801 *
1802 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001804 * @param cell The cell it should be added to, optional
1805 * @param position The location on the screen where it was dropped, optional
1806 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001807 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001808 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001809 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001811 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001812 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001813
1814 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001815 mPendingAddInfo.cellX = cell[0];
1816 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001817 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818
1819 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1820 createShortcutIntent.setComponent(componentName);
1821 processShortcut(createShortcutIntent);
1822 }
1823
Adam Cohenfbba09b2011-07-18 21:43:05 -07001824 /**
1825 * Process a widget drop.
1826 *
1827 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001828 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001829 * @param cell The cell it should be added to, optional
1830 * @param position The location on the screen where it was dropped, optional
1831 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001833 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001834 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001836 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001838 mPendingAddInfo.minSpanX = info.minSpanX;
1839 mPendingAddInfo.minSpanY = info.minSpanY;
1840
Adam Cohenfbba09b2011-07-18 21:43:05 -07001841 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001842 mPendingAddInfo.cellX = cell[0];
1843 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001844 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001845 if (span != null) {
1846 mPendingAddInfo.spanX = span[0];
1847 mPendingAddInfo.spanY = span[1];
1848 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001849
Adam Cohened66b2b2012-01-23 17:28:51 -08001850 AppWidgetHostView hostView = info.boundWidget;
1851 int appWidgetId;
1852 if (hostView != null) {
1853 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001854 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001855 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001856 // In this case, we either need to start an activity to get permission to bind
1857 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001858 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001859 Bundle options = info.bindOptions;
1860
1861 boolean success = false;
1862 if (options != null) {
1863 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1864 info.componentName, options);
1865 } else {
1866 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1867 info.componentName);
1868 }
1869 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001870 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001871 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001872 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001873 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1874 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1875 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001876 // TODO: we need to make sure that this accounts for the options bundle.
1877 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001878 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1879 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001880 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001881 }
1882
Joe Onoratodeb98af2010-02-19 14:59:39 -08001883 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001884 // Handle case where user selected "Applications"
1885 String applicationName = getResources().getString(R.string.group_applications);
1886 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001888 if (applicationName != null && applicationName.equals(shortcutName)) {
1889 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1890 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001891
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001892 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1893 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001894 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001895 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001896 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001897 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
Winson Chung24ab2f12010-09-16 14:10:47 -07001901 void processWallpaper(Intent intent) {
1902 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1903 }
1904
Adam Cohendcd297f2013-06-18 13:13:40 -07001905 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001906 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001907 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 folderInfo.title = getText(R.string.folder_name);
1909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001911 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001913 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914
1915 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 FolderIcon newFolder =
1917 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001918 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001920 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
Romain Guycbb89e42009-06-08 15:52:54 -07001922
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001924 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 }
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001928 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001930 Intent chooser = Intent.createChooser(pickWallpaper,
1931 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001932 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1933 // Removed in Eclair MR1
1934// WallpaperManager wm = (WallpaperManager)
1935// getSystemService(Context.WALLPAPER_SERVICE);
1936// WallpaperInfo wi = wm.getWallpaperInfo();
1937// if (wi != null && wi.getSettingsActivity() != null) {
1938// LabeledIntent li = new LabeledIntent(getPackageName(),
1939// R.string.configure_wallpaper, 0);
1940// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1941// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1942// }
Mike Clerona0618e42009-10-22 13:55:21 -07001943 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001946 /**
1947 * Registers various content observers. The current implementation registers
1948 * only a favorites observer to keep track of the favorites applications.
1949 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001950 private void registerContentObservers() {
1951 ContentResolver resolver = getContentResolver();
1952 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1953 true, mWidgetObserver);
1954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 @Override
1957 public boolean dispatchKeyEvent(KeyEvent event) {
1958 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1959 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001961 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001962 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001963 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001964 dumpState();
1965 return true;
1966 }
1967 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001968 }
1969 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1970 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001971 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 return true;
1973 }
1974 }
1975
1976 return super.dispatchKeyEvent(event);
1977 }
1978
Joe Onorato88ec0992009-11-19 13:16:06 -08001979 @Override
1980 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001981 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001982 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001983 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001984 Folder openFolder = mWorkspace.getOpenFolder();
1985 if (openFolder.isEditingName()) {
1986 openFolder.dismissEditingName();
1987 } else {
1988 closeFolder();
1989 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001990 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001991 mWorkspace.exitWidgetResizeMode();
1992
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001993 // Back button is a no-op here, but give at least some feedback for the button press
1994 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001995 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001999 * Re-listen when widgets are reset.
2000 */
2001 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002002 if (mAppWidgetHost != null) {
2003 mAppWidgetHost.startListening();
2004 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002005 }
2006
2007 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 * Launches the intent referred by the clicked shortcut.
2009 *
2010 * @param v The view representing the clicked shortcut.
2011 */
2012 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002013 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2014 // view has detached (it's possible for this to happen if the view is removed mid touch).
2015 if (v.getWindowToken() == null) {
2016 return;
2017 }
2018
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002019 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002020 return;
2021 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002024 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002026 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002027
2028 ComponentName widgetComp = new ComponentName(this, WidgetAdder.class);
Adam Cohen99894d92013-06-14 11:22:59 -07002029 if (intent.getComponent() != null &&
2030 intent.getComponent().getClassName().equals(widgetComp.getClassName())) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002031 showAllApps(true);
2032 return;
2033 }
Joe Onorato13724ea2009-12-02 21:16:35 -08002034 int[] pos = new int[2];
2035 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002036 intent.setSourceBounds(new Rect(pos[0], pos[1],
2037 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002038
2039 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002040
2041 if (success && v instanceof BubbleTextView) {
2042 mWaitingForResume = (BubbleTextView) v;
2043 mWaitingForResume.setStayPressed(true);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002046 if (v instanceof FolderIcon) {
2047 FolderIcon fi = (FolderIcon) v;
2048 handleFolderClick(fi);
2049 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002050 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002051 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002052 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002053 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002054 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002055 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
2057 }
2058
Michael Jurka0e260592010-06-30 17:07:39 -07002059 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002060 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002061 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002062 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2063 showWorkspace(true);
2064 }
Michael Jurka0e260592010-06-30 17:07:39 -07002065 return false;
2066 }
2067
Michael Jurkaaf442092010-06-10 17:01:57 -07002068 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002069 * Event handler for the search button
2070 *
2071 * @param v The view that was clicked.
2072 */
2073 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002074 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2075
Amith Yamasania135ba82011-08-09 17:42:01 -07002076 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002077 }
2078
2079 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002080 * Event handler for the voice button
2081 *
2082 * @param v The view that was clicked.
2083 */
2084 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002085 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002086
Bjorn Bringertc459e522013-06-07 19:36:01 +01002087 startVoice();
2088 }
2089
2090 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002091 try {
2092 final SearchManager searchManager =
2093 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2094 ComponentName activityName = searchManager.getGlobalSearchActivity();
2095 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002096 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002097 if (activityName != null) {
2098 intent.setPackage(activityName.getPackageName());
2099 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002100 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002101 } catch (ActivityNotFoundException e) {
2102 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002103 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002104 startActivitySafely(null, intent, "onClickVoiceButton");
2105 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002106 }
2107
2108 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002109 * Event handler for the "grid" button that appears on the home screen, which
2110 * enters all apps mode.
2111 *
2112 * @param v The view that was clicked.
2113 */
2114 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002115 showAllApps(true);
2116 }
2117
2118 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002119 // Provide the same haptic feedback that the system offers for virtual keys.
2120 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002121 }
2122
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002123 public void onClickAppMarketButton(View v) {
2124 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002125 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002126 } else {
2127 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002128 }
2129 }
2130
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002131 void startApplicationDetailsActivity(ComponentName componentName) {
2132 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002133 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2134 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002135 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002136 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002137 }
2138
Patrick Dubroy5539af72010-09-07 15:22:01 -07002139 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2140 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2141 // System applications cannot be installed. For now, show a toast explaining that.
2142 // We may give them the option of disabling apps this way.
2143 int messageId = R.string.uninstall_system_app_text;
2144 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2145 } else {
2146 String packageName = appInfo.componentName.getPackageName();
2147 String className = appInfo.componentName.getClassName();
2148 Intent intent = new Intent(
2149 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002150 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2151 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002152 startActivity(intent);
2153 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002154 }
2155
Michael Jurka86a720e2012-05-09 11:23:23 -07002156 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002160 // Only launch using the new animation if the shortcut has not opted out (this is a
2161 // private contract between launcher and may be ignored in the future).
2162 boolean useLaunchAnimation = (v != null) &&
2163 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2164 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002165 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2166 v.getMeasuredWidth(), v.getMeasuredHeight());
2167
2168 startActivity(intent, opts.toBundle());
2169 } else {
2170 startActivity(intent);
2171 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002172 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 } catch (SecurityException e) {
2174 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002175 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002177 "or use the exported attribute for this activity. "
2178 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002180 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002182
Michael Jurka86a720e2012-05-09 11:23:23 -07002183 boolean startActivitySafely(View v, Intent intent, Object tag) {
2184 boolean success = false;
2185 try {
2186 success = startActivity(v, intent, tag);
2187 } catch (ActivityNotFoundException e) {
2188 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2189 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2190 }
2191 return success;
2192 }
2193
Romain Guy8e633c52010-03-04 12:51:36 -08002194 void startActivityForResultSafely(Intent intent, int requestCode) {
2195 try {
2196 startActivityForResult(intent, requestCode);
2197 } catch (ActivityNotFoundException e) {
2198 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2199 } catch (SecurityException e) {
2200 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2201 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2202 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2203 "or use the exported attribute for this activity.", e);
2204 }
2205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206
Adam Cohena9cf38f2011-05-02 15:36:58 -07002207 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002208 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002209 Folder openFolder = mWorkspace.getFolderForTag(info);
2210
2211 // If the folder info reports that the associated folder is open, then verify that
2212 // it is actually opened. There have been a few instances where this gets out of sync.
2213 if (info.opened && openFolder == null) {
2214 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002216 info.opened = false;
2217 }
2218
Adam Cohenfb91f302012-06-11 15:45:18 -07002219 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 // Close any open folder
2221 closeFolder();
2222 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002223 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 } else {
2225 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 int folderScreen;
2227 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002228 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 // .. and close it
2230 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002231 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 // Close any folder open on the current screen
2233 closeFolder();
2234 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002235 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 }
2237 }
2238 }
2239 }
2240
Adam Cohen268c4752012-06-06 17:47:33 -07002241 /**
2242 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2243 * in the DragLayer in the exact absolute location of the original FolderIcon.
2244 */
2245 private void copyFolderIconToImage(FolderIcon fi) {
2246 final int width = fi.getMeasuredWidth();
2247 final int height = fi.getMeasuredHeight();
2248
2249 // Lazy load ImageView, Bitmap and Canvas
2250 if (mFolderIconImageView == null) {
2251 mFolderIconImageView = new ImageView(this);
2252 }
2253 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2254 mFolderIconBitmap.getHeight() != height) {
2255 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2256 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2257 }
2258
2259 DragLayer.LayoutParams lp;
2260 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2261 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2262 } else {
2263 lp = new DragLayer.LayoutParams(width, height);
2264 }
2265
Adam Cohen307fe232012-08-16 17:55:58 -07002266 // The layout from which the folder is being opened may be scaled, adjust the starting
2267 // view size by this scale factor.
2268 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002269 lp.customPosition = true;
2270 lp.x = mRectForFolderAnimation.left;
2271 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002272 lp.width = (int) (scale * width);
2273 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002274
2275 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2276 fi.draw(mFolderIconCanvas);
2277 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002278 if (fi.getFolder() != null) {
2279 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2280 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002281 }
Adam Cohen268c4752012-06-06 17:47:33 -07002282 // Just in case this image view is still in the drag layer from a previous animation,
2283 // we remove it and re-add it.
2284 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2285 mDragLayer.removeView(mFolderIconImageView);
2286 }
2287 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002288 if (fi.getFolder() != null) {
2289 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002290 }
Adam Cohen268c4752012-06-06 17:47:33 -07002291 }
2292
Adam Cohen2801caf2011-05-13 20:57:39 -07002293 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002294 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002295 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2296 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2297 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2298
Adam Cohenc51934b2011-07-26 21:07:43 -07002299 FolderInfo info = (FolderInfo) fi.getTag();
2300 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2301 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002302 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2303 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002304 }
2305
Adam Cohen268c4752012-06-06 17:47:33 -07002306 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2307 copyFolderIconToImage(fi);
2308 fi.setVisibility(View.INVISIBLE);
2309
Michael Jurka2ecf9952012-06-18 12:52:28 -07002310 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002311 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002312 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2313 oa.start();
2314 }
2315
Adam Cohen268c4752012-06-06 17:47:33 -07002316 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002317 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002318 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2319 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2320 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2321
Adam Cohen268c4752012-06-06 17:47:33 -07002322 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002323
Adam Cohen268c4752012-06-06 17:47:33 -07002324 // We remove and re-draw the FolderIcon in-case it has changed
2325 mDragLayer.removeView(mFolderIconImageView);
2326 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002327 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002328 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002329 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002330 oa.addListener(new AnimatorListenerAdapter() {
2331 @Override
2332 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002333 if (cl != null) {
2334 cl.clearFolderLeaveBehind();
2335 // Remove the ImageView copy of the FolderIcon and make the original visible.
2336 mDragLayer.removeView(mFolderIconImageView);
2337 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002338 }
2339 }
2340 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002341 oa.start();
2342 }
2343
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002345 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 * is animated relative to the specified View. If the View is null, no animation
2347 * is played.
2348 *
2349 * @param folderInfo The FolderInfo describing the folder to open.
2350 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002351 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002352 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002353 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354
Adam Cohena9cf38f2011-05-02 15:36:58 -07002355 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356
Adam Cohen4554ee12011-08-03 16:13:21 -07002357 // Just verify that the folder hasn't already been added to the DragLayer.
2358 // There was a one-off crash where the folder had a parent already.
2359 if (folder.getParent() == null) {
2360 mDragLayer.addView(folder);
2361 mDragController.addDropTarget((DropTarget) folder);
2362 } else {
2363 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2364 folder.getParent() + ").");
2365 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002366 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002367 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002368
2369 // Notify the accessibility manager that this folder "window" has appeared and occluded
2370 // the workspace items
2371 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2372 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002373 }
2374
2375 public void closeFolder() {
2376 Folder folder = mWorkspace.getOpenFolder();
2377 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002378 if (folder.isEditingName()) {
2379 folder.dismissEditingName();
2380 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002381 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002382
2383 // Dismiss the folder cling
2384 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002385 }
2386 }
2387
2388 void closeFolder(Folder folder) {
2389 folder.getInfo().opened = false;
2390
2391 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2392 if (parent != null) {
2393 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2394 shrinkAndFadeInFolderIcon(fi);
2395 }
2396 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002397
2398 // Notify the accessibility manager that this folder "window" has disappeard and no
2399 // longer occludeds the workspace items
2400 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 }
2402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002404 if (!isDraggingEnabled()) return false;
2405 if (isWorkspaceLocked()) return false;
2406 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407
2408 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002409 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 }
2411
Michael Jurka0280c3b2010-09-17 15:00:07 -07002412 resetAddInfo();
2413 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002415 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 return true;
2417 }
2418
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 // The hotseat touch handling does not go through Workspace, and we always allow long press
2420 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002421 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002422 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2423 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002424 if (itemUnderLongClick == null) {
2425 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002426 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2427 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002428 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002430 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002432 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 }
2434 }
2435 }
2436 return true;
2437 }
2438
Winson Chung3d503fb2011-07-13 17:25:49 -07002439 boolean isHotseatLayout(View layout) {
2440 return mHotseat != null && layout != null &&
2441 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2442 }
2443 Hotseat getHotseat() {
2444 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002445 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002446 SearchDropTargetBar getSearchBar() {
2447 return mSearchDropTargetBar;
2448 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002449
Winson Chung3d503fb2011-07-13 17:25:49 -07002450 /**
2451 * Returns the CellLayout of the specified container at the specified screen.
2452 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002453 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002454 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2455 if (mHotseat != null) {
2456 return mHotseat.getLayout();
2457 } else {
2458 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002459 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002460 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002462 }
2463 }
2464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 Workspace getWorkspace() {
2466 return mWorkspace;
2467 }
2468
Daniel Sandler843e8602010-06-07 14:59:01 -04002469 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002470 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002471 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002472 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002473 }
2474
Craig Mautner360310b2012-10-26 15:13:08 -07002475 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002476 public boolean isAllAppsButtonRank(int rank) {
2477 return mHotseat.isAllAppsButtonRank(rank);
2478 }
2479
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002480 /**
2481 * Helper method for the cameraZoomIn/cameraZoomOut animations
2482 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002483 * @param scaleFactor The scale factor used for the zoom
2484 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002485 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002486 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002487 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002488 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002489
Winson Chung18f41f82012-05-09 13:28:10 -07002490 void disableWallpaperIfInAllApps() {
2491 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002492 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002493 if (mAppsCustomizeTabHost != null &&
2494 !mAppsCustomizeTabHost.isTransitioning()) {
2495 updateWallpaperVisibility(false);
2496 }
2497 }
2498 }
2499
Craig Mautner360310b2012-10-26 15:13:08 -07002500 private void setWorkspaceBackground(boolean workspace) {
2501 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002502 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002503 }
2504
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002505 void updateWallpaperVisibility(boolean visible) {
2506 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2507 int curflags = getWindow().getAttributes().flags
2508 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2509 if (wpflags != curflags) {
2510 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2511 }
Craig Mautner360310b2012-10-26 15:13:08 -07002512 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002513 }
2514
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002515 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2516 if (v instanceof LauncherTransitionable) {
2517 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2518 }
2519 }
2520
Michael Jurkabed61d22012-02-14 22:51:29 -08002521 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2522 if (v instanceof LauncherTransitionable) {
2523 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2524 }
Winson Chung70442722012-02-10 15:43:22 -08002525
2526 // Update the workspace transition step as well
2527 dispatchOnLauncherTransitionStep(v, 0f);
2528 }
2529
2530 private void dispatchOnLauncherTransitionStep(View v, float t) {
2531 if (v instanceof LauncherTransitionable) {
2532 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2533 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002534 }
2535
2536 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2537 if (v instanceof LauncherTransitionable) {
2538 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2539 }
Winson Chung70442722012-02-10 15:43:22 -08002540
2541 // Update the workspace transition step as well
2542 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002543 }
2544
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002545 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002546 * Things to test when changing the following seven functions.
2547 * - Home from workspace
2548 * - from center screen
2549 * - from other screens
2550 * - Home from all apps
2551 * - from center screen
2552 * - from other screens
2553 * - Back from all apps
2554 * - from center screen
2555 * - from other screens
2556 * - Launch app from workspace and quit
2557 * - with back
2558 * - with home
2559 * - Launch app from all apps and quit
2560 * - with back
2561 * - with home
2562 * - Go to a screen that's not the default, then all
2563 * apps, and launch and app, and go back
2564 * - with back
2565 * -with home
2566 * - On workspace, long press power and go back
2567 * - with back
2568 * - with home
2569 * - On all apps, long press power and go back
2570 * - with back
2571 * - with home
2572 * - On workspace, power off
2573 * - On all apps, power off
2574 * - Launch an app and turn off the screen while in that app
2575 * - Go back with home key
2576 * - Go back with back key TODO: make this not go to workspace
2577 * - From all apps
2578 * - From workspace
2579 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2580 * - From all apps
2581 * - From the center workspace
2582 * - From another workspace
2583 */
2584
2585 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002586 * Zoom the camera out from the workspace to reveal 'toView'.
2587 * Assumes that the view to show is anchored at either the very top or very bottom
2588 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002589 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002590 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002591 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002592 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593 mStateAnimation.cancel();
2594 mStateAnimation = null;
2595 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002596 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002597
Winson Chungf0ea4d32011-06-06 14:27:16 -07002598 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2599 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2600 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002601 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002602 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002603 final int startDelay =
2604 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002605
Michael Jurkab3e22d92011-10-31 15:58:33 -07002606 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607
Michael Jurkad74c9842011-07-10 12:44:21 -07002608 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002609 Animator workspaceAnim =
2610 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002611
2612 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002613 toView.setScaleX(scale);
2614 toView.setScaleY(scale);
2615 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2616 scaleAnim.
2617 scaleX(1f).scaleY(1f).
2618 setDuration(duration).
2619 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002620
Winson Chungf0ea4d32011-06-06 14:27:16 -07002621 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002622 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002623 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002624 .ofFloat(toView, "alpha", 0f, 1f)
2625 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002626 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002627 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2628 @Override
2629 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002630 if (animation == null) {
2631 throw new RuntimeException("animation is null");
2632 }
Winson Chung70442722012-02-10 15:43:22 -08002633 float t = (Float) animation.getAnimatedValue();
2634 dispatchOnLauncherTransitionStep(fromView, t);
2635 dispatchOnLauncherTransitionStep(toView, t);
2636 }
2637 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002638
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002639 // toView should appear right at the end of the workspace shrink
2640 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002641 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002642 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002643 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002644
2645 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002646 boolean animationCancelled = false;
2647
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002648 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002649 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002650 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002651 // Prepare the position
2652 toView.setTranslationX(0.0f);
2653 toView.setTranslationY(0.0f);
2654 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002655 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002656 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002657 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002658 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002659 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2660 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002661
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002662 if (mWorkspace != null && !springLoaded && !LauncherAppState.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002663 // Hide the workspace scrollbar
2664 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002665 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002666 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002667 if (!animationCancelled) {
2668 updateWallpaperVisibility(false);
2669 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002670
2671 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002672 if (mSearchDropTargetBar != null) {
2673 mSearchDropTargetBar.hideSearchBar(false);
2674 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002675 }
2676
Adam Cohencff6af82011-09-13 14:51:53 -07002677 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002678 public void onAnimationCancel(Animator animation) {
2679 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002680 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 });
2682
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002683 if (workspaceAnim != null) {
2684 mStateAnimation.play(workspaceAnim);
2685 }
Michael Jurka1899a362011-11-03 13:50:45 -07002686
2687 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002688
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002689 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2690 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002691
2692 // If any of the objects being animated haven't been measured/laid out
2693 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002694 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002695 (mWorkspace.getMeasuredWidth() == 0) ||
2696 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002697 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002698 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002699
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002700 final AnimatorSet stateAnimation = mStateAnimation;
2701 final Runnable startAnimRunnable = new Runnable() {
2702 public void run() {
2703 // Check that mStateAnimation hasn't changed while
2704 // we waited for a layout/draw pass
2705 if (mStateAnimation != stateAnimation)
2706 return;
2707 setPivotsForZoom(toView, scale);
2708 dispatchOnLauncherTransitionStart(fromView, animated, false);
2709 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002710 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002711 }
2712 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002713 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002714 final ViewTreeObserver observer = toView.getViewTreeObserver();
2715 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2716 public void onGlobalLayout() {
2717 startAnimRunnable.run();
2718 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2719 }
2720 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002721 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002722 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002723 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002724 } else {
2725 toView.setTranslationX(0.0f);
2726 toView.setTranslationY(0.0f);
2727 toView.setScaleX(1.0f);
2728 toView.setScaleY(1.0f);
2729 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002730 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002731
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002732 if (!springLoaded && !LauncherAppState.isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002733 // Hide the workspace scrollbar
2734 mWorkspace.hideScrollingIndicator(true);
2735 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002736
2737 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002738 if (mSearchDropTargetBar != null) {
2739 mSearchDropTargetBar.hideSearchBar(false);
2740 }
Michael Jurkaabded662011-03-04 12:06:57 -08002741 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002742 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002743 dispatchOnLauncherTransitionStart(fromView, animated, false);
2744 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002745 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002746 dispatchOnLauncherTransitionStart(toView, animated, false);
2747 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002748 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002749 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002750 }
2751
2752 /**
2753 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002754 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002755 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002757 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2758 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002759
Michael Jurkab3e22d92011-10-31 15:58:33 -07002760 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002761 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002762 mStateAnimation.cancel();
2763 mStateAnimation = null;
2764 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002766
Winson Chungf0ea4d32011-06-06 14:27:16 -07002767 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002768 final int fadeOutDuration =
2769 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002770 final float scaleFactor = (float)
2771 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2772 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002773 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002774 Animator workspaceAnim = null;
2775
2776 if (toState == State.WORKSPACE) {
2777 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2778 workspaceAnim = mWorkspace.getChangeStateAnimation(
2779 Workspace.State.NORMAL, animated, stagger);
2780 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2781 workspaceAnim = mWorkspace.getChangeStateAnimation(
2782 Workspace.State.SPRING_LOADED, animated);
2783 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002784
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002786 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002787 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002788 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002789 final LauncherViewPropertyAnimator scaleAnim =
2790 new LauncherViewPropertyAnimator(fromView);
2791 scaleAnim.
2792 scaleX(scaleFactor).scaleY(scaleFactor).
2793 setDuration(duration).
2794 setInterpolator(new Workspace.ZoomInInterpolator());
2795
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002796 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002797 .ofFloat(fromView, "alpha", 1f, 0f)
2798 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002799 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002800 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2801 @Override
2802 public void onAnimationUpdate(ValueAnimator animation) {
2803 float t = 1f - (Float) animation.getAnimatedValue();
2804 dispatchOnLauncherTransitionStep(fromView, t);
2805 dispatchOnLauncherTransitionStep(toView, t);
2806 }
2807 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002808
Michael Jurka2ecf9952012-06-18 12:52:28 -07002809 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002810
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002811 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2812 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002813 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002814
2815 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002816 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002817 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002818 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002819 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002820 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2821 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002822 if (mWorkspace != null) {
2823 mWorkspace.hideScrollingIndicator(false);
2824 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002825 if (onCompleteRunnable != null) {
2826 onCompleteRunnable.run();
2827 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002828 mAppsCustomizeContent.updateCurrentPageScroll();
2829 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002830 }
2831 });
2832
Winson Chungf0ea4d32011-06-06 14:27:16 -07002833 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002834 if (workspaceAnim != null) {
2835 mStateAnimation.play(workspaceAnim);
2836 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002837 dispatchOnLauncherTransitionStart(fromView, animated, true);
2838 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002839 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002840 } else {
2841 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002842 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002843 dispatchOnLauncherTransitionStart(fromView, animated, true);
2844 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002845 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002846 dispatchOnLauncherTransitionStart(toView, animated, true);
2847 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002848 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002850 }
2851
Michael Jurkae326f182011-11-21 14:05:46 -08002852 @Override
2853 public void onTrimMemory(int level) {
2854 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002855 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002856 mAppsCustomizeTabHost.onTrimMemory();
2857 }
2858 }
2859
Winson Chung18f41f82012-05-09 13:28:10 -07002860 @Override
2861 public void onWindowFocusChanged(boolean hasFocus) {
2862 if (!hasFocus) {
2863 // When another window occludes launcher (like the notification shade, or recents),
2864 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2865 updateWallpaperVisibility(true);
2866 } else {
2867 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002868 mWorkspace.postDelayed(new Runnable() {
2869 @Override
2870 public void run() {
2871 disableWallpaperIfInAllApps();
2872 }
2873 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002874 }
2875 }
2876
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002877 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002878 showWorkspace(animated, null);
2879 }
2880
2881 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002882 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002883 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002884 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002885 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002886
Winson Chungc7d2b602012-05-16 17:02:20 -07002887 // Show the search bar (only animate if we were showing the drop target bar in spring
2888 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002889 if (mSearchDropTargetBar != null) {
2890 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2891 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002892
Michael Jurkab737ee62011-11-15 15:57:22 -08002893 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002894 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895
2896 // Set focus to the AppsCustomize button
2897 if (mAllAppsButton != null) {
2898 mAllAppsButton.requestFocus();
2899 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002900 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002901
Michael Jurkab737ee62011-11-15 15:57:22 -08002902 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002903
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002904 // Change the state *after* we've called all the transition code
2905 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002906
Winson Chung5fb63472011-02-02 17:03:37 -08002907 // Resume the auto-advance of widgets
2908 mUserPresent = true;
2909 updateRunning();
2910
alanv1d4fde62012-10-17 13:15:47 -07002911 // Send an accessibility event to announce the context change
2912 getWindow().getDecorView()
2913 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002914
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002915 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002916 }
2917
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002918 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002919 }
2920
Michael Jurkab3e22d92011-10-31 15:58:33 -07002921 void showAllApps(boolean animated) {
2922 if (mState != State.WORKSPACE) return;
2923
2924 showAppsCustomizeHelper(animated, false);
2925 mAppsCustomizeTabHost.requestFocus();
2926
Michael Jurkab3e22d92011-10-31 15:58:33 -07002927 // Change the state *after* we've called all the transition code
2928 mState = State.APPS_CUSTOMIZE;
2929
2930 // Pause the auto-advance of widgets until we are out of AllApps
2931 mUserPresent = false;
2932 updateRunning();
2933 closeFolder();
2934
2935 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002936 getWindow().getDecorView()
2937 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002938 }
2939
Adam Cohen7777d962011-08-18 18:58:38 -07002940 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002941 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002942 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002943 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002944 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002945 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002946 }
Adam Cohen7777d962011-08-18 18:58:38 -07002947
Adam Cohened66b2b2012-01-23 17:28:51 -08002948 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2949 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002950 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2951
Winson Chunge7a03942011-08-05 15:05:12 -07002952 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002953 @Override
2954 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002955 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002956 // Before we show workspace, hide all apps again because
2957 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2958 // clean up our state transition functions
2959 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002960 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002961 } else {
2962 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002963 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002964 }
2965 }, (extendedDelay ?
2966 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2967 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2968 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002969
Michael Jurkad3ef3062010-11-23 16:23:58 -08002970 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002971 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002972 final boolean animated = true;
2973 final boolean springLoaded = true;
2974 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002975 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002976 }
2977 // Otherwise, we are not in spring loaded mode, so don't do anything.
2978 }
2979
Michael Jurkab737ee62011-11-15 15:57:22 -08002980 void hideDockDivider() {
2981 if (mQsbDivider != null && mDockDivider != null) {
2982 mQsbDivider.setVisibility(View.INVISIBLE);
2983 mDockDivider.setVisibility(View.INVISIBLE);
2984 }
2985 }
2986
2987 void showDockDivider(boolean animated) {
2988 if (mQsbDivider != null && mDockDivider != null) {
2989 mQsbDivider.setVisibility(View.VISIBLE);
2990 mDockDivider.setVisibility(View.VISIBLE);
2991 if (mDividerAnimator != null) {
2992 mDividerAnimator.cancel();
2993 mQsbDivider.setAlpha(1f);
2994 mDockDivider.setAlpha(1f);
2995 mDividerAnimator = null;
2996 }
2997 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002998 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2999 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3000 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003001 int duration = 0;
3002 if (mSearchDropTargetBar != null) {
3003 duration = mSearchDropTargetBar.getTransitionInDuration();
3004 }
3005 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003006 mDividerAnimator.start();
3007 }
3008 }
3009 }
3010
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011 void lockAllApps() {
3012 // TODO
3013 }
3014
3015 void unlockAllApps() {
3016 // TODO
3017 }
3018
Winson Chungf0ea4d32011-06-06 14:27:16 -07003019 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003020 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003021 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003022 void showHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003023 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003024 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003025 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003026 int duration = 0;
3027 if (mSearchDropTargetBar != null) {
3028 duration = mSearchDropTargetBar.getTransitionInDuration();
3029 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003030 mHotseat.animate().alpha(1f).setDuration(duration);
3031 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003032 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003033 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003034 }
3035 }
3036 }
3037
3038 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003039 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003041 void hideHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003042 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003044 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003045 int duration = 0;
3046 if (mSearchDropTargetBar != null) {
3047 duration = mSearchDropTargetBar.getTransitionOutDuration();
3048 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003049 mHotseat.animate().alpha(0f).setDuration(duration);
3050 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003051 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003052 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003054 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003055 }
3056
Patrick Dubroy5f445422011-02-18 14:35:21 -08003057 /**
3058 * Add an item from all apps or customize onto the given workspace screen.
3059 * If layout is null, add to the current screen.
3060 */
3061 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003062 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003063 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003064 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003065 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003066
Winson Chungdff8ebb2011-09-08 17:25:31 -07003067 /** Maps the current orientation to an index for referencing orientation correct global icons */
3068 private int getCurrentOrientationIndexForGlobalIcons() {
3069 // default - 0, landscape - 1
3070 switch (getResources().getConfiguration().orientation) {
3071 case Configuration.ORIENTATION_LANDSCAPE:
3072 return 1;
3073 default:
3074 return 0;
3075 }
3076 }
3077
Michael Jurkaae65ee32012-04-30 11:45:54 -07003078 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003079 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003080 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003081 // Look for the toolbar icon specified in the activity meta-data
3082 Bundle metaData = packageManager.getActivityInfo(
3083 activityName, PackageManager.GET_META_DATA).metaData;
3084 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003085 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003086 if (iconResId != 0) {
3087 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003088 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 }
3090 }
3091 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003092 // This can happen if the activity defines an invalid drawable
3093 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3094 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003095 } catch (Resources.NotFoundException nfe) {
3096 // This can happen if the activity defines an invalid drawable
3097 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3098 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003099 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003100 return null;
3101 }
3102
3103 // if successful in getting icon, return it; otherwise, set button to use default drawable
3104 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003105 int buttonId, ComponentName activityName, int fallbackDrawableId,
3106 String toolbarResourceName) {
3107 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003108 Resources r = getResources();
3109 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3110 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003111
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003112 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003113 // If we were unable to find the icon via the meta-data, use a generic one
3114 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003115 toolbarIcon = r.getDrawable(fallbackDrawableId);
3116 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003117 if (button != null) {
3118 button.setCompoundDrawables(toolbarIcon, null, null, null);
3119 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003120 return null;
3121 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003122 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003123 if (button != null) {
3124 button.setCompoundDrawables(toolbarIcon, null, null, null);
3125 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003126 return toolbarIcon.getConstantState();
3127 }
3128 }
3129
3130 // if successful in getting icon, return it; otherwise, set button to use default drawable
3131 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003132 int buttonId, ComponentName activityName, int fallbackDrawableId,
3133 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003134 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003135 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003136
Michael Jurka19e0fc52011-07-22 18:00:21 -07003137 if (button != null) {
3138 // If we were unable to find the icon via the meta-data, use a
3139 // generic one
3140 if (toolbarIcon == null) {
3141 button.setImageResource(fallbackDrawableId);
3142 } else {
3143 button.setImageDrawable(toolbarIcon);
3144 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003146
3147 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3148
Michael Jurka0423dcf2010-10-05 14:56:18 -07003149 }
3150
Winson Chung1b884092012-06-08 17:13:02 -07003151 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003152 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003153 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003154 }
3155
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003156 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003157 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003158 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003159 }
3160
Winson Chungbb185bd2011-11-21 12:31:42 -08003161 private void invalidatePressedFocusedStates(View container, View button) {
3162 if (container instanceof HolographicLinearLayout) {
3163 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3164 layout.invalidatePressedFocusedStates();
3165 } else if (button instanceof HolographicImageView) {
3166 HolographicImageView view = (HolographicImageView) button;
3167 view.invalidatePressedFocusedStates();
3168 }
3169 }
3170
Winson Chungc51db6a2011-10-05 11:44:49 -07003171 private boolean updateGlobalSearchIcon() {
3172 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003173 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003174 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003175 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003176
Winson Chung1cad91e2011-05-25 17:41:01 -07003177 final SearchManager searchManager =
3178 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3179 ComponentName activityName = searchManager.getGlobalSearchActivity();
3180 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003181 int coi = getCurrentOrientationIndexForGlobalIcons();
3182 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003183 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3184 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3185 if (sGlobalSearchIcon[coi] == null) {
3186 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3187 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3188 TOOLBAR_ICON_METADATA_NAME);
3189 }
3190
Winson Chungc51db6a2011-10-05 11:44:49 -07003191 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3192 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003193 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003194 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003195 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003196 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003197 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3198 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3199 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003200 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003201 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003202 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003203 }
3204 }
3205
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003206 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003207 final View searchButtonContainer = findViewById(R.id.search_button_container);
3208 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003209 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003210 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003211 }
3212
Winson Chungc51db6a2011-10-05 11:44:49 -07003213 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003214 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003215 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003216
Winson Chungc51db6a2011-10-05 11:44:49 -07003217 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003218 final SearchManager searchManager =
3219 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3220 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3221
3222 ComponentName activityName = null;
3223 if (globalSearchActivity != null) {
3224 // Check if the global search activity handles voice search
3225 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3226 intent.setPackage(globalSearchActivity.getPackageName());
3227 activityName = intent.resolveActivity(getPackageManager());
3228 }
3229
3230 if (activityName == null) {
3231 // Fallback: check if an activity other than the global search activity
3232 // resolves this
3233 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3234 activityName = intent.resolveActivity(getPackageManager());
3235 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003236 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003237 int coi = getCurrentOrientationIndexForGlobalIcons();
3238 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003239 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3240 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3241 if (sVoiceSearchIcon[coi] == null) {
3242 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3243 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3244 TOOLBAR_ICON_METADATA_NAME);
3245 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003246 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003247 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003248 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003249 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003250 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003251 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003252 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003253 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003254 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003255 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003256 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003257 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003258
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003259 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003260 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3261 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003262 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003263 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003264 }
3265
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003266 public void setVoiceButtonProxyVisible(boolean visible) {
3267 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3268 if (voiceButtonProxy != null) {
3269 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3270 }
3271 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003272 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003273 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003274 */
3275 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003276 final View marketButton = findViewById(R.id.market_button);
3277 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3278 // Find the app market activity by resolving an intent.
3279 // (If multiple app markets are installed, it will return the ResolverActivity.)
3280 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003281 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003282 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003283 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003284 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003285 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3286 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003287 marketButton.setVisibility(View.VISIBLE);
3288 } else {
3289 // We should hide and disable the view so that we don't try and restore the visibility
3290 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3291 marketButton.setVisibility(View.GONE);
3292 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003293 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003294 }
3295
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003296 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003297 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3298 Resources r = getResources();
3299 Drawable marketIconDrawable = d.newDrawable(r);
3300 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3301 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3302 marketIconDrawable.setBounds(0, 0, w, h);
3303
3304 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003305 }
3306
Michael Jurkad7c28052012-04-27 15:43:36 -07003307 @Override
3308 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003309 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003310 final List<CharSequence> text = event.getText();
3311 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003312 // Populate event with a fake title based on the current state.
3313 if (mState == State.APPS_CUSTOMIZE) {
3314 text.add(getString(R.string.all_apps_button_label));
3315 } else {
3316 text.add(getString(R.string.all_apps_home_button_label));
3317 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003318 return result;
3319 }
3320
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003321 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003322 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003323 */
3324 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3325 @Override
3326 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003327 closeSystemDialogs();
3328 }
3329 }
3330
3331 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003332 * Receives notifications whenever the appwidgets are reset.
3333 */
3334 private class AppWidgetResetObserver extends ContentObserver {
3335 public AppWidgetResetObserver() {
3336 super(new Handler());
3337 }
3338
3339 @Override
3340 public void onChange(boolean selfChange) {
3341 onAppWidgetReset();
3342 }
3343 }
3344
3345 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003346 * If the activity is currently paused, signal that we need to run the passed Runnable
3347 * in onResume.
3348 *
3349 * This needs to be called from incoming places where resources might have been loaded
3350 * while we are paused. That is becaues the Configuration might be wrong
3351 * when we're not running, and if it comes back to what it was when we
3352 * were paused, we are not restarted.
3353 *
3354 * Implementation of the method from LauncherModel.Callbacks.
3355 *
3356 * @return true if we are currently paused. The caller might be able to
3357 * skip some work in that case since we will come back again.
3358 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003359 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003360 if (mPaused) {
3361 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003362 if (deletePreviousRunnables) {
3363 while (mOnResumeCallbacks.remove(run)) {
3364 }
3365 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003366 mOnResumeCallbacks.add(run);
3367 return true;
3368 } else {
3369 return false;
3370 }
3371 }
3372
Michael Jurkac402cd92013-05-20 15:49:32 +02003373 private boolean waitUntilResume(Runnable run) {
3374 return waitUntilResume(run, false);
3375 }
3376
Michael Jurka7607c2f2013-04-03 14:33:19 -07003377 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003378 * If the activity is currently paused, signal that we need to re-run the loader
3379 * in onResume.
3380 *
3381 * This needs to be called from incoming places where resources might have been loaded
3382 * while we are paused. That is becaues the Configuration might be wrong
3383 * when we're not running, and if it comes back to what it was when we
3384 * were paused, we are not restarted.
3385 *
3386 * Implementation of the method from LauncherModel.Callbacks.
3387 *
3388 * @return true if we are currently paused. The caller might be able to
3389 * skip some work in that case since we will come back again.
3390 */
3391 public boolean setLoadOnResume() {
3392 if (mPaused) {
3393 Log.i(TAG, "setLoadOnResume");
3394 mOnResumeNeedsLoad = true;
3395 return true;
3396 } else {
3397 return false;
3398 }
3399 }
3400
3401 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003403 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003405 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003406 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003407 } else {
3408 return SCREEN_COUNT / 2;
3409 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003411
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 /**
3413 * Refreshes the shortcuts shown on the workspace.
3414 *
3415 * Implementation of the method from LauncherModel.Callbacks.
3416 */
3417 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003418 // If we're starting binding all over again, clear any bind calls we'd postponed in
3419 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3420 // from scratch again
3421 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003422
Michael Jurka7607c2f2013-04-03 14:33:19 -07003423 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003424 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003425 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003426 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 int count = workspace.getChildCount();
3428 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003429 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003430 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3431 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003433 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003434 if (mHotseat != null) {
3435 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436 }
3437 }
3438
Adam Cohendcd297f2013-06-18 13:13:40 -07003439 @Override
3440 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3441 int count = orderedScreenIds.size();
3442 for (int i = 0; i < count; i++) {
3443 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3444 }
3445 mWorkspace.addExtraEmptyScreen();
3446 }
3447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003448 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003449 * Bind the items start-end from the list.
3450 *
3451 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003452 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003453 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3454 if (waitUntilResume(new Runnable() {
3455 public void run() {
3456 bindItems(shortcuts, start, end);
3457 }
3458 })) {
3459 return;
3460 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003461
Winson Chungf0c6ae02012-03-21 16:10:31 -07003462 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3463 Set<String> newApps = new HashSet<String>();
3464 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3465
3466 Workspace workspace = mWorkspace;
3467 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003469
3470 // Short circuit if we are loading dock items for a configuration which has no dock
3471 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3472 mHotseat == null) {
3473 continue;
3474 }
3475
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 switch (item.itemType) {
3477 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3478 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003479 ShortcutInfo info = (ShortcutInfo) item;
3480 String uri = info.intent.toUri(0).toString();
3481 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003482
3483 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3484 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003485 boolean animateIconUp = false;
3486 synchronized (newApps) {
3487 if (newApps.contains(uri)) {
3488 animateIconUp = newApps.remove(uri);
3489 }
3490 }
3491 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003492 // Prepare the view to be animated up
3493 shortcut.setAlpha(0f);
3494 shortcut.setScaleX(0f);
3495 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003496 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003497 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3498 mNewShortcutAnimateViews.add(shortcut);
3499 }
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003502 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003503 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003504 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003505 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003506 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3507 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003510 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003511
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003513 }
3514
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 /**
3516 * Implementation of the method from LauncherModel.Callbacks.
3517 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003518 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3519 if (waitUntilResume(new Runnable() {
3520 public void run() {
3521 bindFolders(folders);
3522 }
3523 })) {
3524 return;
3525 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003526 sFolders.clear();
3527 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529
3530 /**
3531 * Add the views for a widget to the workspace.
3532 *
3533 * Implementation of the method from LauncherModel.Callbacks.
3534 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003535 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3536 if (waitUntilResume(new Runnable() {
3537 public void run() {
3538 bindAppWidget(item);
3539 }
3540 })) {
3541 return;
3542 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003543
Daniel Sandler843e8602010-06-07 14:59:01 -04003544 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3545 if (DEBUG_WIDGETS) {
3546 Log.d(TAG, "bindAppWidget: " + item);
3547 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 final Workspace workspace = mWorkspace;
3549
3550 final int appWidgetId = item.appWidgetId;
3551 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003552 if (DEBUG_WIDGETS) {
3553 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3554 }
3555
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3557
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003559 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560
Adam Cohendcd297f2013-06-18 13:13:40 -07003561 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003563 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3564
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 workspace.requestLayout();
3566
Daniel Sandler843e8602010-06-07 14:59:01 -04003567 if (DEBUG_WIDGETS) {
3568 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3569 + (SystemClock.uptimeMillis()-start) + "ms");
3570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
3572
Adam Cohen1462de32012-07-24 22:34:36 -07003573 public void onPageBoundSynchronously(int page) {
3574 mSynchronouslyBoundPages.add(page);
3575 }
3576
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 /**
3578 * Callback saying that there aren't any more items to bind.
3579 *
3580 * Implementation of the method from LauncherModel.Callbacks.
3581 */
Adam Cohene25af792013-06-06 23:08:25 -07003582 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003583 if (waitUntilResume(new Runnable() {
3584 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003585 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003586 }
3587 })) {
3588 return;
3589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 if (mSavedState != null) {
3591 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003592 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 mSavedState = null;
3595 }
3596
Adam Cohen1462de32012-07-24 22:34:36 -07003597 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003599 // If we received the result of any pending adds while the loader was running (e.g. the
3600 // widget configuration forced an orientation change), process them now.
3601 for (int i = 0; i < sPendingAddList.size(); i++) {
3602 completeAdd(sPendingAddList.get(i));
3603 }
3604 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605
Winson Chungc51db6a2011-10-05 11:44:49 -07003606 // Update the market app icon as necessary (the other icons will be managed in response to
3607 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003608 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003609
Winson Chungf0c6ae02012-03-21 16:10:31 -07003610 // Animate up any icons as necessary
3611 if (mVisible || mWorkspaceLoading) {
3612 Runnable newAppsRunnable = new Runnable() {
3613 @Override
3614 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003615 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003616 }
3617 };
Winson Chunga2413752012-04-03 14:22:34 -07003618
Adam Cohendcd297f2013-06-18 13:13:40 -07003619 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3620 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003621 if (canRunNewAppsAnimation()) {
3622 // If the user has not interacted recently, then either snap to the new page to show
3623 // the new-apps animation or just run them if they are to appear on the current page
3624 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003625 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003626 } else {
3627 runNewAppsAnimation(false);
3628 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003629 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003630 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003631 // are on another page (or just normally if they are added to the current page)
3632 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003633 }
3634 }
3635
3636 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003637 if (upgradePath) {
3638 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003639 mWorkspace.stripDuplicateApps();
3640 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003641 }
Adam Cohen99894d92013-06-14 11:22:59 -07003642
Adam Cohen66a01fd2013-06-11 12:48:00 -07003643 mWorkspace.post(new Runnable() {
3644 @Override
3645 public void run() {
3646 onFinishBindingItems();
3647 }
3648 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003649 }
3650
Winson Chunga2413752012-04-03 14:22:34 -07003651 private boolean canRunNewAppsAnimation() {
3652 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3653 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3654 }
3655
Winson Chungf0c6ae02012-03-21 16:10:31 -07003656 /**
3657 * Runs a new animation that scales up icons that were added while Launcher was in the
3658 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003659 *
3660 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003661 */
Winson Chunga2413752012-04-03 14:22:34 -07003662 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003663 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003664 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003665
3666 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003667 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3668 @Override
3669 public int compare(View a, View b) {
3670 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3671 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3672 int cellCountX = LauncherModel.getCellCountX();
3673 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3674 }
3675 });
Winson Chunga2413752012-04-03 14:22:34 -07003676
3677 // Animate each of the views in place (or show them immediately if requested)
3678 if (immediate) {
3679 for (View v : mNewShortcutAnimateViews) {
3680 v.setAlpha(1f);
3681 v.setScaleX(1f);
3682 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 }
Winson Chunga2413752012-04-03 14:22:34 -07003684 } else {
3685 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3686 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003687 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003688 PropertyValuesHolder.ofFloat("alpha", 1f),
3689 PropertyValuesHolder.ofFloat("scaleX", 1f),
3690 PropertyValuesHolder.ofFloat("scaleY", 1f));
3691 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3692 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3693 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3694 bounceAnims.add(bounceAnim);
3695 }
3696 anim.playTogether(bounceAnims);
3697 anim.addListener(new AnimatorListenerAdapter() {
3698 @Override
3699 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003700 if (mWorkspace != null) {
3701 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3702 }
Winson Chunga2413752012-04-03 14:22:34 -07003703 }
3704 });
3705 anim.start();
3706 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707
3708 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003709 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003710 mNewShortcutAnimateViews.clear();
3711 new Thread("clearNewAppsThread") {
3712 public void run() {
3713 mSharedPrefs.edit()
3714 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3715 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3716 .commit();
3717 }
3718 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003719 }
3720
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003721 @Override
3722 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003723 boolean searchVisible = updateGlobalSearchIcon();
3724 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003725 if (mSearchDropTargetBar != null) {
3726 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3727 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003728 }
3729
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003730 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 * Add the icons for all apps.
3732 *
3733 * Implementation of the method from LauncherModel.Callbacks.
3734 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003735 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003736 Runnable setAllAppsRunnable = new Runnable() {
3737 public void run() {
3738 if (mAppsCustomizeContent != null) {
3739 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003740
3741 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3742 getHotseat().addAllAppsFolder(mIconCache, apps,
3743 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3744 mIntentsOnWorkspaceFromUpgradePath = null;
3745 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003746 }
3747 }
3748 };
3749
Michael Jurkac57b7a82011-08-09 22:02:20 -07003750 // Remove the progress bar entirely; we could also make it GONE
3751 // but better to remove it since we know it's not going to be used
3752 View progressBar = mAppsCustomizeTabHost.
3753 findViewById(R.id.apps_customize_progress_bar);
3754 if (progressBar != null) {
3755 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003756
3757 // We just post the call to setApps so the user sees the progress bar
3758 // disappear-- otherwise, it just looks like the progress bar froze
3759 // which doesn't look great
3760 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3761 } else {
3762 // If we did not initialize the spinner in onCreate, then we can directly set the
3763 // list of applications without waiting for any progress bars views to be hidden.
3764 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 }
3767
3768 /**
3769 * A package was installed.
3770 *
3771 * Implementation of the method from LauncherModel.Callbacks.
3772 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003773 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3774 if (waitUntilResume(new Runnable() {
3775 public void run() {
3776 bindAppsAdded(apps);
3777 }
3778 })) {
3779 return;
3780 }
3781
Winson Chungf0ea4d32011-06-06 14:27:16 -07003782
Winson Chung785d2eb2011-04-14 16:08:02 -07003783 if (mAppsCustomizeContent != null) {
3784 mAppsCustomizeContent.addApps(apps);
3785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 }
3787
3788 /**
3789 * A package was updated.
3790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003793 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3794 if (waitUntilResume(new Runnable() {
3795 public void run() {
3796 bindAppsUpdated(apps);
3797 }
3798 })) {
3799 return;
3800 }
3801
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003802 if (mWorkspace != null) {
3803 mWorkspace.updateShortcuts(apps);
3804 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003805
Winson Chung785d2eb2011-04-14 16:08:02 -07003806 if (mAppsCustomizeContent != null) {
3807 mAppsCustomizeContent.updateApps(apps);
3808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003809 }
3810
3811 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003812 * A package was uninstalled. We take both the super set of packageNames
3813 * in addition to specific applications to remove, the reason being that
3814 * this can be called when a package is updated as well. In that scenario,
3815 * we only remove specific components from the workspace, where as
3816 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003817 *
3818 * Implementation of the method from LauncherModel.Callbacks.
3819 */
Winson Chung83892cc2013-05-01 16:53:33 -07003820 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3821 final ArrayList<ApplicationInfo> appInfos,
3822 final boolean matchPackageNamesOnly) {
3823 if (waitUntilResume(new Runnable() {
3824 public void run() {
3825 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3826 }
3827 })) {
3828 return;
3829 }
3830
3831 if (matchPackageNamesOnly) {
3832 mWorkspace.removeItemsByPackageName(packageNames);
3833 } else {
3834 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003835 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003836
Winson Chung785d2eb2011-04-14 16:08:02 -07003837 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003838 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003839 }
Winson Chunga1820962011-10-03 16:31:06 -07003840
3841 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003842 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003843 }
Joe Onoratobe386092009-11-17 17:32:16 -08003844
3845 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003846 * A number of packages were updated.
3847 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003848
3849 private ArrayList<Object> mWidgetsAndShortcuts;
3850 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003851 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003852 bindPackagesUpdated(mWidgetsAndShortcuts);
3853 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003854 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003855 };
3856
3857 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3858 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3859 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003860 return;
3861 }
3862
Winson Chung785d2eb2011-04-14 16:08:02 -07003863 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003864 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003865 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003866 }
3867
Winson Chung400438b2011-01-16 17:53:48 -08003868 private int mapConfigurationOriActivityInfoOri(int configOri) {
3869 final Display d = getWindowManager().getDefaultDisplay();
3870 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3871 switch (d.getRotation()) {
3872 case Surface.ROTATION_0:
3873 case Surface.ROTATION_180:
3874 // We are currently in the same basic orientation as the natural orientation
3875 naturalOri = configOri;
3876 break;
3877 case Surface.ROTATION_90:
3878 case Surface.ROTATION_270:
3879 // We are currently in the other basic orientation to the natural orientation
3880 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3881 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3882 break;
3883 }
3884
3885 int[] oriMap = {
3886 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3887 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3888 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3889 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3890 };
3891 // Since the map starts at portrait, we need to offset if this device's natural orientation
3892 // is landscape.
3893 int indexOffset = 0;
3894 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3895 indexOffset = 1;
3896 }
3897 return oriMap[(d.getRotation() + indexOffset) % 4];
3898 }
Adam Cohen446e9402011-09-15 18:21:21 -07003899
Winson Chung4b919f82012-05-01 10:44:08 -07003900 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003901 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003902 getResources().getBoolean(R.bool.allow_rotation);
3903 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003904 }
Winson Chung4b919f82012-05-01 10:44:08 -07003905 public void lockScreenOrientation() {
3906 if (isRotationEnabled()) {
3907 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3908 .getConfiguration().orientation));
3909 }
3910 }
3911 public void unlockScreenOrientation(boolean immediate) {
3912 if (isRotationEnabled()) {
3913 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003914 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003915 } else {
3916 mHandler.postDelayed(new Runnable() {
3917 public void run() {
3918 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3919 }
3920 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003921 }
Winson Chung4b919f82012-05-01 10:44:08 -07003922 }
Winson Chung400438b2011-01-16 17:53:48 -08003923 }
3924
Winson Chung82f55532011-08-09 14:14:23 -07003925 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003926 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003927 if (DISABLE_CLINGS) {
3928 return false;
3929 }
3930
Brett Chabot2a9e2282011-08-23 15:03:13 -07003931 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003932 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003933
Michael Jurkae233a8b2013-03-19 13:49:20 +01003934 // Restricted secondary users (child mode) will potentially have very few apps
3935 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003936// boolean supportsLimitedUsers =
3937// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3938// Account[] accounts = AccountManager.get(this).getAccounts();
3939// if (supportsLimitedUsers && accounts.length == 0) {
3940// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3941// Bundle restrictions = um.getUserRestrictions();
3942// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3943// return false;
3944// }
3945// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003946 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003947 }
Michael Jurka22143132012-10-04 17:42:38 +02003948
Winson Chung7d7541e2011-09-16 20:14:36 -07003949 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003950 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003951 if (cling != null) {
3952 cling.init(this, positionData);
3953 cling.setVisibility(View.VISIBLE);
3954 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3955 if (animate) {
3956 cling.buildLayer();
3957 cling.setAlpha(0f);
3958 cling.animate()
3959 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003960 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003961 .setDuration(SHOW_CLING_DURATION)
3962 .setStartDelay(delay)
3963 .start();
3964 } else {
3965 cling.setAlpha(1f);
3966 }
Michael Jurka22143132012-10-04 17:42:38 +02003967 cling.setFocusableInTouchMode(true);
3968 cling.post(new Runnable() {
3969 public void run() {
3970 cling.setFocusable(true);
3971 cling.requestFocus();
3972 }
3973 });
3974 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3975 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003976 }
3977 return cling;
3978 }
Michael Jurka22143132012-10-04 17:42:38 +02003979
Winson Chung7d7541e2011-09-16 20:14:36 -07003980 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003981 // To catch cases where siblings of top-level views are made invisible, just check whether
3982 // the cling is directly set to GONE before dismissing it.
3983 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003984 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003985 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003986 anim.addListener(new AnimatorListenerAdapter() {
3987 public void onAnimationEnd(Animator animation) {
3988 cling.setVisibility(View.GONE);
3989 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003990 // We should update the shared preferences on a background thread
3991 new Thread("dismissClingThread") {
3992 public void run() {
3993 SharedPreferences.Editor editor = mSharedPrefs.edit();
3994 editor.putBoolean(flag, true);
3995 editor.commit();
3996 }
3997 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003998 };
3999 });
4000 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004001 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004002 }
4003 }
Michael Jurka22143132012-10-04 17:42:38 +02004004
Winson Chung9d9d74f2011-09-19 11:49:12 -07004005 private void removeCling(int id) {
4006 final View cling = findViewById(id);
4007 if (cling != null) {
4008 final ViewGroup parent = (ViewGroup) cling.getParent();
4009 parent.post(new Runnable() {
4010 @Override
4011 public void run() {
4012 parent.removeView(cling);
4013 }
4014 });
Michael Jurka22143132012-10-04 17:42:38 +02004015 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004016 }
4017 }
Michael Jurka974c3862012-05-22 22:00:31 -07004018
4019 private boolean skipCustomClingIfNoAccounts() {
4020 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4021 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4022 if (customCling) {
4023 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004024 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004025 Account[] accounts = am.getAccountsByType("com.google");
4026 return accounts.length == 0;
4027 }
4028 return false;
4029 }
4030
Winson Chung7d7541e2011-09-16 20:14:36 -07004031 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004032 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004033 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004034 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4035 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004036 // If we're not using the default workspace layout, replace workspace cling
4037 // with a custom workspace cling (usually specified in an overlay)
4038 // For now, only do this on tablets
4039 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004040 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004041 // Use a custom cling
4042 View cling = findViewById(R.id.workspace_cling);
4043 ViewGroup clingParent = (ViewGroup) cling.getParent();
4044 int clingIndex = clingParent.indexOfChild(cling);
4045 clingParent.removeViewAt(clingIndex);
4046 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4047 clingParent.addView(customCling, clingIndex);
4048 customCling.setId(R.id.workspace_cling);
4049 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004050 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004051 } else {
4052 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004053 }
4054 }
4055 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004057 if (isClingsEnabled() &&
4058 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004060 } else {
4061 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 }
4063 }
4064 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004066 if (isClingsEnabled() &&
4067 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4068 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004069 } else {
4070 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004071 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 }
4074 public boolean isFolderClingVisible() {
4075 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004076 if (cling != null) {
4077 return cling.getVisibility() == View.VISIBLE;
4078 }
4079 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004080 }
Winson Chung82f55532011-08-09 14:14:23 -07004081 public void dismissWorkspaceCling(View v) {
4082 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004083 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004084 }
4085 public void dismissAllAppsCling(View v) {
4086 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004087 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4088 }
4089 public void dismissFolderCling(View v) {
4090 Cling cling = (Cling) findViewById(R.id.folder_cling);
4091 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004092 }
4093
Winson Chung80baf5a2010-08-09 16:03:15 -07004094 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004095 * Prints out out state for debugging.
4096 */
4097 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004098 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004099 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004100 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4101 Log.d(TAG, "mRestoring=" + mRestoring);
4102 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4103 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004104 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004105 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004106
Winson Chung785d2eb2011-04-14 16:08:02 -07004107 if (mAppsCustomizeContent != null) {
4108 mAppsCustomizeContent.dumpState();
4109 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004110 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004111 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004112
4113 @Override
4114 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4115 super.dump(prefix, fd, writer, args);
4116 writer.println(" ");
4117 writer.println("Debug logs: ");
4118 for (int i = 0; i < sDumpLogs.size(); i++) {
4119 writer.println(" " + sDumpLogs.get(i));
4120 }
4121 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004122
4123 public static void dumpDebugLogsToConsole() {
4124 Log.d(TAG, "");
4125 Log.d(TAG, "*********************");
4126 Log.d(TAG, "Launcher debug logs: ");
4127 for (int i = 0; i < sDumpLogs.size(); i++) {
4128 Log.d(TAG, " " + sDumpLogs.get(i));
4129 }
4130 Log.d(TAG, "*********************");
4131 Log.d(TAG, "");
4132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004133}
Michael Jurka2763be32011-02-24 11:19:57 -08004134
Michael Jurkaabded662011-03-04 12:06:57 -08004135interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004136 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004137 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004138 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004139 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004140 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004141}