blob: 6c9a5042fd208052253c30612c8d12d6636595f3 [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 Sandlerf061f822013-06-27 13:59:36 -0400111 static final boolean PROFILE_STARTUP = false;
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 Sandlerf061f822013-06-27 13:59:36 -0400114 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700115
Winson Chung70d72102011-08-12 11:24:35 -0700116 private static final int MENU_GROUP_WALLPAPER = 1;
117 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
118 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700119 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
120 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int REQUEST_PICK_APPLICATION = 6;
125 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700127 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Michael Jurka8b805b12012-04-18 14:23:14 -0700129 private static final int REQUEST_BIND_APPWIDGET = 11;
130
Mathew Inwood876a8462013-06-14 14:12:41 +0100131 /**
132 * IntentStarter uses request codes starting with this. This must be greater than all activity
133 * request codes used internally.
134 */
135 protected static final int REQUEST_LAST = 100;
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Romain Guy98d01652009-06-30 16:21:04 -0700142 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800143 // To turn on these properties, type
144 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
145 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
146 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Winson Chung2672ff92012-05-04 16:22:30 -0700148 // The Intent extra that defines whether to ignore the launch animation
149 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400150 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700157 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
158 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
170 // Type: int
171 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
172 // Type: parcelable
173 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100175 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700176 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100177 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700178 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100179 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700181 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700182 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700183 private State mState = State.WORKSPACE;
184 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800185 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700188 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
189 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700190 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700191 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800194 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Winson Chunga2413752012-04-03 14:22:34 -0700196 // How long to wait before the new-shortcut animation automatically pans the workspace
197 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
198
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800199 private final BroadcastReceiver mCloseSystemDialogsReceiver
200 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800201 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private LayoutInflater mInflater;
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800206 private View mQsbDivider;
207 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700208 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800209 private DragLayer mDragLayer;
210 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 private AppWidgetManager mAppWidgetManager;
213 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Michael Jurkac9d95c52011-08-29 14:03:34 -0700215 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700216 private AppWidgetProviderInfo mPendingAddWidgetInfo;
217
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218 private int[] mTmpAddItemCellCoordinates = new int[2];
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private FolderInfo mFolderInfo;
221
Winson Chung3d503fb2011-07-13 17:25:49 -0700222 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800223 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700224
Winson Chungc51db6a2011-10-05 11:44:49 -0700225 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700226 private AppsCustomizeTabHost mAppsCustomizeTabHost;
227 private AppsCustomizePagedView mAppsCustomizeContent;
228 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700231 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
232 // scroll issues (because the workspace may not have been measured yet) and extra work.
233 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
234 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
236 private SpannableStringBuilder mDefaultKeySsb = null;
237
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238 private boolean mWorkspaceLoading = true;
239
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800240 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private boolean mRestoring;
242 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700243 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244
Michael Jurka7607c2f2013-04-03 14:33:19 -0700245 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
246
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // Keep track of whether the user has left launcher
248 private static boolean sPausedFromUserAction = false;
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private Bundle mSavedInstanceState;
251
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800254 private boolean mUserPresent = true;
255 private boolean mVisible = false;
256 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700257 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static LocaleConfiguration sLocaleConfiguration = null;
260
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700261 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700279 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
280 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Craig Mautner360310b2012-10-26 15:13:08 -0700283 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700284
Adam Cohen1462de32012-07-24 22:34:36 -0700285 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
286
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700287 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
288
Winson Chung46353de2012-02-16 14:05:10 -0800289 // We only want to get the SharedPreferences once since it does an FS stat each time we get
290 // it from the context.
291 private SharedPreferences mSharedPrefs;
292
Adam Cohene25af792013-06-06 23:08:25 -0700293 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
294
Winson Chungf0c6ae02012-03-21 16:10:31 -0700295 // Holds the page that we need to animate to, and the icon views that we need to animate up
296 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700297 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700298 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700299 private ImageView mFolderIconImageView;
300 private Bitmap mFolderIconBitmap;
301 private Canvas mFolderIconCanvas;
302 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700303
Michael Jurkaddd62e92011-02-16 17:49:14 -0800304 private BubbleTextView mWaitingForResume;
305
Michael Jurka22143132012-10-04 17:42:38 +0200306 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
307 = new HideFromAccessibilityHelper();
308
Michael Jurkac1f5d262011-09-30 19:32:27 -0700309 private Runnable mBuildLayersRunnable = new Runnable() {
310 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700311 if (mWorkspace != null) {
312 mWorkspace.buildPageHardwareLayers();
313 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700314 }
315 };
316
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800317 private static ArrayList<PendingAddArguments> sPendingAddList
318 = new ArrayList<PendingAddArguments>();
319
Michael Jurka0a457bf2012-11-19 14:05:05 -0800320 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
321
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800322 private static class PendingAddArguments {
323 int requestCode;
324 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700325 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700326 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800327 int cellX;
328 int cellY;
329 }
330
Michael Jurka0a457bf2012-11-19 14:05:05 -0800331 private static boolean isPropertyEnabled(String propertyName) {
332 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700333 }
334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 @Override
336 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700337 if (DEBUG_STRICT_MODE) {
338 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
339 .detectDiskReads()
340 .detectDiskWrites()
341 .detectNetwork() // or .detectAll() for all detectable problems
342 .penaltyLog()
343 .build());
344 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
345 .detectLeakedSqlLiteObjects()
346 .detectLeakedClosableObjects()
347 .penaltyLog()
348 .penaltyDeath()
349 .build());
350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400353 LauncherAppState app = LauncherAppState.getInstance();
354 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700355 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800356 mModel = app.setLauncher(this);
357 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400358 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700361 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
363 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700364
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700365 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
366 // this also ensures that any synchronous binding below doesn't re-trigger another
367 // LauncherModel load.
368 mPaused = false;
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200371 android.os.Debug.startMethodTracing(
372 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
374
375 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 setContentView(R.layout.launcher);
377 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700378 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800380 registerContentObservers();
381
Joe Onorato7c312c12009-08-13 21:36:53 -0700382 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 mSavedState = savedInstanceState;
385 restoreState(mSavedState);
386
Winson Chunga12a2502010-12-20 14:41:35 -0800387 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700388 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200389 mAppsCustomizeContent.onPackagesUpdated(
390 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700391 }
Winson Chunga12a2502010-12-20 14:41:35 -0800392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 if (PROFILE_STARTUP) {
394 android.os.Debug.stopMethodTracing();
395 }
396
397 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700398 if (sPausedFromUserAction) {
399 // If the user leaves launcher, then we should just load items asynchronously when
400 // they return.
401 mModel.startLoader(true, -1);
402 } else {
403 // We only load the page synchronously if the user rotates (or triggers a
404 // configuration change) while launcher is in the foreground
405 mModel.startLoader(true, mWorkspace.getCurrentPage());
406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Michael Jurkac57b7a82011-08-09 22:02:20 -0700409 if (!mModel.isAllAppsLoaded()) {
410 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
411 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 // For handling default keys
415 mDefaultKeySsb = new SpannableStringBuilder();
416 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800417
418 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
419 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800420
Adam Cohenf9426d52012-06-04 17:26:21 -0700421 updateGlobalIcons();
422
423 // On large interfaces, we want the screen to auto-rotate based on the current orientation
424 unlockScreenOrientation(true);
425 }
426
Winson Chung4a2afa32012-07-19 14:53:05 -0700427 protected void onUserLeaveHint() {
428 super.onUserLeaveHint();
429 sPausedFromUserAction = true;
430 }
431
Adam Cohenf9426d52012-06-04 17:26:21 -0700432 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700433 boolean searchVisible = false;
434 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800435 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700436 int coi = getCurrentOrientationIndexForGlobalIcons();
437 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
438 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700439 updateAppMarketIcon();
440 searchVisible = updateGlobalSearchIcon();
441 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700443 if (sGlobalSearchIcon[coi] != null) {
444 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700445 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sVoiceSearchIcon[coi] != null) {
448 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700449 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 if (sAppMarketIcon[coi] != null) {
452 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800453 }
Winson Chungadf0c182012-08-23 14:59:07 -0700454 if (mSearchDropTargetBar != null) {
455 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
Romain Guycbb89e42009-06-08 15:52:54 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 if (sLocaleConfiguration == null) {
461 new AsyncTask<Void, Void, LocaleConfiguration>() {
462 @Override
463 protected LocaleConfiguration doInBackground(Void... unused) {
464 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
465 readConfiguration(Launcher.this, localeConfiguration);
466 return localeConfiguration;
467 }
468
469 @Override
470 protected void onPostExecute(LocaleConfiguration result) {
471 sLocaleConfiguration = result;
472 checkForLocaleChange(); // recursive, but now with a locale configuration
473 }
474 }.execute();
475 return;
476 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 final Configuration configuration = getResources().getConfiguration();
479
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final String locale = configuration.locale.toString();
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final int mcc = configuration.mcc;
485
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final int mnc = configuration.mnc;
488
Romain Guy84f296c2009-11-04 15:00:44 -0800489 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490
Romain Guy84f296c2009-11-04 15:00:44 -0800491 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700492 sLocaleConfiguration.locale = locale;
493 sLocaleConfiguration.mcc = mcc;
494 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700495
Joe Onorato0589f0f2010-02-08 13:44:00 -0800496 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497
498 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
499 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700500 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 public void run() {
502 writeConfiguration(Launcher.this, localeConfiguration);
503 }
504 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700505 }
506 }
507
508 private static class LocaleConfiguration {
509 public String locale;
510 public int mcc = -1;
511 public int mnc = -1;
512 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700513
Romain Guy98d01652009-06-30 16:21:04 -0700514 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
515 DataInputStream in = null;
516 try {
517 in = new DataInputStream(context.openFileInput(PREFERENCES));
518 configuration.locale = in.readUTF();
519 configuration.mcc = in.readInt();
520 configuration.mnc = in.readInt();
521 } catch (FileNotFoundException e) {
522 // Ignore
523 } catch (IOException e) {
524 // Ignore
525 } finally {
526 if (in != null) {
527 try {
528 in.close();
529 } catch (IOException e) {
530 // Ignore
531 }
532 }
533 }
534 }
535
536 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
537 DataOutputStream out = null;
538 try {
539 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
540 out.writeUTF(configuration.locale);
541 out.writeInt(configuration.mcc);
542 out.writeInt(configuration.mnc);
543 out.flush();
544 } catch (FileNotFoundException e) {
545 // Ignore
546 } catch (IOException e) {
547 //noinspection ResultOfMethodCallIgnored
548 context.getFileStreamPath(PREFERENCES).delete();
549 } finally {
550 if (out != null) {
551 try {
552 out.close();
553 } catch (IOException e) {
554 // Ignore
555 }
556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558 }
559
Bjorn Bringertc459e522013-06-07 19:36:01 +0100560 public LayoutInflater getInflater() {
561 return mInflater;
562 }
563
Adam Cohen716b51e2011-06-30 12:09:54 -0700564 public DragLayer getDragLayer() {
565 return mDragLayer;
566 }
567
Winson Chung36a62fe2012-05-06 18:04:42 -0700568 boolean isDraggingEnabled() {
569 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
570 // that is subsequently removed from the workspace in startBinding().
571 return !mModel.isLoadingWorkspace();
572 }
573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 static int getScreen() {
575 synchronized (sLock) {
576 return sScreen;
577 }
578 }
579
580 static void setScreen(int screen) {
581 synchronized (sLock) {
582 sScreen = screen;
583 }
584 }
585
Winson Chung557d6ed2011-07-08 15:34:52 -0700586 /**
587 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
588 * a configuration step, this allows the proper animations to run after other transitions.
589 */
590 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700591 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 switch (args.requestCode) {
593 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700594 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700595 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800596 break;
597 case REQUEST_PICK_SHORTCUT:
598 processShortcut(args.intent);
599 break;
600 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700601 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700602 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 result = true;
604 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 case REQUEST_CREATE_APPWIDGET:
606 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700607 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700608 result = true;
609 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_WALLPAPER:
611 // We just wanted the activity result here so we can clear mWaitingForResult
612 break;
613 }
Michael Jurka27614d22012-04-02 06:40:22 -0700614 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
615 // if you turned the screen off and then back while in All Apps, Launcher would not
616 // return to the workspace. Clearing mAddInfo.container here fixes this issue
617 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 }
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 protected void onActivityResult(
623 final int requestCode, final int resultCode, final Intent data) {
624 if (requestCode == REQUEST_BIND_APPWIDGET) {
625 int appWidgetId = data != null ?
626 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
627 if (resultCode == RESULT_CANCELED) {
628 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
629 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700630 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
632 return;
633 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700634 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
636 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700637 mWaitingForResult = false;
638
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 // We have special handling for widgets
640 if (isWidgetDrop) {
641 int appWidgetId = data != null ?
642 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700643 if (appWidgetId < 0) {
644 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
645 "widget configuration activity.");
646 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
647 } else {
648 completeTwoStageWidgetDrop(resultCode, appWidgetId);
649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 return;
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700658 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 final PendingAddArguments args = new PendingAddArguments();
660 args.requestCode = requestCode;
661 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700663 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700664 args.cellX = mPendingAddInfo.cellX;
665 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 if (isWorkspaceLocked()) {
667 sPendingAddList.add(args);
668 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
675 null);
676 }
677
678 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700680 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 Runnable onCompleteRunnable = null;
682 int animationType = 0;
683
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 if (resultCode == RESULT_OK) {
686 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
687 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700688 mPendingAddWidgetInfo);
689 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 onCompleteRunnable = new Runnable() {
691 @Override
692 public void run() {
693 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
696 null);
697 }
698 };
699 } else if (resultCode == RESULT_CANCELED) {
700 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
705 null);
706 }
707 };
708 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700709 if (mDragLayer.getAnimatedView() != null) {
710 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
711 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
712 animationType, boundWidget, true);
713 } else {
714 // The animated view may be null in the case of a rotation during widget configuration
715 onCompleteRunnable.run();
716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
719 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700720 protected void onStop() {
721 super.onStop();
722 FirstFrameAnimatorHelper.setIsVisible(false);
723 }
724
725 @Override
726 protected void onStart() {
727 super.onStart();
728 FirstFrameAnimatorHelper.setIsVisible(true);
729 }
730
731 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200733 long startTime = 0;
734 if (DEBUG_RESUME_TIME) {
735 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400736 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700739
Winson Chung4a2afa32012-07-19 14:53:05 -0700740 // Restore the previous launcher state
741 if (mOnResumeState == State.WORKSPACE) {
742 showWorkspace(false);
743 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
744 showAllApps(false);
745 }
746 mOnResumeState = State.NONE;
747
Craig Mautner360310b2012-10-26 15:13:08 -0700748 // Background was set to gradient in onPause(), restore to black if in all apps.
749 setWorkspaceBackground(mState == State.WORKSPACE);
750
Winson Chungde0fb8f2012-05-08 14:37:08 -0700751 // Process any items that were added while Launcher was away
752 InstallShortcutReceiver.flushInstallQueue(this);
753
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800754 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700755 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700756 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700758 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700760 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200762 if (mOnResumeCallbacks.size() > 0) {
763 // We might have postponed some bind calls until onResume (see waitUntilResume) --
764 // execute them here
765 long startTimeCallbacks = 0;
766 if (DEBUG_RESUME_TIME) {
767 startTimeCallbacks = System.currentTimeMillis();
768 }
769
770 if (mAppsCustomizeContent != null) {
771 mAppsCustomizeContent.setBulkBind(true);
772 }
773 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
774 mOnResumeCallbacks.get(i).run();
775 }
776 if (mAppsCustomizeContent != null) {
777 mAppsCustomizeContent.setBulkBind(false);
778 }
779 mOnResumeCallbacks.clear();
780 if (DEBUG_RESUME_TIME) {
781 Log.d(TAG, "Time spent processing callbacks in onResume: " +
782 (System.currentTimeMillis() - startTimeCallbacks));
783 }
Michael Jurka447bf842013-05-15 14:52:15 +0200784 }
Winson Chunge4e50662012-01-23 14:45:13 -0800785
786 // Reset the pressed state of icons that were locked in the press state while activities
787 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800788 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800789 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800790 mWaitingForResume.setStayPressed(false);
791 }
Winson Chunge4e50662012-01-23 14:45:13 -0800792 if (mAppsCustomizeContent != null) {
793 // Resets the previous all apps icon press state
794 mAppsCustomizeContent.resetDrawableState();
795 }
Adam Cohen06dff352012-06-01 17:17:08 -0700796 // It is possible that widgets can receive updates while launcher is not in the foreground.
797 // Consequently, the widgets will be inflated in the orientation of the foreground activity
798 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
799 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700800 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700801
802 // Again, as with the above scenario, it's possible that one or more of the global icons
803 // were updated in the wrong orientation.
804 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200805 if (DEBUG_RESUME_TIME) {
806 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
807 }
Adam Cohen06dff352012-06-01 17:17:08 -0700808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700811 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700812 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
813 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
814 // when Launcher resumes and we are still in AllApps.
815 updateWallpaperVisibility(true);
816
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700817 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700818 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800819 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700820 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700821 }
Romain Guycbb89e42009-06-08 15:52:54 -0700822
Adam Cohen66a01fd2013-06-11 12:48:00 -0700823 protected void onFinishBindingItems() {
824 }
825
826 // Add a fullscreen unpadded view to the workspace to the left all other screens.
827 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700828 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700829 }
830
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700831 @Override
832 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833 // Flag the loader to stop early before switching
834 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700835 if (mAppsCustomizeContent != null) {
836 mAppsCustomizeContent.surrender();
837 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800838 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700839 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700840
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800841 // We can't hide the IME if it was forced open. So don't bother
842 /*
843 @Override
844 public void onWindowFocusChanged(boolean hasFocus) {
845 super.onWindowFocusChanged(hasFocus);
846
847 if (hasFocus) {
848 final InputMethodManager inputManager = (InputMethodManager)
849 getSystemService(Context.INPUT_METHOD_SERVICE);
850 WindowManager.LayoutParams lp = getWindow().getAttributes();
851 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
852 android.os.Handler()) {
853 protected void onReceiveResult(int resultCode, Bundle resultData) {
854 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
855 }
856 });
857 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
858 }
859 }
860 */
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 private boolean acceptFilter() {
863 final InputMethodManager inputManager = (InputMethodManager)
864 getSystemService(Context.INPUT_METHOD_SERVICE);
865 return !inputManager.isFullscreenMode();
866 }
867
868 @Override
869 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700870 final int uniChar = event.getUnicodeChar();
871 final boolean handled = super.onKeyDown(keyCode, event);
872 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
873 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
875 keyCode, event);
876 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700877 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700878 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700879 // showSearchDialog()
880 // If there are multiple keystrokes before the search dialog takes focus,
881 // onSearchRequested() will be called for every keystroke,
882 // but it is idempotent, so it's fine.
883 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885 }
886
Joe Onorato8a9625e2010-01-28 15:55:35 -0800887 // Eat the long press event so the keyboard doesn't come up.
888 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
889 return true;
890 }
891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 return handled;
893 }
894
Karl Rosaen138a0412009-04-23 19:00:21 -0700895 private String getTypedText() {
896 return mDefaultKeySsb.toString();
897 }
898
899 private void clearTypedText() {
900 mDefaultKeySsb.clear();
901 mDefaultKeySsb.clearSpans();
902 Selection.setSelection(mDefaultKeySsb, 0);
903 }
904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700906 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
907 * State
908 */
909 private static State intToState(int stateOrdinal) {
910 State state = State.WORKSPACE;
911 final State[] stateValues = State.values();
912 for (int i = 0; i < stateValues.length; i++) {
913 if (stateValues[i].ordinal() == stateOrdinal) {
914 state = stateValues[i];
915 break;
916 }
917 }
918 return state;
919 }
920
921 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 * Restores the previous state, if it exists.
923 *
924 * @param savedState The previous state.
925 */
926 private void restoreState(Bundle savedState) {
927 if (savedState == null) {
928 return;
929 }
930
Michael Jurka883f55b2010-10-21 15:47:14 -0700931 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700932 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700933 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800934 }
935
Winson Chungf0c6ae02012-03-21 16:10:31 -0700936 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700938 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
Winson Chung3d503fb2011-07-13 17:25:49 -0700941 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700943
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
945 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700946 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
948 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700949 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
950 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
951 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700952 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 mRestoring = true;
954 }
955
956 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
957 if (renameFolder) {
958 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700959 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 mRestoring = true;
961 }
Winson Chunga12a2502010-12-20 14:41:35 -0800962
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 // Restore the AppsCustomize tab
964 if (mAppsCustomizeTabHost != null) {
965 String curTab = savedState.getString("apps_customize_currentTab");
966 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700967 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700968 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700969 mAppsCustomizeContent.loadAssociatedPages(
970 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 }
972
Winson Chung5afbf7b2011-07-25 11:53:08 -0700973 int currentIndex = savedState.getInt("apps_customize_currentIndex");
974 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 /**
979 * Finds all the views we need and configure them properly.
980 */
981 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700982 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400983
Craig Mautner360310b2012-10-26 15:13:08 -0700984 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700985 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
986 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700987 mQsbDivider = findViewById(R.id.qsb_divider);
988 mDockDivider = findViewById(R.id.dock_divider);
989
990 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
991 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
Winson Chung4c98d922011-05-31 16:50:48 -0700993 // Setup the drag layer
994 mDragLayer.setup(this, dragController);
995
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 // Setup the hotseat
997 mHotseat = (Hotseat) findViewById(R.id.hotseat);
998 if (mHotseat != null) {
999 mHotseat.setup(this);
1000 }
1001
Winson Chung4c98d922011-05-31 16:50:48 -07001002 // Setup the workspace
1003 mWorkspace.setHapticFeedbackEnabled(false);
1004 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001005 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001006 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001007
Winson Chungf0ea4d32011-06-06 14:27:16 -07001008 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001009 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001010
Winson Chungf0ea4d32011-06-06 14:27:16 -07001011 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001012 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001013 mAppsCustomizeContent = (AppsCustomizePagedView)
1014 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1015 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001016
Winson Chung3d503fb2011-07-13 17:25:49 -07001017 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001018 dragController.setDragScoller(mWorkspace);
1019 dragController.setScrollView(mDragLayer);
1020 dragController.setMoveTarget(mWorkspace);
1021 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001022 if (mSearchDropTargetBar != null) {
1023 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001024 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001025
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001026 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001027 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001028 final View ww = new WeightWatcher(this);
1029 ww.setAlpha(0.5f);
1030 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001031 new FrameLayout.LayoutParams(
1032 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001033 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001034 Gravity.BOTTOM)
1035 );
1036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038
1039 /**
1040 * Creates a view representing a shortcut.
1041 *
1042 * @param info The data structure describing the shortcut.
1043 *
1044 * @return A View inflated from R.layout.application.
1045 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001046 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001048 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050
1051 /**
1052 * Creates a view representing a shortcut inflated from the specified resource.
1053 *
1054 * @param layoutResId The id of the XML layout used to create the shortcut.
1055 * @param parent The group the shortcut belongs to.
1056 * @param info The data structure describing the shortcut.
1057 *
1058 * @return A View inflated from layoutResId.
1059 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001060 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001061 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1062 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 return favorite;
1065 }
1066
1067 /**
1068 * Add an application shortcut to the workspace.
1069 *
1070 * @param data The intent describing the application.
1071 * @param cellInfo The position on screen where to create the shortcut.
1072 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001073 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001075 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076
Adam Cohenfbba09b2011-07-18 21:43:05 -07001077 // First we check if we already know the exact location where we want to add this item.
1078 if (cellX >= 0 && cellY >= 0) {
1079 cellXY[0] = cellX;
1080 cellXY[1] = cellY;
1081 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001082 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 return;
1084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001086 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001087
Romain Guy73b979d2009-06-09 12:57:21 -07001088 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001089 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001091 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001092 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001093 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001094 } else {
1095 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
1097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 /**
1100 * Add a shortcut to the workspace.
1101 *
1102 * @param data The intent describing the shortcut.
1103 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001105 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001106 int cellY) {
1107 int[] cellXY = mTmpAddItemCellCoordinates;
1108 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001109 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001110
Michael Jurkad3ef3062010-11-23 16:23:58 -08001111 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001112
Adam Cohen558baaf2011-08-15 15:22:57 -07001113 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001114 if (info == null) {
1115 return;
1116 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001117 final View view = createShortcut(info);
1118
Adam Cohenfbba09b2011-07-18 21:43:05 -07001119 // First we check if we already know the exact location where we want to add this item.
1120 if (cellX >= 0 && cellY >= 0) {
1121 cellXY[0] = cellX;
1122 cellXY[1] = cellY;
1123 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001124
1125 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001126 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001127 true, null,null)) {
1128 return;
1129 }
1130 DragObject dragObject = new DragObject();
1131 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001132 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1133 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001134 return;
1135 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001136 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001137 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001138 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001139 foundCellSpan = (result != null);
1140 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001141 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001142 }
1143
1144 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001145 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001146 return;
1147 }
1148
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150
1151 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001153 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155 }
1156
Adam Cohen2f093b62012-04-30 18:59:53 -07001157 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1158 int minHeight) {
1159 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001160 // We want to account for the extra amount of padding that we are adding to the widget
1161 // to ensure that it gets the full amount of space that it has requested
1162 int requiredWidth = minWidth + padding.left + padding.right;
1163 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001164 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001165 }
1166
Adam Cohen2f093b62012-04-30 18:59:53 -07001167 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1168 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001169 }
1170
Adam Cohen2f093b62012-04-30 18:59:53 -07001171 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1172 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001173 }
1174
Adam Cohen2f093b62012-04-30 18:59:53 -07001175 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1176 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001177 }
1178
Adam Cohen2f093b62012-04-30 18:59:53 -07001179 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1180 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001181 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001182 }
1183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001187 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001190 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001191 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1192 if (appWidgetInfo == null) {
1193 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1194 }
Romain Guycbb89e42009-06-08 15:52:54 -07001195
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001197 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001198
Adam Cohen2f093b62012-04-30 18:59:53 -07001199 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1200 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001203 // We have saved the position to which the widget was dragged-- this really only matters
1204 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001205 int[] cellXY = mTmpAddItemCellCoordinates;
1206 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001208 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001209 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1210 cellXY[0] = mPendingAddInfo.cellX;
1211 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 spanXY[0] = mPendingAddInfo.spanX;
1213 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001214 foundCellSpan = true;
1215 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001217 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001218 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1219 spanXY[1], cellXY, finalSpan);
1220 spanXY[0] = finalSpan[0];
1221 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001222 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001225 }
1226
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001228 if (appWidgetId != -1) {
1229 // Deleting an app widget ID is a void call but writes to disk before returning
1230 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001231 new Thread("deleteAppWidgetId") {
1232 public void run() {
1233 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1234 }
1235 }.start();
1236 }
Winson Chung93eef082012-03-23 15:59:27 -07001237 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001238 return;
1239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001241 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001242 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1243 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 launcherInfo.spanX = spanXY[0];
1245 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001246 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1247 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001250 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251
1252 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001253 if (hostView == null) {
1254 // Perform actual inflation because we're live
1255 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1256 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1257 } else {
1258 // The AppWidgetHostView has already been inflated and instantiated
1259 launcherInfo.hostView = hostView;
1260 }
Romain Guycbb89e42009-06-08 15:52:54 -07001261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001263 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001264 launcherInfo.notifyWidgetSizeChanged(this);
1265
Adam Cohendcd297f2013-06-18 13:13:40 -07001266 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001268
1269 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001271 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
Romain Guycbb89e42009-06-08 15:52:54 -07001273
Adam Cohended9f8d2010-11-03 13:25:16 -07001274 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1275 @Override
1276 public void onReceive(Context context, Intent intent) {
1277 final String action = intent.getAction();
1278 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1279 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001280 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001282
Winson Chungc100e8e2011-08-09 16:02:43 -07001283 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001284 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001285 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1286 mAppsCustomizeTabHost.reset();
1287 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001288 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1290 mUserPresent = true;
1291 updateRunning();
1292 }
1293 }
1294 };
1295
1296 @Override
1297 public void onAttachedToWindow() {
1298 super.onAttachedToWindow();
1299
1300 // Listen for broadcasts related to user-presence
1301 final IntentFilter filter = new IntentFilter();
1302 filter.addAction(Intent.ACTION_SCREEN_OFF);
1303 filter.addAction(Intent.ACTION_USER_PRESENT);
1304 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001305 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001306 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001307 mVisible = true;
1308 }
1309
1310 @Override
1311 public void onDetachedFromWindow() {
1312 super.onDetachedFromWindow();
1313 mVisible = false;
1314
Adam Cohend113e0c2010-11-11 10:48:05 -08001315 if (mAttached) {
1316 unregisterReceiver(mReceiver);
1317 mAttached = false;
1318 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001319 updateRunning();
1320 }
1321
1322 public void onWindowVisibilityChanged(int visibility) {
1323 mVisible = visibility == View.VISIBLE;
1324 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001325 // The following code used to be in onResume, but it turns out onResume is called when
1326 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1327 // is a more appropriate event to handle
1328 if (mVisible) {
1329 mAppsCustomizeTabHost.onWindowVisible();
1330 if (!mWorkspaceLoading) {
1331 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001332 // We want to let Launcher draw itself at least once before we force it to build
1333 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001334 // apps is nice and speedy.
1335 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001336 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001337 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001338 if (mStarted) return;
1339 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001340 // We delay the layer building a bit in order to give
1341 // other message processing a time to run. In particular
1342 // this avoids a delay in hiding the IME if it was
1343 // currently shown, because doing that may involve
1344 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001345 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001346 final ViewTreeObserver.OnDrawListener listener = this;
1347 mWorkspace.post(new Runnable() {
1348 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001349 if (mWorkspace != null &&
1350 mWorkspace.getViewTreeObserver() != null) {
1351 mWorkspace.getViewTreeObserver().
1352 removeOnDrawListener(listener);
1353 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001354 }
1355 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001356 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001357 }
1358 });
1359 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001360 // When Launcher comes back to foreground, a different Activity might be responsible for
1361 // the app market intent, so refresh the icon
1362 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001363 clearTypedText();
1364 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 }
1366
1367 private void sendAdvanceMessage(long delay) {
1368 mHandler.removeMessages(ADVANCE_MSG);
1369 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1370 mHandler.sendMessageDelayed(msg, delay);
1371 mAutoAdvanceSentTime = System.currentTimeMillis();
1372 }
1373
1374 private void updateRunning() {
1375 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1376 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1377 mAutoAdvanceRunning = autoAdvanceRunning;
1378 if (autoAdvanceRunning) {
1379 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1380 sendAdvanceMessage(delay);
1381 } else {
1382 if (!mWidgetsToAdvance.isEmpty()) {
1383 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1384 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1385 }
1386 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001387 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001388 }
1389 }
1390 }
1391
1392 private final Handler mHandler = new Handler() {
1393 @Override
1394 public void handleMessage(Message msg) {
1395 if (msg.what == ADVANCE_MSG) {
1396 int i = 0;
1397 for (View key: mWidgetsToAdvance.keySet()) {
1398 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1399 final int delay = mAdvanceStagger * i;
1400 if (v instanceof Advanceable) {
1401 postDelayed(new Runnable() {
1402 public void run() {
1403 ((Advanceable) v).advance();
1404 }
1405 }, delay);
1406 }
1407 i++;
1408 }
1409 sendAdvanceMessage(mAdvanceInterval);
1410 }
1411 }
1412 };
1413
1414 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001415 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001416 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1417 if (v instanceof Advanceable) {
1418 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001419 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001420 updateRunning();
1421 }
1422 }
1423
1424 void removeWidgetToAutoAdvance(View hostView) {
1425 if (mWidgetsToAdvance.containsKey(hostView)) {
1426 mWidgetsToAdvance.remove(hostView);
1427 updateRunning();
1428 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001429 }
1430
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001432 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001433 launcherInfo.hostView = null;
1434 }
1435
Winson Chung93eef082012-03-23 15:59:27 -07001436 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1437 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1438 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001439 }
1440
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001441 public LauncherAppWidgetHost getAppWidgetHost() {
1442 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Winson Chunga9abd0e2010-10-27 17:18:37 -07001445 public LauncherModel getModel() {
1446 return mModel;
1447 }
1448
Bjorn Bringertc459e522013-06-07 19:36:01 +01001449 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001450 getWindow().closeAllPanels();
1451
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001452 // Whatever we were doing is hereby canceled.
1453 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001454 }
1455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 @Override
1457 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001458 long startTime = 0;
1459 if (DEBUG_RESUME_TIME) {
1460 startTime = System.currentTimeMillis();
1461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 super.onNewIntent(intent);
1463
1464 // Close the menu
1465 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001466 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001467 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001468
Jamie Genniscb222e82012-10-23 15:44:26 -07001469 final boolean alreadyOnHome =
1470 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001471 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001472
Jamie Genniscb222e82012-10-23 15:44:26 -07001473 Runnable processIntent = new Runnable() {
1474 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001475 if (mWorkspace == null) {
1476 // Can be cases where mWorkspace is null, this prevents a NPE
1477 return;
1478 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001479 Folder openFolder = mWorkspace.getOpenFolder();
1480 // In all these cases, only animate if we're already on home
1481 mWorkspace.exitWidgetResizeMode();
1482 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1483 openFolder == null) {
1484 mWorkspace.moveToDefaultScreen(true);
1485 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001486
Jamie Genniscb222e82012-10-23 15:44:26 -07001487 closeFolder();
1488 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001489
Jamie Genniscb222e82012-10-23 15:44:26 -07001490 // If we are already on home, then just animate back to the workspace,
1491 // otherwise, just wait until onResume to set the state back to Workspace
1492 if (alreadyOnHome) {
1493 showWorkspace(true);
1494 } else {
1495 mOnResumeState = State.WORKSPACE;
1496 }
1497
1498 final View v = getWindow().peekDecorView();
1499 if (v != null && v.getWindowToken() != null) {
1500 InputMethodManager imm = (InputMethodManager)getSystemService(
1501 INPUT_METHOD_SERVICE);
1502 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1503 }
1504
1505 // Reset AllApps to its initial state
1506 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1507 mAppsCustomizeTabHost.reset();
1508 }
1509 }
1510 };
1511
1512 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1513 // Delay processing of the intent to allow the status bar animation to finish
1514 // first in order to avoid janky animations.
1515 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001516 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001517 // Process the intent immediately.
1518 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001519 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
Michael Jurka447bf842013-05-15 14:52:15 +02001522 if (DEBUG_RESUME_TIME) {
1523 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
1526
1527 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001528 public void onRestoreInstanceState(Bundle state) {
1529 super.onRestoreInstanceState(state);
1530 for (int page: mSynchronouslyBoundPages) {
1531 mWorkspace.restoreInstanceStateForChild(page);
1532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
1534
1535 @Override
1536 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001537 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001538 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
Michael Jurka883f55b2010-10-21 15:47:14 -07001540 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001541 // We close any open folder since it will not be re-opened, and we need to make sure
1542 // this state is reflected.
1543 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 mWaitingForResult) {
1547 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1550 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001551 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1552 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1553 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
1556 if (mFolderInfo != null && mWaitingForResult) {
1557 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1558 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1559 }
Michael Jurka946ad472010-07-09 18:05:18 -07001560
Winson Chung785d2eb2011-04-14 16:08:02 -07001561 // Save the current AppsCustomize tab
1562 if (mAppsCustomizeTabHost != null) {
1563 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1564 if (currentTabTag != null) {
1565 outState.putString("apps_customize_currentTab", currentTabTag);
1566 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001567 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1568 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
1572 @Override
1573 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Winson Chunge7a03942011-08-05 15:05:12 -07001576 // Remove all pending runnables
1577 mHandler.removeMessages(ADVANCE_MSG);
1578 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001579 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001580
Winson Chungcd2b0142011-06-08 16:02:26 -07001581 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001582 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001583 mModel.stopLoader();
1584 app.setLauncher(null);
1585
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001587 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001589 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001591 mAppWidgetHost = null;
1592
1593 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594
1595 TextKeyListener.getInstance().release();
1596
Winson Chung81b52252012-08-27 15:34:29 -07001597 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1598 // to prevent leaking Launcher activities on orientation change.
1599 if (mModel != null) {
1600 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1601 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001602
1603 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001604 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001605
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001606 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001607 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1608 mWorkspace.removeAllViews();
1609 mWorkspace = null;
1610 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001611
Michael Jurka2ecf9952012-06-18 12:52:28 -07001612 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
1614
Adam Cohena9cf38f2011-05-02 15:36:58 -07001615 public DragController getDragController() {
1616 return mDragController;
1617 }
1618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 @Override
1620 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001621 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 super.startActivityForResult(intent, requestCode);
1623 }
1624
Winson Chung70d72102011-08-12 11:24:35 -07001625 /**
1626 * Indicates that we want global search for this activity by setting the globalSearch
1627 * argument for {@link #startSearch} to true.
1628 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001630 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001632
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001633 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001634
Karl Rosaen138a0412009-04-23 19:00:21 -07001635 if (initialQuery == null) {
1636 // Use any text typed in the launcher as the initial query
1637 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001638 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 if (appSearchData == null) {
1640 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001641 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
Winson Chungadf0c182012-08-23 14:59:07 -07001643 Rect sourceBounds = new Rect();
1644 if (mSearchDropTargetBar != null) {
1645 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1646 }
Romain Guycbb89e42009-06-08 15:52:54 -07001647
Bjorn Bringertc459e522013-06-07 19:36:01 +01001648 startSearch(initialQuery, selectInitialQuery,
1649 appSearchData, sourceBounds);
1650 }
1651
1652 public void startSearch(String initialQuery,
1653 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001654 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001655 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001656 }
1657
1658 /**
1659 * Starts the global search activity. This code is a copied from SearchManager
1660 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001661 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001662 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001663 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001664 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1665 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1666 if (globalSearchActivity == null) {
1667 Log.w(TAG, "No global search activity found.");
1668 return;
1669 }
1670 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1671 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1672 intent.setComponent(globalSearchActivity);
1673 // Make sure that we have a Bundle to put source in
1674 if (appSearchData == null) {
1675 appSearchData = new Bundle();
1676 } else {
1677 appSearchData = new Bundle(appSearchData);
1678 }
1679 // Set source to package name of app that starts global search, if not set already.
1680 if (!appSearchData.containsKey("source")) {
1681 appSearchData.putString("source", getPackageName());
1682 }
1683 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1684 if (!TextUtils.isEmpty(initialQuery)) {
1685 intent.putExtra(SearchManager.QUERY, initialQuery);
1686 }
1687 if (selectInitialQuery) {
1688 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1689 }
1690 intent.setSourceBounds(sourceBounds);
1691 try {
1692 startActivity(intent);
1693 } catch (ActivityNotFoundException ex) {
1694 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
1697
Winson Chung70d72102011-08-12 11:24:35 -07001698 @Override
1699 public boolean onCreateOptionsMenu(Menu menu) {
1700 if (isWorkspaceLocked()) {
1701 return false;
1702 }
1703
1704 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001705
1706 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1707 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1708 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001709 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1710 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1711 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001712 String helpUrl = getString(R.string.help_url);
1713 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001714 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1715 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1716
Winson Chung70d72102011-08-12 11:24:35 -07001717 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1718 .setIcon(android.R.drawable.ic_menu_gallery)
1719 .setAlphabeticShortcut('W');
1720 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1721 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001722 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001723 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001724 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1725 .setIcon(android.R.drawable.ic_menu_preferences)
1726 .setIntent(settings)
1727 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001728 if (!helpUrl.isEmpty()) {
1729 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1730 .setIcon(android.R.drawable.ic_menu_help)
1731 .setIntent(help)
1732 .setAlphabeticShortcut('H');
1733 }
Winson Chung70d72102011-08-12 11:24:35 -07001734 return true;
1735 }
1736
1737 @Override
1738 public boolean onPrepareOptionsMenu(Menu menu) {
1739 super.onPrepareOptionsMenu(menu);
1740
1741 if (mAppsCustomizeTabHost.isTransitioning()) {
1742 return false;
1743 }
1744 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1745 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1746
1747 return true;
1748 }
1749
1750 @Override
1751 public boolean onOptionsItemSelected(MenuItem item) {
1752 switch (item.getItemId()) {
1753 case MENU_WALLPAPER_SETTINGS:
1754 startWallpaper();
1755 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001756 }
1757
1758 return super.onOptionsItemSelected(item);
1759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001761 @Override
1762 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001763 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001764 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001765 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001766 }
1767
Joe Onorato9c1289c2009-08-17 11:03:03 -04001768 public boolean isWorkspaceLocked() {
1769 return mWorkspaceLoading || mWaitingForResult;
1770 }
1771
Michael Jurka0280c3b2010-09-17 15:00:07 -07001772 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001774 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001775 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1776 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001777 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001778 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001780
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001781 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1782 AppWidgetProviderInfo appWidgetInfo) {
1783 if (appWidgetInfo.configure != null) {
1784 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001785
Bjorn Bringert7984c942009-12-09 15:38:25 +00001786 // Launch over to configure widget, if needed
1787 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001788 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001789 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001790 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001792 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001793 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001794 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001795 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001796 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798 }
Romain Guycbb89e42009-06-08 15:52:54 -07001799
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800 /**
1801 * Process a shortcut drop.
1802 *
1803 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001804 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001805 * @param cell The cell it should be added to, optional
1806 * @param position The location on the screen where it was dropped, optional
1807 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001808 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001809 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001811 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001812 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001813 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001814
1815 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001816 mPendingAddInfo.cellX = cell[0];
1817 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001818 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001819
1820 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1821 createShortcutIntent.setComponent(componentName);
1822 processShortcut(createShortcutIntent);
1823 }
1824
Adam Cohenfbba09b2011-07-18 21:43:05 -07001825 /**
1826 * Process a widget drop.
1827 *
1828 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001829 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001830 * @param cell The cell it should be added to, optional
1831 * @param position The location on the screen where it was dropped, optional
1832 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001833 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001834 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001835 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001836 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001837 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001838 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001839 mPendingAddInfo.minSpanX = info.minSpanX;
1840 mPendingAddInfo.minSpanY = info.minSpanY;
1841
Adam Cohenfbba09b2011-07-18 21:43:05 -07001842 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001843 mPendingAddInfo.cellX = cell[0];
1844 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001845 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001846 if (span != null) {
1847 mPendingAddInfo.spanX = span[0];
1848 mPendingAddInfo.spanY = span[1];
1849 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001850
Adam Cohened66b2b2012-01-23 17:28:51 -08001851 AppWidgetHostView hostView = info.boundWidget;
1852 int appWidgetId;
1853 if (hostView != null) {
1854 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001855 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001856 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001857 // In this case, we either need to start an activity to get permission to bind
1858 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001859 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001860 Bundle options = info.bindOptions;
1861
1862 boolean success = false;
1863 if (options != null) {
1864 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1865 info.componentName, options);
1866 } else {
1867 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1868 info.componentName);
1869 }
1870 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001871 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001872 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001873 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001874 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1875 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1876 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001877 // TODO: we need to make sure that this accounts for the options bundle.
1878 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001879 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1880 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001881 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001882 }
1883
Joe Onoratodeb98af2010-02-19 14:59:39 -08001884 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001885 // Handle case where user selected "Applications"
1886 String applicationName = getResources().getString(R.string.group_applications);
1887 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001888
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001889 if (applicationName != null && applicationName.equals(shortcutName)) {
1890 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1891 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001892
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001893 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1894 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001895 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001896 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001897 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001898 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
1901
Winson Chung24ab2f12010-09-16 14:10:47 -07001902 void processWallpaper(Intent intent) {
1903 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1904 }
1905
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001908 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 folderInfo.title = getText(R.string.folder_name);
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001912 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001913 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001914 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915
1916 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 FolderIcon newFolder =
1918 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001919 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001920 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001921 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
Romain Guycbb89e42009-06-08 15:52:54 -07001923
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001925 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 }
1927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001929 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001931 Intent chooser = Intent.createChooser(pickWallpaper,
1932 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001933 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1934 // Removed in Eclair MR1
1935// WallpaperManager wm = (WallpaperManager)
1936// getSystemService(Context.WALLPAPER_SERVICE);
1937// WallpaperInfo wi = wm.getWallpaperInfo();
1938// if (wi != null && wi.getSettingsActivity() != null) {
1939// LabeledIntent li = new LabeledIntent(getPackageName(),
1940// R.string.configure_wallpaper, 0);
1941// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1942// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1943// }
Mike Clerona0618e42009-10-22 13:55:21 -07001944 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001947 /**
1948 * Registers various content observers. The current implementation registers
1949 * only a favorites observer to keep track of the favorites applications.
1950 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001951 private void registerContentObservers() {
1952 ContentResolver resolver = getContentResolver();
1953 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1954 true, mWidgetObserver);
1955 }
1956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 @Override
1958 public boolean dispatchKeyEvent(KeyEvent event) {
1959 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1960 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001962 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001963 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001964 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001965 dumpState();
1966 return true;
1967 }
1968 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001969 }
1970 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1971 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001972 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 return true;
1974 }
1975 }
1976
1977 return super.dispatchKeyEvent(event);
1978 }
1979
Joe Onorato88ec0992009-11-19 13:16:06 -08001980 @Override
1981 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001982 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001983 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001984 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001985 Folder openFolder = mWorkspace.getOpenFolder();
1986 if (openFolder.isEditingName()) {
1987 openFolder.dismissEditingName();
1988 } else {
1989 closeFolder();
1990 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001991 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001992 mWorkspace.exitWidgetResizeMode();
1993
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001994 // Back button is a no-op here, but give at least some feedback for the button press
1995 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001996 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001997 }
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002000 * Re-listen when widgets are reset.
2001 */
2002 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002003 if (mAppWidgetHost != null) {
2004 mAppWidgetHost.startListening();
2005 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002006 }
2007
2008 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 * Launches the intent referred by the clicked shortcut.
2010 *
2011 * @param v The view representing the clicked shortcut.
2012 */
2013 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002014 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2015 // view has detached (it's possible for this to happen if the view is removed mid touch).
2016 if (v.getWindowToken() == null) {
2017 return;
2018 }
2019
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002020 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002021 return;
2022 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002025 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002027 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002028
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002029 // Check for special shortcuts
2030 if (intent.getComponent() != null) {
2031 final String shortcutClass = intent.getComponent().getClassName();
2032
2033 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2034 showAllApps(true);
2035 return;
2036 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2037 MemoryDumpActivity.startDump(this);
2038 return;
2039 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002040 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002041
2042 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002043 int[] pos = new int[2];
2044 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002045 intent.setSourceBounds(new Rect(pos[0], pos[1],
2046 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002047
2048 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002049
2050 if (success && v instanceof BubbleTextView) {
2051 mWaitingForResume = (BubbleTextView) v;
2052 mWaitingForResume.setStayPressed(true);
2053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002055 if (v instanceof FolderIcon) {
2056 FolderIcon fi = (FolderIcon) v;
2057 handleFolderClick(fi);
2058 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002059 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002060 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002061 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002062 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002063 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066 }
2067
Michael Jurka0e260592010-06-30 17:07:39 -07002068 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002069 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002070 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002071 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2072 showWorkspace(true);
2073 }
Michael Jurka0e260592010-06-30 17:07:39 -07002074 return false;
2075 }
2076
Michael Jurkaaf442092010-06-10 17:01:57 -07002077 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002078 * Event handler for the search button
2079 *
2080 * @param v The view that was clicked.
2081 */
2082 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002083 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2084
Amith Yamasania135ba82011-08-09 17:42:01 -07002085 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002086 }
2087
2088 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002089 * Event handler for the voice button
2090 *
2091 * @param v The view that was clicked.
2092 */
2093 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002094 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002095
Bjorn Bringertc459e522013-06-07 19:36:01 +01002096 startVoice();
2097 }
2098
2099 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002100 try {
2101 final SearchManager searchManager =
2102 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2103 ComponentName activityName = searchManager.getGlobalSearchActivity();
2104 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002105 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002106 if (activityName != null) {
2107 intent.setPackage(activityName.getPackageName());
2108 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002109 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002110 } catch (ActivityNotFoundException e) {
2111 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002112 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002113 startActivitySafely(null, intent, "onClickVoiceButton");
2114 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002115 }
2116
2117 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002118 * Event handler for the "grid" button that appears on the home screen, which
2119 * enters all apps mode.
2120 *
2121 * @param v The view that was clicked.
2122 */
2123 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002124 showAllApps(true);
2125 }
2126
2127 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002128 // Provide the same haptic feedback that the system offers for virtual keys.
2129 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002130 }
2131
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002132 public void onClickAppMarketButton(View v) {
2133 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002134 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002135 } else {
2136 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002137 }
2138 }
2139
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002140 void startApplicationDetailsActivity(ComponentName componentName) {
2141 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002142 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2143 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002144 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002145 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002146 }
2147
Patrick Dubroy5539af72010-09-07 15:22:01 -07002148 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2149 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2150 // System applications cannot be installed. For now, show a toast explaining that.
2151 // We may give them the option of disabling apps this way.
2152 int messageId = R.string.uninstall_system_app_text;
2153 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2154 } else {
2155 String packageName = appInfo.componentName.getPackageName();
2156 String className = appInfo.componentName.getClassName();
2157 Intent intent = new Intent(
2158 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002159 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2160 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002161 startActivity(intent);
2162 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002163 }
2164
Michael Jurka86a720e2012-05-09 11:23:23 -07002165 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002169 // Only launch using the new animation if the shortcut has not opted out (this is a
2170 // private contract between launcher and may be ignored in the future).
2171 boolean useLaunchAnimation = (v != null) &&
2172 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2173 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002174 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2175 v.getMeasuredWidth(), v.getMeasuredHeight());
2176
2177 startActivity(intent, opts.toBundle());
2178 } else {
2179 startActivity(intent);
2180 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002181 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 } catch (SecurityException e) {
2183 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002184 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002186 "or use the exported attribute for this activity. "
2187 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002189 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002191
Michael Jurka86a720e2012-05-09 11:23:23 -07002192 boolean startActivitySafely(View v, Intent intent, Object tag) {
2193 boolean success = false;
2194 try {
2195 success = startActivity(v, intent, tag);
2196 } catch (ActivityNotFoundException e) {
2197 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2198 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2199 }
2200 return success;
2201 }
2202
Romain Guy8e633c52010-03-04 12:51:36 -08002203 void startActivityForResultSafely(Intent intent, int requestCode) {
2204 try {
2205 startActivityForResult(intent, requestCode);
2206 } catch (ActivityNotFoundException e) {
2207 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2208 } catch (SecurityException e) {
2209 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2210 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2211 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2212 "or use the exported attribute for this activity.", e);
2213 }
2214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
Adam Cohena9cf38f2011-05-02 15:36:58 -07002216 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002217 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002218 Folder openFolder = mWorkspace.getFolderForTag(info);
2219
2220 // If the folder info reports that the associated folder is open, then verify that
2221 // it is actually opened. There have been a few instances where this gets out of sync.
2222 if (info.opened && openFolder == null) {
2223 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002225 info.opened = false;
2226 }
2227
Adam Cohenfb91f302012-06-11 15:45:18 -07002228 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 // Close any open folder
2230 closeFolder();
2231 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002232 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 } else {
2234 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 int folderScreen;
2236 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002237 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 // .. and close it
2239 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002240 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 // Close any folder open on the current screen
2242 closeFolder();
2243 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002244 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 }
2246 }
2247 }
2248 }
2249
Adam Cohen268c4752012-06-06 17:47:33 -07002250 /**
2251 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2252 * in the DragLayer in the exact absolute location of the original FolderIcon.
2253 */
2254 private void copyFolderIconToImage(FolderIcon fi) {
2255 final int width = fi.getMeasuredWidth();
2256 final int height = fi.getMeasuredHeight();
2257
2258 // Lazy load ImageView, Bitmap and Canvas
2259 if (mFolderIconImageView == null) {
2260 mFolderIconImageView = new ImageView(this);
2261 }
2262 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2263 mFolderIconBitmap.getHeight() != height) {
2264 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2265 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2266 }
2267
2268 DragLayer.LayoutParams lp;
2269 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2270 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2271 } else {
2272 lp = new DragLayer.LayoutParams(width, height);
2273 }
2274
Adam Cohen307fe232012-08-16 17:55:58 -07002275 // The layout from which the folder is being opened may be scaled, adjust the starting
2276 // view size by this scale factor.
2277 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002278 lp.customPosition = true;
2279 lp.x = mRectForFolderAnimation.left;
2280 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002281 lp.width = (int) (scale * width);
2282 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002283
2284 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2285 fi.draw(mFolderIconCanvas);
2286 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002287 if (fi.getFolder() != null) {
2288 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2289 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002290 }
Adam Cohen268c4752012-06-06 17:47:33 -07002291 // Just in case this image view is still in the drag layer from a previous animation,
2292 // we remove it and re-add it.
2293 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2294 mDragLayer.removeView(mFolderIconImageView);
2295 }
2296 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002297 if (fi.getFolder() != null) {
2298 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002299 }
Adam Cohen268c4752012-06-06 17:47:33 -07002300 }
2301
Adam Cohen2801caf2011-05-13 20:57:39 -07002302 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002303 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002304 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2305 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2306 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2307
Adam Cohenc51934b2011-07-26 21:07:43 -07002308 FolderInfo info = (FolderInfo) fi.getTag();
2309 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2310 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002311 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2312 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002313 }
2314
Adam Cohen268c4752012-06-06 17:47:33 -07002315 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2316 copyFolderIconToImage(fi);
2317 fi.setVisibility(View.INVISIBLE);
2318
Michael Jurka2ecf9952012-06-18 12:52:28 -07002319 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002320 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002321 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2322 oa.start();
2323 }
2324
Adam Cohen268c4752012-06-06 17:47:33 -07002325 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002326 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002327 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2328 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2329 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2330
Adam Cohen268c4752012-06-06 17:47:33 -07002331 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002332
Adam Cohen268c4752012-06-06 17:47:33 -07002333 // We remove and re-draw the FolderIcon in-case it has changed
2334 mDragLayer.removeView(mFolderIconImageView);
2335 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002336 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002337 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002338 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002339 oa.addListener(new AnimatorListenerAdapter() {
2340 @Override
2341 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002342 if (cl != null) {
2343 cl.clearFolderLeaveBehind();
2344 // Remove the ImageView copy of the FolderIcon and make the original visible.
2345 mDragLayer.removeView(mFolderIconImageView);
2346 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002347 }
2348 }
2349 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002350 oa.start();
2351 }
2352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002354 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 * is animated relative to the specified View. If the View is null, no animation
2356 * is played.
2357 *
2358 * @param folderInfo The FolderInfo describing the folder to open.
2359 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002360 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002361 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002362 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363
Adam Cohena9cf38f2011-05-02 15:36:58 -07002364 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365
Adam Cohen4554ee12011-08-03 16:13:21 -07002366 // Just verify that the folder hasn't already been added to the DragLayer.
2367 // There was a one-off crash where the folder had a parent already.
2368 if (folder.getParent() == null) {
2369 mDragLayer.addView(folder);
2370 mDragController.addDropTarget((DropTarget) folder);
2371 } else {
2372 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2373 folder.getParent() + ").");
2374 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002375 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002376 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002377
2378 // Notify the accessibility manager that this folder "window" has appeared and occluded
2379 // the workspace items
2380 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2381 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002382 }
2383
2384 public void closeFolder() {
2385 Folder folder = mWorkspace.getOpenFolder();
2386 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002387 if (folder.isEditingName()) {
2388 folder.dismissEditingName();
2389 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002390 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002391
2392 // Dismiss the folder cling
2393 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002394 }
2395 }
2396
2397 void closeFolder(Folder folder) {
2398 folder.getInfo().opened = false;
2399
2400 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2401 if (parent != null) {
2402 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2403 shrinkAndFadeInFolderIcon(fi);
2404 }
2405 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002406
2407 // Notify the accessibility manager that this folder "window" has disappeard and no
2408 // longer occludeds the workspace items
2409 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 }
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002413 if (!isDraggingEnabled()) return false;
2414 if (isWorkspaceLocked()) return false;
2415 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416
2417 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002418 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 }
2420
Michael Jurka0280c3b2010-09-17 15:00:07 -07002421 resetAddInfo();
2422 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002424 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 return true;
2426 }
2427
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 // The hotseat touch handling does not go through Workspace, and we always allow long press
2429 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002430 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2432 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002433 if (itemUnderLongClick == null) {
2434 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002435 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2436 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002437 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002439 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002441 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
2443 }
2444 }
2445 return true;
2446 }
2447
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 boolean isHotseatLayout(View layout) {
2449 return mHotseat != null && layout != null &&
2450 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2451 }
2452 Hotseat getHotseat() {
2453 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002454 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002455 SearchDropTargetBar getSearchBar() {
2456 return mSearchDropTargetBar;
2457 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002458
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 /**
2460 * Returns the CellLayout of the specified container at the specified screen.
2461 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002462 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002463 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2464 if (mHotseat != null) {
2465 return mHotseat.getLayout();
2466 } else {
2467 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002468 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002469 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002471 }
2472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 Workspace getWorkspace() {
2475 return mWorkspace;
2476 }
2477
Daniel Sandler843e8602010-06-07 14:59:01 -04002478 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002479 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002480 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002481 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002482 }
2483
Craig Mautner360310b2012-10-26 15:13:08 -07002484 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002485 public boolean isAllAppsButtonRank(int rank) {
2486 return mHotseat.isAllAppsButtonRank(rank);
2487 }
2488
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002489 /**
2490 * Helper method for the cameraZoomIn/cameraZoomOut animations
2491 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002492 * @param scaleFactor The scale factor used for the zoom
2493 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002494 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002495 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002496 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002497 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002498
Winson Chung18f41f82012-05-09 13:28:10 -07002499 void disableWallpaperIfInAllApps() {
2500 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002501 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002502 if (mAppsCustomizeTabHost != null &&
2503 !mAppsCustomizeTabHost.isTransitioning()) {
2504 updateWallpaperVisibility(false);
2505 }
2506 }
2507 }
2508
Craig Mautner360310b2012-10-26 15:13:08 -07002509 private void setWorkspaceBackground(boolean workspace) {
2510 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002511 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002512 }
2513
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002514 void updateWallpaperVisibility(boolean visible) {
2515 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2516 int curflags = getWindow().getAttributes().flags
2517 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2518 if (wpflags != curflags) {
2519 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2520 }
Craig Mautner360310b2012-10-26 15:13:08 -07002521 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002522 }
2523
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002524 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2525 if (v instanceof LauncherTransitionable) {
2526 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2527 }
2528 }
2529
Michael Jurkabed61d22012-02-14 22:51:29 -08002530 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2531 if (v instanceof LauncherTransitionable) {
2532 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2533 }
Winson Chung70442722012-02-10 15:43:22 -08002534
2535 // Update the workspace transition step as well
2536 dispatchOnLauncherTransitionStep(v, 0f);
2537 }
2538
2539 private void dispatchOnLauncherTransitionStep(View v, float t) {
2540 if (v instanceof LauncherTransitionable) {
2541 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2542 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002543 }
2544
2545 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2546 if (v instanceof LauncherTransitionable) {
2547 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2548 }
Winson Chung70442722012-02-10 15:43:22 -08002549
2550 // Update the workspace transition step as well
2551 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002552 }
2553
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002554 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002555 * Things to test when changing the following seven functions.
2556 * - Home from workspace
2557 * - from center screen
2558 * - from other screens
2559 * - Home from all apps
2560 * - from center screen
2561 * - from other screens
2562 * - Back from all apps
2563 * - from center screen
2564 * - from other screens
2565 * - Launch app from workspace and quit
2566 * - with back
2567 * - with home
2568 * - Launch app from all apps and quit
2569 * - with back
2570 * - with home
2571 * - Go to a screen that's not the default, then all
2572 * apps, and launch and app, and go back
2573 * - with back
2574 * -with home
2575 * - On workspace, long press power and go back
2576 * - with back
2577 * - with home
2578 * - On all apps, long press power and go back
2579 * - with back
2580 * - with home
2581 * - On workspace, power off
2582 * - On all apps, power off
2583 * - Launch an app and turn off the screen while in that app
2584 * - Go back with home key
2585 * - Go back with back key TODO: make this not go to workspace
2586 * - From all apps
2587 * - From workspace
2588 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2589 * - From all apps
2590 * - From the center workspace
2591 * - From another workspace
2592 */
2593
2594 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002595 * Zoom the camera out from the workspace to reveal 'toView'.
2596 * Assumes that the view to show is anchored at either the very top or very bottom
2597 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002599 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002600 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002601 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002602 mStateAnimation.cancel();
2603 mStateAnimation = null;
2604 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002605 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002606
Winson Chungf0ea4d32011-06-06 14:27:16 -07002607 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2608 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2609 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002610 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002611 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002612 final int startDelay =
2613 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002614
Michael Jurkab3e22d92011-10-31 15:58:33 -07002615 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616
Michael Jurkad74c9842011-07-10 12:44:21 -07002617 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002618 Animator workspaceAnim =
2619 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002620
2621 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002622 toView.setScaleX(scale);
2623 toView.setScaleY(scale);
2624 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2625 scaleAnim.
2626 scaleX(1f).scaleY(1f).
2627 setDuration(duration).
2628 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002629
Winson Chungf0ea4d32011-06-06 14:27:16 -07002630 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002631 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002632 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002633 .ofFloat(toView, "alpha", 0f, 1f)
2634 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002635 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002636 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2637 @Override
2638 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002639 if (animation == null) {
2640 throw new RuntimeException("animation is null");
2641 }
Winson Chung70442722012-02-10 15:43:22 -08002642 float t = (Float) animation.getAnimatedValue();
2643 dispatchOnLauncherTransitionStep(fromView, t);
2644 dispatchOnLauncherTransitionStep(toView, t);
2645 }
2646 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002647
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002648 // toView should appear right at the end of the workspace shrink
2649 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002650 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002651 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002652 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002653
2654 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002655 boolean animationCancelled = false;
2656
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002657 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002658 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002659 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002660 // Prepare the position
2661 toView.setTranslationX(0.0f);
2662 toView.setTranslationY(0.0f);
2663 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002664 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002665 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002666 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002667 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002668 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2669 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002670
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002671 if (mWorkspace != null && !springLoaded && !LauncherAppState.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002672 // Hide the workspace scrollbar
2673 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002674 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002675 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002676 if (!animationCancelled) {
2677 updateWallpaperVisibility(false);
2678 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002679
2680 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002681 if (mSearchDropTargetBar != null) {
2682 mSearchDropTargetBar.hideSearchBar(false);
2683 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002684 }
2685
Adam Cohencff6af82011-09-13 14:51:53 -07002686 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002687 public void onAnimationCancel(Animator animation) {
2688 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002689 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002690 });
2691
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002692 if (workspaceAnim != null) {
2693 mStateAnimation.play(workspaceAnim);
2694 }
Michael Jurka1899a362011-11-03 13:50:45 -07002695
2696 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002697
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002698 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2699 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002700
2701 // If any of the objects being animated haven't been measured/laid out
2702 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002703 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002704 (mWorkspace.getMeasuredWidth() == 0) ||
2705 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002706 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002707 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002708
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002709 final AnimatorSet stateAnimation = mStateAnimation;
2710 final Runnable startAnimRunnable = new Runnable() {
2711 public void run() {
2712 // Check that mStateAnimation hasn't changed while
2713 // we waited for a layout/draw pass
2714 if (mStateAnimation != stateAnimation)
2715 return;
2716 setPivotsForZoom(toView, scale);
2717 dispatchOnLauncherTransitionStart(fromView, animated, false);
2718 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002719 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002720 }
2721 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002722 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002723 final ViewTreeObserver observer = toView.getViewTreeObserver();
2724 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2725 public void onGlobalLayout() {
2726 startAnimRunnable.run();
2727 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2728 }
2729 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002730 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002731 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002732 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002733 } else {
2734 toView.setTranslationX(0.0f);
2735 toView.setTranslationY(0.0f);
2736 toView.setScaleX(1.0f);
2737 toView.setScaleY(1.0f);
2738 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002739 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002740
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002741 if (!springLoaded && !LauncherAppState.isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002742 // Hide the workspace scrollbar
2743 mWorkspace.hideScrollingIndicator(true);
2744 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002745
2746 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002747 if (mSearchDropTargetBar != null) {
2748 mSearchDropTargetBar.hideSearchBar(false);
2749 }
Michael Jurkaabded662011-03-04 12:06:57 -08002750 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002751 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002752 dispatchOnLauncherTransitionStart(fromView, animated, false);
2753 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002754 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002755 dispatchOnLauncherTransitionStart(toView, animated, false);
2756 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002757 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002759 }
2760
2761 /**
2762 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002763 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002764 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002766 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2767 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002768
Michael Jurkab3e22d92011-10-31 15:58:33 -07002769 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002770 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002771 mStateAnimation.cancel();
2772 mStateAnimation = null;
2773 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002774 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002775
Winson Chungf0ea4d32011-06-06 14:27:16 -07002776 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002777 final int fadeOutDuration =
2778 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 final float scaleFactor = (float)
2780 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2781 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002782 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002783 Animator workspaceAnim = null;
2784
2785 if (toState == State.WORKSPACE) {
2786 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2787 workspaceAnim = mWorkspace.getChangeStateAnimation(
2788 Workspace.State.NORMAL, animated, stagger);
2789 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2790 workspaceAnim = mWorkspace.getChangeStateAnimation(
2791 Workspace.State.SPRING_LOADED, animated);
2792 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002793
Michael Jurkab3e22d92011-10-31 15:58:33 -07002794 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002795 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002796 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002798 final LauncherViewPropertyAnimator scaleAnim =
2799 new LauncherViewPropertyAnimator(fromView);
2800 scaleAnim.
2801 scaleX(scaleFactor).scaleY(scaleFactor).
2802 setDuration(duration).
2803 setInterpolator(new Workspace.ZoomInInterpolator());
2804
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002805 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002806 .ofFloat(fromView, "alpha", 1f, 0f)
2807 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002808 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002809 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2810 @Override
2811 public void onAnimationUpdate(ValueAnimator animation) {
2812 float t = 1f - (Float) animation.getAnimatedValue();
2813 dispatchOnLauncherTransitionStep(fromView, t);
2814 dispatchOnLauncherTransitionStep(toView, t);
2815 }
2816 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002817
Michael Jurka2ecf9952012-06-18 12:52:28 -07002818 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002819
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002820 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2821 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002822 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002823
2824 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002825 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002826 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002827 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002828 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002829 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2830 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002831 if (mWorkspace != null) {
2832 mWorkspace.hideScrollingIndicator(false);
2833 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002834 if (onCompleteRunnable != null) {
2835 onCompleteRunnable.run();
2836 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002837 mAppsCustomizeContent.updateCurrentPageScroll();
2838 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839 }
2840 });
2841
Winson Chungf0ea4d32011-06-06 14:27:16 -07002842 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002843 if (workspaceAnim != null) {
2844 mStateAnimation.play(workspaceAnim);
2845 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002846 dispatchOnLauncherTransitionStart(fromView, animated, true);
2847 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002848 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 } else {
2850 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002851 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002852 dispatchOnLauncherTransitionStart(fromView, animated, true);
2853 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 dispatchOnLauncherTransitionStart(toView, animated, true);
2856 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002857 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002859 }
2860
Michael Jurkae326f182011-11-21 14:05:46 -08002861 @Override
2862 public void onTrimMemory(int level) {
2863 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002864 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002865 mAppsCustomizeTabHost.onTrimMemory();
2866 }
2867 }
2868
Winson Chung18f41f82012-05-09 13:28:10 -07002869 @Override
2870 public void onWindowFocusChanged(boolean hasFocus) {
2871 if (!hasFocus) {
2872 // When another window occludes launcher (like the notification shade, or recents),
2873 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2874 updateWallpaperVisibility(true);
2875 } else {
2876 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002877 mWorkspace.postDelayed(new Runnable() {
2878 @Override
2879 public void run() {
2880 disableWallpaperIfInAllApps();
2881 }
2882 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002883 }
2884 }
2885
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002886 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002887 showWorkspace(animated, null);
2888 }
2889
2890 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002891 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002892 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002893 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002894 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002895
Winson Chungc7d2b602012-05-16 17:02:20 -07002896 // Show the search bar (only animate if we were showing the drop target bar in spring
2897 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002898 if (mSearchDropTargetBar != null) {
2899 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2900 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002901
Michael Jurkab737ee62011-11-15 15:57:22 -08002902 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002903 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904
2905 // Set focus to the AppsCustomize button
2906 if (mAllAppsButton != null) {
2907 mAllAppsButton.requestFocus();
2908 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002909 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002910
Michael Jurkab737ee62011-11-15 15:57:22 -08002911 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002912
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002913 // Change the state *after* we've called all the transition code
2914 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002915
Winson Chung5fb63472011-02-02 17:03:37 -08002916 // Resume the auto-advance of widgets
2917 mUserPresent = true;
2918 updateRunning();
2919
alanv1d4fde62012-10-17 13:15:47 -07002920 // Send an accessibility event to announce the context change
2921 getWindow().getDecorView()
2922 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002923
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002924 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002925 }
2926
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002927 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002928 }
2929
Michael Jurkab3e22d92011-10-31 15:58:33 -07002930 void showAllApps(boolean animated) {
2931 if (mState != State.WORKSPACE) return;
2932
2933 showAppsCustomizeHelper(animated, false);
2934 mAppsCustomizeTabHost.requestFocus();
2935
Michael Jurkab3e22d92011-10-31 15:58:33 -07002936 // Change the state *after* we've called all the transition code
2937 mState = State.APPS_CUSTOMIZE;
2938
2939 // Pause the auto-advance of widgets until we are out of AllApps
2940 mUserPresent = false;
2941 updateRunning();
2942 closeFolder();
2943
2944 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002945 getWindow().getDecorView()
2946 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002947 }
2948
Adam Cohen7777d962011-08-18 18:58:38 -07002949 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002950 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002951 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002952 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002953 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002954 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002955 }
Adam Cohen7777d962011-08-18 18:58:38 -07002956
Adam Cohened66b2b2012-01-23 17:28:51 -08002957 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2958 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002959 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2960
Winson Chunge7a03942011-08-05 15:05:12 -07002961 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002962 @Override
2963 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002964 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002965 // Before we show workspace, hide all apps again because
2966 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2967 // clean up our state transition functions
2968 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002969 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002970 } else {
2971 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002972 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002973 }
2974 }, (extendedDelay ?
2975 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2976 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2977 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002978
Michael Jurkad3ef3062010-11-23 16:23:58 -08002979 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002980 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981 final boolean animated = true;
2982 final boolean springLoaded = true;
2983 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002984 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002985 }
2986 // Otherwise, we are not in spring loaded mode, so don't do anything.
2987 }
2988
Michael Jurkab737ee62011-11-15 15:57:22 -08002989 void hideDockDivider() {
2990 if (mQsbDivider != null && mDockDivider != null) {
2991 mQsbDivider.setVisibility(View.INVISIBLE);
2992 mDockDivider.setVisibility(View.INVISIBLE);
2993 }
2994 }
2995
2996 void showDockDivider(boolean animated) {
2997 if (mQsbDivider != null && mDockDivider != null) {
2998 mQsbDivider.setVisibility(View.VISIBLE);
2999 mDockDivider.setVisibility(View.VISIBLE);
3000 if (mDividerAnimator != null) {
3001 mDividerAnimator.cancel();
3002 mQsbDivider.setAlpha(1f);
3003 mDockDivider.setAlpha(1f);
3004 mDividerAnimator = null;
3005 }
3006 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003007 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3008 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3009 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003010 int duration = 0;
3011 if (mSearchDropTargetBar != null) {
3012 duration = mSearchDropTargetBar.getTransitionInDuration();
3013 }
3014 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003015 mDividerAnimator.start();
3016 }
3017 }
3018 }
3019
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 void lockAllApps() {
3021 // TODO
3022 }
3023
3024 void unlockAllApps() {
3025 // TODO
3026 }
3027
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003029 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003030 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003031 void showHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003032 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003034 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003035 int duration = 0;
3036 if (mSearchDropTargetBar != null) {
3037 duration = mSearchDropTargetBar.getTransitionInDuration();
3038 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003039 mHotseat.animate().alpha(1f).setDuration(duration);
3040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003041 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003042 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 }
3044 }
3045 }
3046
3047 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003048 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003050 void hideHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003051 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003052 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003053 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003054 int duration = 0;
3055 if (mSearchDropTargetBar != null) {
3056 duration = mSearchDropTargetBar.getTransitionOutDuration();
3057 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003058 mHotseat.animate().alpha(0f).setDuration(duration);
3059 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003060 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003061 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003062 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003063 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003064 }
3065
Patrick Dubroy5f445422011-02-18 14:35:21 -08003066 /**
3067 * Add an item from all apps or customize onto the given workspace screen.
3068 * If layout is null, add to the current screen.
3069 */
3070 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003071 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003072 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003073 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003074 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003075
Winson Chungdff8ebb2011-09-08 17:25:31 -07003076 /** Maps the current orientation to an index for referencing orientation correct global icons */
3077 private int getCurrentOrientationIndexForGlobalIcons() {
3078 // default - 0, landscape - 1
3079 switch (getResources().getConfiguration().orientation) {
3080 case Configuration.ORIENTATION_LANDSCAPE:
3081 return 1;
3082 default:
3083 return 0;
3084 }
3085 }
3086
Michael Jurkaae65ee32012-04-30 11:45:54 -07003087 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003088 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003089 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003090 // Look for the toolbar icon specified in the activity meta-data
3091 Bundle metaData = packageManager.getActivityInfo(
3092 activityName, PackageManager.GET_META_DATA).metaData;
3093 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003094 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003095 if (iconResId != 0) {
3096 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003097 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 }
3099 }
3100 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003101 // This can happen if the activity defines an invalid drawable
3102 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3103 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003104 } catch (Resources.NotFoundException nfe) {
3105 // This can happen if the activity defines an invalid drawable
3106 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3107 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003108 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003109 return null;
3110 }
3111
3112 // if successful in getting icon, return it; otherwise, set button to use default drawable
3113 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003114 int buttonId, ComponentName activityName, int fallbackDrawableId,
3115 String toolbarResourceName) {
3116 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003117 Resources r = getResources();
3118 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3119 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003120
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003121 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003122 // If we were unable to find the icon via the meta-data, use a generic one
3123 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003124 toolbarIcon = r.getDrawable(fallbackDrawableId);
3125 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003126 if (button != null) {
3127 button.setCompoundDrawables(toolbarIcon, null, null, null);
3128 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003129 return null;
3130 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003131 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003132 if (button != null) {
3133 button.setCompoundDrawables(toolbarIcon, null, null, null);
3134 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003135 return toolbarIcon.getConstantState();
3136 }
3137 }
3138
3139 // if successful in getting icon, return it; otherwise, set button to use default drawable
3140 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003141 int buttonId, ComponentName activityName, int fallbackDrawableId,
3142 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003143 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003145
Michael Jurka19e0fc52011-07-22 18:00:21 -07003146 if (button != null) {
3147 // If we were unable to find the icon via the meta-data, use a
3148 // generic one
3149 if (toolbarIcon == null) {
3150 button.setImageResource(fallbackDrawableId);
3151 } else {
3152 button.setImageDrawable(toolbarIcon);
3153 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003155
3156 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3157
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 }
3159
Winson Chung1b884092012-06-08 17:13:02 -07003160 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003161 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003162 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003163 }
3164
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003165 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003166 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003167 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003168 }
3169
Winson Chungbb185bd2011-11-21 12:31:42 -08003170 private void invalidatePressedFocusedStates(View container, View button) {
3171 if (container instanceof HolographicLinearLayout) {
3172 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3173 layout.invalidatePressedFocusedStates();
3174 } else if (button instanceof HolographicImageView) {
3175 HolographicImageView view = (HolographicImageView) button;
3176 view.invalidatePressedFocusedStates();
3177 }
3178 }
3179
Winson Chungc51db6a2011-10-05 11:44:49 -07003180 private boolean updateGlobalSearchIcon() {
3181 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003182 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003183 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003184 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003185
Winson Chung1cad91e2011-05-25 17:41:01 -07003186 final SearchManager searchManager =
3187 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3188 ComponentName activityName = searchManager.getGlobalSearchActivity();
3189 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003190 int coi = getCurrentOrientationIndexForGlobalIcons();
3191 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003192 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3193 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3194 if (sGlobalSearchIcon[coi] == null) {
3195 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3196 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3197 TOOLBAR_ICON_METADATA_NAME);
3198 }
3199
Winson Chungc51db6a2011-10-05 11:44:49 -07003200 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3201 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003202 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003204 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003205 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3207 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3208 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003209 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003210 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003211 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003212 }
3213 }
3214
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003215 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003216 final View searchButtonContainer = findViewById(R.id.search_button_container);
3217 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003218 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003219 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003220 }
3221
Winson Chungc51db6a2011-10-05 11:44:49 -07003222 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003223 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003224 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003225
Winson Chungc51db6a2011-10-05 11:44:49 -07003226 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003227 final SearchManager searchManager =
3228 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3229 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3230
3231 ComponentName activityName = null;
3232 if (globalSearchActivity != null) {
3233 // Check if the global search activity handles voice search
3234 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3235 intent.setPackage(globalSearchActivity.getPackageName());
3236 activityName = intent.resolveActivity(getPackageManager());
3237 }
3238
3239 if (activityName == null) {
3240 // Fallback: check if an activity other than the global search activity
3241 // resolves this
3242 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3243 activityName = intent.resolveActivity(getPackageManager());
3244 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003245 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003246 int coi = getCurrentOrientationIndexForGlobalIcons();
3247 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003248 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3249 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3250 if (sVoiceSearchIcon[coi] == null) {
3251 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3252 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3253 TOOLBAR_ICON_METADATA_NAME);
3254 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003255 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003256 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003257 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003258 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003259 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003260 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003262 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003263 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003265 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003266 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003267
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003268 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003269 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3270 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003271 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003272 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003273 }
3274
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003275 public void setVoiceButtonProxyVisible(boolean visible) {
3276 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3277 if (voiceButtonProxy != null) {
3278 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3279 }
3280 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003281 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003282 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003283 */
3284 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003285 final View marketButton = findViewById(R.id.market_button);
3286 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3287 // Find the app market activity by resolving an intent.
3288 // (If multiple app markets are installed, it will return the ResolverActivity.)
3289 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003290 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003291 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003292 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003293 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003294 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3295 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003296 marketButton.setVisibility(View.VISIBLE);
3297 } else {
3298 // We should hide and disable the view so that we don't try and restore the visibility
3299 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3300 marketButton.setVisibility(View.GONE);
3301 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003302 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003303 }
3304
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003305 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003306 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3307 Resources r = getResources();
3308 Drawable marketIconDrawable = d.newDrawable(r);
3309 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3310 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3311 marketIconDrawable.setBounds(0, 0, w, h);
3312
3313 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003314 }
3315
Michael Jurkad7c28052012-04-27 15:43:36 -07003316 @Override
3317 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003318 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003319 final List<CharSequence> text = event.getText();
3320 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003321 // Populate event with a fake title based on the current state.
3322 if (mState == State.APPS_CUSTOMIZE) {
3323 text.add(getString(R.string.all_apps_button_label));
3324 } else {
3325 text.add(getString(R.string.all_apps_home_button_label));
3326 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003327 return result;
3328 }
3329
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003330 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003331 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003332 */
3333 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3334 @Override
3335 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003336 closeSystemDialogs();
3337 }
3338 }
3339
3340 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003341 * Receives notifications whenever the appwidgets are reset.
3342 */
3343 private class AppWidgetResetObserver extends ContentObserver {
3344 public AppWidgetResetObserver() {
3345 super(new Handler());
3346 }
3347
3348 @Override
3349 public void onChange(boolean selfChange) {
3350 onAppWidgetReset();
3351 }
3352 }
3353
3354 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003355 * If the activity is currently paused, signal that we need to run the passed Runnable
3356 * in onResume.
3357 *
3358 * This needs to be called from incoming places where resources might have been loaded
3359 * while we are paused. That is becaues the Configuration might be wrong
3360 * when we're not running, and if it comes back to what it was when we
3361 * were paused, we are not restarted.
3362 *
3363 * Implementation of the method from LauncherModel.Callbacks.
3364 *
3365 * @return true if we are currently paused. The caller might be able to
3366 * skip some work in that case since we will come back again.
3367 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003368 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003369 if (mPaused) {
3370 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 if (deletePreviousRunnables) {
3372 while (mOnResumeCallbacks.remove(run)) {
3373 }
3374 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003375 mOnResumeCallbacks.add(run);
3376 return true;
3377 } else {
3378 return false;
3379 }
3380 }
3381
Michael Jurkac402cd92013-05-20 15:49:32 +02003382 private boolean waitUntilResume(Runnable run) {
3383 return waitUntilResume(run, false);
3384 }
3385
Michael Jurka7607c2f2013-04-03 14:33:19 -07003386 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003387 * If the activity is currently paused, signal that we need to re-run the loader
3388 * in onResume.
3389 *
3390 * This needs to be called from incoming places where resources might have been loaded
3391 * while we are paused. That is becaues the Configuration might be wrong
3392 * when we're not running, and if it comes back to what it was when we
3393 * were paused, we are not restarted.
3394 *
3395 * Implementation of the method from LauncherModel.Callbacks.
3396 *
3397 * @return true if we are currently paused. The caller might be able to
3398 * skip some work in that case since we will come back again.
3399 */
3400 public boolean setLoadOnResume() {
3401 if (mPaused) {
3402 Log.i(TAG, "setLoadOnResume");
3403 mOnResumeNeedsLoad = true;
3404 return true;
3405 } else {
3406 return false;
3407 }
3408 }
3409
3410 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003414 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003415 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003416 } else {
3417 return SCREEN_COUNT / 2;
3418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003420
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 /**
3422 * Refreshes the shortcuts shown on the workspace.
3423 *
3424 * Implementation of the method from LauncherModel.Callbacks.
3425 */
3426 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003427 // If we're starting binding all over again, clear any bind calls we'd postponed in
3428 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3429 // from scratch again
3430 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003431
Michael Jurka7607c2f2013-04-03 14:33:19 -07003432 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003433 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003434 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003435 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 int count = workspace.getChildCount();
3437 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003438 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003439 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3440 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003442 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003443 if (mHotseat != null) {
3444 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
3446 }
3447
Adam Cohendcd297f2013-06-18 13:13:40 -07003448 @Override
3449 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3450 int count = orderedScreenIds.size();
3451 for (int i = 0; i < count; i++) {
3452 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3453 }
3454 mWorkspace.addExtraEmptyScreen();
3455 }
3456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 * Bind the items start-end from the list.
3459 *
3460 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003461 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003462 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3463 if (waitUntilResume(new Runnable() {
3464 public void run() {
3465 bindItems(shortcuts, start, end);
3466 }
3467 })) {
3468 return;
3469 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003470
Winson Chungf0c6ae02012-03-21 16:10:31 -07003471 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3472 Set<String> newApps = new HashSet<String>();
3473 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3474
3475 Workspace workspace = mWorkspace;
3476 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003478
3479 // Short circuit if we are loading dock items for a configuration which has no dock
3480 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3481 mHotseat == null) {
3482 continue;
3483 }
3484
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 switch (item.itemType) {
3486 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3487 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003488 ShortcutInfo info = (ShortcutInfo) item;
3489 String uri = info.intent.toUri(0).toString();
3490 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003491
3492 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3493 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003494 boolean animateIconUp = false;
3495 synchronized (newApps) {
3496 if (newApps.contains(uri)) {
3497 animateIconUp = newApps.remove(uri);
3498 }
3499 }
3500 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003501 // Prepare the view to be animated up
3502 shortcut.setAlpha(0f);
3503 shortcut.setScaleX(0f);
3504 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003505 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003506 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3507 mNewShortcutAnimateViews.add(shortcut);
3508 }
3509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003511 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003512 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003513 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003514 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003515 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3516 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003517 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003519 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003520
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003522 }
3523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 /**
3525 * Implementation of the method from LauncherModel.Callbacks.
3526 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003527 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3528 if (waitUntilResume(new Runnable() {
3529 public void run() {
3530 bindFolders(folders);
3531 }
3532 })) {
3533 return;
3534 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003535 sFolders.clear();
3536 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538
3539 /**
3540 * Add the views for a widget to the workspace.
3541 *
3542 * Implementation of the method from LauncherModel.Callbacks.
3543 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003544 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3545 if (waitUntilResume(new Runnable() {
3546 public void run() {
3547 bindAppWidget(item);
3548 }
3549 })) {
3550 return;
3551 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003552
Daniel Sandler843e8602010-06-07 14:59:01 -04003553 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3554 if (DEBUG_WIDGETS) {
3555 Log.d(TAG, "bindAppWidget: " + item);
3556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 final Workspace workspace = mWorkspace;
3558
3559 final int appWidgetId = item.appWidgetId;
3560 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003561 if (DEBUG_WIDGETS) {
3562 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3563 }
3564
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3566
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003568 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569
Adam Cohendcd297f2013-06-18 13:13:40 -07003570 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003572 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 workspace.requestLayout();
3575
Daniel Sandler843e8602010-06-07 14:59:01 -04003576 if (DEBUG_WIDGETS) {
3577 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3578 + (SystemClock.uptimeMillis()-start) + "ms");
3579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 }
3581
Adam Cohen1462de32012-07-24 22:34:36 -07003582 public void onPageBoundSynchronously(int page) {
3583 mSynchronouslyBoundPages.add(page);
3584 }
3585
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 /**
3587 * Callback saying that there aren't any more items to bind.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
Adam Cohene25af792013-06-06 23:08:25 -07003591 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003592 if (waitUntilResume(new Runnable() {
3593 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003594 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 }
3596 })) {
3597 return;
3598 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 if (mSavedState != null) {
3600 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003601 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 mSavedState = null;
3604 }
3605
Adam Cohen1462de32012-07-24 22:34:36 -07003606 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003608 // If we received the result of any pending adds while the loader was running (e.g. the
3609 // widget configuration forced an orientation change), process them now.
3610 for (int i = 0; i < sPendingAddList.size(); i++) {
3611 completeAdd(sPendingAddList.get(i));
3612 }
3613 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614
Winson Chungc51db6a2011-10-05 11:44:49 -07003615 // Update the market app icon as necessary (the other icons will be managed in response to
3616 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003617 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003618
Winson Chungf0c6ae02012-03-21 16:10:31 -07003619 // Animate up any icons as necessary
3620 if (mVisible || mWorkspaceLoading) {
3621 Runnable newAppsRunnable = new Runnable() {
3622 @Override
3623 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003624 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003625 }
3626 };
Winson Chunga2413752012-04-03 14:22:34 -07003627
Adam Cohendcd297f2013-06-18 13:13:40 -07003628 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3629 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003630 if (canRunNewAppsAnimation()) {
3631 // If the user has not interacted recently, then either snap to the new page to show
3632 // the new-apps animation or just run them if they are to appear on the current page
3633 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003634 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003635 } else {
3636 runNewAppsAnimation(false);
3637 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003638 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003639 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003640 // are on another page (or just normally if they are added to the current page)
3641 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003642 }
3643 }
3644
3645 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003646 if (upgradePath) {
3647 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003648 mWorkspace.stripDuplicateApps();
3649 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003650 }
Adam Cohen99894d92013-06-14 11:22:59 -07003651
Adam Cohen66a01fd2013-06-11 12:48:00 -07003652 mWorkspace.post(new Runnable() {
3653 @Override
3654 public void run() {
3655 onFinishBindingItems();
3656 }
3657 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003658 }
3659
Winson Chunga2413752012-04-03 14:22:34 -07003660 private boolean canRunNewAppsAnimation() {
3661 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3662 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3663 }
3664
Winson Chungf0c6ae02012-03-21 16:10:31 -07003665 /**
3666 * Runs a new animation that scales up icons that were added while Launcher was in the
3667 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003668 *
3669 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003670 */
Winson Chunga2413752012-04-03 14:22:34 -07003671 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003672 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003673 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003674
3675 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003676 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3677 @Override
3678 public int compare(View a, View b) {
3679 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3680 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3681 int cellCountX = LauncherModel.getCellCountX();
3682 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3683 }
3684 });
Winson Chunga2413752012-04-03 14:22:34 -07003685
3686 // Animate each of the views in place (or show them immediately if requested)
3687 if (immediate) {
3688 for (View v : mNewShortcutAnimateViews) {
3689 v.setAlpha(1f);
3690 v.setScaleX(1f);
3691 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003692 }
Winson Chunga2413752012-04-03 14:22:34 -07003693 } else {
3694 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3695 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003696 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003697 PropertyValuesHolder.ofFloat("alpha", 1f),
3698 PropertyValuesHolder.ofFloat("scaleX", 1f),
3699 PropertyValuesHolder.ofFloat("scaleY", 1f));
3700 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3701 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3702 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3703 bounceAnims.add(bounceAnim);
3704 }
3705 anim.playTogether(bounceAnims);
3706 anim.addListener(new AnimatorListenerAdapter() {
3707 @Override
3708 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003709 if (mWorkspace != null) {
3710 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3711 }
Winson Chunga2413752012-04-03 14:22:34 -07003712 }
3713 });
3714 anim.start();
3715 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003716
3717 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003718 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003719 mNewShortcutAnimateViews.clear();
3720 new Thread("clearNewAppsThread") {
3721 public void run() {
3722 mSharedPrefs.edit()
3723 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3724 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3725 .commit();
3726 }
3727 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003728 }
3729
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003730 @Override
3731 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003732 boolean searchVisible = updateGlobalSearchIcon();
3733 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003734 if (mSearchDropTargetBar != null) {
3735 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3736 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003737 }
3738
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 * Add the icons for all apps.
3741 *
3742 * Implementation of the method from LauncherModel.Callbacks.
3743 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003744 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003745 Runnable setAllAppsRunnable = new Runnable() {
3746 public void run() {
3747 if (mAppsCustomizeContent != null) {
3748 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003749
3750 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3751 getHotseat().addAllAppsFolder(mIconCache, apps,
3752 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3753 mIntentsOnWorkspaceFromUpgradePath = null;
3754 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003755 }
3756 }
3757 };
3758
Michael Jurkac57b7a82011-08-09 22:02:20 -07003759 // Remove the progress bar entirely; we could also make it GONE
3760 // but better to remove it since we know it's not going to be used
3761 View progressBar = mAppsCustomizeTabHost.
3762 findViewById(R.id.apps_customize_progress_bar);
3763 if (progressBar != null) {
3764 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003765
3766 // We just post the call to setApps so the user sees the progress bar
3767 // disappear-- otherwise, it just looks like the progress bar froze
3768 // which doesn't look great
3769 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3770 } else {
3771 // If we did not initialize the spinner in onCreate, then we can directly set the
3772 // list of applications without waiting for any progress bars views to be hidden.
3773 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003774 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775 }
3776
3777 /**
3778 * A package was installed.
3779 *
3780 * Implementation of the method from LauncherModel.Callbacks.
3781 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003782 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3783 if (waitUntilResume(new Runnable() {
3784 public void run() {
3785 bindAppsAdded(apps);
3786 }
3787 })) {
3788 return;
3789 }
3790
Winson Chungf0ea4d32011-06-06 14:27:16 -07003791
Winson Chung785d2eb2011-04-14 16:08:02 -07003792 if (mAppsCustomizeContent != null) {
3793 mAppsCustomizeContent.addApps(apps);
3794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003795 }
3796
3797 /**
3798 * A package was updated.
3799 *
3800 * Implementation of the method from LauncherModel.Callbacks.
3801 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003802 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3803 if (waitUntilResume(new Runnable() {
3804 public void run() {
3805 bindAppsUpdated(apps);
3806 }
3807 })) {
3808 return;
3809 }
3810
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003811 if (mWorkspace != null) {
3812 mWorkspace.updateShortcuts(apps);
3813 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003814
Winson Chung785d2eb2011-04-14 16:08:02 -07003815 if (mAppsCustomizeContent != null) {
3816 mAppsCustomizeContent.updateApps(apps);
3817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 }
3819
3820 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003821 * A package was uninstalled. We take both the super set of packageNames
3822 * in addition to specific applications to remove, the reason being that
3823 * this can be called when a package is updated as well. In that scenario,
3824 * we only remove specific components from the workspace, where as
3825 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 *
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Winson Chung83892cc2013-05-01 16:53:33 -07003829 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3830 final ArrayList<ApplicationInfo> appInfos,
3831 final boolean matchPackageNamesOnly) {
3832 if (waitUntilResume(new Runnable() {
3833 public void run() {
3834 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3835 }
3836 })) {
3837 return;
3838 }
3839
3840 if (matchPackageNamesOnly) {
3841 mWorkspace.removeItemsByPackageName(packageNames);
3842 } else {
3843 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003844 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003845
Winson Chung785d2eb2011-04-14 16:08:02 -07003846 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003847 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003848 }
Winson Chunga1820962011-10-03 16:31:06 -07003849
3850 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003851 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 }
Joe Onoratobe386092009-11-17 17:32:16 -08003853
3854 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003855 * A number of packages were updated.
3856 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003857
3858 private ArrayList<Object> mWidgetsAndShortcuts;
3859 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003860 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003861 bindPackagesUpdated(mWidgetsAndShortcuts);
3862 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003863 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003864 };
3865
3866 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3867 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3868 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003869 return;
3870 }
3871
Winson Chung785d2eb2011-04-14 16:08:02 -07003872 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003873 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003874 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003875 }
3876
Winson Chung400438b2011-01-16 17:53:48 -08003877 private int mapConfigurationOriActivityInfoOri(int configOri) {
3878 final Display d = getWindowManager().getDefaultDisplay();
3879 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3880 switch (d.getRotation()) {
3881 case Surface.ROTATION_0:
3882 case Surface.ROTATION_180:
3883 // We are currently in the same basic orientation as the natural orientation
3884 naturalOri = configOri;
3885 break;
3886 case Surface.ROTATION_90:
3887 case Surface.ROTATION_270:
3888 // We are currently in the other basic orientation to the natural orientation
3889 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3890 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3891 break;
3892 }
3893
3894 int[] oriMap = {
3895 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3896 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3897 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3898 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3899 };
3900 // Since the map starts at portrait, we need to offset if this device's natural orientation
3901 // is landscape.
3902 int indexOffset = 0;
3903 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3904 indexOffset = 1;
3905 }
3906 return oriMap[(d.getRotation() + indexOffset) % 4];
3907 }
Adam Cohen446e9402011-09-15 18:21:21 -07003908
Winson Chung4b919f82012-05-01 10:44:08 -07003909 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003910 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003911 getResources().getBoolean(R.bool.allow_rotation);
3912 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003913 }
Winson Chung4b919f82012-05-01 10:44:08 -07003914 public void lockScreenOrientation() {
3915 if (isRotationEnabled()) {
3916 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3917 .getConfiguration().orientation));
3918 }
3919 }
3920 public void unlockScreenOrientation(boolean immediate) {
3921 if (isRotationEnabled()) {
3922 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003923 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003924 } else {
3925 mHandler.postDelayed(new Runnable() {
3926 public void run() {
3927 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3928 }
3929 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003930 }
Winson Chung4b919f82012-05-01 10:44:08 -07003931 }
Winson Chung400438b2011-01-16 17:53:48 -08003932 }
3933
Winson Chung82f55532011-08-09 14:14:23 -07003934 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003935 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003936 if (DISABLE_CLINGS) {
3937 return false;
3938 }
3939
Brett Chabot2a9e2282011-08-23 15:03:13 -07003940 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003941 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003942
Michael Jurkae233a8b2013-03-19 13:49:20 +01003943 // Restricted secondary users (child mode) will potentially have very few apps
3944 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003945// boolean supportsLimitedUsers =
3946// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3947// Account[] accounts = AccountManager.get(this).getAccounts();
3948// if (supportsLimitedUsers && accounts.length == 0) {
3949// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3950// Bundle restrictions = um.getUserRestrictions();
3951// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3952// return false;
3953// }
3954// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003955 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003956 }
Michael Jurka22143132012-10-04 17:42:38 +02003957
Winson Chung7d7541e2011-09-16 20:14:36 -07003958 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003959 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003960 if (cling != null) {
3961 cling.init(this, positionData);
3962 cling.setVisibility(View.VISIBLE);
3963 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3964 if (animate) {
3965 cling.buildLayer();
3966 cling.setAlpha(0f);
3967 cling.animate()
3968 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003969 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003970 .setDuration(SHOW_CLING_DURATION)
3971 .setStartDelay(delay)
3972 .start();
3973 } else {
3974 cling.setAlpha(1f);
3975 }
Michael Jurka22143132012-10-04 17:42:38 +02003976 cling.setFocusableInTouchMode(true);
3977 cling.post(new Runnable() {
3978 public void run() {
3979 cling.setFocusable(true);
3980 cling.requestFocus();
3981 }
3982 });
3983 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3984 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003985 }
3986 return cling;
3987 }
Michael Jurka22143132012-10-04 17:42:38 +02003988
Winson Chung7d7541e2011-09-16 20:14:36 -07003989 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003990 // To catch cases where siblings of top-level views are made invisible, just check whether
3991 // the cling is directly set to GONE before dismissing it.
3992 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003993 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003994 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003995 anim.addListener(new AnimatorListenerAdapter() {
3996 public void onAnimationEnd(Animator animation) {
3997 cling.setVisibility(View.GONE);
3998 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003999 // We should update the shared preferences on a background thread
4000 new Thread("dismissClingThread") {
4001 public void run() {
4002 SharedPreferences.Editor editor = mSharedPrefs.edit();
4003 editor.putBoolean(flag, true);
4004 editor.commit();
4005 }
4006 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004007 };
4008 });
4009 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004010 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004011 }
4012 }
Michael Jurka22143132012-10-04 17:42:38 +02004013
Winson Chung9d9d74f2011-09-19 11:49:12 -07004014 private void removeCling(int id) {
4015 final View cling = findViewById(id);
4016 if (cling != null) {
4017 final ViewGroup parent = (ViewGroup) cling.getParent();
4018 parent.post(new Runnable() {
4019 @Override
4020 public void run() {
4021 parent.removeView(cling);
4022 }
4023 });
Michael Jurka22143132012-10-04 17:42:38 +02004024 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004025 }
4026 }
Michael Jurka974c3862012-05-22 22:00:31 -07004027
4028 private boolean skipCustomClingIfNoAccounts() {
4029 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4030 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4031 if (customCling) {
4032 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004033 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004034 Account[] accounts = am.getAccountsByType("com.google");
4035 return accounts.length == 0;
4036 }
4037 return false;
4038 }
4039
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004042 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004043 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4044 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004045 // If we're not using the default workspace layout, replace workspace cling
4046 // with a custom workspace cling (usually specified in an overlay)
4047 // For now, only do this on tablets
4048 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004049 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004050 // Use a custom cling
4051 View cling = findViewById(R.id.workspace_cling);
4052 ViewGroup clingParent = (ViewGroup) cling.getParent();
4053 int clingIndex = clingParent.indexOfChild(cling);
4054 clingParent.removeViewAt(clingIndex);
4055 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4056 clingParent.addView(customCling, clingIndex);
4057 customCling.setId(R.id.workspace_cling);
4058 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004060 } else {
4061 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 }
4063 }
4064 public void showFirstRunAllAppsCling(int[] position) {
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.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004069 } else {
4070 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004071 }
4072 }
4073 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004074 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004075 if (isClingsEnabled() &&
4076 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4077 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004078 } else {
4079 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004080 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004081 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004082 }
4083 public boolean isFolderClingVisible() {
4084 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004085 if (cling != null) {
4086 return cling.getVisibility() == View.VISIBLE;
4087 }
4088 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004089 }
Winson Chung82f55532011-08-09 14:14:23 -07004090 public void dismissWorkspaceCling(View v) {
4091 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004092 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004093 }
4094 public void dismissAllAppsCling(View v) {
4095 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004096 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4097 }
4098 public void dismissFolderCling(View v) {
4099 Cling cling = (Cling) findViewById(R.id.folder_cling);
4100 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004101 }
4102
Winson Chung80baf5a2010-08-09 16:03:15 -07004103 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004104 * Prints out out state for debugging.
4105 */
4106 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004107 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004108 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004109 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4110 Log.d(TAG, "mRestoring=" + mRestoring);
4111 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4112 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004113 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004114 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004115
Winson Chung785d2eb2011-04-14 16:08:02 -07004116 if (mAppsCustomizeContent != null) {
4117 mAppsCustomizeContent.dumpState();
4118 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004119 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004120 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004121
4122 @Override
4123 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4124 super.dump(prefix, fd, writer, args);
4125 writer.println(" ");
4126 writer.println("Debug logs: ");
4127 for (int i = 0; i < sDumpLogs.size(); i++) {
4128 writer.println(" " + sDumpLogs.get(i));
4129 }
4130 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004131
4132 public static void dumpDebugLogsToConsole() {
4133 Log.d(TAG, "");
4134 Log.d(TAG, "*********************");
4135 Log.d(TAG, "Launcher debug logs: ");
4136 for (int i = 0; i < sDumpLogs.size(); i++) {
4137 Log.d(TAG, " " + sDumpLogs.get(i));
4138 }
4139 Log.d(TAG, "*********************");
4140 Log.d(TAG, "");
4141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004142}
Michael Jurka2763be32011-02-24 11:19:57 -08004143
Michael Jurkaabded662011-03-04 12:06:57 -08004144interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004145 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004146 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004147 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004148 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004149 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004150}