blob: d46deb2cb2ddc6d28918e2461c74614a27ec2078 [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;
Winson Chung64359a52013-07-08 17:17:08 -0700196 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700197
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800198 private final BroadcastReceiver mCloseSystemDialogsReceiver
199 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800200 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private LayoutInflater mInflater;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800205 private View mQsbDivider;
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
410 // For handling default keys
411 mDefaultKeySsb = new SpannableStringBuilder();
412 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800413
414 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
415 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800416
Adam Cohenf9426d52012-06-04 17:26:21 -0700417 updateGlobalIcons();
418
419 // On large interfaces, we want the screen to auto-rotate based on the current orientation
420 unlockScreenOrientation(true);
421 }
422
Winson Chung4a2afa32012-07-19 14:53:05 -0700423 protected void onUserLeaveHint() {
424 super.onUserLeaveHint();
425 sPausedFromUserAction = true;
426 }
427
Adam Cohenf9426d52012-06-04 17:26:21 -0700428 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700429 boolean searchVisible = false;
430 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800431 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700432 int coi = getCurrentOrientationIndexForGlobalIcons();
433 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
434 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700435 updateAppMarketIcon();
436 searchVisible = updateGlobalSearchIcon();
437 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700438 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700439 if (sGlobalSearchIcon[coi] != null) {
440 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700441 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700443 if (sVoiceSearchIcon[coi] != null) {
444 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700445 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sAppMarketIcon[coi] != null) {
448 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800449 }
Winson Chungadf0c182012-08-23 14:59:07 -0700450 if (mSearchDropTargetBar != null) {
451 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
Romain Guycbb89e42009-06-08 15:52:54 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456 if (sLocaleConfiguration == null) {
457 new AsyncTask<Void, Void, LocaleConfiguration>() {
458 @Override
459 protected LocaleConfiguration doInBackground(Void... unused) {
460 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
461 readConfiguration(Launcher.this, localeConfiguration);
462 return localeConfiguration;
463 }
464
465 @Override
466 protected void onPostExecute(LocaleConfiguration result) {
467 sLocaleConfiguration = result;
468 checkForLocaleChange(); // recursive, but now with a locale configuration
469 }
470 }.execute();
471 return;
472 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 final Configuration configuration = getResources().getConfiguration();
475
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700476 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 final String locale = configuration.locale.toString();
478
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final int mcc = configuration.mcc;
481
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final int mnc = configuration.mnc;
484
Romain Guy84f296c2009-11-04 15:00:44 -0800485 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486
Romain Guy84f296c2009-11-04 15:00:44 -0800487 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 sLocaleConfiguration.locale = locale;
489 sLocaleConfiguration.mcc = mcc;
490 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700491
Joe Onorato0589f0f2010-02-08 13:44:00 -0800492 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493
494 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
495 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700496 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497 public void run() {
498 writeConfiguration(Launcher.this, localeConfiguration);
499 }
500 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700501 }
502 }
503
504 private static class LocaleConfiguration {
505 public String locale;
506 public int mcc = -1;
507 public int mnc = -1;
508 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700509
Romain Guy98d01652009-06-30 16:21:04 -0700510 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
511 DataInputStream in = null;
512 try {
513 in = new DataInputStream(context.openFileInput(PREFERENCES));
514 configuration.locale = in.readUTF();
515 configuration.mcc = in.readInt();
516 configuration.mnc = in.readInt();
517 } catch (FileNotFoundException e) {
518 // Ignore
519 } catch (IOException e) {
520 // Ignore
521 } finally {
522 if (in != null) {
523 try {
524 in.close();
525 } catch (IOException e) {
526 // Ignore
527 }
528 }
529 }
530 }
531
532 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
533 DataOutputStream out = null;
534 try {
535 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
536 out.writeUTF(configuration.locale);
537 out.writeInt(configuration.mcc);
538 out.writeInt(configuration.mnc);
539 out.flush();
540 } catch (FileNotFoundException e) {
541 // Ignore
542 } catch (IOException e) {
543 //noinspection ResultOfMethodCallIgnored
544 context.getFileStreamPath(PREFERENCES).delete();
545 } finally {
546 if (out != null) {
547 try {
548 out.close();
549 } catch (IOException e) {
550 // Ignore
551 }
552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 }
554 }
555
Bjorn Bringertc459e522013-06-07 19:36:01 +0100556 public LayoutInflater getInflater() {
557 return mInflater;
558 }
559
Adam Cohen716b51e2011-06-30 12:09:54 -0700560 public DragLayer getDragLayer() {
561 return mDragLayer;
562 }
563
Winson Chung36a62fe2012-05-06 18:04:42 -0700564 boolean isDraggingEnabled() {
565 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
566 // that is subsequently removed from the workspace in startBinding().
567 return !mModel.isLoadingWorkspace();
568 }
569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 static int getScreen() {
571 synchronized (sLock) {
572 return sScreen;
573 }
574 }
575
576 static void setScreen(int screen) {
577 synchronized (sLock) {
578 sScreen = screen;
579 }
580 }
581
Winson Chung557d6ed2011-07-08 15:34:52 -0700582 /**
583 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
584 * a configuration step, this allows the proper animations to run after other transitions.
585 */
586 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700587 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 switch (args.requestCode) {
589 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700590 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700591 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 break;
593 case REQUEST_PICK_SHORTCUT:
594 processShortcut(args.intent);
595 break;
596 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700597 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700598 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700599 result = true;
600 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800601 case REQUEST_CREATE_APPWIDGET:
602 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700603 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700604 result = true;
605 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 case REQUEST_PICK_WALLPAPER:
607 // We just wanted the activity result here so we can clear mWaitingForResult
608 break;
609 }
Michael Jurka27614d22012-04-02 06:40:22 -0700610 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
611 // if you turned the screen off and then back while in All Apps, Launcher would not
612 // return to the workspace. Clearing mAddInfo.container here fixes this issue
613 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700614 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800615 }
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 protected void onActivityResult(
619 final int requestCode, final int resultCode, final Intent data) {
620 if (requestCode == REQUEST_BIND_APPWIDGET) {
621 int appWidgetId = data != null ?
622 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
623 if (resultCode == RESULT_CANCELED) {
624 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
625 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700626 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 }
628 return;
629 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700630 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800631 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
632 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700633 mWaitingForResult = false;
634
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 // We have special handling for widgets
636 if (isWidgetDrop) {
637 int appWidgetId = data != null ?
638 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700639 if (appWidgetId < 0) {
640 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
641 "widget configuration activity.");
642 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
643 } else {
644 completeTwoStageWidgetDrop(resultCode, appWidgetId);
645 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 return;
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 // The pattern used here is that a user PICKs a specific application,
650 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
653 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700654 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800655 final PendingAddArguments args = new PendingAddArguments();
656 args.requestCode = requestCode;
657 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700658 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700659 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700660 args.cellX = mPendingAddInfo.cellX;
661 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 if (isWorkspaceLocked()) {
663 sPendingAddList.add(args);
664 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700665 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
671 null);
672 }
673
674 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700675 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700676 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 Runnable onCompleteRunnable = null;
678 int animationType = 0;
679
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 if (resultCode == RESULT_OK) {
682 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
683 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 mPendingAddWidgetInfo);
685 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 onCompleteRunnable = new Runnable() {
687 @Override
688 public void run() {
689 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700690 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800691 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
692 null);
693 }
694 };
695 } else if (resultCode == RESULT_CANCELED) {
696 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
697 onCompleteRunnable = new Runnable() {
698 @Override
699 public void run() {
700 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
701 null);
702 }
703 };
704 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700705 if (mDragLayer.getAnimatedView() != null) {
706 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
707 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
708 animationType, boundWidget, true);
709 } else {
710 // The animated view may be null in the case of a rotation during widget configuration
711 onCompleteRunnable.run();
712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714
715 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700716 protected void onStop() {
717 super.onStop();
718 FirstFrameAnimatorHelper.setIsVisible(false);
719 }
720
721 @Override
722 protected void onStart() {
723 super.onStart();
724 FirstFrameAnimatorHelper.setIsVisible(true);
725 }
726
727 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200729 long startTime = 0;
730 if (DEBUG_RESUME_TIME) {
731 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400732 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700735
Winson Chung4a2afa32012-07-19 14:53:05 -0700736 // Restore the previous launcher state
737 if (mOnResumeState == State.WORKSPACE) {
738 showWorkspace(false);
739 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
740 showAllApps(false);
741 }
742 mOnResumeState = State.NONE;
743
Craig Mautner360310b2012-10-26 15:13:08 -0700744 // Background was set to gradient in onPause(), restore to black if in all apps.
745 setWorkspaceBackground(mState == State.WORKSPACE);
746
Winson Chungde0fb8f2012-05-08 14:37:08 -0700747 // Process any items that were added while Launcher was away
748 InstallShortcutReceiver.flushInstallQueue(this);
749
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800750 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700751 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700752 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700754 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400755 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700756 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200758 if (mOnResumeCallbacks.size() > 0) {
759 // We might have postponed some bind calls until onResume (see waitUntilResume) --
760 // execute them here
761 long startTimeCallbacks = 0;
762 if (DEBUG_RESUME_TIME) {
763 startTimeCallbacks = System.currentTimeMillis();
764 }
765
766 if (mAppsCustomizeContent != null) {
767 mAppsCustomizeContent.setBulkBind(true);
768 }
769 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
770 mOnResumeCallbacks.get(i).run();
771 }
772 if (mAppsCustomizeContent != null) {
773 mAppsCustomizeContent.setBulkBind(false);
774 }
775 mOnResumeCallbacks.clear();
776 if (DEBUG_RESUME_TIME) {
777 Log.d(TAG, "Time spent processing callbacks in onResume: " +
778 (System.currentTimeMillis() - startTimeCallbacks));
779 }
Michael Jurka447bf842013-05-15 14:52:15 +0200780 }
Winson Chunge4e50662012-01-23 14:45:13 -0800781
782 // Reset the pressed state of icons that were locked in the press state while activities
783 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800784 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800785 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800786 mWaitingForResume.setStayPressed(false);
787 }
Winson Chunge4e50662012-01-23 14:45:13 -0800788 if (mAppsCustomizeContent != null) {
789 // Resets the previous all apps icon press state
790 mAppsCustomizeContent.resetDrawableState();
791 }
Adam Cohen06dff352012-06-01 17:17:08 -0700792 // It is possible that widgets can receive updates while launcher is not in the foreground.
793 // Consequently, the widgets will be inflated in the orientation of the foreground activity
794 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
795 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700796 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700797
798 // Again, as with the above scenario, it's possible that one or more of the global icons
799 // were updated in the wrong orientation.
800 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200801 if (DEBUG_RESUME_TIME) {
802 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
803 }
Adam Cohen06dff352012-06-01 17:17:08 -0700804 }
805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700807 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700808 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
809 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
810 // when Launcher resumes and we are still in AllApps.
811 updateWallpaperVisibility(true);
812
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700813 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700814 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800815 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700816 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700817 }
Romain Guycbb89e42009-06-08 15:52:54 -0700818
Adam Cohen66a01fd2013-06-11 12:48:00 -0700819 protected void onFinishBindingItems() {
820 }
821
822 // Add a fullscreen unpadded view to the workspace to the left all other screens.
823 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700824 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700825 }
826
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700827 @Override
828 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400829 // Flag the loader to stop early before switching
830 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700831 if (mAppsCustomizeContent != null) {
832 mAppsCustomizeContent.surrender();
833 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800834 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700835 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700836
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800837 // We can't hide the IME if it was forced open. So don't bother
838 /*
839 @Override
840 public void onWindowFocusChanged(boolean hasFocus) {
841 super.onWindowFocusChanged(hasFocus);
842
843 if (hasFocus) {
844 final InputMethodManager inputManager = (InputMethodManager)
845 getSystemService(Context.INPUT_METHOD_SERVICE);
846 WindowManager.LayoutParams lp = getWindow().getAttributes();
847 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
848 android.os.Handler()) {
849 protected void onReceiveResult(int resultCode, Bundle resultData) {
850 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
851 }
852 });
853 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
854 }
855 }
856 */
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 private boolean acceptFilter() {
859 final InputMethodManager inputManager = (InputMethodManager)
860 getSystemService(Context.INPUT_METHOD_SERVICE);
861 return !inputManager.isFullscreenMode();
862 }
863
864 @Override
865 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700866 final int uniChar = event.getUnicodeChar();
867 final boolean handled = super.onKeyDown(keyCode, event);
868 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
869 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
871 keyCode, event);
872 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700873 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700874 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700875 // showSearchDialog()
876 // If there are multiple keystrokes before the search dialog takes focus,
877 // onSearchRequested() will be called for every keystroke,
878 // but it is idempotent, so it's fine.
879 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 }
881 }
882
Joe Onorato8a9625e2010-01-28 15:55:35 -0800883 // Eat the long press event so the keyboard doesn't come up.
884 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
885 return true;
886 }
887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 return handled;
889 }
890
Karl Rosaen138a0412009-04-23 19:00:21 -0700891 private String getTypedText() {
892 return mDefaultKeySsb.toString();
893 }
894
895 private void clearTypedText() {
896 mDefaultKeySsb.clear();
897 mDefaultKeySsb.clearSpans();
898 Selection.setSelection(mDefaultKeySsb, 0);
899 }
900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700902 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
903 * State
904 */
905 private static State intToState(int stateOrdinal) {
906 State state = State.WORKSPACE;
907 final State[] stateValues = State.values();
908 for (int i = 0; i < stateValues.length; i++) {
909 if (stateValues[i].ordinal() == stateOrdinal) {
910 state = stateValues[i];
911 break;
912 }
913 }
914 return state;
915 }
916
917 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 * Restores the previous state, if it exists.
919 *
920 * @param savedState The previous state.
921 */
922 private void restoreState(Bundle savedState) {
923 if (savedState == null) {
924 return;
925 }
926
Michael Jurka883f55b2010-10-21 15:47:14 -0700927 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700928 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700929 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800930 }
931
Winson Chungf0c6ae02012-03-21 16:10:31 -0700932 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700934 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
Winson Chung3d503fb2011-07-13 17:25:49 -0700937 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700938 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700939
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
941 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700942 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
944 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
946 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
947 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700948 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 mRestoring = true;
950 }
951
952 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
953 if (renameFolder) {
954 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700955 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 mRestoring = true;
957 }
Winson Chunga12a2502010-12-20 14:41:35 -0800958
Winson Chung785d2eb2011-04-14 16:08:02 -0700959 // Restore the AppsCustomize tab
960 if (mAppsCustomizeTabHost != null) {
961 String curTab = savedState.getString("apps_customize_currentTab");
962 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700963 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700964 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700965 mAppsCustomizeContent.loadAssociatedPages(
966 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700967 }
968
Winson Chung5afbf7b2011-07-25 11:53:08 -0700969 int currentIndex = savedState.getInt("apps_customize_currentIndex");
970 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 }
973
974 /**
975 * Finds all the views we need and configure them properly.
976 */
977 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700978 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400979
Craig Mautner360310b2012-10-26 15:13:08 -0700980 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700981 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
982 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700983 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -0700984
985 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
986 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987
Winson Chung4c98d922011-05-31 16:50:48 -0700988 // Setup the drag layer
989 mDragLayer.setup(this, dragController);
990
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 // Setup the hotseat
992 mHotseat = (Hotseat) findViewById(R.id.hotseat);
993 if (mHotseat != null) {
994 mHotseat.setup(this);
995 }
996
Winson Chung4c98d922011-05-31 16:50:48 -0700997 // Setup the workspace
998 mWorkspace.setHapticFeedbackEnabled(false);
999 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001000 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001001 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001002
Winson Chungf0ea4d32011-06-06 14:27:16 -07001003 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001004 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001005
Winson Chungf0ea4d32011-06-06 14:27:16 -07001006 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001007 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001008 mAppsCustomizeContent = (AppsCustomizePagedView)
1009 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1010 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001011
Winson Chung3d503fb2011-07-13 17:25:49 -07001012 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001013 dragController.setDragScoller(mWorkspace);
1014 dragController.setScrollView(mDragLayer);
1015 dragController.setMoveTarget(mWorkspace);
1016 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001017 if (mSearchDropTargetBar != null) {
1018 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001019 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001020
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001021 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001022 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001023 final View ww = new WeightWatcher(this);
1024 ww.setAlpha(0.5f);
1025 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001026 new FrameLayout.LayoutParams(
1027 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001028 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001029 Gravity.BOTTOM)
1030 );
1031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 }
1033
1034 /**
1035 * Creates a view representing a shortcut.
1036 *
1037 * @param info The data structure describing the shortcut.
1038 *
1039 * @return A View inflated from R.layout.application.
1040 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001041 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001043 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 }
1045
1046 /**
1047 * Creates a view representing a shortcut inflated from the specified resource.
1048 *
1049 * @param layoutResId The id of the XML layout used to create the shortcut.
1050 * @param parent The group the shortcut belongs to.
1051 * @param info The data structure describing the shortcut.
1052 *
1053 * @return A View inflated from layoutResId.
1054 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001055 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001056 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1057 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 return favorite;
1060 }
1061
1062 /**
1063 * Add an application shortcut to the workspace.
1064 *
1065 * @param data The intent describing the application.
1066 * @param cellInfo The position on screen where to create the shortcut.
1067 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001068 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001069 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001070 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071
Adam Cohenfbba09b2011-07-18 21:43:05 -07001072 // First we check if we already know the exact location where we want to add this item.
1073 if (cellX >= 0 && cellY >= 0) {
1074 cellXY[0] = cellX;
1075 cellXY[1] = cellY;
1076 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001077 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 return;
1079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001081 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001082
Romain Guy73b979d2009-06-09 12:57:21 -07001083 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001084 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001088 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001089 } else {
1090 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 }
1092 }
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 /**
1095 * Add a shortcut to the workspace.
1096 *
1097 * @param data The intent describing the shortcut.
1098 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001100 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001101 int cellY) {
1102 int[] cellXY = mTmpAddItemCellCoordinates;
1103 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Michael Jurkad3ef3062010-11-23 16:23:58 -08001106 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001107
Adam Cohen558baaf2011-08-15 15:22:57 -07001108 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001109 if (info == null) {
1110 return;
1111 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001112 final View view = createShortcut(info);
1113
Adam Cohenfbba09b2011-07-18 21:43:05 -07001114 // First we check if we already know the exact location where we want to add this item.
1115 if (cellX >= 0 && cellY >= 0) {
1116 cellXY[0] = cellX;
1117 cellXY[1] = cellY;
1118 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001119
1120 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001121 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001122 true, null,null)) {
1123 return;
1124 }
1125 DragObject dragObject = new DragObject();
1126 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001127 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1128 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001129 return;
1130 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001131 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001132 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001133 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001134 foundCellSpan = (result != null);
1135 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001136 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001137 }
1138
1139 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001140 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 return;
1142 }
1143
Adam Cohendcd297f2013-06-18 13:13:40 -07001144 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145
1146 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001147 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001148 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 }
1150 }
1151
Adam Cohen2f093b62012-04-30 18:59:53 -07001152 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1153 int minHeight) {
1154 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001155 // We want to account for the extra amount of padding that we are adding to the widget
1156 // to ensure that it gets the full amount of space that it has requested
1157 int requiredWidth = minWidth + padding.left + padding.right;
1158 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001159 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001160 }
1161
Adam Cohen2f093b62012-04-30 18:59:53 -07001162 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1163 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001164 }
1165
Adam Cohen2f093b62012-04-30 18:59:53 -07001166 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1167 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001168 }
1169
Adam Cohen2f093b62012-04-30 18:59:53 -07001170 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1171 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001172 }
1173
Adam Cohen2f093b62012-04-30 18:59:53 -07001174 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1175 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001176 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001182 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001186 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1187 if (appWidgetInfo == null) {
1188 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1189 }
Romain Guycbb89e42009-06-08 15:52:54 -07001190
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001192 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001193
Adam Cohen2f093b62012-04-30 18:59:53 -07001194 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1195 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001198 // We have saved the position to which the widget was dragged-- this really only matters
1199 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001200 int[] cellXY = mTmpAddItemCellCoordinates;
1201 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001202 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001203 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001204 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1205 cellXY[0] = mPendingAddInfo.cellX;
1206 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001207 spanXY[0] = mPendingAddInfo.spanX;
1208 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001209 foundCellSpan = true;
1210 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001213 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1214 spanXY[1], cellXY, finalSpan);
1215 spanXY[0] = finalSpan[0];
1216 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001217 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001220 }
1221
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001223 if (appWidgetId != -1) {
1224 // Deleting an app widget ID is a void call but writes to disk before returning
1225 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001226 new Thread("deleteAppWidgetId") {
1227 public void run() {
1228 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1229 }
1230 }.start();
1231 }
Winson Chung93eef082012-03-23 15:59:27 -07001232 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001233 return;
1234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001236 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001237 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1238 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 launcherInfo.spanX = spanXY[0];
1240 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001241 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1242 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001245 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
1247 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001248 if (hostView == null) {
1249 // Perform actual inflation because we're live
1250 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1251 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1252 } else {
1253 // The AppWidgetHostView has already been inflated and instantiated
1254 launcherInfo.hostView = hostView;
1255 }
Romain Guycbb89e42009-06-08 15:52:54 -07001256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001259 launcherInfo.notifyWidgetSizeChanged(this);
1260
Adam Cohendcd297f2013-06-18 13:13:40 -07001261 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001263
1264 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001266 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
Romain Guycbb89e42009-06-08 15:52:54 -07001268
Adam Cohended9f8d2010-11-03 13:25:16 -07001269 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1270 @Override
1271 public void onReceive(Context context, Intent intent) {
1272 final String action = intent.getAction();
1273 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1274 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001275 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001276 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001277
Winson Chungc100e8e2011-08-09 16:02:43 -07001278 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001279 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001280 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1281 mAppsCustomizeTabHost.reset();
1282 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001283 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1285 mUserPresent = true;
1286 updateRunning();
1287 }
1288 }
1289 };
1290
1291 @Override
1292 public void onAttachedToWindow() {
1293 super.onAttachedToWindow();
1294
1295 // Listen for broadcasts related to user-presence
1296 final IntentFilter filter = new IntentFilter();
1297 filter.addAction(Intent.ACTION_SCREEN_OFF);
1298 filter.addAction(Intent.ACTION_USER_PRESENT);
1299 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001300 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001301 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001302 mVisible = true;
1303 }
1304
1305 @Override
1306 public void onDetachedFromWindow() {
1307 super.onDetachedFromWindow();
1308 mVisible = false;
1309
Adam Cohend113e0c2010-11-11 10:48:05 -08001310 if (mAttached) {
1311 unregisterReceiver(mReceiver);
1312 mAttached = false;
1313 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 updateRunning();
1315 }
1316
1317 public void onWindowVisibilityChanged(int visibility) {
1318 mVisible = visibility == View.VISIBLE;
1319 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001320 // The following code used to be in onResume, but it turns out onResume is called when
1321 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1322 // is a more appropriate event to handle
1323 if (mVisible) {
1324 mAppsCustomizeTabHost.onWindowVisible();
1325 if (!mWorkspaceLoading) {
1326 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001327 // We want to let Launcher draw itself at least once before we force it to build
1328 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001329 // apps is nice and speedy.
1330 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001331 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001332 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001333 if (mStarted) return;
1334 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001335 // We delay the layer building a bit in order to give
1336 // other message processing a time to run. In particular
1337 // this avoids a delay in hiding the IME if it was
1338 // currently shown, because doing that may involve
1339 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001340 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001341 final ViewTreeObserver.OnDrawListener listener = this;
1342 mWorkspace.post(new Runnable() {
1343 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001344 if (mWorkspace != null &&
1345 mWorkspace.getViewTreeObserver() != null) {
1346 mWorkspace.getViewTreeObserver().
1347 removeOnDrawListener(listener);
1348 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001349 }
1350 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001351 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001352 }
1353 });
1354 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001355 // When Launcher comes back to foreground, a different Activity might be responsible for
1356 // the app market intent, so refresh the icon
1357 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001358 clearTypedText();
1359 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001360 }
1361
1362 private void sendAdvanceMessage(long delay) {
1363 mHandler.removeMessages(ADVANCE_MSG);
1364 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1365 mHandler.sendMessageDelayed(msg, delay);
1366 mAutoAdvanceSentTime = System.currentTimeMillis();
1367 }
1368
1369 private void updateRunning() {
1370 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1371 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1372 mAutoAdvanceRunning = autoAdvanceRunning;
1373 if (autoAdvanceRunning) {
1374 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1375 sendAdvanceMessage(delay);
1376 } else {
1377 if (!mWidgetsToAdvance.isEmpty()) {
1378 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1379 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1380 }
1381 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001382 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001383 }
1384 }
1385 }
1386
1387 private final Handler mHandler = new Handler() {
1388 @Override
1389 public void handleMessage(Message msg) {
1390 if (msg.what == ADVANCE_MSG) {
1391 int i = 0;
1392 for (View key: mWidgetsToAdvance.keySet()) {
1393 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1394 final int delay = mAdvanceStagger * i;
1395 if (v instanceof Advanceable) {
1396 postDelayed(new Runnable() {
1397 public void run() {
1398 ((Advanceable) v).advance();
1399 }
1400 }, delay);
1401 }
1402 i++;
1403 }
1404 sendAdvanceMessage(mAdvanceInterval);
1405 }
1406 }
1407 };
1408
1409 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001410 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001411 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1412 if (v instanceof Advanceable) {
1413 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001414 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001415 updateRunning();
1416 }
1417 }
1418
1419 void removeWidgetToAutoAdvance(View hostView) {
1420 if (mWidgetsToAdvance.containsKey(hostView)) {
1421 mWidgetsToAdvance.remove(hostView);
1422 updateRunning();
1423 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001424 }
1425
Joe Onorato9c1289c2009-08-17 11:03:03 -04001426 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001427 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001428 launcherInfo.hostView = null;
1429 }
1430
Winson Chung93eef082012-03-23 15:59:27 -07001431 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1432 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1433 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001434 }
1435
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001436 public LauncherAppWidgetHost getAppWidgetHost() {
1437 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
Romain Guycbb89e42009-06-08 15:52:54 -07001439
Winson Chunga9abd0e2010-10-27 17:18:37 -07001440 public LauncherModel getModel() {
1441 return mModel;
1442 }
1443
Bjorn Bringertc459e522013-06-07 19:36:01 +01001444 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001445 getWindow().closeAllPanels();
1446
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001447 // Whatever we were doing is hereby canceled.
1448 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001449 }
1450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 @Override
1452 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001453 long startTime = 0;
1454 if (DEBUG_RESUME_TIME) {
1455 startTime = System.currentTimeMillis();
1456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 super.onNewIntent(intent);
1458
1459 // Close the menu
1460 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001461 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001462 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001463
Jamie Genniscb222e82012-10-23 15:44:26 -07001464 final boolean alreadyOnHome =
1465 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001466 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001467
Jamie Genniscb222e82012-10-23 15:44:26 -07001468 Runnable processIntent = new Runnable() {
1469 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001470 if (mWorkspace == null) {
1471 // Can be cases where mWorkspace is null, this prevents a NPE
1472 return;
1473 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001474 Folder openFolder = mWorkspace.getOpenFolder();
1475 // In all these cases, only animate if we're already on home
1476 mWorkspace.exitWidgetResizeMode();
1477 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1478 openFolder == null) {
1479 mWorkspace.moveToDefaultScreen(true);
1480 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001481
Jamie Genniscb222e82012-10-23 15:44:26 -07001482 closeFolder();
1483 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001484
Jamie Genniscb222e82012-10-23 15:44:26 -07001485 // If we are already on home, then just animate back to the workspace,
1486 // otherwise, just wait until onResume to set the state back to Workspace
1487 if (alreadyOnHome) {
1488 showWorkspace(true);
1489 } else {
1490 mOnResumeState = State.WORKSPACE;
1491 }
1492
1493 final View v = getWindow().peekDecorView();
1494 if (v != null && v.getWindowToken() != null) {
1495 InputMethodManager imm = (InputMethodManager)getSystemService(
1496 INPUT_METHOD_SERVICE);
1497 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1498 }
1499
1500 // Reset AllApps to its initial state
1501 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1502 mAppsCustomizeTabHost.reset();
1503 }
1504 }
1505 };
1506
1507 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1508 // Delay processing of the intent to allow the status bar animation to finish
1509 // first in order to avoid janky animations.
1510 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001511 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001512 // Process the intent immediately.
1513 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001514 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
Michael Jurka447bf842013-05-15 14:52:15 +02001517 if (DEBUG_RESUME_TIME) {
1518 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521
1522 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001523 public void onRestoreInstanceState(Bundle state) {
1524 super.onRestoreInstanceState(state);
1525 for (int page: mSynchronouslyBoundPages) {
1526 mWorkspace.restoreInstanceStateForChild(page);
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
1530 @Override
1531 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001532 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001533 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
Michael Jurka883f55b2010-10-21 15:47:14 -07001535 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001536 // We close any open folder since it will not be re-opened, and we need to make sure
1537 // this state is reflected.
1538 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001541 mWaitingForResult) {
1542 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1545 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001546 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1547 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1548 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
1551 if (mFolderInfo != null && mWaitingForResult) {
1552 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1553 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1554 }
Michael Jurka946ad472010-07-09 18:05:18 -07001555
Winson Chung785d2eb2011-04-14 16:08:02 -07001556 // Save the current AppsCustomize tab
1557 if (mAppsCustomizeTabHost != null) {
1558 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1559 if (currentTabTag != null) {
1560 outState.putString("apps_customize_currentTab", currentTabTag);
1561 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001562 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1563 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
1567 @Override
1568 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001570
Winson Chunge7a03942011-08-05 15:05:12 -07001571 // Remove all pending runnables
1572 mHandler.removeMessages(ADVANCE_MSG);
1573 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001574 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001575
Winson Chungcd2b0142011-06-08 16:02:26 -07001576 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001577 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001578 mModel.stopLoader();
1579 app.setLauncher(null);
1580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001582 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001584 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001586 mAppWidgetHost = null;
1587
1588 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589
1590 TextKeyListener.getInstance().release();
1591
Winson Chung81b52252012-08-27 15:34:29 -07001592 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1593 // to prevent leaking Launcher activities on orientation change.
1594 if (mModel != null) {
1595 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1596 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001597
1598 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001599 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001600
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001601 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001602 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1603 mWorkspace.removeAllViews();
1604 mWorkspace = null;
1605 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001606
Michael Jurka2ecf9952012-06-18 12:52:28 -07001607 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
1609
Adam Cohena9cf38f2011-05-02 15:36:58 -07001610 public DragController getDragController() {
1611 return mDragController;
1612 }
1613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 @Override
1615 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001616 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 super.startActivityForResult(intent, requestCode);
1618 }
1619
Winson Chung70d72102011-08-12 11:24:35 -07001620 /**
1621 * Indicates that we want global search for this activity by setting the globalSearch
1622 * argument for {@link #startSearch} to true.
1623 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001625 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001627
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001628 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001629
Karl Rosaen138a0412009-04-23 19:00:21 -07001630 if (initialQuery == null) {
1631 // Use any text typed in the launcher as the initial query
1632 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 if (appSearchData == null) {
1635 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001636 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Winson Chungadf0c182012-08-23 14:59:07 -07001638 Rect sourceBounds = new Rect();
1639 if (mSearchDropTargetBar != null) {
1640 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1641 }
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Bjorn Bringertc459e522013-06-07 19:36:01 +01001643 startSearch(initialQuery, selectInitialQuery,
1644 appSearchData, sourceBounds);
1645 }
1646
1647 public void startSearch(String initialQuery,
1648 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001649 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001650 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001651 }
1652
1653 /**
1654 * Starts the global search activity. This code is a copied from SearchManager
1655 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001656 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001657 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001658 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001659 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1660 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1661 if (globalSearchActivity == null) {
1662 Log.w(TAG, "No global search activity found.");
1663 return;
1664 }
1665 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1666 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1667 intent.setComponent(globalSearchActivity);
1668 // Make sure that we have a Bundle to put source in
1669 if (appSearchData == null) {
1670 appSearchData = new Bundle();
1671 } else {
1672 appSearchData = new Bundle(appSearchData);
1673 }
1674 // Set source to package name of app that starts global search, if not set already.
1675 if (!appSearchData.containsKey("source")) {
1676 appSearchData.putString("source", getPackageName());
1677 }
1678 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1679 if (!TextUtils.isEmpty(initialQuery)) {
1680 intent.putExtra(SearchManager.QUERY, initialQuery);
1681 }
1682 if (selectInitialQuery) {
1683 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1684 }
1685 intent.setSourceBounds(sourceBounds);
1686 try {
1687 startActivity(intent);
1688 } catch (ActivityNotFoundException ex) {
1689 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
1692
Winson Chung70d72102011-08-12 11:24:35 -07001693 @Override
1694 public boolean onCreateOptionsMenu(Menu menu) {
1695 if (isWorkspaceLocked()) {
1696 return false;
1697 }
1698
1699 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001700
1701 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1702 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1703 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001704 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1705 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1706 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001707 String helpUrl = getString(R.string.help_url);
1708 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001709 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1710 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1711
Winson Chung70d72102011-08-12 11:24:35 -07001712 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1713 .setIcon(android.R.drawable.ic_menu_gallery)
1714 .setAlphabeticShortcut('W');
1715 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1716 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001717 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001718 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001719 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1720 .setIcon(android.R.drawable.ic_menu_preferences)
1721 .setIntent(settings)
1722 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001723 if (!helpUrl.isEmpty()) {
1724 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1725 .setIcon(android.R.drawable.ic_menu_help)
1726 .setIntent(help)
1727 .setAlphabeticShortcut('H');
1728 }
Winson Chung70d72102011-08-12 11:24:35 -07001729 return true;
1730 }
1731
1732 @Override
1733 public boolean onPrepareOptionsMenu(Menu menu) {
1734 super.onPrepareOptionsMenu(menu);
1735
1736 if (mAppsCustomizeTabHost.isTransitioning()) {
1737 return false;
1738 }
1739 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1740 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1741
1742 return true;
1743 }
1744
1745 @Override
1746 public boolean onOptionsItemSelected(MenuItem item) {
1747 switch (item.getItemId()) {
1748 case MENU_WALLPAPER_SETTINGS:
1749 startWallpaper();
1750 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001751 }
1752
1753 return super.onOptionsItemSelected(item);
1754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001756 @Override
1757 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001758 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001759 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001760 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001761 }
1762
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 public boolean isWorkspaceLocked() {
1764 return mWorkspaceLoading || mWaitingForResult;
1765 }
1766
Michael Jurka0280c3b2010-09-17 15:00:07 -07001767 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001768 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001769 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001770 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1771 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001772 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001775
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001776 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1777 AppWidgetProviderInfo appWidgetInfo) {
1778 if (appWidgetInfo.configure != null) {
1779 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001780
Bjorn Bringert7984c942009-12-09 15:38:25 +00001781 // Launch over to configure widget, if needed
1782 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001783 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001784 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001785 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001787 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001788 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001789 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001790 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001791 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
1793 }
Romain Guycbb89e42009-06-08 15:52:54 -07001794
Adam Cohenfbba09b2011-07-18 21:43:05 -07001795 /**
1796 * Process a shortcut drop.
1797 *
1798 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001799 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800 * @param cell The cell it should be added to, optional
1801 * @param position The location on the screen where it was dropped, optional
1802 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001803 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001804 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001806 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001807 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001808 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001809
1810 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001811 mPendingAddInfo.cellX = cell[0];
1812 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001813 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001814
1815 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1816 createShortcutIntent.setComponent(componentName);
1817 processShortcut(createShortcutIntent);
1818 }
1819
Adam Cohenfbba09b2011-07-18 21:43:05 -07001820 /**
1821 * Process a widget drop.
1822 *
1823 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001825 * @param cell The cell it should be added to, optional
1826 * @param position The location on the screen where it was dropped, optional
1827 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001828 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001829 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001830 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001833 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001834 mPendingAddInfo.minSpanX = info.minSpanX;
1835 mPendingAddInfo.minSpanY = info.minSpanY;
1836
Adam Cohenfbba09b2011-07-18 21:43:05 -07001837 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001838 mPendingAddInfo.cellX = cell[0];
1839 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001840 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001841 if (span != null) {
1842 mPendingAddInfo.spanX = span[0];
1843 mPendingAddInfo.spanY = span[1];
1844 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001845
Adam Cohened66b2b2012-01-23 17:28:51 -08001846 AppWidgetHostView hostView = info.boundWidget;
1847 int appWidgetId;
1848 if (hostView != null) {
1849 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001850 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001851 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001852 // In this case, we either need to start an activity to get permission to bind
1853 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001854 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001855 Bundle options = info.bindOptions;
1856
1857 boolean success = false;
1858 if (options != null) {
1859 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1860 info.componentName, options);
1861 } else {
1862 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1863 info.componentName);
1864 }
1865 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001866 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001867 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001868 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001869 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1870 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1871 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001872 // TODO: we need to make sure that this accounts for the options bundle.
1873 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001874 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1875 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001876 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001877 }
1878
Joe Onoratodeb98af2010-02-19 14:59:39 -08001879 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001880 // Handle case where user selected "Applications"
1881 String applicationName = getResources().getString(R.string.group_applications);
1882 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001883
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001884 if (applicationName != null && applicationName.equals(shortcutName)) {
1885 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1886 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001888 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1889 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001890 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001891 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001892 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001893 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
1896
Winson Chung24ab2f12010-09-16 14:10:47 -07001897 void processWallpaper(Intent intent) {
1898 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1899 }
1900
Adam Cohendcd297f2013-06-18 13:13:40 -07001901 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001902 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001903 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 folderInfo.title = getText(R.string.folder_name);
1905
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001907 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001908 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001909 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910
1911 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 FolderIcon newFolder =
1913 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001914 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001915 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001916 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
Romain Guycbb89e42009-06-08 15:52:54 -07001918
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001920 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 }
1922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001924 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001926 Intent chooser = Intent.createChooser(pickWallpaper,
1927 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001928 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1929 // Removed in Eclair MR1
1930// WallpaperManager wm = (WallpaperManager)
1931// getSystemService(Context.WALLPAPER_SERVICE);
1932// WallpaperInfo wi = wm.getWallpaperInfo();
1933// if (wi != null && wi.getSettingsActivity() != null) {
1934// LabeledIntent li = new LabeledIntent(getPackageName(),
1935// R.string.configure_wallpaper, 0);
1936// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1937// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1938// }
Mike Clerona0618e42009-10-22 13:55:21 -07001939 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001942 /**
1943 * Registers various content observers. The current implementation registers
1944 * only a favorites observer to keep track of the favorites applications.
1945 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001946 private void registerContentObservers() {
1947 ContentResolver resolver = getContentResolver();
1948 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1949 true, mWidgetObserver);
1950 }
1951
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 @Override
1953 public boolean dispatchKeyEvent(KeyEvent event) {
1954 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1955 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001957 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001958 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001959 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001960 dumpState();
1961 return true;
1962 }
1963 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001964 }
1965 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1966 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001967 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 return true;
1969 }
1970 }
1971
1972 return super.dispatchKeyEvent(event);
1973 }
1974
Joe Onorato88ec0992009-11-19 13:16:06 -08001975 @Override
1976 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001977 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001978 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001979 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001980 Folder openFolder = mWorkspace.getOpenFolder();
1981 if (openFolder.isEditingName()) {
1982 openFolder.dismissEditingName();
1983 } else {
1984 closeFolder();
1985 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001986 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001987 mWorkspace.exitWidgetResizeMode();
1988
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001989 // Back button is a no-op here, but give at least some feedback for the button press
1990 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001991 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001992 }
1993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001995 * Re-listen when widgets are reset.
1996 */
1997 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001998 if (mAppWidgetHost != null) {
1999 mAppWidgetHost.startListening();
2000 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002001 }
2002
2003 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 * Launches the intent referred by the clicked shortcut.
2005 *
2006 * @param v The view representing the clicked shortcut.
2007 */
2008 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002009 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2010 // view has detached (it's possible for this to happen if the view is removed mid touch).
2011 if (v.getWindowToken() == null) {
2012 return;
2013 }
2014
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002015 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002016 return;
2017 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002020 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002022 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002023
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002024 // Check for special shortcuts
2025 if (intent.getComponent() != null) {
2026 final String shortcutClass = intent.getComponent().getClassName();
2027
2028 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2029 showAllApps(true);
2030 return;
2031 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2032 MemoryDumpActivity.startDump(this);
2033 return;
2034 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002035 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002036
2037 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002038 int[] pos = new int[2];
2039 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002040 intent.setSourceBounds(new Rect(pos[0], pos[1],
2041 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002042
2043 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002044
2045 if (success && v instanceof BubbleTextView) {
2046 mWaitingForResume = (BubbleTextView) v;
2047 mWaitingForResume.setStayPressed(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002050 if (v instanceof FolderIcon) {
2051 FolderIcon fi = (FolderIcon) v;
2052 handleFolderClick(fi);
2053 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002054 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002055 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002056 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002057 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002058 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061 }
2062
Michael Jurka0e260592010-06-30 17:07:39 -07002063 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002064 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002065 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002066 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2067 showWorkspace(true);
2068 }
Michael Jurka0e260592010-06-30 17:07:39 -07002069 return false;
2070 }
2071
Michael Jurkaaf442092010-06-10 17:01:57 -07002072 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002073 * Event handler for the search button
2074 *
2075 * @param v The view that was clicked.
2076 */
2077 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002078 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2079
Amith Yamasania135ba82011-08-09 17:42:01 -07002080 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002081 }
2082
2083 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002084 * Event handler for the voice button
2085 *
2086 * @param v The view that was clicked.
2087 */
2088 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002089 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002090
Bjorn Bringertc459e522013-06-07 19:36:01 +01002091 startVoice();
2092 }
2093
2094 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002095 try {
2096 final SearchManager searchManager =
2097 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2098 ComponentName activityName = searchManager.getGlobalSearchActivity();
2099 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002100 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002101 if (activityName != null) {
2102 intent.setPackage(activityName.getPackageName());
2103 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002104 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002105 } catch (ActivityNotFoundException e) {
2106 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002107 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002108 startActivitySafely(null, intent, "onClickVoiceButton");
2109 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002110 }
2111
2112 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002113 * Event handler for the "grid" button that appears on the home screen, which
2114 * enters all apps mode.
2115 *
2116 * @param v The view that was clicked.
2117 */
2118 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002119 showAllApps(true);
2120 }
2121
2122 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002123 // Provide the same haptic feedback that the system offers for virtual keys.
2124 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002125 }
2126
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002127 public void onClickAppMarketButton(View v) {
2128 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002129 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002130 } else {
2131 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002132 }
2133 }
2134
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002135 void startApplicationDetailsActivity(ComponentName componentName) {
2136 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002137 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2138 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002139 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002140 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002141 }
2142
Patrick Dubroy5539af72010-09-07 15:22:01 -07002143 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2144 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2145 // System applications cannot be installed. For now, show a toast explaining that.
2146 // We may give them the option of disabling apps this way.
2147 int messageId = R.string.uninstall_system_app_text;
2148 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2149 } else {
2150 String packageName = appInfo.componentName.getPackageName();
2151 String className = appInfo.componentName.getClassName();
2152 Intent intent = new Intent(
2153 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002154 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2155 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002156 startActivity(intent);
2157 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002158 }
2159
Michael Jurka86a720e2012-05-09 11:23:23 -07002160 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002164 // Only launch using the new animation if the shortcut has not opted out (this is a
2165 // private contract between launcher and may be ignored in the future).
2166 boolean useLaunchAnimation = (v != null) &&
2167 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2168 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002169 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2170 v.getMeasuredWidth(), v.getMeasuredHeight());
2171
2172 startActivity(intent, opts.toBundle());
2173 } else {
2174 startActivity(intent);
2175 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002176 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 } catch (SecurityException e) {
2178 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002179 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002181 "or use the exported attribute for this activity. "
2182 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002184 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002186
Michael Jurka86a720e2012-05-09 11:23:23 -07002187 boolean startActivitySafely(View v, Intent intent, Object tag) {
2188 boolean success = false;
2189 try {
2190 success = startActivity(v, intent, tag);
2191 } catch (ActivityNotFoundException e) {
2192 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2193 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2194 }
2195 return success;
2196 }
2197
Romain Guy8e633c52010-03-04 12:51:36 -08002198 void startActivityForResultSafely(Intent intent, int requestCode) {
2199 try {
2200 startActivityForResult(intent, requestCode);
2201 } catch (ActivityNotFoundException e) {
2202 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2203 } catch (SecurityException e) {
2204 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2205 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2206 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2207 "or use the exported attribute for this activity.", e);
2208 }
2209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210
Adam Cohena9cf38f2011-05-02 15:36:58 -07002211 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002212 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002213 Folder openFolder = mWorkspace.getFolderForTag(info);
2214
2215 // If the folder info reports that the associated folder is open, then verify that
2216 // it is actually opened. There have been a few instances where this gets out of sync.
2217 if (info.opened && openFolder == null) {
2218 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002220 info.opened = false;
2221 }
2222
Adam Cohenfb91f302012-06-11 15:45:18 -07002223 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 // Close any open folder
2225 closeFolder();
2226 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002227 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 } else {
2229 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 int folderScreen;
2231 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002232 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 // .. and close it
2234 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002235 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 // Close any folder open on the current screen
2237 closeFolder();
2238 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002239 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241 }
2242 }
2243 }
2244
Adam Cohen268c4752012-06-06 17:47:33 -07002245 /**
2246 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2247 * in the DragLayer in the exact absolute location of the original FolderIcon.
2248 */
2249 private void copyFolderIconToImage(FolderIcon fi) {
2250 final int width = fi.getMeasuredWidth();
2251 final int height = fi.getMeasuredHeight();
2252
2253 // Lazy load ImageView, Bitmap and Canvas
2254 if (mFolderIconImageView == null) {
2255 mFolderIconImageView = new ImageView(this);
2256 }
2257 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2258 mFolderIconBitmap.getHeight() != height) {
2259 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2260 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2261 }
2262
2263 DragLayer.LayoutParams lp;
2264 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2265 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2266 } else {
2267 lp = new DragLayer.LayoutParams(width, height);
2268 }
2269
Adam Cohen307fe232012-08-16 17:55:58 -07002270 // The layout from which the folder is being opened may be scaled, adjust the starting
2271 // view size by this scale factor.
2272 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002273 lp.customPosition = true;
2274 lp.x = mRectForFolderAnimation.left;
2275 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002276 lp.width = (int) (scale * width);
2277 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002278
2279 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2280 fi.draw(mFolderIconCanvas);
2281 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002282 if (fi.getFolder() != null) {
2283 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2284 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002285 }
Adam Cohen268c4752012-06-06 17:47:33 -07002286 // Just in case this image view is still in the drag layer from a previous animation,
2287 // we remove it and re-add it.
2288 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2289 mDragLayer.removeView(mFolderIconImageView);
2290 }
2291 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002292 if (fi.getFolder() != null) {
2293 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002294 }
Adam Cohen268c4752012-06-06 17:47:33 -07002295 }
2296
Adam Cohen2801caf2011-05-13 20:57:39 -07002297 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002298 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002299 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2300 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2301 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2302
Adam Cohenc51934b2011-07-26 21:07:43 -07002303 FolderInfo info = (FolderInfo) fi.getTag();
2304 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2305 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002306 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2307 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002308 }
2309
Adam Cohen268c4752012-06-06 17:47:33 -07002310 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2311 copyFolderIconToImage(fi);
2312 fi.setVisibility(View.INVISIBLE);
2313
Michael Jurka2ecf9952012-06-18 12:52:28 -07002314 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002315 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002316 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2317 oa.start();
2318 }
2319
Adam Cohen268c4752012-06-06 17:47:33 -07002320 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002321 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002322 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2323 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2324 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2325
Adam Cohen268c4752012-06-06 17:47:33 -07002326 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002327
Adam Cohen268c4752012-06-06 17:47:33 -07002328 // We remove and re-draw the FolderIcon in-case it has changed
2329 mDragLayer.removeView(mFolderIconImageView);
2330 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002331 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002332 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002333 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002334 oa.addListener(new AnimatorListenerAdapter() {
2335 @Override
2336 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002337 if (cl != null) {
2338 cl.clearFolderLeaveBehind();
2339 // Remove the ImageView copy of the FolderIcon and make the original visible.
2340 mDragLayer.removeView(mFolderIconImageView);
2341 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002342 }
2343 }
2344 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002345 oa.start();
2346 }
2347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002349 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 * is animated relative to the specified View. If the View is null, no animation
2351 * is played.
2352 *
2353 * @param folderInfo The FolderInfo describing the folder to open.
2354 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002355 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002356 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002357 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358
Adam Cohena9cf38f2011-05-02 15:36:58 -07002359 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360
Adam Cohen4554ee12011-08-03 16:13:21 -07002361 // Just verify that the folder hasn't already been added to the DragLayer.
2362 // There was a one-off crash where the folder had a parent already.
2363 if (folder.getParent() == null) {
2364 mDragLayer.addView(folder);
2365 mDragController.addDropTarget((DropTarget) folder);
2366 } else {
2367 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2368 folder.getParent() + ").");
2369 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002370 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002371 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002372
2373 // Notify the accessibility manager that this folder "window" has appeared and occluded
2374 // the workspace items
2375 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2376 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002377 }
2378
2379 public void closeFolder() {
2380 Folder folder = mWorkspace.getOpenFolder();
2381 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002382 if (folder.isEditingName()) {
2383 folder.dismissEditingName();
2384 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002385 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002386
2387 // Dismiss the folder cling
2388 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002389 }
2390 }
2391
2392 void closeFolder(Folder folder) {
2393 folder.getInfo().opened = false;
2394
2395 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2396 if (parent != null) {
2397 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2398 shrinkAndFadeInFolderIcon(fi);
2399 }
2400 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002401
2402 // Notify the accessibility manager that this folder "window" has disappeard and no
2403 // longer occludeds the workspace items
2404 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 }
2406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002408 if (!isDraggingEnabled()) return false;
2409 if (isWorkspaceLocked()) return false;
2410 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411
2412 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002413 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 }
2415
Michael Jurka0280c3b2010-09-17 15:00:07 -07002416 resetAddInfo();
2417 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002419 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 return true;
2421 }
2422
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 // The hotseat touch handling does not go through Workspace, and we always allow long press
2424 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002425 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2427 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002428 if (itemUnderLongClick == null) {
2429 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002430 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2431 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002432 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002433 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2434 mWorkspace.startReordering();
2435 } else {
2436 startWallpaper();
2437 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002439 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002441 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
2443 }
2444 }
2445 return true;
2446 }
2447
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 boolean isHotseatLayout(View layout) {
2449 return mHotseat != null && layout != null &&
2450 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2451 }
2452 Hotseat getHotseat() {
2453 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002454 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002455 SearchDropTargetBar getSearchBar() {
2456 return mSearchDropTargetBar;
2457 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002458
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 /**
2460 * Returns the CellLayout of the specified container at the specified screen.
2461 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002462 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002463 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2464 if (mHotseat != null) {
2465 return mHotseat.getLayout();
2466 } else {
2467 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002468 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002469 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002470 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002471 }
2472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 Workspace getWorkspace() {
2475 return mWorkspace;
2476 }
2477
Daniel Sandler843e8602010-06-07 14:59:01 -04002478 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002479 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002480 }
2481
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002482 /**
2483 * Helper method for the cameraZoomIn/cameraZoomOut animations
2484 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002485 * @param scaleFactor The scale factor used for the zoom
2486 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002487 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002488 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002489 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002491
Winson Chung18f41f82012-05-09 13:28:10 -07002492 void disableWallpaperIfInAllApps() {
2493 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002494 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002495 if (mAppsCustomizeTabHost != null &&
2496 !mAppsCustomizeTabHost.isTransitioning()) {
2497 updateWallpaperVisibility(false);
2498 }
2499 }
2500 }
2501
Craig Mautner360310b2012-10-26 15:13:08 -07002502 private void setWorkspaceBackground(boolean workspace) {
2503 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002504 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002505 }
2506
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002507 void updateWallpaperVisibility(boolean visible) {
2508 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2509 int curflags = getWindow().getAttributes().flags
2510 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2511 if (wpflags != curflags) {
2512 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2513 }
Craig Mautner360310b2012-10-26 15:13:08 -07002514 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002515 }
2516
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002517 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2518 if (v instanceof LauncherTransitionable) {
2519 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2520 }
2521 }
2522
Michael Jurkabed61d22012-02-14 22:51:29 -08002523 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2524 if (v instanceof LauncherTransitionable) {
2525 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2526 }
Winson Chung70442722012-02-10 15:43:22 -08002527
2528 // Update the workspace transition step as well
2529 dispatchOnLauncherTransitionStep(v, 0f);
2530 }
2531
2532 private void dispatchOnLauncherTransitionStep(View v, float t) {
2533 if (v instanceof LauncherTransitionable) {
2534 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2535 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002536 }
2537
2538 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2539 if (v instanceof LauncherTransitionable) {
2540 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2541 }
Winson Chung70442722012-02-10 15:43:22 -08002542
2543 // Update the workspace transition step as well
2544 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002545 }
2546
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002547 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002548 * Things to test when changing the following seven functions.
2549 * - Home from workspace
2550 * - from center screen
2551 * - from other screens
2552 * - Home from all apps
2553 * - from center screen
2554 * - from other screens
2555 * - Back from all apps
2556 * - from center screen
2557 * - from other screens
2558 * - Launch app from workspace and quit
2559 * - with back
2560 * - with home
2561 * - Launch app from all apps and quit
2562 * - with back
2563 * - with home
2564 * - Go to a screen that's not the default, then all
2565 * apps, and launch and app, and go back
2566 * - with back
2567 * -with home
2568 * - On workspace, long press power and go back
2569 * - with back
2570 * - with home
2571 * - On all apps, long press power and go back
2572 * - with back
2573 * - with home
2574 * - On workspace, power off
2575 * - On all apps, power off
2576 * - Launch an app and turn off the screen while in that app
2577 * - Go back with home key
2578 * - Go back with back key TODO: make this not go to workspace
2579 * - From all apps
2580 * - From workspace
2581 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2582 * - From all apps
2583 * - From the center workspace
2584 * - From another workspace
2585 */
2586
2587 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002588 * Zoom the camera out from the workspace to reveal 'toView'.
2589 * Assumes that the view to show is anchored at either the very top or very bottom
2590 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002591 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002592 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002594 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002595 mStateAnimation.cancel();
2596 mStateAnimation = null;
2597 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002599
Winson Chungf0ea4d32011-06-06 14:27:16 -07002600 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2601 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2602 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002603 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002604 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002605 final int startDelay =
2606 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002607
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609
Michael Jurkad74c9842011-07-10 12:44:21 -07002610 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002611 Animator workspaceAnim =
2612 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002613
2614 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002615 toView.setScaleX(scale);
2616 toView.setScaleY(scale);
2617 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2618 scaleAnim.
2619 scaleX(1f).scaleY(1f).
2620 setDuration(duration).
2621 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002622
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002624 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002625 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002626 .ofFloat(toView, "alpha", 0f, 1f)
2627 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002628 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002629 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2630 @Override
2631 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002632 if (animation == null) {
2633 throw new RuntimeException("animation is null");
2634 }
Winson Chung70442722012-02-10 15:43:22 -08002635 float t = (Float) animation.getAnimatedValue();
2636 dispatchOnLauncherTransitionStep(fromView, t);
2637 dispatchOnLauncherTransitionStep(toView, t);
2638 }
2639 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002640
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002641 // toView should appear right at the end of the workspace shrink
2642 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002643 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002644 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002645 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002646
2647 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002648 boolean animationCancelled = false;
2649
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002650 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002651 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002652 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002653 // Prepare the position
2654 toView.setTranslationX(0.0f);
2655 toView.setTranslationY(0.0f);
2656 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002657 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002659 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002660 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002661 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2662 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002663
Daniel Sandlere4f98912013-06-25 15:13:26 -04002664 if (mWorkspace != null
2665 && !springLoaded
2666 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002667 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002668 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002669 if (!animationCancelled) {
2670 updateWallpaperVisibility(false);
2671 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002672
2673 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002674 if (mSearchDropTargetBar != null) {
2675 mSearchDropTargetBar.hideSearchBar(false);
2676 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002677 }
2678
Adam Cohencff6af82011-09-13 14:51:53 -07002679 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002680 public void onAnimationCancel(Animator animation) {
2681 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002682 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002683 });
2684
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002685 if (workspaceAnim != null) {
2686 mStateAnimation.play(workspaceAnim);
2687 }
Michael Jurka1899a362011-11-03 13:50:45 -07002688
2689 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002690
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002691 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2692 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002693
2694 // If any of the objects being animated haven't been measured/laid out
2695 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002696 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002697 (mWorkspace.getMeasuredWidth() == 0) ||
2698 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002699 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002700 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002701
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002702 final AnimatorSet stateAnimation = mStateAnimation;
2703 final Runnable startAnimRunnable = new Runnable() {
2704 public void run() {
2705 // Check that mStateAnimation hasn't changed while
2706 // we waited for a layout/draw pass
2707 if (mStateAnimation != stateAnimation)
2708 return;
2709 setPivotsForZoom(toView, scale);
2710 dispatchOnLauncherTransitionStart(fromView, animated, false);
2711 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002712 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002713 }
2714 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002715 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002716 final ViewTreeObserver observer = toView.getViewTreeObserver();
2717 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2718 public void onGlobalLayout() {
2719 startAnimRunnable.run();
2720 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2721 }
2722 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002723 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002724 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002725 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726 } else {
2727 toView.setTranslationX(0.0f);
2728 toView.setTranslationY(0.0f);
2729 toView.setScaleX(1.0f);
2730 toView.setScaleY(1.0f);
2731 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002732 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002733
Daniel Sandlere4f98912013-06-25 15:13:26 -04002734 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002735 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002736
2737 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002738 if (mSearchDropTargetBar != null) {
2739 mSearchDropTargetBar.hideSearchBar(false);
2740 }
Michael Jurkaabded662011-03-04 12:06:57 -08002741 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002742 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002743 dispatchOnLauncherTransitionStart(fromView, animated, false);
2744 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002745 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002746 dispatchOnLauncherTransitionStart(toView, animated, false);
2747 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002748 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002749 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002750 }
2751
2752 /**
2753 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002754 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002755 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002757 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2758 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002759
Michael Jurkab3e22d92011-10-31 15:58:33 -07002760 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002761 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002762 mStateAnimation.cancel();
2763 mStateAnimation = null;
2764 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002766
Winson Chungf0ea4d32011-06-06 14:27:16 -07002767 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002768 final int fadeOutDuration =
2769 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002770 final float scaleFactor = (float)
2771 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2772 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002773 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002774 Animator workspaceAnim = null;
2775
2776 if (toState == State.WORKSPACE) {
2777 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2778 workspaceAnim = mWorkspace.getChangeStateAnimation(
2779 Workspace.State.NORMAL, animated, stagger);
2780 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2781 workspaceAnim = mWorkspace.getChangeStateAnimation(
2782 Workspace.State.SPRING_LOADED, animated);
2783 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002784
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002786 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002787 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002788 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002789 final LauncherViewPropertyAnimator scaleAnim =
2790 new LauncherViewPropertyAnimator(fromView);
2791 scaleAnim.
2792 scaleX(scaleFactor).scaleY(scaleFactor).
2793 setDuration(duration).
2794 setInterpolator(new Workspace.ZoomInInterpolator());
2795
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002796 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002797 .ofFloat(fromView, "alpha", 1f, 0f)
2798 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002799 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002800 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2801 @Override
2802 public void onAnimationUpdate(ValueAnimator animation) {
2803 float t = 1f - (Float) animation.getAnimatedValue();
2804 dispatchOnLauncherTransitionStep(fromView, t);
2805 dispatchOnLauncherTransitionStep(toView, t);
2806 }
2807 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002808
Michael Jurka2ecf9952012-06-18 12:52:28 -07002809 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002810
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002811 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2812 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002813 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002814
2815 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002816 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002817 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002818 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002819 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002820 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2821 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002822 if (onCompleteRunnable != null) {
2823 onCompleteRunnable.run();
2824 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002825 mAppsCustomizeContent.updateCurrentPageScroll();
2826 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002827 }
2828 });
2829
Winson Chungf0ea4d32011-06-06 14:27:16 -07002830 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002831 if (workspaceAnim != null) {
2832 mStateAnimation.play(workspaceAnim);
2833 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002834 dispatchOnLauncherTransitionStart(fromView, animated, true);
2835 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002836 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 } else {
2838 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002839 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002840 dispatchOnLauncherTransitionStart(fromView, animated, true);
2841 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002842 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002843 dispatchOnLauncherTransitionStart(toView, animated, true);
2844 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002846 }
2847
Michael Jurkae326f182011-11-21 14:05:46 -08002848 @Override
2849 public void onTrimMemory(int level) {
2850 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002851 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002852 mAppsCustomizeTabHost.onTrimMemory();
2853 }
2854 }
2855
Winson Chung18f41f82012-05-09 13:28:10 -07002856 @Override
2857 public void onWindowFocusChanged(boolean hasFocus) {
2858 if (!hasFocus) {
2859 // When another window occludes launcher (like the notification shade, or recents),
2860 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2861 updateWallpaperVisibility(true);
2862 } else {
2863 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002864 mWorkspace.postDelayed(new Runnable() {
2865 @Override
2866 public void run() {
2867 disableWallpaperIfInAllApps();
2868 }
2869 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002870 }
2871 }
2872
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002873 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002874 showWorkspace(animated, null);
2875 }
2876
2877 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002878 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002879 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002880 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002881 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002882
Winson Chungc7d2b602012-05-16 17:02:20 -07002883 // Show the search bar (only animate if we were showing the drop target bar in spring
2884 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002885 if (mSearchDropTargetBar != null) {
2886 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2887 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002888
Michael Jurkab737ee62011-11-15 15:57:22 -08002889 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002890 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002891
2892 // Set focus to the AppsCustomize button
2893 if (mAllAppsButton != null) {
2894 mAllAppsButton.requestFocus();
2895 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002896 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002897
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002898 // Change the state *after* we've called all the transition code
2899 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002900
Winson Chung5fb63472011-02-02 17:03:37 -08002901 // Resume the auto-advance of widgets
2902 mUserPresent = true;
2903 updateRunning();
2904
alanv1d4fde62012-10-17 13:15:47 -07002905 // Send an accessibility event to announce the context change
2906 getWindow().getDecorView()
2907 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002908
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002909 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002910 }
2911
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002912 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002913 }
2914
Michael Jurkab3e22d92011-10-31 15:58:33 -07002915 void showAllApps(boolean animated) {
2916 if (mState != State.WORKSPACE) return;
2917
2918 showAppsCustomizeHelper(animated, false);
2919 mAppsCustomizeTabHost.requestFocus();
2920
Michael Jurkab3e22d92011-10-31 15:58:33 -07002921 // Change the state *after* we've called all the transition code
2922 mState = State.APPS_CUSTOMIZE;
2923
2924 // Pause the auto-advance of widgets until we are out of AllApps
2925 mUserPresent = false;
2926 updateRunning();
2927 closeFolder();
2928
2929 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002930 getWindow().getDecorView()
2931 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002932 }
2933
Adam Cohen7777d962011-08-18 18:58:38 -07002934 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002935 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002936 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002937 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002938 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002939 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002940 }
Adam Cohen7777d962011-08-18 18:58:38 -07002941
Adam Cohened66b2b2012-01-23 17:28:51 -08002942 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2943 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002944 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2945
Winson Chunge7a03942011-08-05 15:05:12 -07002946 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002947 @Override
2948 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002949 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002950 // Before we show workspace, hide all apps again because
2951 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2952 // clean up our state transition functions
2953 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002954 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002955 } else {
2956 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002957 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002958 }
2959 }, (extendedDelay ?
2960 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2961 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2962 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002963
Michael Jurkad3ef3062010-11-23 16:23:58 -08002964 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002965 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002966 final boolean animated = true;
2967 final boolean springLoaded = true;
2968 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002969 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002970 }
2971 // Otherwise, we are not in spring loaded mode, so don't do anything.
2972 }
2973
Michael Jurkab737ee62011-11-15 15:57:22 -08002974 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07002975 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002976 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002977 }
2978 }
2979
2980 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07002981 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002982 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002983 if (mDividerAnimator != null) {
2984 mDividerAnimator.cancel();
2985 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08002986 mDividerAnimator = null;
2987 }
2988 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002989 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07002990 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002991 int duration = 0;
2992 if (mSearchDropTargetBar != null) {
2993 duration = mSearchDropTargetBar.getTransitionInDuration();
2994 }
2995 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002996 mDividerAnimator.start();
2997 }
2998 }
2999 }
3000
Michael Jurkab3e22d92011-10-31 15:58:33 -07003001 void lockAllApps() {
3002 // TODO
3003 }
3004
3005 void unlockAllApps() {
3006 // TODO
3007 }
3008
Winson Chungf0ea4d32011-06-06 14:27:16 -07003009 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003010 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003011 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003012 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003013 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003014 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003015 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003016 int duration = 0;
3017 if (mSearchDropTargetBar != null) {
3018 duration = mSearchDropTargetBar.getTransitionInDuration();
3019 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003020 mHotseat.animate().alpha(1f).setDuration(duration);
3021 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003022 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003023 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003024 }
3025 }
3026 }
3027
3028 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003029 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003030 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003031 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003032 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003034 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003035 int duration = 0;
3036 if (mSearchDropTargetBar != null) {
3037 duration = mSearchDropTargetBar.getTransitionOutDuration();
3038 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003039 mHotseat.animate().alpha(0f).setDuration(duration);
3040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003041 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003042 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003044 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003045 }
3046
Patrick Dubroy5f445422011-02-18 14:35:21 -08003047 /**
3048 * Add an item from all apps or customize onto the given workspace screen.
3049 * If layout is null, add to the current screen.
3050 */
3051 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003052 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003053 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003054 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003055 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003056
Winson Chungdff8ebb2011-09-08 17:25:31 -07003057 /** Maps the current orientation to an index for referencing orientation correct global icons */
3058 private int getCurrentOrientationIndexForGlobalIcons() {
3059 // default - 0, landscape - 1
3060 switch (getResources().getConfiguration().orientation) {
3061 case Configuration.ORIENTATION_LANDSCAPE:
3062 return 1;
3063 default:
3064 return 0;
3065 }
3066 }
3067
Michael Jurkaae65ee32012-04-30 11:45:54 -07003068 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003069 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003070 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003071 // Look for the toolbar icon specified in the activity meta-data
3072 Bundle metaData = packageManager.getActivityInfo(
3073 activityName, PackageManager.GET_META_DATA).metaData;
3074 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003075 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003076 if (iconResId != 0) {
3077 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003078 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003079 }
3080 }
3081 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003082 // This can happen if the activity defines an invalid drawable
3083 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3084 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003085 } catch (Resources.NotFoundException nfe) {
3086 // This can happen if the activity defines an invalid drawable
3087 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3088 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003090 return null;
3091 }
3092
3093 // if successful in getting icon, return it; otherwise, set button to use default drawable
3094 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003095 int buttonId, ComponentName activityName, int fallbackDrawableId,
3096 String toolbarResourceName) {
3097 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003098 Resources r = getResources();
3099 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3100 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003101
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003102 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003103 // If we were unable to find the icon via the meta-data, use a generic one
3104 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003105 toolbarIcon = r.getDrawable(fallbackDrawableId);
3106 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003107 if (button != null) {
3108 button.setCompoundDrawables(toolbarIcon, null, null, null);
3109 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003110 return null;
3111 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003112 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003113 if (button != null) {
3114 button.setCompoundDrawables(toolbarIcon, null, null, null);
3115 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003116 return toolbarIcon.getConstantState();
3117 }
3118 }
3119
3120 // if successful in getting icon, return it; otherwise, set button to use default drawable
3121 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003122 int buttonId, ComponentName activityName, int fallbackDrawableId,
3123 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003124 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003125 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003126
Michael Jurka19e0fc52011-07-22 18:00:21 -07003127 if (button != null) {
3128 // If we were unable to find the icon via the meta-data, use a
3129 // generic one
3130 if (toolbarIcon == null) {
3131 button.setImageResource(fallbackDrawableId);
3132 } else {
3133 button.setImageDrawable(toolbarIcon);
3134 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003135 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003136
3137 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3138
Michael Jurka0423dcf2010-10-05 14:56:18 -07003139 }
3140
Winson Chung1b884092012-06-08 17:13:02 -07003141 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003142 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003143 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003144 }
3145
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003146 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003147 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003148 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003149 }
3150
Winson Chungbb185bd2011-11-21 12:31:42 -08003151 private void invalidatePressedFocusedStates(View container, View button) {
3152 if (container instanceof HolographicLinearLayout) {
3153 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3154 layout.invalidatePressedFocusedStates();
3155 } else if (button instanceof HolographicImageView) {
3156 HolographicImageView view = (HolographicImageView) button;
3157 view.invalidatePressedFocusedStates();
3158 }
3159 }
3160
Winson Chungc51db6a2011-10-05 11:44:49 -07003161 private boolean updateGlobalSearchIcon() {
3162 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003163 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003164 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003165 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003166
Winson Chung1cad91e2011-05-25 17:41:01 -07003167 final SearchManager searchManager =
3168 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3169 ComponentName activityName = searchManager.getGlobalSearchActivity();
3170 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003171 int coi = getCurrentOrientationIndexForGlobalIcons();
3172 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003173 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3174 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3175 if (sGlobalSearchIcon[coi] == null) {
3176 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3177 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3178 TOOLBAR_ICON_METADATA_NAME);
3179 }
3180
Winson Chungc51db6a2011-10-05 11:44:49 -07003181 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3182 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003183 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003184 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003185 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003186 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003187 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3188 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3189 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003190 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003191 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003192 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003193 }
3194 }
3195
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003196 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003197 final View searchButtonContainer = findViewById(R.id.search_button_container);
3198 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003199 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003200 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003201 }
3202
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003204 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003205 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003206
Winson Chungc51db6a2011-10-05 11:44:49 -07003207 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003208 final SearchManager searchManager =
3209 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3210 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3211
3212 ComponentName activityName = null;
3213 if (globalSearchActivity != null) {
3214 // Check if the global search activity handles voice search
3215 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3216 intent.setPackage(globalSearchActivity.getPackageName());
3217 activityName = intent.resolveActivity(getPackageManager());
3218 }
3219
3220 if (activityName == null) {
3221 // Fallback: check if an activity other than the global search activity
3222 // resolves this
3223 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3224 activityName = intent.resolveActivity(getPackageManager());
3225 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003226 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003227 int coi = getCurrentOrientationIndexForGlobalIcons();
3228 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003229 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3230 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3231 if (sVoiceSearchIcon[coi] == null) {
3232 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3233 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3234 TOOLBAR_ICON_METADATA_NAME);
3235 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003236 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003237 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003238 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003239 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003240 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003241 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003242 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003243 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003244 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003245 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003246 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003247 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003248
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003249 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003250 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3251 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003252 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003253 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003254 }
3255
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003256 public void setVoiceButtonProxyVisible(boolean visible) {
3257 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3258 if (voiceButtonProxy != null) {
3259 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3260 }
3261 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003262 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003263 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003264 */
3265 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003266 final View marketButton = findViewById(R.id.market_button);
3267 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3268 // Find the app market activity by resolving an intent.
3269 // (If multiple app markets are installed, it will return the ResolverActivity.)
3270 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003271 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003272 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003273 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003274 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003275 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3276 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003277 marketButton.setVisibility(View.VISIBLE);
3278 } else {
3279 // We should hide and disable the view so that we don't try and restore the visibility
3280 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3281 marketButton.setVisibility(View.GONE);
3282 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003283 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003284 }
3285
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003286 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003287 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3288 Resources r = getResources();
3289 Drawable marketIconDrawable = d.newDrawable(r);
3290 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3291 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3292 marketIconDrawable.setBounds(0, 0, w, h);
3293
3294 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003295 }
3296
Michael Jurkad7c28052012-04-27 15:43:36 -07003297 @Override
3298 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003299 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003300 final List<CharSequence> text = event.getText();
3301 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003302 // Populate event with a fake title based on the current state.
3303 if (mState == State.APPS_CUSTOMIZE) {
3304 text.add(getString(R.string.all_apps_button_label));
3305 } else {
3306 text.add(getString(R.string.all_apps_home_button_label));
3307 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003308 return result;
3309 }
3310
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003311 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003312 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003313 */
3314 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3315 @Override
3316 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003317 closeSystemDialogs();
3318 }
3319 }
3320
3321 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003322 * Receives notifications whenever the appwidgets are reset.
3323 */
3324 private class AppWidgetResetObserver extends ContentObserver {
3325 public AppWidgetResetObserver() {
3326 super(new Handler());
3327 }
3328
3329 @Override
3330 public void onChange(boolean selfChange) {
3331 onAppWidgetReset();
3332 }
3333 }
3334
3335 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003336 * If the activity is currently paused, signal that we need to run the passed Runnable
3337 * in onResume.
3338 *
3339 * This needs to be called from incoming places where resources might have been loaded
3340 * while we are paused. That is becaues the Configuration might be wrong
3341 * when we're not running, and if it comes back to what it was when we
3342 * were paused, we are not restarted.
3343 *
3344 * Implementation of the method from LauncherModel.Callbacks.
3345 *
3346 * @return true if we are currently paused. The caller might be able to
3347 * skip some work in that case since we will come back again.
3348 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003349 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003350 if (mPaused) {
3351 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003352 if (deletePreviousRunnables) {
3353 while (mOnResumeCallbacks.remove(run)) {
3354 }
3355 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003356 mOnResumeCallbacks.add(run);
3357 return true;
3358 } else {
3359 return false;
3360 }
3361 }
3362
Michael Jurkac402cd92013-05-20 15:49:32 +02003363 private boolean waitUntilResume(Runnable run) {
3364 return waitUntilResume(run, false);
3365 }
3366
Michael Jurka7607c2f2013-04-03 14:33:19 -07003367 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003368 * If the activity is currently paused, signal that we need to re-run the loader
3369 * in onResume.
3370 *
3371 * This needs to be called from incoming places where resources might have been loaded
3372 * while we are paused. That is becaues the Configuration might be wrong
3373 * when we're not running, and if it comes back to what it was when we
3374 * were paused, we are not restarted.
3375 *
3376 * Implementation of the method from LauncherModel.Callbacks.
3377 *
3378 * @return true if we are currently paused. The caller might be able to
3379 * skip some work in that case since we will come back again.
3380 */
3381 public boolean setLoadOnResume() {
3382 if (mPaused) {
3383 Log.i(TAG, "setLoadOnResume");
3384 mOnResumeNeedsLoad = true;
3385 return true;
3386 } else {
3387 return false;
3388 }
3389 }
3390
3391 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003393 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003395 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003396 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003397 } else {
3398 return SCREEN_COUNT / 2;
3399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003401
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 /**
3403 * Refreshes the shortcuts shown on the workspace.
3404 *
3405 * Implementation of the method from LauncherModel.Callbacks.
3406 */
3407 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003408 // If we're starting binding all over again, clear any bind calls we'd postponed in
3409 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3410 // from scratch again
3411 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003412
Michael Jurka7607c2f2013-04-03 14:33:19 -07003413 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003414 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003415 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003416 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 int count = workspace.getChildCount();
3418 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003419 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003420 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3421 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003423 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003424 if (mHotseat != null) {
3425 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003426 }
3427 }
3428
Adam Cohendcd297f2013-06-18 13:13:40 -07003429 @Override
3430 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003431 bindAddScreens(orderedScreenIds);
3432 mWorkspace.addExtraEmptyScreen();
3433 }
3434
3435 @Override
3436 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003437 int count = orderedScreenIds.size();
3438 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003439 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003440 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003441 }
3442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003443 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 * Bind the items start-end from the list.
3445 *
3446 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003447 */
Winson Chung64359a52013-07-08 17:17:08 -07003448 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3449 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003450 if (waitUntilResume(new Runnable() {
3451 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003452 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003453 }
3454 })) {
3455 return;
3456 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003457
Winson Chungf0c6ae02012-03-21 16:10:31 -07003458 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3459 Set<String> newApps = new HashSet<String>();
3460 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3461
Winson Chung64359a52013-07-08 17:17:08 -07003462 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3463 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003464 Workspace workspace = mWorkspace;
3465 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003466 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003467
3468 // Short circuit if we are loading dock items for a configuration which has no dock
3469 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3470 mHotseat == null) {
3471 continue;
3472 }
3473
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 switch (item.itemType) {
3475 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3476 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003477 ShortcutInfo info = (ShortcutInfo) item;
3478 String uri = info.intent.toUri(0).toString();
3479 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003480
Winson Chung64359a52013-07-08 17:17:08 -07003481 /*
3482 * TODO: FIX collision case
3483 */
Winson Chungce376632013-07-11 16:12:41 -07003484 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3485 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3486 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3487 throw new RuntimeException("OCCUPIED");
3488 }
Winson Chung64359a52013-07-08 17:17:08 -07003489 }
3490
Adam Cohendcd297f2013-06-18 13:13:40 -07003491 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3492 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003493 boolean animateIconUp = false;
3494 synchronized (newApps) {
3495 if (newApps.contains(uri)) {
3496 animateIconUp = newApps.remove(uri);
3497 }
3498 }
Winson Chung64359a52013-07-08 17:17:08 -07003499 if (forceAnimateIcons) {
3500 // Animate all the applications up now
3501 shortcut.setAlpha(0f);
3502 shortcut.setScaleX(0f);
3503 shortcut.setScaleY(0f);
3504 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3505 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003506 // Prepare the view to be animated up
3507 shortcut.setAlpha(0f);
3508 shortcut.setScaleX(0f);
3509 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003510 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003511 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3512 mNewShortcutAnimateViews.add(shortcut);
3513 }
3514 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003516 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003517 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003518 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003519 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003520 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3521 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003524 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003525
Winson Chung64359a52013-07-08 17:17:08 -07003526 if (forceAnimateIcons) {
3527 // We post the animation slightly delayed to prevent slowdowns when we are loading
3528 // right after we return to launcher.
3529 mWorkspace.postDelayed(new Runnable() {
3530 public void run() {
3531 anim.playTogether(bounceAnims);
3532 anim.start();
3533 }
3534 }, NEW_APPS_ANIMATION_DELAY);
3535 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003537 }
3538
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 /**
3540 * Implementation of the method from LauncherModel.Callbacks.
3541 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003542 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3543 if (waitUntilResume(new Runnable() {
3544 public void run() {
3545 bindFolders(folders);
3546 }
3547 })) {
3548 return;
3549 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003550 sFolders.clear();
3551 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553
3554 /**
3555 * Add the views for a widget to the workspace.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003559 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3560 if (waitUntilResume(new Runnable() {
3561 public void run() {
3562 bindAppWidget(item);
3563 }
3564 })) {
3565 return;
3566 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003567
Daniel Sandler843e8602010-06-07 14:59:01 -04003568 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3569 if (DEBUG_WIDGETS) {
3570 Log.d(TAG, "bindAppWidget: " + item);
3571 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 final Workspace workspace = mWorkspace;
3573
3574 final int appWidgetId = item.appWidgetId;
3575 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003576 if (DEBUG_WIDGETS) {
3577 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3578 }
3579
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3581
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003583 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584
Adam Cohendcd297f2013-06-18 13:13:40 -07003585 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003587 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3588
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 workspace.requestLayout();
3590
Daniel Sandler843e8602010-06-07 14:59:01 -04003591 if (DEBUG_WIDGETS) {
3592 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3593 + (SystemClock.uptimeMillis()-start) + "ms");
3594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 }
3596
Adam Cohen1462de32012-07-24 22:34:36 -07003597 public void onPageBoundSynchronously(int page) {
3598 mSynchronouslyBoundPages.add(page);
3599 }
3600
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 /**
3602 * Callback saying that there aren't any more items to bind.
3603 *
3604 * Implementation of the method from LauncherModel.Callbacks.
3605 */
Adam Cohene25af792013-06-06 23:08:25 -07003606 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003607 if (waitUntilResume(new Runnable() {
3608 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003609 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003610 }
3611 })) {
3612 return;
3613 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 if (mSavedState != null) {
3615 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003616 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 mSavedState = null;
3619 }
3620
Adam Cohen1462de32012-07-24 22:34:36 -07003621 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003623 // If we received the result of any pending adds while the loader was running (e.g. the
3624 // widget configuration forced an orientation change), process them now.
3625 for (int i = 0; i < sPendingAddList.size(); i++) {
3626 completeAdd(sPendingAddList.get(i));
3627 }
3628 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629
Winson Chungc51db6a2011-10-05 11:44:49 -07003630 // Update the market app icon as necessary (the other icons will be managed in response to
3631 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003632 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003633
Winson Chungf0c6ae02012-03-21 16:10:31 -07003634 // Animate up any icons as necessary
3635 if (mVisible || mWorkspaceLoading) {
3636 Runnable newAppsRunnable = new Runnable() {
3637 @Override
3638 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003639 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003640 }
3641 };
Winson Chunga2413752012-04-03 14:22:34 -07003642
Adam Cohendcd297f2013-06-18 13:13:40 -07003643 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3644 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003645 if (canRunNewAppsAnimation()) {
3646 // If the user has not interacted recently, then either snap to the new page to show
3647 // the new-apps animation or just run them if they are to appear on the current page
3648 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003649 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003650 } else {
3651 runNewAppsAnimation(false);
3652 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003653 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003654 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003655 // are on another page (or just normally if they are added to the current page)
3656 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003657 }
3658 }
3659
3660 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003661 if (upgradePath) {
3662 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003663 mWorkspace.stripDuplicateApps();
3664 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003665 }
Adam Cohen99894d92013-06-14 11:22:59 -07003666
Adam Cohen66a01fd2013-06-11 12:48:00 -07003667 mWorkspace.post(new Runnable() {
3668 @Override
3669 public void run() {
3670 onFinishBindingItems();
3671 }
3672 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003673 }
3674
Winson Chunga2413752012-04-03 14:22:34 -07003675 private boolean canRunNewAppsAnimation() {
3676 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3677 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3678 }
3679
Winson Chungc9168342013-06-26 14:54:55 -07003680 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3681 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3682 PropertyValuesHolder.ofFloat("alpha", 1f),
3683 PropertyValuesHolder.ofFloat("scaleX", 1f),
3684 PropertyValuesHolder.ofFloat("scaleY", 1f));
3685 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3686 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3687 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3688 return bounceAnim;
3689 }
3690
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 /**
3692 * Runs a new animation that scales up icons that were added while Launcher was in the
3693 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003694 *
3695 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003696 */
Winson Chunga2413752012-04-03 14:22:34 -07003697 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003698 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003699 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003700
3701 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003702 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3703 @Override
3704 public int compare(View a, View b) {
3705 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3706 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3707 int cellCountX = LauncherModel.getCellCountX();
3708 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3709 }
3710 });
Winson Chunga2413752012-04-03 14:22:34 -07003711
3712 // Animate each of the views in place (or show them immediately if requested)
3713 if (immediate) {
3714 for (View v : mNewShortcutAnimateViews) {
3715 v.setAlpha(1f);
3716 v.setScaleX(1f);
3717 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003718 }
Winson Chunga2413752012-04-03 14:22:34 -07003719 } else {
3720 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3721 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003722 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003723 }
3724 anim.playTogether(bounceAnims);
3725 anim.addListener(new AnimatorListenerAdapter() {
3726 @Override
3727 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003728 if (mWorkspace != null) {
3729 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3730 }
Winson Chunga2413752012-04-03 14:22:34 -07003731 }
3732 });
3733 anim.start();
3734 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003735
3736 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003737 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003738 mNewShortcutAnimateViews.clear();
3739 new Thread("clearNewAppsThread") {
3740 public void run() {
3741 mSharedPrefs.edit()
3742 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3743 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3744 .commit();
3745 }
3746 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003747 }
3748
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003749 @Override
3750 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003751 boolean searchVisible = updateGlobalSearchIcon();
3752 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003753 if (mSearchDropTargetBar != null) {
3754 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3755 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003756 }
3757
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003758 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 * Add the icons for all apps.
3760 *
3761 * Implementation of the method from LauncherModel.Callbacks.
3762 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003763 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003764 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3765 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003766 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003767 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 }
3770
3771 /**
3772 * A package was updated.
3773 *
3774 * Implementation of the method from LauncherModel.Callbacks.
3775 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003776 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3777 if (waitUntilResume(new Runnable() {
3778 public void run() {
3779 bindAppsUpdated(apps);
3780 }
3781 })) {
3782 return;
3783 }
3784
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003785 if (mWorkspace != null) {
3786 mWorkspace.updateShortcuts(apps);
3787 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 }
3789
3790 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003791 * A package was uninstalled. We take both the super set of packageNames
3792 * in addition to specific applications to remove, the reason being that
3793 * this can be called when a package is updated as well. In that scenario,
3794 * we only remove specific components from the workspace, where as
3795 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 *
3797 * Implementation of the method from LauncherModel.Callbacks.
3798 */
Winson Chung83892cc2013-05-01 16:53:33 -07003799 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3800 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003801 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003802 if (waitUntilResume(new Runnable() {
3803 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003804 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003805 }
3806 })) {
3807 return;
3808 }
3809
Winson Chung64359a52013-07-08 17:17:08 -07003810 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003811 mWorkspace.removeItemsByPackageName(packageNames);
3812 } else {
3813 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003814 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003815
Winson Chunga1820962011-10-03 16:31:06 -07003816 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003817 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 }
Joe Onoratobe386092009-11-17 17:32:16 -08003819
3820 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003821 * A number of packages were updated.
3822 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003823 private ArrayList<Object> mWidgetsAndShortcuts;
3824 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003825 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003826 bindPackagesUpdated(mWidgetsAndShortcuts);
3827 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003828 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003829 };
3830
3831 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3832 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3833 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003834 return;
3835 }
3836
Winson Chung64359a52013-07-08 17:17:08 -07003837 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003838 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003839 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003840 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003841 }
3842
Winson Chung400438b2011-01-16 17:53:48 -08003843 private int mapConfigurationOriActivityInfoOri(int configOri) {
3844 final Display d = getWindowManager().getDefaultDisplay();
3845 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3846 switch (d.getRotation()) {
3847 case Surface.ROTATION_0:
3848 case Surface.ROTATION_180:
3849 // We are currently in the same basic orientation as the natural orientation
3850 naturalOri = configOri;
3851 break;
3852 case Surface.ROTATION_90:
3853 case Surface.ROTATION_270:
3854 // We are currently in the other basic orientation to the natural orientation
3855 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3856 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3857 break;
3858 }
3859
3860 int[] oriMap = {
3861 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3862 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3863 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3864 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3865 };
3866 // Since the map starts at portrait, we need to offset if this device's natural orientation
3867 // is landscape.
3868 int indexOffset = 0;
3869 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3870 indexOffset = 1;
3871 }
3872 return oriMap[(d.getRotation() + indexOffset) % 4];
3873 }
Adam Cohen446e9402011-09-15 18:21:21 -07003874
Winson Chung4b919f82012-05-01 10:44:08 -07003875 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003876 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003877 getResources().getBoolean(R.bool.allow_rotation);
3878 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003879 }
Winson Chung4b919f82012-05-01 10:44:08 -07003880 public void lockScreenOrientation() {
3881 if (isRotationEnabled()) {
3882 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3883 .getConfiguration().orientation));
3884 }
3885 }
3886 public void unlockScreenOrientation(boolean immediate) {
3887 if (isRotationEnabled()) {
3888 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003889 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003890 } else {
3891 mHandler.postDelayed(new Runnable() {
3892 public void run() {
3893 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3894 }
3895 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003896 }
Winson Chung4b919f82012-05-01 10:44:08 -07003897 }
Winson Chung400438b2011-01-16 17:53:48 -08003898 }
3899
Winson Chung82f55532011-08-09 14:14:23 -07003900 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003901 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003902 if (DISABLE_CLINGS) {
3903 return false;
3904 }
3905
Brett Chabot2a9e2282011-08-23 15:03:13 -07003906 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003907 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003908
Michael Jurkae233a8b2013-03-19 13:49:20 +01003909 // Restricted secondary users (child mode) will potentially have very few apps
3910 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003911// boolean supportsLimitedUsers =
3912// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3913// Account[] accounts = AccountManager.get(this).getAccounts();
3914// if (supportsLimitedUsers && accounts.length == 0) {
3915// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3916// Bundle restrictions = um.getUserRestrictions();
3917// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3918// return false;
3919// }
3920// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003921 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003922 }
Michael Jurka22143132012-10-04 17:42:38 +02003923
Winson Chung7d7541e2011-09-16 20:14:36 -07003924 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003925 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003926 if (cling != null) {
3927 cling.init(this, positionData);
3928 cling.setVisibility(View.VISIBLE);
3929 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3930 if (animate) {
3931 cling.buildLayer();
3932 cling.setAlpha(0f);
3933 cling.animate()
3934 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003935 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003936 .setDuration(SHOW_CLING_DURATION)
3937 .setStartDelay(delay)
3938 .start();
3939 } else {
3940 cling.setAlpha(1f);
3941 }
Michael Jurka22143132012-10-04 17:42:38 +02003942 cling.setFocusableInTouchMode(true);
3943 cling.post(new Runnable() {
3944 public void run() {
3945 cling.setFocusable(true);
3946 cling.requestFocus();
3947 }
3948 });
3949 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3950 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003951 }
3952 return cling;
3953 }
Michael Jurka22143132012-10-04 17:42:38 +02003954
Winson Chung7d7541e2011-09-16 20:14:36 -07003955 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003956 // To catch cases where siblings of top-level views are made invisible, just check whether
3957 // the cling is directly set to GONE before dismissing it.
3958 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003959 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003960 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003961 anim.addListener(new AnimatorListenerAdapter() {
3962 public void onAnimationEnd(Animator animation) {
3963 cling.setVisibility(View.GONE);
3964 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003965 // We should update the shared preferences on a background thread
3966 new Thread("dismissClingThread") {
3967 public void run() {
3968 SharedPreferences.Editor editor = mSharedPrefs.edit();
3969 editor.putBoolean(flag, true);
3970 editor.commit();
3971 }
3972 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003973 };
3974 });
3975 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003976 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003977 }
3978 }
Michael Jurka22143132012-10-04 17:42:38 +02003979
Winson Chung9d9d74f2011-09-19 11:49:12 -07003980 private void removeCling(int id) {
3981 final View cling = findViewById(id);
3982 if (cling != null) {
3983 final ViewGroup parent = (ViewGroup) cling.getParent();
3984 parent.post(new Runnable() {
3985 @Override
3986 public void run() {
3987 parent.removeView(cling);
3988 }
3989 });
Michael Jurka22143132012-10-04 17:42:38 +02003990 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003991 }
3992 }
Michael Jurka974c3862012-05-22 22:00:31 -07003993
3994 private boolean skipCustomClingIfNoAccounts() {
3995 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3996 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3997 if (customCling) {
3998 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04003999 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004000 Account[] accounts = am.getAccountsByType("com.google");
4001 return accounts.length == 0;
4002 }
4003 return false;
4004 }
4005
Winson Chung7d7541e2011-09-16 20:14:36 -07004006 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004007 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004008 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004009 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4010 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004011 // If we're not using the default workspace layout, replace workspace cling
4012 // with a custom workspace cling (usually specified in an overlay)
4013 // For now, only do this on tablets
4014 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004015 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004016 // Use a custom cling
4017 View cling = findViewById(R.id.workspace_cling);
4018 ViewGroup clingParent = (ViewGroup) cling.getParent();
4019 int clingIndex = clingParent.indexOfChild(cling);
4020 clingParent.removeViewAt(clingIndex);
4021 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4022 clingParent.addView(customCling, clingIndex);
4023 customCling.setId(R.id.workspace_cling);
4024 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004025 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004026 } else {
4027 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004028 }
4029 }
4030 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004031 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004032 if (isClingsEnabled() &&
4033 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004034 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004035 } else {
4036 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004037 }
4038 }
4039 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004041 if (isClingsEnabled() &&
4042 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4043 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004044 } else {
4045 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004046 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004047 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004048 }
4049 public boolean isFolderClingVisible() {
4050 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004051 if (cling != null) {
4052 return cling.getVisibility() == View.VISIBLE;
4053 }
4054 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004055 }
Winson Chung82f55532011-08-09 14:14:23 -07004056 public void dismissWorkspaceCling(View v) {
4057 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004058 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004059 }
4060 public void dismissAllAppsCling(View v) {
4061 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4063 }
4064 public void dismissFolderCling(View v) {
4065 Cling cling = (Cling) findViewById(R.id.folder_cling);
4066 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004067 }
4068
Winson Chung80baf5a2010-08-09 16:03:15 -07004069 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004070 * Prints out out state for debugging.
4071 */
4072 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004073 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004074 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004075 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4076 Log.d(TAG, "mRestoring=" + mRestoring);
4077 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4078 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004079 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004080 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004081
Winson Chung785d2eb2011-04-14 16:08:02 -07004082 if (mAppsCustomizeContent != null) {
4083 mAppsCustomizeContent.dumpState();
4084 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004085 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004086 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004087
4088 @Override
4089 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4090 super.dump(prefix, fd, writer, args);
4091 writer.println(" ");
4092 writer.println("Debug logs: ");
4093 for (int i = 0; i < sDumpLogs.size(); i++) {
4094 writer.println(" " + sDumpLogs.get(i));
4095 }
4096 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004097
4098 public static void dumpDebugLogsToConsole() {
4099 Log.d(TAG, "");
4100 Log.d(TAG, "*********************");
4101 Log.d(TAG, "Launcher debug logs: ");
4102 for (int i = 0; i < sDumpLogs.size(); i++) {
4103 Log.d(TAG, " " + sDumpLogs.get(i));
4104 }
4105 Log.d(TAG, "*********************");
4106 Log.d(TAG, "");
4107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004108}
Michael Jurka2763be32011-02-24 11:19:57 -08004109
Michael Jurkaabded662011-03-04 12:06:57 -08004110interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004111 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004112 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004113 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004114 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004115 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004116}