blob: b8fce6def6c055d5835acb3e8ec78aed064b844d [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070071import android.util.Pair;
Daniel Sandler924b9932013-06-12 00:38:25 -040072import android.view.*;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080073import android.view.View.OnLongClickListener;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080074import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080075import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070076import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070077import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080078import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.inputmethod.InputMethodManager;
Daniel Sandler924b9932013-06-12 00:38:25 -040080import android.widget.*;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070081
Daniel Sandler325dc232013-06-05 22:57:57 -040082import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080083
Adam Cohenc0dcf592011-06-01 15:30:43 -070084import java.io.DataInputStream;
85import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070086import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070087import java.io.FileNotFoundException;
88import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070089import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -070090import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070091import java.util.Collection;
92import java.util.Collections;
93import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -070095import java.util.HashSet;
Winson Chungc9168342013-06-26 14:54:55 -070096import java.util.Iterator;
Michael Jurkad7c28052012-04-27 15:43:36 -070097import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -070098import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Default launcher application.
102 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100103public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700104 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700105 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Daniel Sandlerf061f822013-06-27 13:59:36 -0400109 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400110 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700111 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400112 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700113
Winson Chung70d72102011-08-12 11:24:35 -0700114 private static final int MENU_GROUP_WALLPAPER = 1;
115 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
116 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700117 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
118 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int REQUEST_PICK_APPLICATION = 6;
123 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Michael Jurka8b805b12012-04-18 14:23:14 -0700127 private static final int REQUEST_BIND_APPWIDGET = 11;
128
Mathew Inwood876a8462013-06-14 14:12:41 +0100129 /**
130 * IntentStarter uses request codes starting with this. This must be greater than all activity
131 * request codes used internally.
132 */
133 protected static final int REQUEST_LAST = 100;
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
136
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800137 static final int SCREEN_COUNT = 5;
138 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Romain Guy98d01652009-06-30 16:21:04 -0700140 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800141 // To turn on these properties, type
142 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
143 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
144 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Winson Chung2672ff92012-05-04 16:22:30 -0700146 // The Intent extra that defines whether to ignore the launch animation
147 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400148 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700152 // Type: int
153 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
156 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: boolean
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
164 // Type: long
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
170 // Type: parcelable
171 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100173 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700174 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100175 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700176 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100177 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700178
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700179 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700180 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700181 private State mState = State.WORKSPACE;
182 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800183 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700186 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
187 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700188 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700189 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Winson Chunga2413752012-04-03 14:22:34 -0700194 // How long to wait before the new-shortcut animation automatically pans the workspace
195 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
196
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800197 private final BroadcastReceiver mCloseSystemDialogsReceiver
198 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800199 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private LayoutInflater mInflater;
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800204 private View mQsbDivider;
205 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700206 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800207 private DragLayer mDragLayer;
208 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private AppWidgetManager mAppWidgetManager;
211 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
Michael Jurkac9d95c52011-08-29 14:03:34 -0700213 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700214 private AppWidgetProviderInfo mPendingAddWidgetInfo;
215
Michael Jurka0280c3b2010-09-17 15:00:07 -0700216 private int[] mTmpAddItemCellCoordinates = new int[2];
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private FolderInfo mFolderInfo;
219
Winson Chung3d503fb2011-07-13 17:25:49 -0700220 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700222
Winson Chungc51db6a2011-10-05 11:44:49 -0700223 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700224 private AppsCustomizeTabHost mAppsCustomizeTabHost;
225 private AppsCustomizePagedView mAppsCustomizeContent;
226 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700229 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
230 // scroll issues (because the workspace may not have been measured yet) and extra work.
231 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
232 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
234 private SpannableStringBuilder mDefaultKeySsb = null;
235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private boolean mWorkspaceLoading = true;
237
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800238 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private boolean mRestoring;
240 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700241 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
Michael Jurka7607c2f2013-04-03 14:33:19 -0700243 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
244
Winson Chung4a2afa32012-07-19 14:53:05 -0700245 // Keep track of whether the user has left launcher
246 private static boolean sPausedFromUserAction = false;
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private Bundle mSavedInstanceState;
249
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800252 private boolean mUserPresent = true;
253 private boolean mVisible = false;
254 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700255 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700257 private static LocaleConfiguration sLocaleConfiguration = null;
258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700261 private Intent mAppMarketIntent = null;
262
Adam Cohended9f8d2010-11-03 13:25:16 -0700263 // Related to the auto-advancing of widgets
264 private final int ADVANCE_MSG = 1;
265 private final int mAdvanceInterval = 20000;
266 private final int mAdvanceStagger = 250;
267 private long mAutoAdvanceSentTime;
268 private long mAutoAdvanceTimeLeft = -1;
269 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
270 new HashMap<View, AppWidgetProviderInfo>();
271
Winson Chung400438b2011-01-16 17:53:48 -0800272 // Determines how long to wait after a rotation before restoring the screen orientation to
273 // match the sensor state.
274 private final int mRestoreScreenOrientationDelay = 500;
275
Michael Jurka4ef207b2010-11-29 17:05:45 -0800276 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700277 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
278 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
279 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800280
Craig Mautner360310b2012-10-26 15:13:08 -0700281 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700282
Adam Cohen1462de32012-07-24 22:34:36 -0700283 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
284
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700285 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
286
Winson Chung46353de2012-02-16 14:05:10 -0800287 // We only want to get the SharedPreferences once since it does an FS stat each time we get
288 // it from the context.
289 private SharedPreferences mSharedPrefs;
290
Adam Cohene25af792013-06-06 23:08:25 -0700291 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
292
Winson Chungf0c6ae02012-03-21 16:10:31 -0700293 // Holds the page that we need to animate to, and the icon views that we need to animate up
294 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700295 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700296 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700297 private ImageView mFolderIconImageView;
298 private Bitmap mFolderIconBitmap;
299 private Canvas mFolderIconCanvas;
300 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700301
Michael Jurkaddd62e92011-02-16 17:49:14 -0800302 private BubbleTextView mWaitingForResume;
303
Michael Jurka22143132012-10-04 17:42:38 +0200304 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
305 = new HideFromAccessibilityHelper();
306
Michael Jurkac1f5d262011-09-30 19:32:27 -0700307 private Runnable mBuildLayersRunnable = new Runnable() {
308 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700309 if (mWorkspace != null) {
310 mWorkspace.buildPageHardwareLayers();
311 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700312 }
313 };
314
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315 private static ArrayList<PendingAddArguments> sPendingAddList
316 = new ArrayList<PendingAddArguments>();
317
Michael Jurka0a457bf2012-11-19 14:05:05 -0800318 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
319
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800320 private static class PendingAddArguments {
321 int requestCode;
322 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700323 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700324 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800325 int cellX;
326 int cellY;
327 }
328
Michael Jurka0a457bf2012-11-19 14:05:05 -0800329 private static boolean isPropertyEnabled(String propertyName) {
330 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700331 }
332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 @Override
334 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700335 if (DEBUG_STRICT_MODE) {
336 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
337 .detectDiskReads()
338 .detectDiskWrites()
339 .detectNetwork() // or .detectAll() for all detectable problems
340 .penaltyLog()
341 .build());
342 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
343 .detectLeakedSqlLiteObjects()
344 .detectLeakedClosableObjects()
345 .penaltyLog()
346 .penaltyDeath()
347 .build());
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400351
352 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
353 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400354 LauncherAppState app = LauncherAppState.getInstance();
355 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700356 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 mModel = app.setLauncher(this);
358 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400359 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
364 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700366 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
367 // this also ensures that any synchronous binding below doesn't re-trigger another
368 // LauncherModel load.
369 mPaused = false;
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200372 android.os.Debug.startMethodTracing(
373 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 }
375
376 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 setContentView(R.layout.launcher);
378 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700379 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800381 registerContentObservers();
382
Joe Onorato7c312c12009-08-13 21:36:53 -0700383 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mSavedState = savedInstanceState;
386 restoreState(mSavedState);
387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700389 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200390 mAppsCustomizeContent.onPackagesUpdated(
391 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700392 }
Winson Chunga12a2502010-12-20 14:41:35 -0800393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 if (PROFILE_STARTUP) {
395 android.os.Debug.stopMethodTracing();
396 }
397
398 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700399 if (sPausedFromUserAction) {
400 // If the user leaves launcher, then we should just load items asynchronously when
401 // they return.
402 mModel.startLoader(true, -1);
403 } else {
404 // We only load the page synchronously if the user rotates (or triggers a
405 // configuration change) while launcher is in the foreground
406 mModel.startLoader(true, mWorkspace.getCurrentPage());
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
Michael Jurkac57b7a82011-08-09 22:02:20 -0700410 if (!mModel.isAllAppsLoaded()) {
411 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
412 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 // For handling default keys
416 mDefaultKeySsb = new SpannableStringBuilder();
417 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800418
419 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
420 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800421
Adam Cohenf9426d52012-06-04 17:26:21 -0700422 updateGlobalIcons();
423
424 // On large interfaces, we want the screen to auto-rotate based on the current orientation
425 unlockScreenOrientation(true);
426 }
427
Winson Chung4a2afa32012-07-19 14:53:05 -0700428 protected void onUserLeaveHint() {
429 super.onUserLeaveHint();
430 sPausedFromUserAction = true;
431 }
432
Adam Cohenf9426d52012-06-04 17:26:21 -0700433 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700434 boolean searchVisible = false;
435 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800436 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700437 int coi = getCurrentOrientationIndexForGlobalIcons();
438 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
439 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700440 updateAppMarketIcon();
441 searchVisible = updateGlobalSearchIcon();
442 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700444 if (sGlobalSearchIcon[coi] != null) {
445 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700447 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 if (sVoiceSearchIcon[coi] != null) {
449 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700450 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700452 if (sAppMarketIcon[coi] != null) {
453 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800454 }
Winson Chungadf0c182012-08-23 14:59:07 -0700455 if (mSearchDropTargetBar != null) {
456 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
Romain Guycbb89e42009-06-08 15:52:54 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700461 if (sLocaleConfiguration == null) {
462 new AsyncTask<Void, Void, LocaleConfiguration>() {
463 @Override
464 protected LocaleConfiguration doInBackground(Void... unused) {
465 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
466 readConfiguration(Launcher.this, localeConfiguration);
467 return localeConfiguration;
468 }
469
470 @Override
471 protected void onPostExecute(LocaleConfiguration result) {
472 sLocaleConfiguration = result;
473 checkForLocaleChange(); // recursive, but now with a locale configuration
474 }
475 }.execute();
476 return;
477 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final Configuration configuration = getResources().getConfiguration();
480
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final String locale = configuration.locale.toString();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final int mcc = configuration.mcc;
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mnc = configuration.mnc;
489
Romain Guy84f296c2009-11-04 15:00:44 -0800490 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493 sLocaleConfiguration.locale = locale;
494 sLocaleConfiguration.mcc = mcc;
495 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700496
Joe Onorato0589f0f2010-02-08 13:44:00 -0800497 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498
499 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
500 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700501 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700502 public void run() {
503 writeConfiguration(Launcher.this, localeConfiguration);
504 }
505 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700506 }
507 }
508
509 private static class LocaleConfiguration {
510 public String locale;
511 public int mcc = -1;
512 public int mnc = -1;
513 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700514
Romain Guy98d01652009-06-30 16:21:04 -0700515 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
516 DataInputStream in = null;
517 try {
518 in = new DataInputStream(context.openFileInput(PREFERENCES));
519 configuration.locale = in.readUTF();
520 configuration.mcc = in.readInt();
521 configuration.mnc = in.readInt();
522 } catch (FileNotFoundException e) {
523 // Ignore
524 } catch (IOException e) {
525 // Ignore
526 } finally {
527 if (in != null) {
528 try {
529 in.close();
530 } catch (IOException e) {
531 // Ignore
532 }
533 }
534 }
535 }
536
537 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
538 DataOutputStream out = null;
539 try {
540 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
541 out.writeUTF(configuration.locale);
542 out.writeInt(configuration.mcc);
543 out.writeInt(configuration.mnc);
544 out.flush();
545 } catch (FileNotFoundException e) {
546 // Ignore
547 } catch (IOException e) {
548 //noinspection ResultOfMethodCallIgnored
549 context.getFileStreamPath(PREFERENCES).delete();
550 } finally {
551 if (out != null) {
552 try {
553 out.close();
554 } catch (IOException e) {
555 // Ignore
556 }
557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 }
559 }
560
Bjorn Bringertc459e522013-06-07 19:36:01 +0100561 public LayoutInflater getInflater() {
562 return mInflater;
563 }
564
Adam Cohen716b51e2011-06-30 12:09:54 -0700565 public DragLayer getDragLayer() {
566 return mDragLayer;
567 }
568
Winson Chung36a62fe2012-05-06 18:04:42 -0700569 boolean isDraggingEnabled() {
570 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
571 // that is subsequently removed from the workspace in startBinding().
572 return !mModel.isLoadingWorkspace();
573 }
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 static int getScreen() {
576 synchronized (sLock) {
577 return sScreen;
578 }
579 }
580
581 static void setScreen(int screen) {
582 synchronized (sLock) {
583 sScreen = screen;
584 }
585 }
586
Winson Chung557d6ed2011-07-08 15:34:52 -0700587 /**
588 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
589 * a configuration step, this allows the proper animations to run after other transitions.
590 */
591 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700592 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 switch (args.requestCode) {
594 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700595 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700596 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 break;
598 case REQUEST_PICK_SHORTCUT:
599 processShortcut(args.intent);
600 break;
601 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700602 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700603 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700604 result = true;
605 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 case REQUEST_CREATE_APPWIDGET:
607 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700608 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700609 result = true;
610 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 case REQUEST_PICK_WALLPAPER:
612 // We just wanted the activity result here so we can clear mWaitingForResult
613 break;
614 }
Michael Jurka27614d22012-04-02 06:40:22 -0700615 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
616 // if you turned the screen off and then back while in All Apps, Launcher would not
617 // return to the workspace. Clearing mAddInfo.container here fixes this issue
618 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700619 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700623 protected void onActivityResult(
624 final int requestCode, final int resultCode, final Intent data) {
625 if (requestCode == REQUEST_BIND_APPWIDGET) {
626 int appWidgetId = data != null ?
627 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
628 if (resultCode == RESULT_CANCELED) {
629 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
630 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700631 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700632 }
633 return;
634 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700635 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
637 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700638 mWaitingForResult = false;
639
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 // We have special handling for widgets
641 if (isWidgetDrop) {
642 int appWidgetId = data != null ?
643 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700644 if (appWidgetId < 0) {
645 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
646 "widget configuration activity.");
647 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
648 } else {
649 completeTwoStageWidgetDrop(resultCode, appWidgetId);
650 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 return;
652 }
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // The pattern used here is that a user PICKs a specific application,
655 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
658 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700659 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 final PendingAddArguments args = new PendingAddArguments();
661 args.requestCode = requestCode;
662 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700664 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 args.cellX = mPendingAddInfo.cellX;
666 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800667 if (isWorkspaceLocked()) {
668 sPendingAddList.add(args);
669 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
676 null);
677 }
678
679 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700680 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700681 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 Runnable onCompleteRunnable = null;
683 int animationType = 0;
684
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700685 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 if (resultCode == RESULT_OK) {
687 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
688 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700689 mPendingAddWidgetInfo);
690 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 onCompleteRunnable = new Runnable() {
692 @Override
693 public void run() {
694 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700695 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
697 null);
698 }
699 };
700 } else if (resultCode == RESULT_CANCELED) {
701 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
702 onCompleteRunnable = new Runnable() {
703 @Override
704 public void run() {
705 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
706 null);
707 }
708 };
709 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 if (mDragLayer.getAnimatedView() != null) {
711 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
712 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
713 animationType, boundWidget, true);
714 } else {
715 // The animated view may be null in the case of a rotation during widget configuration
716 onCompleteRunnable.run();
717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 }
719
720 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700721 protected void onStop() {
722 super.onStop();
723 FirstFrameAnimatorHelper.setIsVisible(false);
724 }
725
726 @Override
727 protected void onStart() {
728 super.onStart();
729 FirstFrameAnimatorHelper.setIsVisible(true);
730 }
731
732 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200734 long startTime = 0;
735 if (DEBUG_RESUME_TIME) {
736 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400737 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700740
Winson Chung4a2afa32012-07-19 14:53:05 -0700741 // Restore the previous launcher state
742 if (mOnResumeState == State.WORKSPACE) {
743 showWorkspace(false);
744 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
745 showAllApps(false);
746 }
747 mOnResumeState = State.NONE;
748
Craig Mautner360310b2012-10-26 15:13:08 -0700749 // Background was set to gradient in onPause(), restore to black if in all apps.
750 setWorkspaceBackground(mState == State.WORKSPACE);
751
Winson Chungde0fb8f2012-05-08 14:37:08 -0700752 // Process any items that were added while Launcher was away
753 InstallShortcutReceiver.flushInstallQueue(this);
754
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800755 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700756 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700757 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700759 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700761 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200763 if (mOnResumeCallbacks.size() > 0) {
764 // We might have postponed some bind calls until onResume (see waitUntilResume) --
765 // execute them here
766 long startTimeCallbacks = 0;
767 if (DEBUG_RESUME_TIME) {
768 startTimeCallbacks = System.currentTimeMillis();
769 }
770
771 if (mAppsCustomizeContent != null) {
772 mAppsCustomizeContent.setBulkBind(true);
773 }
774 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
775 mOnResumeCallbacks.get(i).run();
776 }
777 if (mAppsCustomizeContent != null) {
778 mAppsCustomizeContent.setBulkBind(false);
779 }
780 mOnResumeCallbacks.clear();
781 if (DEBUG_RESUME_TIME) {
782 Log.d(TAG, "Time spent processing callbacks in onResume: " +
783 (System.currentTimeMillis() - startTimeCallbacks));
784 }
Michael Jurka447bf842013-05-15 14:52:15 +0200785 }
Winson Chunge4e50662012-01-23 14:45:13 -0800786
787 // Reset the pressed state of icons that were locked in the press state while activities
788 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800789 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800790 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800791 mWaitingForResume.setStayPressed(false);
792 }
Winson Chunge4e50662012-01-23 14:45:13 -0800793 if (mAppsCustomizeContent != null) {
794 // Resets the previous all apps icon press state
795 mAppsCustomizeContent.resetDrawableState();
796 }
Adam Cohen06dff352012-06-01 17:17:08 -0700797 // It is possible that widgets can receive updates while launcher is not in the foreground.
798 // Consequently, the widgets will be inflated in the orientation of the foreground activity
799 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
800 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700801 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700802
803 // Again, as with the above scenario, it's possible that one or more of the global icons
804 // were updated in the wrong orientation.
805 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200806 if (DEBUG_RESUME_TIME) {
807 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
808 }
Adam Cohen06dff352012-06-01 17:17:08 -0700809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700812 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700813 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
814 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
815 // when Launcher resumes and we are still in AllApps.
816 updateWallpaperVisibility(true);
817
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700818 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700819 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800820 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700821 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 }
Romain Guycbb89e42009-06-08 15:52:54 -0700823
Adam Cohen66a01fd2013-06-11 12:48:00 -0700824 protected void onFinishBindingItems() {
825 }
826
827 // Add a fullscreen unpadded view to the workspace to the left all other screens.
828 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700829 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700830 }
831
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700832 @Override
833 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400834 // Flag the loader to stop early before switching
835 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700836 if (mAppsCustomizeContent != null) {
837 mAppsCustomizeContent.surrender();
838 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800839 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700840 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700841
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800842 // We can't hide the IME if it was forced open. So don't bother
843 /*
844 @Override
845 public void onWindowFocusChanged(boolean hasFocus) {
846 super.onWindowFocusChanged(hasFocus);
847
848 if (hasFocus) {
849 final InputMethodManager inputManager = (InputMethodManager)
850 getSystemService(Context.INPUT_METHOD_SERVICE);
851 WindowManager.LayoutParams lp = getWindow().getAttributes();
852 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
853 android.os.Handler()) {
854 protected void onReceiveResult(int resultCode, Bundle resultData) {
855 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
856 }
857 });
858 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
859 }
860 }
861 */
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 private boolean acceptFilter() {
864 final InputMethodManager inputManager = (InputMethodManager)
865 getSystemService(Context.INPUT_METHOD_SERVICE);
866 return !inputManager.isFullscreenMode();
867 }
868
869 @Override
870 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700871 final int uniChar = event.getUnicodeChar();
872 final boolean handled = super.onKeyDown(keyCode, event);
873 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
874 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
876 keyCode, event);
877 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700878 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700879 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700880 // showSearchDialog()
881 // If there are multiple keystrokes before the search dialog takes focus,
882 // onSearchRequested() will be called for every keystroke,
883 // but it is idempotent, so it's fine.
884 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
887
Joe Onorato8a9625e2010-01-28 15:55:35 -0800888 // Eat the long press event so the keyboard doesn't come up.
889 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
890 return true;
891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 return handled;
894 }
895
Karl Rosaen138a0412009-04-23 19:00:21 -0700896 private String getTypedText() {
897 return mDefaultKeySsb.toString();
898 }
899
900 private void clearTypedText() {
901 mDefaultKeySsb.clear();
902 mDefaultKeySsb.clearSpans();
903 Selection.setSelection(mDefaultKeySsb, 0);
904 }
905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700907 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
908 * State
909 */
910 private static State intToState(int stateOrdinal) {
911 State state = State.WORKSPACE;
912 final State[] stateValues = State.values();
913 for (int i = 0; i < stateValues.length; i++) {
914 if (stateValues[i].ordinal() == stateOrdinal) {
915 state = stateValues[i];
916 break;
917 }
918 }
919 return state;
920 }
921
922 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 * Restores the previous state, if it exists.
924 *
925 * @param savedState The previous state.
926 */
927 private void restoreState(Bundle savedState) {
928 if (savedState == null) {
929 return;
930 }
931
Michael Jurka883f55b2010-10-21 15:47:14 -0700932 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700933 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700934 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800935 }
936
Winson Chungf0c6ae02012-03-21 16:10:31 -0700937 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700939 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
946 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700947 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700948 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
949 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
951 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
952 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700953 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 mRestoring = true;
955 }
956
957 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
958 if (renameFolder) {
959 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700960 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 mRestoring = true;
962 }
Winson Chunga12a2502010-12-20 14:41:35 -0800963
Winson Chung785d2eb2011-04-14 16:08:02 -0700964 // Restore the AppsCustomize tab
965 if (mAppsCustomizeTabHost != null) {
966 String curTab = savedState.getString("apps_customize_currentTab");
967 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700968 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700969 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700970 mAppsCustomizeContent.loadAssociatedPages(
971 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700972 }
973
Winson Chung5afbf7b2011-07-25 11:53:08 -0700974 int currentIndex = savedState.getInt("apps_customize_currentIndex");
975 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Finds all the views we need and configure them properly.
981 */
982 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700983 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400984
Craig Mautner360310b2012-10-26 15:13:08 -0700985 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700986 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
987 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700988 mQsbDivider = findViewById(R.id.qsb_divider);
989 mDockDivider = findViewById(R.id.dock_divider);
990
991 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
992 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
Winson Chung4c98d922011-05-31 16:50:48 -0700994 // Setup the drag layer
995 mDragLayer.setup(this, dragController);
996
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 // Setup the hotseat
998 mHotseat = (Hotseat) findViewById(R.id.hotseat);
999 if (mHotseat != null) {
1000 mHotseat.setup(this);
1001 }
1002
Winson Chung4c98d922011-05-31 16:50:48 -07001003 // Setup the workspace
1004 mWorkspace.setHapticFeedbackEnabled(false);
1005 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001006 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001007 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001008
Winson Chungf0ea4d32011-06-06 14:27:16 -07001009 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001010 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001011
Winson Chungf0ea4d32011-06-06 14:27:16 -07001012 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001013 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001014 mAppsCustomizeContent = (AppsCustomizePagedView)
1015 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1016 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001017
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001019 dragController.setDragScoller(mWorkspace);
1020 dragController.setScrollView(mDragLayer);
1021 dragController.setMoveTarget(mWorkspace);
1022 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001023 if (mSearchDropTargetBar != null) {
1024 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001025 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001026
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001027 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001028 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001029 final View ww = new WeightWatcher(this);
1030 ww.setAlpha(0.5f);
1031 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001032 new FrameLayout.LayoutParams(
1033 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001034 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001035 Gravity.BOTTOM)
1036 );
1037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
1040 /**
1041 * Creates a view representing a shortcut.
1042 *
1043 * @param info The data structure describing the shortcut.
1044 *
1045 * @return A View inflated from R.layout.application.
1046 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001049 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
1051
1052 /**
1053 * Creates a view representing a shortcut inflated from the specified resource.
1054 *
1055 * @param layoutResId The id of the XML layout used to create the shortcut.
1056 * @param parent The group the shortcut belongs to.
1057 * @param info The data structure describing the shortcut.
1058 *
1059 * @return A View inflated from layoutResId.
1060 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001061 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001062 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1063 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 return favorite;
1066 }
1067
1068 /**
1069 * Add an application shortcut to the workspace.
1070 *
1071 * @param data The intent describing the application.
1072 * @param cellInfo The position on screen where to create the shortcut.
1073 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001074 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077
Adam Cohenfbba09b2011-07-18 21:43:05 -07001078 // First we check if we already know the exact location where we want to add this item.
1079 if (cellX >= 0 && cellY >= 0) {
1080 cellXY[0] = cellX;
1081 cellXY[1] = cellY;
1082 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001083 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 return;
1085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001087 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001088
Romain Guy73b979d2009-06-09 12:57:21 -07001089 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001092 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001093 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001094 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001095 } else {
1096 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
1098 }
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 /**
1101 * Add a shortcut to the workspace.
1102 *
1103 * @param data The intent describing the shortcut.
1104 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001106 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 int cellY) {
1108 int[] cellXY = mTmpAddItemCellCoordinates;
1109 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001110 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
Michael Jurkad3ef3062010-11-23 16:23:58 -08001112 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001113
Adam Cohen558baaf2011-08-15 15:22:57 -07001114 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001115 if (info == null) {
1116 return;
1117 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001118 final View view = createShortcut(info);
1119
Adam Cohenfbba09b2011-07-18 21:43:05 -07001120 // First we check if we already know the exact location where we want to add this item.
1121 if (cellX >= 0 && cellY >= 0) {
1122 cellXY[0] = cellX;
1123 cellXY[1] = cellY;
1124 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001125
1126 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001127 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001128 true, null,null)) {
1129 return;
1130 }
1131 DragObject dragObject = new DragObject();
1132 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001133 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1134 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001135 return;
1136 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001137 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001138 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001139 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001140 foundCellSpan = (result != null);
1141 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001142 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001143 }
1144
1145 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001146 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 return;
1148 }
1149
Adam Cohendcd297f2013-06-18 13:13:40 -07001150 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
1152 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001153 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156 }
1157
Adam Cohen2f093b62012-04-30 18:59:53 -07001158 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1159 int minHeight) {
1160 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001161 // We want to account for the extra amount of padding that we are adding to the widget
1162 // to ensure that it gets the full amount of space that it has requested
1163 int requiredWidth = minWidth + padding.left + padding.right;
1164 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001165 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001166 }
1167
Adam Cohen2f093b62012-04-30 18:59:53 -07001168 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1169 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001170 }
1171
Adam Cohen2f093b62012-04-30 18:59:53 -07001172 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1173 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001174 }
1175
Adam Cohen2f093b62012-04-30 18:59:53 -07001176 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1177 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001178 }
1179
Adam Cohen2f093b62012-04-30 18:59:53 -07001180 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1181 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001182 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001183 }
1184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001188 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001189 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001191 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001192 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1193 if (appWidgetInfo == null) {
1194 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1195 }
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001198 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001199
Adam Cohen2f093b62012-04-30 18:59:53 -07001200 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1201 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001204 // We have saved the position to which the widget was dragged-- this really only matters
1205 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001206 int[] cellXY = mTmpAddItemCellCoordinates;
1207 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001208 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001209 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001210 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1211 cellXY[0] = mPendingAddInfo.cellX;
1212 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 spanXY[0] = mPendingAddInfo.spanX;
1214 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001215 foundCellSpan = true;
1216 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001218 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1220 spanXY[1], cellXY, finalSpan);
1221 spanXY[0] = finalSpan[0];
1222 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001223 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001226 }
1227
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001229 if (appWidgetId != -1) {
1230 // Deleting an app widget ID is a void call but writes to disk before returning
1231 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001232 new Thread("deleteAppWidgetId") {
1233 public void run() {
1234 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1235 }
1236 }.start();
1237 }
Winson Chung93eef082012-03-23 15:59:27 -07001238 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001239 return;
1240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001242 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001243 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1244 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 launcherInfo.spanX = spanXY[0];
1246 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1248 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001251 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252
1253 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001254 if (hostView == null) {
1255 // Perform actual inflation because we're live
1256 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1257 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1258 } else {
1259 // The AppWidgetHostView has already been inflated and instantiated
1260 launcherInfo.hostView = hostView;
1261 }
Romain Guycbb89e42009-06-08 15:52:54 -07001262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001264 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001265 launcherInfo.notifyWidgetSizeChanged(this);
1266
Adam Cohendcd297f2013-06-18 13:13:40 -07001267 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001269
1270 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001272 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Romain Guycbb89e42009-06-08 15:52:54 -07001274
Adam Cohended9f8d2010-11-03 13:25:16 -07001275 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1276 @Override
1277 public void onReceive(Context context, Intent intent) {
1278 final String action = intent.getAction();
1279 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1280 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001281 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001282 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001283
Winson Chungc100e8e2011-08-09 16:02:43 -07001284 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001285 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001286 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1287 mAppsCustomizeTabHost.reset();
1288 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001289 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1291 mUserPresent = true;
1292 updateRunning();
1293 }
1294 }
1295 };
1296
1297 @Override
1298 public void onAttachedToWindow() {
1299 super.onAttachedToWindow();
1300
1301 // Listen for broadcasts related to user-presence
1302 final IntentFilter filter = new IntentFilter();
1303 filter.addAction(Intent.ACTION_SCREEN_OFF);
1304 filter.addAction(Intent.ACTION_USER_PRESENT);
1305 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001306 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001307 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001308 mVisible = true;
1309 }
1310
1311 @Override
1312 public void onDetachedFromWindow() {
1313 super.onDetachedFromWindow();
1314 mVisible = false;
1315
Adam Cohend113e0c2010-11-11 10:48:05 -08001316 if (mAttached) {
1317 unregisterReceiver(mReceiver);
1318 mAttached = false;
1319 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001320 updateRunning();
1321 }
1322
1323 public void onWindowVisibilityChanged(int visibility) {
1324 mVisible = visibility == View.VISIBLE;
1325 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001326 // The following code used to be in onResume, but it turns out onResume is called when
1327 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1328 // is a more appropriate event to handle
1329 if (mVisible) {
1330 mAppsCustomizeTabHost.onWindowVisible();
1331 if (!mWorkspaceLoading) {
1332 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001333 // We want to let Launcher draw itself at least once before we force it to build
1334 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001335 // apps is nice and speedy.
1336 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001337 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001338 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001339 if (mStarted) return;
1340 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001341 // We delay the layer building a bit in order to give
1342 // other message processing a time to run. In particular
1343 // this avoids a delay in hiding the IME if it was
1344 // currently shown, because doing that may involve
1345 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001346 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001347 final ViewTreeObserver.OnDrawListener listener = this;
1348 mWorkspace.post(new Runnable() {
1349 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001350 if (mWorkspace != null &&
1351 mWorkspace.getViewTreeObserver() != null) {
1352 mWorkspace.getViewTreeObserver().
1353 removeOnDrawListener(listener);
1354 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001355 }
1356 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001357 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001358 }
1359 });
1360 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001361 // When Launcher comes back to foreground, a different Activity might be responsible for
1362 // the app market intent, so refresh the icon
1363 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001364 clearTypedText();
1365 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001366 }
1367
1368 private void sendAdvanceMessage(long delay) {
1369 mHandler.removeMessages(ADVANCE_MSG);
1370 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1371 mHandler.sendMessageDelayed(msg, delay);
1372 mAutoAdvanceSentTime = System.currentTimeMillis();
1373 }
1374
1375 private void updateRunning() {
1376 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1377 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1378 mAutoAdvanceRunning = autoAdvanceRunning;
1379 if (autoAdvanceRunning) {
1380 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1381 sendAdvanceMessage(delay);
1382 } else {
1383 if (!mWidgetsToAdvance.isEmpty()) {
1384 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1385 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1386 }
1387 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001388 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001389 }
1390 }
1391 }
1392
1393 private final Handler mHandler = new Handler() {
1394 @Override
1395 public void handleMessage(Message msg) {
1396 if (msg.what == ADVANCE_MSG) {
1397 int i = 0;
1398 for (View key: mWidgetsToAdvance.keySet()) {
1399 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1400 final int delay = mAdvanceStagger * i;
1401 if (v instanceof Advanceable) {
1402 postDelayed(new Runnable() {
1403 public void run() {
1404 ((Advanceable) v).advance();
1405 }
1406 }, delay);
1407 }
1408 i++;
1409 }
1410 sendAdvanceMessage(mAdvanceInterval);
1411 }
1412 }
1413 };
1414
1415 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001416 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001417 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1418 if (v instanceof Advanceable) {
1419 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001420 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001421 updateRunning();
1422 }
1423 }
1424
1425 void removeWidgetToAutoAdvance(View hostView) {
1426 if (mWidgetsToAdvance.containsKey(hostView)) {
1427 mWidgetsToAdvance.remove(hostView);
1428 updateRunning();
1429 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 }
1431
Joe Onorato9c1289c2009-08-17 11:03:03 -04001432 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001433 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 launcherInfo.hostView = null;
1435 }
1436
Winson Chung93eef082012-03-23 15:59:27 -07001437 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1438 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1439 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001440 }
1441
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001442 public LauncherAppWidgetHost getAppWidgetHost() {
1443 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
Romain Guycbb89e42009-06-08 15:52:54 -07001445
Winson Chunga9abd0e2010-10-27 17:18:37 -07001446 public LauncherModel getModel() {
1447 return mModel;
1448 }
1449
Bjorn Bringertc459e522013-06-07 19:36:01 +01001450 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001451 getWindow().closeAllPanels();
1452
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001453 // Whatever we were doing is hereby canceled.
1454 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001455 }
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 @Override
1458 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001459 long startTime = 0;
1460 if (DEBUG_RESUME_TIME) {
1461 startTime = System.currentTimeMillis();
1462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 super.onNewIntent(intent);
1464
1465 // Close the menu
1466 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001467 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001468 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001469
Jamie Genniscb222e82012-10-23 15:44:26 -07001470 final boolean alreadyOnHome =
1471 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001472 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001473
Jamie Genniscb222e82012-10-23 15:44:26 -07001474 Runnable processIntent = new Runnable() {
1475 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001476 if (mWorkspace == null) {
1477 // Can be cases where mWorkspace is null, this prevents a NPE
1478 return;
1479 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001480 Folder openFolder = mWorkspace.getOpenFolder();
1481 // In all these cases, only animate if we're already on home
1482 mWorkspace.exitWidgetResizeMode();
1483 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1484 openFolder == null) {
1485 mWorkspace.moveToDefaultScreen(true);
1486 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001487
Jamie Genniscb222e82012-10-23 15:44:26 -07001488 closeFolder();
1489 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001490
Jamie Genniscb222e82012-10-23 15:44:26 -07001491 // If we are already on home, then just animate back to the workspace,
1492 // otherwise, just wait until onResume to set the state back to Workspace
1493 if (alreadyOnHome) {
1494 showWorkspace(true);
1495 } else {
1496 mOnResumeState = State.WORKSPACE;
1497 }
1498
1499 final View v = getWindow().peekDecorView();
1500 if (v != null && v.getWindowToken() != null) {
1501 InputMethodManager imm = (InputMethodManager)getSystemService(
1502 INPUT_METHOD_SERVICE);
1503 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1504 }
1505
1506 // Reset AllApps to its initial state
1507 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1508 mAppsCustomizeTabHost.reset();
1509 }
1510 }
1511 };
1512
1513 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1514 // Delay processing of the intent to allow the status bar animation to finish
1515 // first in order to avoid janky animations.
1516 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001517 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001518 // Process the intent immediately.
1519 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001520 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
Michael Jurka447bf842013-05-15 14:52:15 +02001523 if (DEBUG_RESUME_TIME) {
1524 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527
1528 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001529 public void onRestoreInstanceState(Bundle state) {
1530 super.onRestoreInstanceState(state);
1531 for (int page: mSynchronouslyBoundPages) {
1532 mWorkspace.restoreInstanceStateForChild(page);
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
1536 @Override
1537 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001538 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001539 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540
Michael Jurka883f55b2010-10-21 15:47:14 -07001541 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001542 // We close any open folder since it will not be re-opened, and we need to make sure
1543 // this state is reflected.
1544 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 mWaitingForResult) {
1548 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001550 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1551 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001552 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1553 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1554 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
1557 if (mFolderInfo != null && mWaitingForResult) {
1558 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1559 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1560 }
Michael Jurka946ad472010-07-09 18:05:18 -07001561
Winson Chung785d2eb2011-04-14 16:08:02 -07001562 // Save the current AppsCustomize tab
1563 if (mAppsCustomizeTabHost != null) {
1564 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1565 if (currentTabTag != null) {
1566 outState.putString("apps_customize_currentTab", currentTabTag);
1567 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001568 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1569 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572
1573 @Override
1574 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001576
Winson Chunge7a03942011-08-05 15:05:12 -07001577 // Remove all pending runnables
1578 mHandler.removeMessages(ADVANCE_MSG);
1579 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001580 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001581
Winson Chungcd2b0142011-06-08 16:02:26 -07001582 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001583 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001584 mModel.stopLoader();
1585 app.setLauncher(null);
1586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001588 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001590 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001592 mAppWidgetHost = null;
1593
1594 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595
1596 TextKeyListener.getInstance().release();
1597
Winson Chung81b52252012-08-27 15:34:29 -07001598 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1599 // to prevent leaking Launcher activities on orientation change.
1600 if (mModel != null) {
1601 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1602 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001603
1604 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001605 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001606
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001607 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001608 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1609 mWorkspace.removeAllViews();
1610 mWorkspace = null;
1611 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001612
Michael Jurka2ecf9952012-06-18 12:52:28 -07001613 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
1615
Adam Cohena9cf38f2011-05-02 15:36:58 -07001616 public DragController getDragController() {
1617 return mDragController;
1618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 @Override
1621 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001622 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 super.startActivityForResult(intent, requestCode);
1624 }
1625
Winson Chung70d72102011-08-12 11:24:35 -07001626 /**
1627 * Indicates that we want global search for this activity by setting the globalSearch
1628 * argument for {@link #startSearch} to true.
1629 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001631 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001633
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001634 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001635
Karl Rosaen138a0412009-04-23 19:00:21 -07001636 if (initialQuery == null) {
1637 // Use any text typed in the launcher as the initial query
1638 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 if (appSearchData == null) {
1641 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001642 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
Winson Chungadf0c182012-08-23 14:59:07 -07001644 Rect sourceBounds = new Rect();
1645 if (mSearchDropTargetBar != null) {
1646 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Bjorn Bringertc459e522013-06-07 19:36:01 +01001649 startSearch(initialQuery, selectInitialQuery,
1650 appSearchData, sourceBounds);
1651 }
1652
1653 public void startSearch(String initialQuery,
1654 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001655 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001656 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001657 }
1658
1659 /**
1660 * Starts the global search activity. This code is a copied from SearchManager
1661 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001662 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001663 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001664 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001665 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1666 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1667 if (globalSearchActivity == null) {
1668 Log.w(TAG, "No global search activity found.");
1669 return;
1670 }
1671 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1672 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1673 intent.setComponent(globalSearchActivity);
1674 // Make sure that we have a Bundle to put source in
1675 if (appSearchData == null) {
1676 appSearchData = new Bundle();
1677 } else {
1678 appSearchData = new Bundle(appSearchData);
1679 }
1680 // Set source to package name of app that starts global search, if not set already.
1681 if (!appSearchData.containsKey("source")) {
1682 appSearchData.putString("source", getPackageName());
1683 }
1684 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1685 if (!TextUtils.isEmpty(initialQuery)) {
1686 intent.putExtra(SearchManager.QUERY, initialQuery);
1687 }
1688 if (selectInitialQuery) {
1689 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1690 }
1691 intent.setSourceBounds(sourceBounds);
1692 try {
1693 startActivity(intent);
1694 } catch (ActivityNotFoundException ex) {
1695 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
1698
Winson Chung70d72102011-08-12 11:24:35 -07001699 @Override
1700 public boolean onCreateOptionsMenu(Menu menu) {
1701 if (isWorkspaceLocked()) {
1702 return false;
1703 }
1704
1705 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001706
1707 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1708 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1709 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001710 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1711 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1712 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001713 String helpUrl = getString(R.string.help_url);
1714 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001715 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1716 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1717
Winson Chung70d72102011-08-12 11:24:35 -07001718 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1719 .setIcon(android.R.drawable.ic_menu_gallery)
1720 .setAlphabeticShortcut('W');
1721 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1722 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001723 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001724 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001725 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1726 .setIcon(android.R.drawable.ic_menu_preferences)
1727 .setIntent(settings)
1728 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001729 if (!helpUrl.isEmpty()) {
1730 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1731 .setIcon(android.R.drawable.ic_menu_help)
1732 .setIntent(help)
1733 .setAlphabeticShortcut('H');
1734 }
Winson Chung70d72102011-08-12 11:24:35 -07001735 return true;
1736 }
1737
1738 @Override
1739 public boolean onPrepareOptionsMenu(Menu menu) {
1740 super.onPrepareOptionsMenu(menu);
1741
1742 if (mAppsCustomizeTabHost.isTransitioning()) {
1743 return false;
1744 }
1745 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1746 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1747
1748 return true;
1749 }
1750
1751 @Override
1752 public boolean onOptionsItemSelected(MenuItem item) {
1753 switch (item.getItemId()) {
1754 case MENU_WALLPAPER_SETTINGS:
1755 startWallpaper();
1756 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001757 }
1758
1759 return super.onOptionsItemSelected(item);
1760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001762 @Override
1763 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001764 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001765 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001766 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001767 }
1768
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769 public boolean isWorkspaceLocked() {
1770 return mWorkspaceLoading || mWaitingForResult;
1771 }
1772
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001774 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001775 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001776 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1777 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001778 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001779 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001781
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001782 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1783 AppWidgetProviderInfo appWidgetInfo) {
1784 if (appWidgetInfo.configure != null) {
1785 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001786
Bjorn Bringert7984c942009-12-09 15:38:25 +00001787 // Launch over to configure widget, if needed
1788 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001789 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001790 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001791 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001793 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001794 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001795 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001796 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001797 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799 }
Romain Guycbb89e42009-06-08 15:52:54 -07001800
Adam Cohenfbba09b2011-07-18 21:43:05 -07001801 /**
1802 * Process a shortcut drop.
1803 *
1804 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001806 * @param cell The cell it should be added to, optional
1807 * @param position The location on the screen where it was dropped, optional
1808 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001812 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001813 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001814 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001815
1816 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001817 mPendingAddInfo.cellX = cell[0];
1818 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001819 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820
1821 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1822 createShortcutIntent.setComponent(componentName);
1823 processShortcut(createShortcutIntent);
1824 }
1825
Adam Cohenfbba09b2011-07-18 21:43:05 -07001826 /**
1827 * Process a widget drop.
1828 *
1829 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001830 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001831 * @param cell The cell it should be added to, optional
1832 * @param position The location on the screen where it was dropped, optional
1833 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001834 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001835 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001836 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001838 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001839 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001840 mPendingAddInfo.minSpanX = info.minSpanX;
1841 mPendingAddInfo.minSpanY = info.minSpanY;
1842
Adam Cohenfbba09b2011-07-18 21:43:05 -07001843 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001844 mPendingAddInfo.cellX = cell[0];
1845 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001846 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001847 if (span != null) {
1848 mPendingAddInfo.spanX = span[0];
1849 mPendingAddInfo.spanY = span[1];
1850 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001851
Adam Cohened66b2b2012-01-23 17:28:51 -08001852 AppWidgetHostView hostView = info.boundWidget;
1853 int appWidgetId;
1854 if (hostView != null) {
1855 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001856 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001857 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001858 // In this case, we either need to start an activity to get permission to bind
1859 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001860 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001861 Bundle options = info.bindOptions;
1862
1863 boolean success = false;
1864 if (options != null) {
1865 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1866 info.componentName, options);
1867 } else {
1868 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1869 info.componentName);
1870 }
1871 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001872 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001873 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001874 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001875 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1876 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1877 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001878 // TODO: we need to make sure that this accounts for the options bundle.
1879 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001880 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1881 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001882 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001883 }
1884
Joe Onoratodeb98af2010-02-19 14:59:39 -08001885 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001886 // Handle case where user selected "Applications"
1887 String applicationName = getResources().getString(R.string.group_applications);
1888 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001889
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001890 if (applicationName != null && applicationName.equals(shortcutName)) {
1891 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1892 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001894 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1895 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001896 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001897 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001898 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001899 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902
Winson Chung24ab2f12010-09-16 14:10:47 -07001903 void processWallpaper(Intent intent) {
1904 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1905 }
1906
Adam Cohendcd297f2013-06-18 13:13:40 -07001907 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001908 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001909 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 folderInfo.title = getText(R.string.folder_name);
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001913 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001915 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916
1917 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 FolderIcon newFolder =
1919 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001920 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001922 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
Romain Guycbb89e42009-06-08 15:52:54 -07001924
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001926 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001930 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001932 Intent chooser = Intent.createChooser(pickWallpaper,
1933 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001934 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1935 // Removed in Eclair MR1
1936// WallpaperManager wm = (WallpaperManager)
1937// getSystemService(Context.WALLPAPER_SERVICE);
1938// WallpaperInfo wi = wm.getWallpaperInfo();
1939// if (wi != null && wi.getSettingsActivity() != null) {
1940// LabeledIntent li = new LabeledIntent(getPackageName(),
1941// R.string.configure_wallpaper, 0);
1942// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1943// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1944// }
Mike Clerona0618e42009-10-22 13:55:21 -07001945 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001948 /**
1949 * Registers various content observers. The current implementation registers
1950 * only a favorites observer to keep track of the favorites applications.
1951 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001952 private void registerContentObservers() {
1953 ContentResolver resolver = getContentResolver();
1954 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1955 true, mWidgetObserver);
1956 }
1957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 @Override
1959 public boolean dispatchKeyEvent(KeyEvent event) {
1960 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1961 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001963 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001964 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001965 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001966 dumpState();
1967 return true;
1968 }
1969 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001970 }
1971 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1972 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001973 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 return true;
1975 }
1976 }
1977
1978 return super.dispatchKeyEvent(event);
1979 }
1980
Joe Onorato88ec0992009-11-19 13:16:06 -08001981 @Override
1982 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001983 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001984 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001985 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001986 Folder openFolder = mWorkspace.getOpenFolder();
1987 if (openFolder.isEditingName()) {
1988 openFolder.dismissEditingName();
1989 } else {
1990 closeFolder();
1991 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001992 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001993 mWorkspace.exitWidgetResizeMode();
1994
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001995 // Back button is a no-op here, but give at least some feedback for the button press
1996 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001997 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002001 * Re-listen when widgets are reset.
2002 */
2003 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002004 if (mAppWidgetHost != null) {
2005 mAppWidgetHost.startListening();
2006 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002007 }
2008
2009 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 * Launches the intent referred by the clicked shortcut.
2011 *
2012 * @param v The view representing the clicked shortcut.
2013 */
2014 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002015 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2016 // view has detached (it's possible for this to happen if the view is removed mid touch).
2017 if (v.getWindowToken() == null) {
2018 return;
2019 }
2020
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002021 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002022 return;
2023 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002026 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002028 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002029
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002030 // Check for special shortcuts
2031 if (intent.getComponent() != null) {
2032 final String shortcutClass = intent.getComponent().getClassName();
2033
2034 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2035 showAllApps(true);
2036 return;
2037 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2038 MemoryDumpActivity.startDump(this);
2039 return;
2040 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002041 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002042
2043 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002044 int[] pos = new int[2];
2045 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002046 intent.setSourceBounds(new Rect(pos[0], pos[1],
2047 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002048
2049 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002050
2051 if (success && v instanceof BubbleTextView) {
2052 mWaitingForResume = (BubbleTextView) v;
2053 mWaitingForResume.setStayPressed(true);
2054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002056 if (v instanceof FolderIcon) {
2057 FolderIcon fi = (FolderIcon) v;
2058 handleFolderClick(fi);
2059 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002060 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002061 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002062 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002063 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002064 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067 }
2068
Michael Jurka0e260592010-06-30 17:07:39 -07002069 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002070 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002071 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002072 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2073 showWorkspace(true);
2074 }
Michael Jurka0e260592010-06-30 17:07:39 -07002075 return false;
2076 }
2077
Michael Jurkaaf442092010-06-10 17:01:57 -07002078 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002079 * Event handler for the search button
2080 *
2081 * @param v The view that was clicked.
2082 */
2083 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002084 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2085
Amith Yamasania135ba82011-08-09 17:42:01 -07002086 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002087 }
2088
2089 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002090 * Event handler for the voice button
2091 *
2092 * @param v The view that was clicked.
2093 */
2094 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002095 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002096
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 startVoice();
2098 }
2099
2100 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002101 try {
2102 final SearchManager searchManager =
2103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName activityName = searchManager.getGlobalSearchActivity();
2105 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002106 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002107 if (activityName != null) {
2108 intent.setPackage(activityName.getPackageName());
2109 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002110 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002111 } catch (ActivityNotFoundException e) {
2112 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002113 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002114 startActivitySafely(null, intent, "onClickVoiceButton");
2115 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002116 }
2117
2118 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002119 * Event handler for the "grid" button that appears on the home screen, which
2120 * enters all apps mode.
2121 *
2122 * @param v The view that was clicked.
2123 */
2124 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002125 showAllApps(true);
2126 }
2127
2128 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002129 // Provide the same haptic feedback that the system offers for virtual keys.
2130 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002131 }
2132
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002133 public void onClickAppMarketButton(View v) {
2134 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002135 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002136 } else {
2137 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002138 }
2139 }
2140
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002141 void startApplicationDetailsActivity(ComponentName componentName) {
2142 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002143 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2144 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002145 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002146 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002147 }
2148
Patrick Dubroy5539af72010-09-07 15:22:01 -07002149 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2150 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2151 // System applications cannot be installed. For now, show a toast explaining that.
2152 // We may give them the option of disabling apps this way.
2153 int messageId = R.string.uninstall_system_app_text;
2154 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2155 } else {
2156 String packageName = appInfo.componentName.getPackageName();
2157 String className = appInfo.componentName.getClassName();
2158 Intent intent = new Intent(
2159 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002160 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2161 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002162 startActivity(intent);
2163 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002164 }
2165
Michael Jurka86a720e2012-05-09 11:23:23 -07002166 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002170 // Only launch using the new animation if the shortcut has not opted out (this is a
2171 // private contract between launcher and may be ignored in the future).
2172 boolean useLaunchAnimation = (v != null) &&
2173 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2174 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002175 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2176 v.getMeasuredWidth(), v.getMeasuredHeight());
2177
2178 startActivity(intent, opts.toBundle());
2179 } else {
2180 startActivity(intent);
2181 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002182 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 } catch (SecurityException e) {
2184 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002185 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002187 "or use the exported attribute for this activity. "
2188 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002190 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002192
Michael Jurka86a720e2012-05-09 11:23:23 -07002193 boolean startActivitySafely(View v, Intent intent, Object tag) {
2194 boolean success = false;
2195 try {
2196 success = startActivity(v, intent, tag);
2197 } catch (ActivityNotFoundException e) {
2198 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2199 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2200 }
2201 return success;
2202 }
2203
Romain Guy8e633c52010-03-04 12:51:36 -08002204 void startActivityForResultSafely(Intent intent, int requestCode) {
2205 try {
2206 startActivityForResult(intent, requestCode);
2207 } catch (ActivityNotFoundException e) {
2208 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2209 } catch (SecurityException e) {
2210 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2211 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2212 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2213 "or use the exported attribute for this activity.", e);
2214 }
2215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216
Adam Cohena9cf38f2011-05-02 15:36:58 -07002217 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002218 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002219 Folder openFolder = mWorkspace.getFolderForTag(info);
2220
2221 // If the folder info reports that the associated folder is open, then verify that
2222 // it is actually opened. There have been a few instances where this gets out of sync.
2223 if (info.opened && openFolder == null) {
2224 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002226 info.opened = false;
2227 }
2228
Adam Cohenfb91f302012-06-11 15:45:18 -07002229 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 // Close any open folder
2231 closeFolder();
2232 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002233 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 } else {
2235 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 int folderScreen;
2237 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002238 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // .. and close it
2240 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002241 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 // Close any folder open on the current screen
2243 closeFolder();
2244 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002245 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
2247 }
2248 }
2249 }
2250
Adam Cohen268c4752012-06-06 17:47:33 -07002251 /**
2252 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2253 * in the DragLayer in the exact absolute location of the original FolderIcon.
2254 */
2255 private void copyFolderIconToImage(FolderIcon fi) {
2256 final int width = fi.getMeasuredWidth();
2257 final int height = fi.getMeasuredHeight();
2258
2259 // Lazy load ImageView, Bitmap and Canvas
2260 if (mFolderIconImageView == null) {
2261 mFolderIconImageView = new ImageView(this);
2262 }
2263 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2264 mFolderIconBitmap.getHeight() != height) {
2265 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2266 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2267 }
2268
2269 DragLayer.LayoutParams lp;
2270 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2271 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2272 } else {
2273 lp = new DragLayer.LayoutParams(width, height);
2274 }
2275
Adam Cohen307fe232012-08-16 17:55:58 -07002276 // The layout from which the folder is being opened may be scaled, adjust the starting
2277 // view size by this scale factor.
2278 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002279 lp.customPosition = true;
2280 lp.x = mRectForFolderAnimation.left;
2281 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002282 lp.width = (int) (scale * width);
2283 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002284
2285 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2286 fi.draw(mFolderIconCanvas);
2287 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002288 if (fi.getFolder() != null) {
2289 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2290 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002291 }
Adam Cohen268c4752012-06-06 17:47:33 -07002292 // Just in case this image view is still in the drag layer from a previous animation,
2293 // we remove it and re-add it.
2294 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2295 mDragLayer.removeView(mFolderIconImageView);
2296 }
2297 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002298 if (fi.getFolder() != null) {
2299 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002300 }
Adam Cohen268c4752012-06-06 17:47:33 -07002301 }
2302
Adam Cohen2801caf2011-05-13 20:57:39 -07002303 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002304 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002305 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2306 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2307 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2308
Adam Cohenc51934b2011-07-26 21:07:43 -07002309 FolderInfo info = (FolderInfo) fi.getTag();
2310 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2311 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002312 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2313 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002314 }
2315
Adam Cohen268c4752012-06-06 17:47:33 -07002316 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2317 copyFolderIconToImage(fi);
2318 fi.setVisibility(View.INVISIBLE);
2319
Michael Jurka2ecf9952012-06-18 12:52:28 -07002320 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002321 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002322 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2323 oa.start();
2324 }
2325
Adam Cohen268c4752012-06-06 17:47:33 -07002326 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002327 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002328 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2329 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2330 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2331
Adam Cohen268c4752012-06-06 17:47:33 -07002332 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002333
Adam Cohen268c4752012-06-06 17:47:33 -07002334 // We remove and re-draw the FolderIcon in-case it has changed
2335 mDragLayer.removeView(mFolderIconImageView);
2336 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002337 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002338 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002339 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002340 oa.addListener(new AnimatorListenerAdapter() {
2341 @Override
2342 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002343 if (cl != null) {
2344 cl.clearFolderLeaveBehind();
2345 // Remove the ImageView copy of the FolderIcon and make the original visible.
2346 mDragLayer.removeView(mFolderIconImageView);
2347 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002348 }
2349 }
2350 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002351 oa.start();
2352 }
2353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002355 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 * is animated relative to the specified View. If the View is null, no animation
2357 * is played.
2358 *
2359 * @param folderInfo The FolderInfo describing the folder to open.
2360 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002361 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002362 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002363 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364
Adam Cohena9cf38f2011-05-02 15:36:58 -07002365 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366
Adam Cohen4554ee12011-08-03 16:13:21 -07002367 // Just verify that the folder hasn't already been added to the DragLayer.
2368 // There was a one-off crash where the folder had a parent already.
2369 if (folder.getParent() == null) {
2370 mDragLayer.addView(folder);
2371 mDragController.addDropTarget((DropTarget) folder);
2372 } else {
2373 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2374 folder.getParent() + ").");
2375 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002376 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002377 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002378
2379 // Notify the accessibility manager that this folder "window" has appeared and occluded
2380 // the workspace items
2381 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2382 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002383 }
2384
2385 public void closeFolder() {
2386 Folder folder = mWorkspace.getOpenFolder();
2387 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002388 if (folder.isEditingName()) {
2389 folder.dismissEditingName();
2390 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002391 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002392
2393 // Dismiss the folder cling
2394 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002395 }
2396 }
2397
2398 void closeFolder(Folder folder) {
2399 folder.getInfo().opened = false;
2400
2401 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2402 if (parent != null) {
2403 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2404 shrinkAndFadeInFolderIcon(fi);
2405 }
2406 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002407
2408 // Notify the accessibility manager that this folder "window" has disappeard and no
2409 // longer occludeds the workspace items
2410 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 }
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002414 if (!isDraggingEnabled()) return false;
2415 if (isWorkspaceLocked()) return false;
2416 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002419 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
2421
Michael Jurka0280c3b2010-09-17 15:00:07 -07002422 resetAddInfo();
2423 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002425 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 return true;
2427 }
2428
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 // The hotseat touch handling does not go through Workspace, and we always allow long press
2430 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002432 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2433 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002434 if (itemUnderLongClick == null) {
2435 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002436 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2437 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002438 // Disabling reordering until we sort out some issues.
2439 //mWorkspace.startReordering();
2440 // TODO: need to have a new way to set wallpaper or start reordering
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002441 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002443 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002445 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 }
2447 }
2448 }
2449 return true;
2450 }
2451
Winson Chung3d503fb2011-07-13 17:25:49 -07002452 boolean isHotseatLayout(View layout) {
2453 return mHotseat != null && layout != null &&
2454 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2455 }
2456 Hotseat getHotseat() {
2457 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002458 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002459 SearchDropTargetBar getSearchBar() {
2460 return mSearchDropTargetBar;
2461 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002462
Winson Chung3d503fb2011-07-13 17:25:49 -07002463 /**
2464 * Returns the CellLayout of the specified container at the specified screen.
2465 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002466 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002467 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2468 if (mHotseat != null) {
2469 return mHotseat.getLayout();
2470 } else {
2471 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002472 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002473 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002475 }
2476 }
2477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 Workspace getWorkspace() {
2479 return mWorkspace;
2480 }
2481
Daniel Sandler843e8602010-06-07 14:59:01 -04002482 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002483 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002484 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002485 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002486 }
2487
Craig Mautner360310b2012-10-26 15:13:08 -07002488 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002489 public boolean isAllAppsButtonRank(int rank) {
2490 return mHotseat.isAllAppsButtonRank(rank);
2491 }
2492
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002493 /**
2494 * Helper method for the cameraZoomIn/cameraZoomOut animations
2495 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 * @param scaleFactor The scale factor used for the zoom
2497 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002498 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002499 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002500 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002501 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002502
Winson Chung18f41f82012-05-09 13:28:10 -07002503 void disableWallpaperIfInAllApps() {
2504 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002505 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002506 if (mAppsCustomizeTabHost != null &&
2507 !mAppsCustomizeTabHost.isTransitioning()) {
2508 updateWallpaperVisibility(false);
2509 }
2510 }
2511 }
2512
Craig Mautner360310b2012-10-26 15:13:08 -07002513 private void setWorkspaceBackground(boolean workspace) {
2514 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002515 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002516 }
2517
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002518 void updateWallpaperVisibility(boolean visible) {
2519 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2520 int curflags = getWindow().getAttributes().flags
2521 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2522 if (wpflags != curflags) {
2523 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2524 }
Craig Mautner360310b2012-10-26 15:13:08 -07002525 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002526 }
2527
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002528 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2529 if (v instanceof LauncherTransitionable) {
2530 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2531 }
2532 }
2533
Michael Jurkabed61d22012-02-14 22:51:29 -08002534 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2535 if (v instanceof LauncherTransitionable) {
2536 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2537 }
Winson Chung70442722012-02-10 15:43:22 -08002538
2539 // Update the workspace transition step as well
2540 dispatchOnLauncherTransitionStep(v, 0f);
2541 }
2542
2543 private void dispatchOnLauncherTransitionStep(View v, float t) {
2544 if (v instanceof LauncherTransitionable) {
2545 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2546 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002547 }
2548
2549 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2550 if (v instanceof LauncherTransitionable) {
2551 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2552 }
Winson Chung70442722012-02-10 15:43:22 -08002553
2554 // Update the workspace transition step as well
2555 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002556 }
2557
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002558 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002559 * Things to test when changing the following seven functions.
2560 * - Home from workspace
2561 * - from center screen
2562 * - from other screens
2563 * - Home from all apps
2564 * - from center screen
2565 * - from other screens
2566 * - Back from all apps
2567 * - from center screen
2568 * - from other screens
2569 * - Launch app from workspace and quit
2570 * - with back
2571 * - with home
2572 * - Launch app from all apps and quit
2573 * - with back
2574 * - with home
2575 * - Go to a screen that's not the default, then all
2576 * apps, and launch and app, and go back
2577 * - with back
2578 * -with home
2579 * - On workspace, long press power and go back
2580 * - with back
2581 * - with home
2582 * - On all apps, long press power and go back
2583 * - with back
2584 * - with home
2585 * - On workspace, power off
2586 * - On all apps, power off
2587 * - Launch an app and turn off the screen while in that app
2588 * - Go back with home key
2589 * - Go back with back key TODO: make this not go to workspace
2590 * - From all apps
2591 * - From workspace
2592 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2593 * - From all apps
2594 * - From the center workspace
2595 * - From another workspace
2596 */
2597
2598 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002599 * Zoom the camera out from the workspace to reveal 'toView'.
2600 * Assumes that the view to show is anchored at either the very top or very bottom
2601 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002602 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002603 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002604 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002605 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002606 mStateAnimation.cancel();
2607 mStateAnimation = null;
2608 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002609 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002610
Winson Chungf0ea4d32011-06-06 14:27:16 -07002611 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2612 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2613 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002614 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002615 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002616 final int startDelay =
2617 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002618
Michael Jurkab3e22d92011-10-31 15:58:33 -07002619 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620
Michael Jurkad74c9842011-07-10 12:44:21 -07002621 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002622 Animator workspaceAnim =
2623 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002624
2625 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002626 toView.setScaleX(scale);
2627 toView.setScaleY(scale);
2628 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2629 scaleAnim.
2630 scaleX(1f).scaleY(1f).
2631 setDuration(duration).
2632 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002633
Winson Chungf0ea4d32011-06-06 14:27:16 -07002634 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002635 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002636 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002637 .ofFloat(toView, "alpha", 0f, 1f)
2638 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002639 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002640 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2641 @Override
2642 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002643 if (animation == null) {
2644 throw new RuntimeException("animation is null");
2645 }
Winson Chung70442722012-02-10 15:43:22 -08002646 float t = (Float) animation.getAnimatedValue();
2647 dispatchOnLauncherTransitionStep(fromView, t);
2648 dispatchOnLauncherTransitionStep(toView, t);
2649 }
2650 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002651
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002652 // toView should appear right at the end of the workspace shrink
2653 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002654 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002655 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002656 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002657
2658 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002659 boolean animationCancelled = false;
2660
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002661 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002662 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002663 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 // Prepare the position
2665 toView.setTranslationX(0.0f);
2666 toView.setTranslationY(0.0f);
2667 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002668 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002669 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002670 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002671 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002672 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2673 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002674
Daniel Sandlere4f98912013-06-25 15:13:26 -04002675 if (mWorkspace != null
2676 && !springLoaded
2677 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002678 // Hide the workspace scrollbar
2679 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002680 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002681 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002682 if (!animationCancelled) {
2683 updateWallpaperVisibility(false);
2684 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002685
2686 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002687 if (mSearchDropTargetBar != null) {
2688 mSearchDropTargetBar.hideSearchBar(false);
2689 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002690 }
2691
Adam Cohencff6af82011-09-13 14:51:53 -07002692 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002693 public void onAnimationCancel(Animator animation) {
2694 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002695 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002696 });
2697
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002698 if (workspaceAnim != null) {
2699 mStateAnimation.play(workspaceAnim);
2700 }
Michael Jurka1899a362011-11-03 13:50:45 -07002701
2702 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002703
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002704 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2705 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002706
2707 // If any of the objects being animated haven't been measured/laid out
2708 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002709 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002710 (mWorkspace.getMeasuredWidth() == 0) ||
2711 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002712 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002713 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002715 final AnimatorSet stateAnimation = mStateAnimation;
2716 final Runnable startAnimRunnable = new Runnable() {
2717 public void run() {
2718 // Check that mStateAnimation hasn't changed while
2719 // we waited for a layout/draw pass
2720 if (mStateAnimation != stateAnimation)
2721 return;
2722 setPivotsForZoom(toView, scale);
2723 dispatchOnLauncherTransitionStart(fromView, animated, false);
2724 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002725 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002726 }
2727 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002728 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002729 final ViewTreeObserver observer = toView.getViewTreeObserver();
2730 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2731 public void onGlobalLayout() {
2732 startAnimRunnable.run();
2733 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2734 }
2735 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002736 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002737 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002738 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002739 } else {
2740 toView.setTranslationX(0.0f);
2741 toView.setTranslationY(0.0f);
2742 toView.setScaleX(1.0f);
2743 toView.setScaleY(1.0f);
2744 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002745 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002746
Daniel Sandlere4f98912013-06-25 15:13:26 -04002747 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002748 // Hide the workspace scrollbar
2749 mWorkspace.hideScrollingIndicator(true);
2750 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002751
2752 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002753 if (mSearchDropTargetBar != null) {
2754 mSearchDropTargetBar.hideSearchBar(false);
2755 }
Michael Jurkaabded662011-03-04 12:06:57 -08002756 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002757 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 dispatchOnLauncherTransitionStart(fromView, animated, false);
2759 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002760 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002761 dispatchOnLauncherTransitionStart(toView, animated, false);
2762 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002763 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002764 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 }
2766
2767 /**
2768 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002769 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002770 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002772 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2773 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002774
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002776 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777 mStateAnimation.cancel();
2778 mStateAnimation = null;
2779 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002781
Winson Chungf0ea4d32011-06-06 14:27:16 -07002782 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002783 final int fadeOutDuration =
2784 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002785 final float scaleFactor = (float)
2786 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2787 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002788 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002789 Animator workspaceAnim = null;
2790
2791 if (toState == State.WORKSPACE) {
2792 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2793 workspaceAnim = mWorkspace.getChangeStateAnimation(
2794 Workspace.State.NORMAL, animated, stagger);
2795 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2796 workspaceAnim = mWorkspace.getChangeStateAnimation(
2797 Workspace.State.SPRING_LOADED, animated);
2798 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002799
Michael Jurkab3e22d92011-10-31 15:58:33 -07002800 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002801 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002802 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002804 final LauncherViewPropertyAnimator scaleAnim =
2805 new LauncherViewPropertyAnimator(fromView);
2806 scaleAnim.
2807 scaleX(scaleFactor).scaleY(scaleFactor).
2808 setDuration(duration).
2809 setInterpolator(new Workspace.ZoomInInterpolator());
2810
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002811 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002812 .ofFloat(fromView, "alpha", 1f, 0f)
2813 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002814 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002815 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2816 @Override
2817 public void onAnimationUpdate(ValueAnimator animation) {
2818 float t = 1f - (Float) animation.getAnimatedValue();
2819 dispatchOnLauncherTransitionStep(fromView, t);
2820 dispatchOnLauncherTransitionStep(toView, t);
2821 }
2822 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002823
Michael Jurka2ecf9952012-06-18 12:52:28 -07002824 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002825
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002826 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2827 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002828 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002829
2830 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002831 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002832 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002833 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002835 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2836 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002837 if (mWorkspace != null) {
2838 mWorkspace.hideScrollingIndicator(false);
2839 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002840 if (onCompleteRunnable != null) {
2841 onCompleteRunnable.run();
2842 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002843 mAppsCustomizeContent.updateCurrentPageScroll();
2844 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 }
2846 });
2847
Winson Chungf0ea4d32011-06-06 14:27:16 -07002848 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002849 if (workspaceAnim != null) {
2850 mStateAnimation.play(workspaceAnim);
2851 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002852 dispatchOnLauncherTransitionStart(fromView, animated, true);
2853 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002854 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 } else {
2856 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 dispatchOnLauncherTransitionStart(fromView, animated, true);
2859 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002860 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002861 dispatchOnLauncherTransitionStart(toView, animated, true);
2862 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002863 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002864 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002865 }
2866
Michael Jurkae326f182011-11-21 14:05:46 -08002867 @Override
2868 public void onTrimMemory(int level) {
2869 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002870 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002871 mAppsCustomizeTabHost.onTrimMemory();
2872 }
2873 }
2874
Winson Chung18f41f82012-05-09 13:28:10 -07002875 @Override
2876 public void onWindowFocusChanged(boolean hasFocus) {
2877 if (!hasFocus) {
2878 // When another window occludes launcher (like the notification shade, or recents),
2879 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2880 updateWallpaperVisibility(true);
2881 } else {
2882 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002883 mWorkspace.postDelayed(new Runnable() {
2884 @Override
2885 public void run() {
2886 disableWallpaperIfInAllApps();
2887 }
2888 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002889 }
2890 }
2891
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002892 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002893 showWorkspace(animated, null);
2894 }
2895
2896 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002897 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002898 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002899 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002900 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002901
Winson Chungc7d2b602012-05-16 17:02:20 -07002902 // Show the search bar (only animate if we were showing the drop target bar in spring
2903 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002904 if (mSearchDropTargetBar != null) {
2905 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2906 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002907
Michael Jurkab737ee62011-11-15 15:57:22 -08002908 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002909 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002910
2911 // Set focus to the AppsCustomize button
2912 if (mAllAppsButton != null) {
2913 mAllAppsButton.requestFocus();
2914 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002915 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002916
Michael Jurkab737ee62011-11-15 15:57:22 -08002917 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002918
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002919 // Change the state *after* we've called all the transition code
2920 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002921
Winson Chung5fb63472011-02-02 17:03:37 -08002922 // Resume the auto-advance of widgets
2923 mUserPresent = true;
2924 updateRunning();
2925
alanv1d4fde62012-10-17 13:15:47 -07002926 // Send an accessibility event to announce the context change
2927 getWindow().getDecorView()
2928 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002929
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002930 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002931 }
2932
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002933 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002934 }
2935
Michael Jurkab3e22d92011-10-31 15:58:33 -07002936 void showAllApps(boolean animated) {
2937 if (mState != State.WORKSPACE) return;
2938
2939 showAppsCustomizeHelper(animated, false);
2940 mAppsCustomizeTabHost.requestFocus();
2941
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942 // Change the state *after* we've called all the transition code
2943 mState = State.APPS_CUSTOMIZE;
2944
2945 // Pause the auto-advance of widgets until we are out of AllApps
2946 mUserPresent = false;
2947 updateRunning();
2948 closeFolder();
2949
2950 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002951 getWindow().getDecorView()
2952 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002953 }
2954
Adam Cohen7777d962011-08-18 18:58:38 -07002955 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002956 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002957 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002958 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002959 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002960 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002961 }
Adam Cohen7777d962011-08-18 18:58:38 -07002962
Adam Cohened66b2b2012-01-23 17:28:51 -08002963 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2964 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002965 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2966
Winson Chunge7a03942011-08-05 15:05:12 -07002967 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002968 @Override
2969 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002970 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002971 // Before we show workspace, hide all apps again because
2972 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2973 // clean up our state transition functions
2974 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002975 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002976 } else {
2977 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002978 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002979 }
2980 }, (extendedDelay ?
2981 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2982 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2983 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984
Michael Jurkad3ef3062010-11-23 16:23:58 -08002985 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002986 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 final boolean animated = true;
2988 final boolean springLoaded = true;
2989 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002990 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002991 }
2992 // Otherwise, we are not in spring loaded mode, so don't do anything.
2993 }
2994
Michael Jurkab737ee62011-11-15 15:57:22 -08002995 void hideDockDivider() {
2996 if (mQsbDivider != null && mDockDivider != null) {
2997 mQsbDivider.setVisibility(View.INVISIBLE);
2998 mDockDivider.setVisibility(View.INVISIBLE);
2999 }
3000 }
3001
3002 void showDockDivider(boolean animated) {
3003 if (mQsbDivider != null && mDockDivider != null) {
3004 mQsbDivider.setVisibility(View.VISIBLE);
3005 mDockDivider.setVisibility(View.VISIBLE);
3006 if (mDividerAnimator != null) {
3007 mDividerAnimator.cancel();
3008 mQsbDivider.setAlpha(1f);
3009 mDockDivider.setAlpha(1f);
3010 mDividerAnimator = null;
3011 }
3012 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003013 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3014 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3015 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003016 int duration = 0;
3017 if (mSearchDropTargetBar != null) {
3018 duration = mSearchDropTargetBar.getTransitionInDuration();
3019 }
3020 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003021 mDividerAnimator.start();
3022 }
3023 }
3024 }
3025
Michael Jurkab3e22d92011-10-31 15:58:33 -07003026 void lockAllApps() {
3027 // TODO
3028 }
3029
3030 void unlockAllApps() {
3031 // TODO
3032 }
3033
Winson Chungf0ea4d32011-06-06 14:27:16 -07003034 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003035 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003036 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003037 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003038 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003039 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003040 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003041 int duration = 0;
3042 if (mSearchDropTargetBar != null) {
3043 duration = mSearchDropTargetBar.getTransitionInDuration();
3044 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003045 mHotseat.animate().alpha(1f).setDuration(duration);
3046 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003047 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003048 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 }
3050 }
3051 }
3052
3053 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003054 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003056 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003057 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003058 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003059 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003060 int duration = 0;
3061 if (mSearchDropTargetBar != null) {
3062 duration = mSearchDropTargetBar.getTransitionOutDuration();
3063 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003064 mHotseat.animate().alpha(0f).setDuration(duration);
3065 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003066 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003067 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003068 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003069 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003070 }
3071
Patrick Dubroy5f445422011-02-18 14:35:21 -08003072 /**
3073 * Add an item from all apps or customize onto the given workspace screen.
3074 * If layout is null, add to the current screen.
3075 */
3076 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003077 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003078 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003079 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003080 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003081
Winson Chungdff8ebb2011-09-08 17:25:31 -07003082 /** Maps the current orientation to an index for referencing orientation correct global icons */
3083 private int getCurrentOrientationIndexForGlobalIcons() {
3084 // default - 0, landscape - 1
3085 switch (getResources().getConfiguration().orientation) {
3086 case Configuration.ORIENTATION_LANDSCAPE:
3087 return 1;
3088 default:
3089 return 0;
3090 }
3091 }
3092
Michael Jurkaae65ee32012-04-30 11:45:54 -07003093 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003094 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003095 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 // Look for the toolbar icon specified in the activity meta-data
3097 Bundle metaData = packageManager.getActivityInfo(
3098 activityName, PackageManager.GET_META_DATA).metaData;
3099 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003100 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003101 if (iconResId != 0) {
3102 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003103 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 }
3105 }
3106 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003107 // This can happen if the activity defines an invalid drawable
3108 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3109 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003110 } catch (Resources.NotFoundException nfe) {
3111 // This can happen if the activity defines an invalid drawable
3112 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3113 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003115 return null;
3116 }
3117
3118 // if successful in getting icon, return it; otherwise, set button to use default drawable
3119 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003120 int buttonId, ComponentName activityName, int fallbackDrawableId,
3121 String toolbarResourceName) {
3122 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003123 Resources r = getResources();
3124 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3125 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003126
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003127 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003128 // If we were unable to find the icon via the meta-data, use a generic one
3129 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003130 toolbarIcon = r.getDrawable(fallbackDrawableId);
3131 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 null;
3136 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003137 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003138 if (button != null) {
3139 button.setCompoundDrawables(toolbarIcon, null, null, null);
3140 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003141 return toolbarIcon.getConstantState();
3142 }
3143 }
3144
3145 // if successful in getting icon, return it; otherwise, set button to use default drawable
3146 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003147 int buttonId, ComponentName activityName, int fallbackDrawableId,
3148 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003149 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003150 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003151
Michael Jurka19e0fc52011-07-22 18:00:21 -07003152 if (button != null) {
3153 // If we were unable to find the icon via the meta-data, use a
3154 // generic one
3155 if (toolbarIcon == null) {
3156 button.setImageResource(fallbackDrawableId);
3157 } else {
3158 button.setImageDrawable(toolbarIcon);
3159 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003160 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003161
3162 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3163
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 }
3165
Winson Chung1b884092012-06-08 17:13:02 -07003166 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003167 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003168 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003169 }
3170
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003171 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003172 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003173 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003174 }
3175
Winson Chungbb185bd2011-11-21 12:31:42 -08003176 private void invalidatePressedFocusedStates(View container, View button) {
3177 if (container instanceof HolographicLinearLayout) {
3178 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3179 layout.invalidatePressedFocusedStates();
3180 } else if (button instanceof HolographicImageView) {
3181 HolographicImageView view = (HolographicImageView) button;
3182 view.invalidatePressedFocusedStates();
3183 }
3184 }
3185
Winson Chungc51db6a2011-10-05 11:44:49 -07003186 private boolean updateGlobalSearchIcon() {
3187 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003188 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003189 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003190 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003191
Winson Chung1cad91e2011-05-25 17:41:01 -07003192 final SearchManager searchManager =
3193 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3194 ComponentName activityName = searchManager.getGlobalSearchActivity();
3195 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003196 int coi = getCurrentOrientationIndexForGlobalIcons();
3197 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003198 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3199 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3200 if (sGlobalSearchIcon[coi] == null) {
3201 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3202 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3203 TOOLBAR_ICON_METADATA_NAME);
3204 }
3205
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3207 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003208 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003209 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003210 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003211 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003212 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3213 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3214 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003215 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003216 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003217 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003218 }
3219 }
3220
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003221 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003222 final View searchButtonContainer = findViewById(R.id.search_button_container);
3223 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003224 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003225 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003226 }
3227
Winson Chungc51db6a2011-10-05 11:44:49 -07003228 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003229 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003230 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003231
Winson Chungc51db6a2011-10-05 11:44:49 -07003232 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003233 final SearchManager searchManager =
3234 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3235 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3236
3237 ComponentName activityName = null;
3238 if (globalSearchActivity != null) {
3239 // Check if the global search activity handles voice search
3240 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3241 intent.setPackage(globalSearchActivity.getPackageName());
3242 activityName = intent.resolveActivity(getPackageManager());
3243 }
3244
3245 if (activityName == null) {
3246 // Fallback: check if an activity other than the global search activity
3247 // resolves this
3248 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3249 activityName = intent.resolveActivity(getPackageManager());
3250 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003251 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003252 int coi = getCurrentOrientationIndexForGlobalIcons();
3253 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003254 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3255 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3256 if (sVoiceSearchIcon[coi] == null) {
3257 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3258 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3259 TOOLBAR_ICON_METADATA_NAME);
3260 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003262 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003263 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003264 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003265 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003266 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003268 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003269 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003270 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003271 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003272 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003273
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003274 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003275 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3276 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003277 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003278 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003279 }
3280
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003281 public void setVoiceButtonProxyVisible(boolean visible) {
3282 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3283 if (voiceButtonProxy != null) {
3284 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3285 }
3286 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003287 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003288 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003289 */
3290 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003291 final View marketButton = findViewById(R.id.market_button);
3292 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3293 // Find the app market activity by resolving an intent.
3294 // (If multiple app markets are installed, it will return the ResolverActivity.)
3295 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003296 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003297 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003298 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003299 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003300 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3301 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003302 marketButton.setVisibility(View.VISIBLE);
3303 } else {
3304 // We should hide and disable the view so that we don't try and restore the visibility
3305 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3306 marketButton.setVisibility(View.GONE);
3307 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003308 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003309 }
3310
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003311 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003312 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3313 Resources r = getResources();
3314 Drawable marketIconDrawable = d.newDrawable(r);
3315 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3316 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3317 marketIconDrawable.setBounds(0, 0, w, h);
3318
3319 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003320 }
3321
Michael Jurkad7c28052012-04-27 15:43:36 -07003322 @Override
3323 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003324 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003325 final List<CharSequence> text = event.getText();
3326 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003327 // Populate event with a fake title based on the current state.
3328 if (mState == State.APPS_CUSTOMIZE) {
3329 text.add(getString(R.string.all_apps_button_label));
3330 } else {
3331 text.add(getString(R.string.all_apps_home_button_label));
3332 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003333 return result;
3334 }
3335
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003336 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003337 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003338 */
3339 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3340 @Override
3341 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003342 closeSystemDialogs();
3343 }
3344 }
3345
3346 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003347 * Receives notifications whenever the appwidgets are reset.
3348 */
3349 private class AppWidgetResetObserver extends ContentObserver {
3350 public AppWidgetResetObserver() {
3351 super(new Handler());
3352 }
3353
3354 @Override
3355 public void onChange(boolean selfChange) {
3356 onAppWidgetReset();
3357 }
3358 }
3359
3360 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003361 * If the activity is currently paused, signal that we need to run the passed Runnable
3362 * in onResume.
3363 *
3364 * This needs to be called from incoming places where resources might have been loaded
3365 * while we are paused. That is becaues the Configuration might be wrong
3366 * when we're not running, and if it comes back to what it was when we
3367 * were paused, we are not restarted.
3368 *
3369 * Implementation of the method from LauncherModel.Callbacks.
3370 *
3371 * @return true if we are currently paused. The caller might be able to
3372 * skip some work in that case since we will come back again.
3373 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003374 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003375 if (mPaused) {
3376 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003377 if (deletePreviousRunnables) {
3378 while (mOnResumeCallbacks.remove(run)) {
3379 }
3380 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003381 mOnResumeCallbacks.add(run);
3382 return true;
3383 } else {
3384 return false;
3385 }
3386 }
3387
Michael Jurkac402cd92013-05-20 15:49:32 +02003388 private boolean waitUntilResume(Runnable run) {
3389 return waitUntilResume(run, false);
3390 }
3391
Michael Jurka7607c2f2013-04-03 14:33:19 -07003392 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003393 * If the activity is currently paused, signal that we need to re-run the loader
3394 * in onResume.
3395 *
3396 * This needs to be called from incoming places where resources might have been loaded
3397 * while we are paused. That is becaues the Configuration might be wrong
3398 * when we're not running, and if it comes back to what it was when we
3399 * were paused, we are not restarted.
3400 *
3401 * Implementation of the method from LauncherModel.Callbacks.
3402 *
3403 * @return true if we are currently paused. The caller might be able to
3404 * skip some work in that case since we will come back again.
3405 */
3406 public boolean setLoadOnResume() {
3407 if (mPaused) {
3408 Log.i(TAG, "setLoadOnResume");
3409 mOnResumeNeedsLoad = true;
3410 return true;
3411 } else {
3412 return false;
3413 }
3414 }
3415
3416 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003418 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003420 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003421 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003422 } else {
3423 return SCREEN_COUNT / 2;
3424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003426
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 /**
3428 * Refreshes the shortcuts shown on the workspace.
3429 *
3430 * Implementation of the method from LauncherModel.Callbacks.
3431 */
3432 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003433 // If we're starting binding all over again, clear any bind calls we'd postponed in
3434 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3435 // from scratch again
3436 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003437
Michael Jurka7607c2f2013-04-03 14:33:19 -07003438 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003439 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003440 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003441 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 int count = workspace.getChildCount();
3443 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003444 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003445 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3446 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003448 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003449 if (mHotseat != null) {
3450 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003451 }
3452 }
3453
Adam Cohendcd297f2013-06-18 13:13:40 -07003454 @Override
3455 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3456 int count = orderedScreenIds.size();
3457 for (int i = 0; i < count; i++) {
3458 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3459 }
3460 mWorkspace.addExtraEmptyScreen();
3461 }
3462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003463 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 * Bind the items start-end from the list.
3465 *
3466 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003467 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003468 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3469 if (waitUntilResume(new Runnable() {
3470 public void run() {
3471 bindItems(shortcuts, start, end);
3472 }
3473 })) {
3474 return;
3475 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003476
Winson Chungf0c6ae02012-03-21 16:10:31 -07003477 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3478 Set<String> newApps = new HashSet<String>();
3479 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3480
3481 Workspace workspace = mWorkspace;
3482 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003484
3485 // Short circuit if we are loading dock items for a configuration which has no dock
3486 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3487 mHotseat == null) {
3488 continue;
3489 }
3490
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 switch (item.itemType) {
3492 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3493 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003494 ShortcutInfo info = (ShortcutInfo) item;
3495 String uri = info.intent.toUri(0).toString();
3496 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003497
3498 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3499 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003500 boolean animateIconUp = false;
3501 synchronized (newApps) {
3502 if (newApps.contains(uri)) {
3503 animateIconUp = newApps.remove(uri);
3504 }
3505 }
3506 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003507 // Prepare the view to be animated up
3508 shortcut.setAlpha(0f);
3509 shortcut.setScaleX(0f);
3510 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003511 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003512 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3513 mNewShortcutAnimateViews.add(shortcut);
3514 }
3515 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003517 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003518 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003519 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003520 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003521 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3522 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003525 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003526
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 }
3529
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 /**
3531 * Implementation of the method from LauncherModel.Callbacks.
3532 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003533 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3534 if (waitUntilResume(new Runnable() {
3535 public void run() {
3536 bindFolders(folders);
3537 }
3538 })) {
3539 return;
3540 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003541 sFolders.clear();
3542 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003543 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544
3545 /**
3546 * Add the views for a widget to the workspace.
3547 *
3548 * Implementation of the method from LauncherModel.Callbacks.
3549 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003550 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3551 if (waitUntilResume(new Runnable() {
3552 public void run() {
3553 bindAppWidget(item);
3554 }
3555 })) {
3556 return;
3557 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003558
Daniel Sandler843e8602010-06-07 14:59:01 -04003559 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3560 if (DEBUG_WIDGETS) {
3561 Log.d(TAG, "bindAppWidget: " + item);
3562 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 final Workspace workspace = mWorkspace;
3564
3565 final int appWidgetId = item.appWidgetId;
3566 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003567 if (DEBUG_WIDGETS) {
3568 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3569 }
3570
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003574 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575
Adam Cohendcd297f2013-06-18 13:13:40 -07003576 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003578 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3579
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 workspace.requestLayout();
3581
Daniel Sandler843e8602010-06-07 14:59:01 -04003582 if (DEBUG_WIDGETS) {
3583 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3584 + (SystemClock.uptimeMillis()-start) + "ms");
3585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 }
3587
Adam Cohen1462de32012-07-24 22:34:36 -07003588 public void onPageBoundSynchronously(int page) {
3589 mSynchronouslyBoundPages.add(page);
3590 }
3591
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 /**
3593 * Callback saying that there aren't any more items to bind.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
3596 */
Adam Cohene25af792013-06-06 23:08:25 -07003597 public void finishBindingItems(final boolean upgradePath) {
Winson Chungc9168342013-06-26 14:54:55 -07003598
Michael Jurka7607c2f2013-04-03 14:33:19 -07003599 if (waitUntilResume(new Runnable() {
3600 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003601 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003602 }
3603 })) {
3604 return;
3605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 if (mSavedState != null) {
3607 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003608 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610 mSavedState = null;
3611 }
3612
Adam Cohen1462de32012-07-24 22:34:36 -07003613 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003615 // If we received the result of any pending adds while the loader was running (e.g. the
3616 // widget configuration forced an orientation change), process them now.
3617 for (int i = 0; i < sPendingAddList.size(); i++) {
3618 completeAdd(sPendingAddList.get(i));
3619 }
3620 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621
Winson Chungc51db6a2011-10-05 11:44:49 -07003622 // Update the market app icon as necessary (the other icons will be managed in response to
3623 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003624 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003625
Winson Chungf0c6ae02012-03-21 16:10:31 -07003626 // Animate up any icons as necessary
3627 if (mVisible || mWorkspaceLoading) {
3628 Runnable newAppsRunnable = new Runnable() {
3629 @Override
3630 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003631 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003632 }
3633 };
Winson Chunga2413752012-04-03 14:22:34 -07003634
Adam Cohendcd297f2013-06-18 13:13:40 -07003635 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3636 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003637 if (canRunNewAppsAnimation()) {
3638 // If the user has not interacted recently, then either snap to the new page to show
3639 // the new-apps animation or just run them if they are to appear on the current page
3640 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003641 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003642 } else {
3643 runNewAppsAnimation(false);
3644 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003645 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003646 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003647 // are on another page (or just normally if they are added to the current page)
3648 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003649 }
3650 }
3651
3652 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003653 if (upgradePath) {
3654 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003655 mWorkspace.stripDuplicateApps();
3656 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003657 }
Adam Cohen99894d92013-06-14 11:22:59 -07003658
Adam Cohen66a01fd2013-06-11 12:48:00 -07003659 mWorkspace.post(new Runnable() {
3660 @Override
3661 public void run() {
3662 onFinishBindingItems();
3663 }
3664 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003665 }
3666
Winson Chunga2413752012-04-03 14:22:34 -07003667 private boolean canRunNewAppsAnimation() {
3668 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3669 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3670 }
3671
Winson Chungc9168342013-06-26 14:54:55 -07003672 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3673 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3674 PropertyValuesHolder.ofFloat("alpha", 1f),
3675 PropertyValuesHolder.ofFloat("scaleX", 1f),
3676 PropertyValuesHolder.ofFloat("scaleY", 1f));
3677 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3678 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3679 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3680 return bounceAnim;
3681 }
3682
Winson Chungf0c6ae02012-03-21 16:10:31 -07003683 /**
3684 * Runs a new animation that scales up icons that were added while Launcher was in the
3685 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003686 *
3687 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 */
Winson Chunga2413752012-04-03 14:22:34 -07003689 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003690 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003692
3693 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003694 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3695 @Override
3696 public int compare(View a, View b) {
3697 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3698 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3699 int cellCountX = LauncherModel.getCellCountX();
3700 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3701 }
3702 });
Winson Chunga2413752012-04-03 14:22:34 -07003703
3704 // Animate each of the views in place (or show them immediately if requested)
3705 if (immediate) {
3706 for (View v : mNewShortcutAnimateViews) {
3707 v.setAlpha(1f);
3708 v.setScaleX(1f);
3709 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003710 }
Winson Chunga2413752012-04-03 14:22:34 -07003711 } else {
3712 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3713 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003714 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003715 }
3716 anim.playTogether(bounceAnims);
3717 anim.addListener(new AnimatorListenerAdapter() {
3718 @Override
3719 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003720 if (mWorkspace != null) {
3721 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3722 }
Winson Chunga2413752012-04-03 14:22:34 -07003723 }
3724 });
3725 anim.start();
3726 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003727
3728 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003729 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003730 mNewShortcutAnimateViews.clear();
3731 new Thread("clearNewAppsThread") {
3732 public void run() {
3733 mSharedPrefs.edit()
3734 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3735 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3736 .commit();
3737 }
3738 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003739 }
3740
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003741 @Override
3742 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003743 boolean searchVisible = updateGlobalSearchIcon();
3744 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003745 if (mSearchDropTargetBar != null) {
3746 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3747 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003748 }
3749
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003750 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 * Add the icons for all apps.
3752 *
3753 * Implementation of the method from LauncherModel.Callbacks.
3754 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003755 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003756 Runnable setAllAppsRunnable = new Runnable() {
3757 public void run() {
3758 if (mAppsCustomizeContent != null) {
3759 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003760
3761 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3762 getHotseat().addAllAppsFolder(mIconCache, apps,
3763 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3764 mIntentsOnWorkspaceFromUpgradePath = null;
3765 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003766 }
3767 }
3768 };
3769
Michael Jurkac57b7a82011-08-09 22:02:20 -07003770 // Remove the progress bar entirely; we could also make it GONE
3771 // but better to remove it since we know it's not going to be used
3772 View progressBar = mAppsCustomizeTabHost.
3773 findViewById(R.id.apps_customize_progress_bar);
3774 if (progressBar != null) {
3775 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003776
3777 // We just post the call to setApps so the user sees the progress bar
3778 // disappear-- otherwise, it just looks like the progress bar froze
3779 // which doesn't look great
3780 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3781 } else {
3782 // If we did not initialize the spinner in onCreate, then we can directly set the
3783 // list of applications without waiting for any progress bars views to be hidden.
3784 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 }
3787
3788 /**
3789 * A package was installed.
3790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003793 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3794 if (waitUntilResume(new Runnable() {
3795 public void run() {
3796 bindAppsAdded(apps);
3797 }
3798 })) {
3799 return;
3800 }
3801
Winson Chungc9168342013-06-26 14:54:55 -07003802 final Launcher launcher = this;
3803 final Context context = this;
3804 final ArrayList<ApplicationInfo> appsCopy = new ArrayList<ApplicationInfo>();
3805 appsCopy.addAll(apps);
3806
3807 // Process newly added apps
3808 mWorkspace.post(new Runnable() {
3809 @Override
3810 public void run() {
3811 // Process newly added apps
3812 LauncherModel model = getModel();
3813 Iterator<ApplicationInfo> iter = appsCopy.iterator();
3814 ArrayList<View> animatedShortcuts = new ArrayList<View>();
3815
3816 while (iter.hasNext()) {
3817 ApplicationInfo a = iter.next();
3818 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
3819 a.title.toString(), a.intent);
3820 if (coords == null) {
3821 // If we can't find a valid position, then just add a new screen.
3822 // This takes time so we need to re-queue the add until the new
3823 // page is added.
Daniel Sandlere4f98912013-06-25 15:13:26 -04003824 long screenId = LauncherAppState.getInstance().getLauncherProvider()
3825 .generateNewScreenId();
Winson Chungc9168342013-06-26 14:54:55 -07003826 mWorkspace.insertNewWorkspaceScreen(screenId, false);
3827 model.updateWorkspaceScreenOrder(launcher, mWorkspace.getScreenOrder(),
3828 new Runnable() {
3829 @Override
3830 public void run() {
3831 bindAppsAdded(appsCopy);
3832 }
3833 });
3834 return;
3835 } else {
3836 final ShortcutInfo shortcutInfo = a.makeShortcut();
3837 final View shortcut = createShortcut(shortcutInfo);
3838 // Add the view to the screen
3839 mWorkspace.addInScreenFromBind(shortcut,
3840 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3841 coords.first, coords.second[0], coords.second[1], 1, 1);
3842 // Add the shortcut to the db
3843 model.addItemToDatabase(context, shortcutInfo,
3844 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3845 coords.first, coords.second[0], coords.second[1], false);
3846 // Animate the shortcut
3847 animatedShortcuts.add(shortcut);
3848 }
3849 iter.remove();
3850 }
3851
3852 // Animate all the applications up
3853 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3854 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3855 for (int i = 0; i < animatedShortcuts.size(); ++i) {
3856 View shortcut = animatedShortcuts.get(i);
3857 shortcut.setAlpha(0f);
3858 shortcut.setScaleX(0f);
3859 shortcut.setScaleY(0f);
3860 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3861 }
3862 anim.playTogether(bounceAnims);
3863 anim.start();
3864 }
3865 });
Winson Chungf0ea4d32011-06-06 14:27:16 -07003866
Winson Chung785d2eb2011-04-14 16:08:02 -07003867 if (mAppsCustomizeContent != null) {
3868 mAppsCustomizeContent.addApps(apps);
3869 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 }
3871
3872 /**
3873 * A package was updated.
3874 *
3875 * Implementation of the method from LauncherModel.Callbacks.
3876 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003877 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3878 if (waitUntilResume(new Runnable() {
3879 public void run() {
3880 bindAppsUpdated(apps);
3881 }
3882 })) {
3883 return;
3884 }
3885
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003886 if (mWorkspace != null) {
3887 mWorkspace.updateShortcuts(apps);
3888 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003889
Winson Chung785d2eb2011-04-14 16:08:02 -07003890 if (mAppsCustomizeContent != null) {
3891 mAppsCustomizeContent.updateApps(apps);
3892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003893 }
3894
3895 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003896 * A package was uninstalled. We take both the super set of packageNames
3897 * in addition to specific applications to remove, the reason being that
3898 * this can be called when a package is updated as well. In that scenario,
3899 * we only remove specific components from the workspace, where as
3900 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 *
3902 * Implementation of the method from LauncherModel.Callbacks.
3903 */
Winson Chung83892cc2013-05-01 16:53:33 -07003904 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3905 final ArrayList<ApplicationInfo> appInfos,
3906 final boolean matchPackageNamesOnly) {
3907 if (waitUntilResume(new Runnable() {
3908 public void run() {
3909 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3910 }
3911 })) {
3912 return;
3913 }
3914
3915 if (matchPackageNamesOnly) {
3916 mWorkspace.removeItemsByPackageName(packageNames);
3917 } else {
3918 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003919 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003920
Winson Chung785d2eb2011-04-14 16:08:02 -07003921 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003922 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003923 }
Winson Chunga1820962011-10-03 16:31:06 -07003924
3925 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003926 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 }
Joe Onoratobe386092009-11-17 17:32:16 -08003928
3929 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003930 * A number of packages were updated.
3931 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003932
3933 private ArrayList<Object> mWidgetsAndShortcuts;
3934 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003935 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003936 bindPackagesUpdated(mWidgetsAndShortcuts);
3937 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003938 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003939 };
3940
3941 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3942 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3943 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003944 return;
3945 }
3946
Winson Chung785d2eb2011-04-14 16:08:02 -07003947 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003948 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003949 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003950 }
3951
Winson Chung400438b2011-01-16 17:53:48 -08003952 private int mapConfigurationOriActivityInfoOri(int configOri) {
3953 final Display d = getWindowManager().getDefaultDisplay();
3954 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3955 switch (d.getRotation()) {
3956 case Surface.ROTATION_0:
3957 case Surface.ROTATION_180:
3958 // We are currently in the same basic orientation as the natural orientation
3959 naturalOri = configOri;
3960 break;
3961 case Surface.ROTATION_90:
3962 case Surface.ROTATION_270:
3963 // We are currently in the other basic orientation to the natural orientation
3964 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3965 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3966 break;
3967 }
3968
3969 int[] oriMap = {
3970 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3971 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3972 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3973 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3974 };
3975 // Since the map starts at portrait, we need to offset if this device's natural orientation
3976 // is landscape.
3977 int indexOffset = 0;
3978 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3979 indexOffset = 1;
3980 }
3981 return oriMap[(d.getRotation() + indexOffset) % 4];
3982 }
Adam Cohen446e9402011-09-15 18:21:21 -07003983
Winson Chung4b919f82012-05-01 10:44:08 -07003984 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003985 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003986 getResources().getBoolean(R.bool.allow_rotation);
3987 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003988 }
Winson Chung4b919f82012-05-01 10:44:08 -07003989 public void lockScreenOrientation() {
3990 if (isRotationEnabled()) {
3991 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3992 .getConfiguration().orientation));
3993 }
3994 }
3995 public void unlockScreenOrientation(boolean immediate) {
3996 if (isRotationEnabled()) {
3997 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003998 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003999 } else {
4000 mHandler.postDelayed(new Runnable() {
4001 public void run() {
4002 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4003 }
4004 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004005 }
Winson Chung4b919f82012-05-01 10:44:08 -07004006 }
Winson Chung400438b2011-01-16 17:53:48 -08004007 }
4008
Winson Chung82f55532011-08-09 14:14:23 -07004009 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004010 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004011 if (DISABLE_CLINGS) {
4012 return false;
4013 }
4014
Brett Chabot2a9e2282011-08-23 15:03:13 -07004015 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004016 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004017
Michael Jurkae233a8b2013-03-19 13:49:20 +01004018 // Restricted secondary users (child mode) will potentially have very few apps
4019 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004020// boolean supportsLimitedUsers =
4021// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4022// Account[] accounts = AccountManager.get(this).getAccounts();
4023// if (supportsLimitedUsers && accounts.length == 0) {
4024// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4025// Bundle restrictions = um.getUserRestrictions();
4026// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4027// return false;
4028// }
4029// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004030 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004031 }
Michael Jurka22143132012-10-04 17:42:38 +02004032
Winson Chung7d7541e2011-09-16 20:14:36 -07004033 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004034 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004035 if (cling != null) {
4036 cling.init(this, positionData);
4037 cling.setVisibility(View.VISIBLE);
4038 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4039 if (animate) {
4040 cling.buildLayer();
4041 cling.setAlpha(0f);
4042 cling.animate()
4043 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004044 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004045 .setDuration(SHOW_CLING_DURATION)
4046 .setStartDelay(delay)
4047 .start();
4048 } else {
4049 cling.setAlpha(1f);
4050 }
Michael Jurka22143132012-10-04 17:42:38 +02004051 cling.setFocusableInTouchMode(true);
4052 cling.post(new Runnable() {
4053 public void run() {
4054 cling.setFocusable(true);
4055 cling.requestFocus();
4056 }
4057 });
4058 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4059 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 }
4061 return cling;
4062 }
Michael Jurka22143132012-10-04 17:42:38 +02004063
Winson Chung7d7541e2011-09-16 20:14:36 -07004064 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004065 // To catch cases where siblings of top-level views are made invisible, just check whether
4066 // the cling is directly set to GONE before dismissing it.
4067 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004068 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004069 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004070 anim.addListener(new AnimatorListenerAdapter() {
4071 public void onAnimationEnd(Animator animation) {
4072 cling.setVisibility(View.GONE);
4073 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004074 // We should update the shared preferences on a background thread
4075 new Thread("dismissClingThread") {
4076 public void run() {
4077 SharedPreferences.Editor editor = mSharedPrefs.edit();
4078 editor.putBoolean(flag, true);
4079 editor.commit();
4080 }
4081 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004082 };
4083 });
4084 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004085 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004086 }
4087 }
Michael Jurka22143132012-10-04 17:42:38 +02004088
Winson Chung9d9d74f2011-09-19 11:49:12 -07004089 private void removeCling(int id) {
4090 final View cling = findViewById(id);
4091 if (cling != null) {
4092 final ViewGroup parent = (ViewGroup) cling.getParent();
4093 parent.post(new Runnable() {
4094 @Override
4095 public void run() {
4096 parent.removeView(cling);
4097 }
4098 });
Michael Jurka22143132012-10-04 17:42:38 +02004099 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004100 }
4101 }
Michael Jurka974c3862012-05-22 22:00:31 -07004102
4103 private boolean skipCustomClingIfNoAccounts() {
4104 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4105 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4106 if (customCling) {
4107 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004108 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004109 Account[] accounts = am.getAccountsByType("com.google");
4110 return accounts.length == 0;
4111 }
4112 return false;
4113 }
4114
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004116 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004117 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004118 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4119 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004120 // If we're not using the default workspace layout, replace workspace cling
4121 // with a custom workspace cling (usually specified in an overlay)
4122 // For now, only do this on tablets
4123 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004124 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004125 // Use a custom cling
4126 View cling = findViewById(R.id.workspace_cling);
4127 ViewGroup clingParent = (ViewGroup) cling.getParent();
4128 int clingIndex = clingParent.indexOfChild(cling);
4129 clingParent.removeViewAt(clingIndex);
4130 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4131 clingParent.addView(customCling, clingIndex);
4132 customCling.setId(R.id.workspace_cling);
4133 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004134 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004135 } else {
4136 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004137 }
4138 }
4139 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004140 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004141 if (isClingsEnabled() &&
4142 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004143 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004144 } else {
4145 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004146 }
4147 }
4148 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004149 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004150 if (isClingsEnabled() &&
4151 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4152 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004153 } else {
4154 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004155 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004156 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004157 }
4158 public boolean isFolderClingVisible() {
4159 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004160 if (cling != null) {
4161 return cling.getVisibility() == View.VISIBLE;
4162 }
4163 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004164 }
Winson Chung82f55532011-08-09 14:14:23 -07004165 public void dismissWorkspaceCling(View v) {
4166 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004167 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004168 }
4169 public void dismissAllAppsCling(View v) {
4170 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004171 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4172 }
4173 public void dismissFolderCling(View v) {
4174 Cling cling = (Cling) findViewById(R.id.folder_cling);
4175 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004176 }
4177
Winson Chung80baf5a2010-08-09 16:03:15 -07004178 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004179 * Prints out out state for debugging.
4180 */
4181 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004182 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004183 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004184 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4185 Log.d(TAG, "mRestoring=" + mRestoring);
4186 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4187 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004188 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004189 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004190
Winson Chung785d2eb2011-04-14 16:08:02 -07004191 if (mAppsCustomizeContent != null) {
4192 mAppsCustomizeContent.dumpState();
4193 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004194 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004195 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004196
4197 @Override
4198 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4199 super.dump(prefix, fd, writer, args);
4200 writer.println(" ");
4201 writer.println("Debug logs: ");
4202 for (int i = 0; i < sDumpLogs.size(); i++) {
4203 writer.println(" " + sDumpLogs.get(i));
4204 }
4205 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004206
4207 public static void dumpDebugLogsToConsole() {
4208 Log.d(TAG, "");
4209 Log.d(TAG, "*********************");
4210 Log.d(TAG, "Launcher debug logs: ");
4211 for (int i = 0; i < sDumpLogs.size(); i++) {
4212 Log.d(TAG, " " + sDumpLogs.get(i));
4213 }
4214 Log.d(TAG, "*********************");
4215 Log.d(TAG, "");
4216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004217}
Michael Jurka2763be32011-02-24 11:19:57 -08004218
Michael Jurkaabded662011-03-04 12:06:57 -08004219interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004220 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004221 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004222 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004223 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004224 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004225}