blob: 545ee2e6c6d93a6ca7e64c7c5daea8d5c58e94f6 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
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;
Craig Mautner360310b2012-10-26 15:13:08 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Craig Mautner360310b2012-10-26 15:13:08 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080074import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070083import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080084import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070092import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070093import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070094import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Adam Cohendf2cc412011-04-27 16:56:57 -070097import com.android.common.Search;
98import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070099import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700108import java.util.Collection;
109import java.util.Collections;
110import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700113import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700114import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
145
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800146 static final int SCREEN_COUNT = 5;
147 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800150 // To turn on these properties, type
151 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
152 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
153 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Winson Chung2672ff92012-05-04 16:22:30 -0700155 // The Intent extra that defines whether to ignore the launch animation
156 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
157 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
160 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700161 // Type: int
162 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700164 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
165 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
167 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700175 // Type: int
176 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
177 // Type: int
178 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
179 // Type: parcelable
180 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700182 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700183 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
184 "com.android.launcher.toolbar_search_icon";
185 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
186 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700187
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700189 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700190 private State mState = State.WORKSPACE;
191 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800192 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700195 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
196 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700197 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700198 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800201 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chunga2413752012-04-03 14:22:34 -0700203 // How long to wait before the new-shortcut animation automatically pans the workspace
204 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
205
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800206 private final BroadcastReceiver mCloseSystemDialogsReceiver
207 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800208 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private LayoutInflater mInflater;
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800213 private View mQsbDivider;
214 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700215 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800216 private DragLayer mDragLayer;
217 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700219 private AppWidgetManager mAppWidgetManager;
220 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700221
Michael Jurkac9d95c52011-08-29 14:03:34 -0700222 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700223 private AppWidgetProviderInfo mPendingAddWidgetInfo;
224
Michael Jurka0280c3b2010-09-17 15:00:07 -0700225 private int[] mTmpAddItemCellCoordinates = new int[2];
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private FolderInfo mFolderInfo;
228
Winson Chung3d503fb2011-07-13 17:25:49 -0700229 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800230 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700231
Winson Chungc51db6a2011-10-05 11:44:49 -0700232 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700233 private AppsCustomizeTabHost mAppsCustomizeTabHost;
234 private AppsCustomizePagedView mAppsCustomizeContent;
235 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700238 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
239 // scroll issues (because the workspace may not have been measured yet) and extra work.
240 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
241 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
243 private SpannableStringBuilder mDefaultKeySsb = null;
244
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 private boolean mWorkspaceLoading = true;
246
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800247 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private boolean mRestoring;
249 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700250 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
Winson Chung4a2afa32012-07-19 14:53:05 -0700252 // Keep track of whether the user has left launcher
253 private static boolean sPausedFromUserAction = false;
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private Bundle mSavedInstanceState;
256
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800258 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800259 private boolean mUserPresent = true;
260 private boolean mVisible = false;
261 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400262
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700263 private static LocaleConfiguration sLocaleConfiguration = null;
264
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700265 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700266
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700267 private Intent mAppMarketIntent = null;
268
Adam Cohended9f8d2010-11-03 13:25:16 -0700269 // Related to the auto-advancing of widgets
270 private final int ADVANCE_MSG = 1;
271 private final int mAdvanceInterval = 20000;
272 private final int mAdvanceStagger = 250;
273 private long mAutoAdvanceSentTime;
274 private long mAutoAdvanceTimeLeft = -1;
275 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
276 new HashMap<View, AppWidgetProviderInfo>();
277
Winson Chung400438b2011-01-16 17:53:48 -0800278 // Determines how long to wait after a rotation before restoring the screen orientation to
279 // match the sensor state.
280 private final int mRestoreScreenOrientationDelay = 500;
281
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700283 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
284 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
285 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800286
Craig Mautner360310b2012-10-26 15:13:08 -0700287 private Drawable mWorkspaceBackgroundDrawable;
288 private Drawable mBlackBackgroundDrawable;
289
Adam Cohen1462de32012-07-24 22:34:36 -0700290 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
291
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700292 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
293
Winson Chung46353de2012-02-16 14:05:10 -0800294 // We only want to get the SharedPreferences once since it does an FS stat each time we get
295 // it from the context.
296 private SharedPreferences mSharedPrefs;
297
Winson Chungf0c6ae02012-03-21 16:10:31 -0700298 // Holds the page that we need to animate to, and the icon views that we need to animate up
299 // when we scroll to that page on resume.
300 private int mNewShortcutAnimatePage = -1;
301 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700302 private ImageView mFolderIconImageView;
303 private Bitmap mFolderIconBitmap;
304 private Canvas mFolderIconCanvas;
305 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700306
Michael Jurkaddd62e92011-02-16 17:49:14 -0800307 private BubbleTextView mWaitingForResume;
308
Michael Jurka22143132012-10-04 17:42:38 +0200309 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
310 = new HideFromAccessibilityHelper();
311
Michael Jurkac1f5d262011-09-30 19:32:27 -0700312 private Runnable mBuildLayersRunnable = new Runnable() {
313 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700314 if (mWorkspace != null) {
315 mWorkspace.buildPageHardwareLayers();
316 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700317 }
318 };
319
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800320 private static ArrayList<PendingAddArguments> sPendingAddList
321 = new ArrayList<PendingAddArguments>();
322
Michael Jurka0a457bf2012-11-19 14:05:05 -0800323 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
324
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800325 private static class PendingAddArguments {
326 int requestCode;
327 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700328 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800329 int screen;
330 int cellX;
331 int cellY;
332 }
333
Michael Jurka0a457bf2012-11-19 14:05:05 -0800334 private static boolean isPropertyEnabled(String propertyName) {
335 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700336 }
337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 @Override
339 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700340 if (DEBUG_STRICT_MODE) {
341 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
342 .detectDiskReads()
343 .detectDiskWrites()
344 .detectNetwork() // or .detectAll() for all detectable problems
345 .penaltyLog()
346 .build());
347 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
348 .detectLeakedSqlLiteObjects()
349 .detectLeakedClosableObjects()
350 .penaltyLog()
351 .penaltyDeath()
352 .build());
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800356 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700357 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
358 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800359 mModel = app.setLauncher(this);
360 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400361 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700363
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700364 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700365 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
366 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700367
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700368 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
369 // this also ensures that any synchronous binding below doesn't re-trigger another
370 // LauncherModel load.
371 mPaused = false;
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200374 android.os.Debug.startMethodTracing(
375 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
377
378 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 setContentView(R.layout.launcher);
380 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700381 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800383 registerContentObservers();
384
Joe Onorato7c312c12009-08-13 21:36:53 -0700385 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 mSavedState = savedInstanceState;
388 restoreState(mSavedState);
389
Winson Chunga12a2502010-12-20 14:41:35 -0800390 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700391 if (mAppsCustomizeContent != null) {
392 mAppsCustomizeContent.onPackagesUpdated();
393 }
Winson Chunga12a2502010-12-20 14:41:35 -0800394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 if (PROFILE_STARTUP) {
396 android.os.Debug.stopMethodTracing();
397 }
398
399 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700400 if (sPausedFromUserAction) {
401 // If the user leaves launcher, then we should just load items asynchronously when
402 // they return.
403 mModel.startLoader(true, -1);
404 } else {
405 // We only load the page synchronously if the user rotates (or triggers a
406 // configuration change) while launcher is in the foreground
407 mModel.startLoader(true, mWorkspace.getCurrentPage());
408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
Michael Jurkac57b7a82011-08-09 22:02:20 -0700411 if (!mModel.isAllAppsLoaded()) {
412 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
413 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
414 }
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 // For handling default keys
417 mDefaultKeySsb = new SpannableStringBuilder();
418 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800419
420 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
421 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800422
Adam Cohenf9426d52012-06-04 17:26:21 -0700423 updateGlobalIcons();
424
425 // On large interfaces, we want the screen to auto-rotate based on the current orientation
426 unlockScreenOrientation(true);
427 }
428
Winson Chung4a2afa32012-07-19 14:53:05 -0700429 protected void onUserLeaveHint() {
430 super.onUserLeaveHint();
431 sPausedFromUserAction = true;
432 }
433
Adam Cohenf9426d52012-06-04 17:26:21 -0700434 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700435 boolean searchVisible = false;
436 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800437 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700438 int coi = getCurrentOrientationIndexForGlobalIcons();
439 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
440 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700441 updateAppMarketIcon();
442 searchVisible = updateGlobalSearchIcon();
443 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700444 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700445 if (sGlobalSearchIcon[coi] != null) {
446 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700447 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700448 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700449 if (sVoiceSearchIcon[coi] != null) {
450 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700451 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700452 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700453 if (sAppMarketIcon[coi] != null) {
454 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800455 }
Winson Chungadf0c182012-08-23 14:59:07 -0700456 if (mSearchDropTargetBar != null) {
457 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
Romain Guycbb89e42009-06-08 15:52:54 -0700460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 if (sLocaleConfiguration == null) {
463 new AsyncTask<Void, Void, LocaleConfiguration>() {
464 @Override
465 protected LocaleConfiguration doInBackground(Void... unused) {
466 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
467 readConfiguration(Launcher.this, localeConfiguration);
468 return localeConfiguration;
469 }
470
471 @Override
472 protected void onPostExecute(LocaleConfiguration result) {
473 sLocaleConfiguration = result;
474 checkForLocaleChange(); // recursive, but now with a locale configuration
475 }
476 }.execute();
477 return;
478 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final Configuration configuration = getResources().getConfiguration();
481
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final String locale = configuration.locale.toString();
484
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700485 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 final int mcc = configuration.mcc;
487
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 final int mnc = configuration.mnc;
490
Romain Guy84f296c2009-11-04 15:00:44 -0800491 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492
Romain Guy84f296c2009-11-04 15:00:44 -0800493 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494 sLocaleConfiguration.locale = locale;
495 sLocaleConfiguration.mcc = mcc;
496 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700497
Joe Onorato0589f0f2010-02-08 13:44:00 -0800498 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700499
500 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
501 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700502 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700503 public void run() {
504 writeConfiguration(Launcher.this, localeConfiguration);
505 }
506 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700507 }
508 }
509
510 private static class LocaleConfiguration {
511 public String locale;
512 public int mcc = -1;
513 public int mnc = -1;
514 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700515
Romain Guy98d01652009-06-30 16:21:04 -0700516 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
517 DataInputStream in = null;
518 try {
519 in = new DataInputStream(context.openFileInput(PREFERENCES));
520 configuration.locale = in.readUTF();
521 configuration.mcc = in.readInt();
522 configuration.mnc = in.readInt();
523 } catch (FileNotFoundException e) {
524 // Ignore
525 } catch (IOException e) {
526 // Ignore
527 } finally {
528 if (in != null) {
529 try {
530 in.close();
531 } catch (IOException e) {
532 // Ignore
533 }
534 }
535 }
536 }
537
538 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
539 DataOutputStream out = null;
540 try {
541 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
542 out.writeUTF(configuration.locale);
543 out.writeInt(configuration.mcc);
544 out.writeInt(configuration.mnc);
545 out.flush();
546 } catch (FileNotFoundException e) {
547 // Ignore
548 } catch (IOException e) {
549 //noinspection ResultOfMethodCallIgnored
550 context.getFileStreamPath(PREFERENCES).delete();
551 } finally {
552 if (out != null) {
553 try {
554 out.close();
555 } catch (IOException e) {
556 // Ignore
557 }
558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560 }
561
Adam Cohen716b51e2011-06-30 12:09:54 -0700562 public DragLayer getDragLayer() {
563 return mDragLayer;
564 }
565
Winson Chung36a62fe2012-05-06 18:04:42 -0700566 boolean isDraggingEnabled() {
567 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
568 // that is subsequently removed from the workspace in startBinding().
569 return !mModel.isLoadingWorkspace();
570 }
571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 static int getScreen() {
573 synchronized (sLock) {
574 return sScreen;
575 }
576 }
577
578 static void setScreen(int screen) {
579 synchronized (sLock) {
580 sScreen = screen;
581 }
582 }
583
Winson Chung557d6ed2011-07-08 15:34:52 -0700584 /**
585 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
586 * a configuration step, this allows the proper animations to run after other transitions.
587 */
588 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700589 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800590 switch (args.requestCode) {
591 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700592 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
593 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800594 break;
595 case REQUEST_PICK_SHORTCUT:
596 processShortcut(args.intent);
597 break;
598 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700599 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
600 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700601 result = true;
602 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800603 case REQUEST_CREATE_APPWIDGET:
604 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700606 result = true;
607 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800608 case REQUEST_PICK_WALLPAPER:
609 // We just wanted the activity result here so we can clear mWaitingForResult
610 break;
611 }
Michael Jurka27614d22012-04-02 06:40:22 -0700612 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
613 // if you turned the screen off and then back while in All Apps, Launcher would not
614 // return to the workspace. Clearing mAddInfo.container here fixes this issue
615 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700616 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 }
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 protected void onActivityResult(
621 final int requestCode, final int resultCode, final Intent data) {
622 if (requestCode == REQUEST_BIND_APPWIDGET) {
623 int appWidgetId = data != null ?
624 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
625 if (resultCode == RESULT_CANCELED) {
626 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
627 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700628 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700629 }
630 return;
631 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700632 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
634 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700635 mWaitingForResult = false;
636
Adam Cohened66b2b2012-01-23 17:28:51 -0800637 // We have special handling for widgets
638 if (isWidgetDrop) {
639 int appWidgetId = data != null ?
640 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700641 if (appWidgetId < 0) {
642 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
643 "widget configuration activity.");
644 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
645 } else {
646 completeTwoStageWidgetDrop(resultCode, appWidgetId);
647 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 return;
649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 // The pattern used here is that a user PICKs a specific application,
652 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
655 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700656 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800657 final PendingAddArguments args = new PendingAddArguments();
658 args.requestCode = requestCode;
659 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700660 args.container = mPendingAddInfo.container;
661 args.screen = mPendingAddInfo.screen;
662 args.cellX = mPendingAddInfo.cellX;
663 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 if (isWorkspaceLocked()) {
665 sPendingAddList.add(args);
666 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700671 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
673 null);
674 }
675
676 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700677 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700678 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 Runnable onCompleteRunnable = null;
680 int animationType = 0;
681
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700682 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 if (resultCode == RESULT_OK) {
684 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
685 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700686 mPendingAddWidgetInfo);
687 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 onCompleteRunnable = new Runnable() {
689 @Override
690 public void run() {
691 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
692 mPendingAddInfo.screen, layout, null);
693 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
694 null);
695 }
696 };
697 } else if (resultCode == RESULT_CANCELED) {
698 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
699 onCompleteRunnable = new Runnable() {
700 @Override
701 public void run() {
702 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
703 null);
704 }
705 };
706 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700707 if (mDragLayer.getAnimatedView() != null) {
708 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
709 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
710 animationType, boundWidget, true);
711 } else {
712 // The animated view may be null in the case of a rotation during widget configuration
713 onCompleteRunnable.run();
714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
716
717 @Override
718 protected void onResume() {
719 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700720
Winson Chung4a2afa32012-07-19 14:53:05 -0700721 // Restore the previous launcher state
722 if (mOnResumeState == State.WORKSPACE) {
723 showWorkspace(false);
724 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
725 showAllApps(false);
726 }
727 mOnResumeState = State.NONE;
728
Craig Mautner360310b2012-10-26 15:13:08 -0700729 // Background was set to gradient in onPause(), restore to black if in all apps.
730 setWorkspaceBackground(mState == State.WORKSPACE);
731
Winson Chungde0fb8f2012-05-08 14:37:08 -0700732 // Process any items that were added while Launcher was away
733 InstallShortcutReceiver.flushInstallQueue(this);
734
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800735 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700736 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700737 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700739 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400740 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700741 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
Winson Chunge4e50662012-01-23 14:45:13 -0800743
744 // Reset the pressed state of icons that were locked in the press state while activities
745 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800746 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800747 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800748 mWaitingForResume.setStayPressed(false);
749 }
Winson Chunge4e50662012-01-23 14:45:13 -0800750 if (mAppsCustomizeContent != null) {
751 // Resets the previous all apps icon press state
752 mAppsCustomizeContent.resetDrawableState();
753 }
Adam Cohen06dff352012-06-01 17:17:08 -0700754 // It is possible that widgets can receive updates while launcher is not in the foreground.
755 // Consequently, the widgets will be inflated in the orientation of the foreground activity
756 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
757 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700758 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700759
760 // Again, as with the above scenario, it's possible that one or more of the global icons
761 // were updated in the wrong orientation.
762 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700763 }
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700766 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700767 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
768 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
769 // when Launcher resumes and we are still in AllApps.
770 updateWallpaperVisibility(true);
771
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700772 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700773 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800774 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700775 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700776 }
Romain Guycbb89e42009-06-08 15:52:54 -0700777
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700778 @Override
779 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 // Flag the loader to stop early before switching
781 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700782 if (mAppsCustomizeContent != null) {
783 mAppsCustomizeContent.surrender();
784 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800785 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700786 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700787
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800788 // We can't hide the IME if it was forced open. So don't bother
789 /*
790 @Override
791 public void onWindowFocusChanged(boolean hasFocus) {
792 super.onWindowFocusChanged(hasFocus);
793
794 if (hasFocus) {
795 final InputMethodManager inputManager = (InputMethodManager)
796 getSystemService(Context.INPUT_METHOD_SERVICE);
797 WindowManager.LayoutParams lp = getWindow().getAttributes();
798 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
799 android.os.Handler()) {
800 protected void onReceiveResult(int resultCode, Bundle resultData) {
801 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
802 }
803 });
804 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
805 }
806 }
807 */
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 private boolean acceptFilter() {
810 final InputMethodManager inputManager = (InputMethodManager)
811 getSystemService(Context.INPUT_METHOD_SERVICE);
812 return !inputManager.isFullscreenMode();
813 }
814
815 @Override
816 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700817 final int uniChar = event.getUnicodeChar();
818 final boolean handled = super.onKeyDown(keyCode, event);
819 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
820 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
822 keyCode, event);
823 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700824 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700825 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700826 // showSearchDialog()
827 // If there are multiple keystrokes before the search dialog takes focus,
828 // onSearchRequested() will be called for every keystroke,
829 // but it is idempotent, so it's fine.
830 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
833
Joe Onorato8a9625e2010-01-28 15:55:35 -0800834 // Eat the long press event so the keyboard doesn't come up.
835 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
836 return true;
837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 return handled;
840 }
841
Karl Rosaen138a0412009-04-23 19:00:21 -0700842 private String getTypedText() {
843 return mDefaultKeySsb.toString();
844 }
845
846 private void clearTypedText() {
847 mDefaultKeySsb.clear();
848 mDefaultKeySsb.clearSpans();
849 Selection.setSelection(mDefaultKeySsb, 0);
850 }
851
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700853 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
854 * State
855 */
856 private static State intToState(int stateOrdinal) {
857 State state = State.WORKSPACE;
858 final State[] stateValues = State.values();
859 for (int i = 0; i < stateValues.length; i++) {
860 if (stateValues[i].ordinal() == stateOrdinal) {
861 state = stateValues[i];
862 break;
863 }
864 }
865 return state;
866 }
867
868 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 * Restores the previous state, if it exists.
870 *
871 * @param savedState The previous state.
872 */
873 private void restoreState(Bundle savedState) {
874 if (savedState == null) {
875 return;
876 }
877
Michael Jurka883f55b2010-10-21 15:47:14 -0700878 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700879 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700880 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800881 }
882
Winson Chungf0c6ae02012-03-21 16:10:31 -0700883 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700885 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
887
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
889 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700890
Winson Chung3d503fb2011-07-13 17:25:49 -0700891 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
892 mPendingAddInfo.container = pendingAddContainer;
893 mPendingAddInfo.screen = pendingAddScreen;
894 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
895 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
897 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
898 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700899 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 mRestoring = true;
901 }
902
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
905 if (renameFolder) {
906 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700907 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 mRestoring = true;
909 }
Winson Chunga12a2502010-12-20 14:41:35 -0800910
Winson Chung785d2eb2011-04-14 16:08:02 -0700911
912 // Restore the AppsCustomize tab
913 if (mAppsCustomizeTabHost != null) {
914 String curTab = savedState.getString("apps_customize_currentTab");
915 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700916 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700917 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700918 mAppsCustomizeContent.loadAssociatedPages(
919 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700920 }
921
Winson Chung5afbf7b2011-07-25 11:53:08 -0700922 int currentIndex = savedState.getInt("apps_customize_currentIndex");
923 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926
927 /**
928 * Finds all the views we need and configure them properly.
929 */
930 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700931 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400932
Craig Mautner360310b2012-10-26 15:13:08 -0700933 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700934 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
935 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700936 mQsbDivider = findViewById(R.id.qsb_divider);
937 mDockDivider = findViewById(R.id.dock_divider);
938
939 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
940 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
941 mBlackBackgroundDrawable = new ColorDrawable(Color.BLACK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
Winson Chung4c98d922011-05-31 16:50:48 -0700943 // Setup the drag layer
944 mDragLayer.setup(this, dragController);
945
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 // Setup the hotseat
947 mHotseat = (Hotseat) findViewById(R.id.hotseat);
948 if (mHotseat != null) {
949 mHotseat.setup(this);
950 }
951
Winson Chung4c98d922011-05-31 16:50:48 -0700952 // Setup the workspace
953 mWorkspace.setHapticFeedbackEnabled(false);
954 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700955 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700956 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700957
Winson Chungf0ea4d32011-06-06 14:27:16 -0700958 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700959 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700960
Winson Chungf0ea4d32011-06-06 14:27:16 -0700961 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -0700962 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700963 mAppsCustomizeContent = (AppsCustomizePagedView)
964 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
965 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -0700966
Winson Chung3d503fb2011-07-13 17:25:49 -0700967 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700968 dragController.setDragScoller(mWorkspace);
969 dragController.setScrollView(mDragLayer);
970 dragController.setMoveTarget(mWorkspace);
971 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700972 if (mSearchDropTargetBar != null) {
973 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976
977 /**
978 * Creates a view representing a shortcut.
979 *
980 * @param info The data structure describing the shortcut.
981 *
982 * @return A View inflated from R.layout.application.
983 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700986 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 /**
990 * Creates a view representing a shortcut inflated from the specified resource.
991 *
992 * @param layoutResId The id of the XML layout used to create the shortcut.
993 * @param parent The group the shortcut belongs to.
994 * @param info The data structure describing the shortcut.
995 *
996 * @return A View inflated from layoutResId.
997 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800999 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1000 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 return favorite;
1003 }
1004
1005 /**
1006 * Add an application shortcut to the workspace.
1007 *
1008 * @param data The intent describing the application.
1009 * @param cellInfo The position on screen where to create the shortcut.
1010 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001011 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001013 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014
Adam Cohenfbba09b2011-07-18 21:43:05 -07001015 // First we check if we already know the exact location where we want to add this item.
1016 if (cellX >= 0 && cellY >= 0) {
1017 cellXY[0] = cellX;
1018 cellXY[1] = cellY;
1019 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001020 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001021 return;
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001024 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001025
Romain Guy73b979d2009-06-09 12:57:21 -07001026 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001029 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001030 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001031 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032 } else {
1033 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 /**
1038 * Add a shortcut to the workspace.
1039 *
1040 * @param data The intent describing the shortcut.
1041 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1044 int cellY) {
1045 int[] cellXY = mTmpAddItemCellCoordinates;
1046 int[] touchXY = mPendingAddInfo.dropPos;
1047 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Michael Jurkad3ef3062010-11-23 16:23:58 -08001049 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001050
Adam Cohen558baaf2011-08-15 15:22:57 -07001051 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001052 if (info == null) {
1053 return;
1054 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001055 final View view = createShortcut(info);
1056
Adam Cohenfbba09b2011-07-18 21:43:05 -07001057 // First we check if we already know the exact location where we want to add this item.
1058 if (cellX >= 0 && cellY >= 0) {
1059 cellXY[0] = cellX;
1060 cellXY[1] = cellY;
1061 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001062
1063 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001064 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001065 true, null,null)) {
1066 return;
1067 }
1068 DragObject dragObject = new DragObject();
1069 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001070 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1071 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001072 return;
1073 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001074 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001075 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001077 foundCellSpan = (result != null);
1078 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001079 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001080 }
1081
1082 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001083 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 return;
1085 }
1086
Adam Cohen558baaf2011-08-15 15:22:57 -07001087 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
1089 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001090 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1091 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093 }
1094
Adam Cohen2f093b62012-04-30 18:59:53 -07001095 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1096 int minHeight) {
1097 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001098 // We want to account for the extra amount of padding that we are adding to the widget
1099 // to ensure that it gets the full amount of space that it has requested
1100 int requiredWidth = minWidth + padding.left + padding.right;
1101 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001102 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001103 }
1104
Adam Cohen2f093b62012-04-30 18:59:53 -07001105 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1106 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001107 }
1108
Adam Cohen2f093b62012-04-30 18:59:53 -07001109 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1110 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001111 }
1112
Adam Cohen2f093b62012-04-30 18:59:53 -07001113 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1114 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001115 }
1116
Adam Cohen2f093b62012-04-30 18:59:53 -07001117 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1118 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001119 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001120 }
1121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001125 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001126 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001128 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1129 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1130 if (appWidgetInfo == null) {
1131 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001134 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001135 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001136
Adam Cohen2f093b62012-04-30 18:59:53 -07001137 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1138 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001141 // We have saved the position to which the widget was dragged-- this really only matters
1142 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001143 int[] cellXY = mTmpAddItemCellCoordinates;
1144 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001145 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001146 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001147 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1148 cellXY[0] = mPendingAddInfo.cellX;
1149 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001150 spanXY[0] = mPendingAddInfo.spanX;
1151 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001152 foundCellSpan = true;
1153 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001154 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001155 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001156 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1157 spanXY[1], cellXY, finalSpan);
1158 spanXY[0] = finalSpan[0];
1159 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001160 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001162 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001163 }
1164
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001166 if (appWidgetId != -1) {
1167 // Deleting an app widget ID is a void call but writes to disk before returning
1168 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001169 new Thread("deleteAppWidgetId") {
1170 public void run() {
1171 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1172 }
1173 }.start();
1174 }
Winson Chung93eef082012-03-23 15:59:27 -07001175 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001176 return;
1177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001180 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1181 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182 launcherInfo.spanX = spanXY[0];
1183 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001184 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1185 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001188 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189
1190 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001191 if (hostView == null) {
1192 // Perform actual inflation because we're live
1193 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1194 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1195 } else {
1196 // The AppWidgetHostView has already been inflated and instantiated
1197 launcherInfo.hostView = hostView;
1198 }
Romain Guycbb89e42009-06-08 15:52:54 -07001199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001201 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001202 launcherInfo.notifyWidgetSizeChanged(this);
1203
Winson Chung3d503fb2011-07-13 17:25:49 -07001204 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001206
1207 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001209 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
Romain Guycbb89e42009-06-08 15:52:54 -07001211
Adam Cohended9f8d2010-11-03 13:25:16 -07001212 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1213 @Override
1214 public void onReceive(Context context, Intent intent) {
1215 final String action = intent.getAction();
1216 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1217 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001218 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001219 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001220
Winson Chungc100e8e2011-08-09 16:02:43 -07001221 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001222 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001223 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1224 mAppsCustomizeTabHost.reset();
1225 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001226 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001227 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1228 mUserPresent = true;
1229 updateRunning();
1230 }
1231 }
1232 };
1233
1234 @Override
1235 public void onAttachedToWindow() {
1236 super.onAttachedToWindow();
1237
1238 // Listen for broadcasts related to user-presence
1239 final IntentFilter filter = new IntentFilter();
1240 filter.addAction(Intent.ACTION_SCREEN_OFF);
1241 filter.addAction(Intent.ACTION_USER_PRESENT);
1242 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001243 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001244 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 mVisible = true;
1246 }
1247
1248 @Override
1249 public void onDetachedFromWindow() {
1250 super.onDetachedFromWindow();
1251 mVisible = false;
1252
Adam Cohend113e0c2010-11-11 10:48:05 -08001253 if (mAttached) {
1254 unregisterReceiver(mReceiver);
1255 mAttached = false;
1256 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001257 updateRunning();
1258 }
1259
1260 public void onWindowVisibilityChanged(int visibility) {
1261 mVisible = visibility == View.VISIBLE;
1262 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001263 // The following code used to be in onResume, but it turns out onResume is called when
1264 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1265 // is a more appropriate event to handle
1266 if (mVisible) {
1267 mAppsCustomizeTabHost.onWindowVisible();
1268 if (!mWorkspaceLoading) {
1269 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001270 // We want to let Launcher draw itself at least once before we force it to build
1271 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001272 // apps is nice and speedy.
1273 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
1274 public void onDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001275 // We delay the layer building a bit in order to give
1276 // other message processing a time to run. In particular
1277 // this avoids a delay in hiding the IME if it was
1278 // currently shown, because doing that may involve
1279 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001280 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001281
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001282 observer.removeOnDrawListener(this);
1283 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001284 }
1285 });
1286 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001287 // When Launcher comes back to foreground, a different Activity might be responsible for
1288 // the app market intent, so refresh the icon
1289 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001290 clearTypedText();
1291 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001292 }
1293
1294 private void sendAdvanceMessage(long delay) {
1295 mHandler.removeMessages(ADVANCE_MSG);
1296 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1297 mHandler.sendMessageDelayed(msg, delay);
1298 mAutoAdvanceSentTime = System.currentTimeMillis();
1299 }
1300
1301 private void updateRunning() {
1302 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1303 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1304 mAutoAdvanceRunning = autoAdvanceRunning;
1305 if (autoAdvanceRunning) {
1306 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1307 sendAdvanceMessage(delay);
1308 } else {
1309 if (!mWidgetsToAdvance.isEmpty()) {
1310 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1311 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1312 }
1313 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001314 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001315 }
1316 }
1317 }
1318
1319 private final Handler mHandler = new Handler() {
1320 @Override
1321 public void handleMessage(Message msg) {
1322 if (msg.what == ADVANCE_MSG) {
1323 int i = 0;
1324 for (View key: mWidgetsToAdvance.keySet()) {
1325 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1326 final int delay = mAdvanceStagger * i;
1327 if (v instanceof Advanceable) {
1328 postDelayed(new Runnable() {
1329 public void run() {
1330 ((Advanceable) v).advance();
1331 }
1332 }, delay);
1333 }
1334 i++;
1335 }
1336 sendAdvanceMessage(mAdvanceInterval);
1337 }
1338 }
1339 };
1340
1341 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001342 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001343 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1344 if (v instanceof Advanceable) {
1345 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001346 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 updateRunning();
1348 }
1349 }
1350
1351 void removeWidgetToAutoAdvance(View hostView) {
1352 if (mWidgetsToAdvance.containsKey(hostView)) {
1353 mWidgetsToAdvance.remove(hostView);
1354 updateRunning();
1355 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356 }
1357
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 launcherInfo.hostView = null;
1361 }
1362
Winson Chung93eef082012-03-23 15:59:27 -07001363 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1364 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1365 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001366 }
1367
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001368 public LauncherAppWidgetHost getAppWidgetHost() {
1369 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
Romain Guycbb89e42009-06-08 15:52:54 -07001371
Winson Chunga9abd0e2010-10-27 17:18:37 -07001372 public LauncherModel getModel() {
1373 return mModel;
1374 }
1375
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001376 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001377 getWindow().closeAllPanels();
1378
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001379 // Whatever we were doing is hereby canceled.
1380 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001381 }
1382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 @Override
1384 protected void onNewIntent(Intent intent) {
1385 super.onNewIntent(intent);
1386
1387 // Close the menu
1388 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001389 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001390 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001391
Jamie Genniscb222e82012-10-23 15:44:26 -07001392 final boolean alreadyOnHome =
1393 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001394 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001395
Jamie Genniscb222e82012-10-23 15:44:26 -07001396 Runnable processIntent = new Runnable() {
1397 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001398 if (mWorkspace == null) {
1399 // Can be cases where mWorkspace is null, this prevents a NPE
1400 return;
1401 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001402 Folder openFolder = mWorkspace.getOpenFolder();
1403 // In all these cases, only animate if we're already on home
1404 mWorkspace.exitWidgetResizeMode();
1405 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1406 openFolder == null) {
1407 mWorkspace.moveToDefaultScreen(true);
1408 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001409
Jamie Genniscb222e82012-10-23 15:44:26 -07001410 closeFolder();
1411 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001412
Jamie Genniscb222e82012-10-23 15:44:26 -07001413 // If we are already on home, then just animate back to the workspace,
1414 // otherwise, just wait until onResume to set the state back to Workspace
1415 if (alreadyOnHome) {
1416 showWorkspace(true);
1417 } else {
1418 mOnResumeState = State.WORKSPACE;
1419 }
1420
1421 final View v = getWindow().peekDecorView();
1422 if (v != null && v.getWindowToken() != null) {
1423 InputMethodManager imm = (InputMethodManager)getSystemService(
1424 INPUT_METHOD_SERVICE);
1425 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1426 }
1427
1428 // Reset AllApps to its initial state
1429 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1430 mAppsCustomizeTabHost.reset();
1431 }
1432 }
1433 };
1434
1435 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1436 // Delay processing of the intent to allow the status bar animation to finish
1437 // first in order to avoid janky animations.
1438 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001439 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001440 // Process the intent immediately.
1441 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001442 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445 }
1446
1447 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001448 public void onRestoreInstanceState(Bundle state) {
1449 super.onRestoreInstanceState(state);
1450 for (int page: mSynchronouslyBoundPages) {
1451 mWorkspace.restoreInstanceStateForChild(page);
1452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454
1455 @Override
1456 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001457 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001458 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
Michael Jurka883f55b2010-10-21 15:47:14 -07001460 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001461 // We close any open folder since it will not be re-opened, and we need to make sure
1462 // this state is reflected.
1463 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1466 mWaitingForResult) {
1467 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1468 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1469 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1470 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001471 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1472 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1473 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
1476 if (mFolderInfo != null && mWaitingForResult) {
1477 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1478 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1479 }
Michael Jurka946ad472010-07-09 18:05:18 -07001480
Winson Chung785d2eb2011-04-14 16:08:02 -07001481 // Save the current AppsCustomize tab
1482 if (mAppsCustomizeTabHost != null) {
1483 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1484 if (currentTabTag != null) {
1485 outState.putString("apps_customize_currentTab", currentTabTag);
1486 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001487 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1488 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
1491
1492 @Override
1493 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001495
Winson Chunge7a03942011-08-05 15:05:12 -07001496 // Remove all pending runnables
1497 mHandler.removeMessages(ADVANCE_MSG);
1498 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001499 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001500
Winson Chungcd2b0142011-06-08 16:02:26 -07001501 // Stop callbacks from LauncherModel
1502 LauncherApplication app = ((LauncherApplication) getApplication());
1503 mModel.stopLoader();
1504 app.setLauncher(null);
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001507 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001509 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001511 mAppWidgetHost = null;
1512
1513 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
1515 TextKeyListener.getInstance().release();
1516
Winson Chung81b52252012-08-27 15:34:29 -07001517 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1518 // to prevent leaking Launcher activities on orientation change.
1519 if (mModel != null) {
1520 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1521 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001522
1523 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001524 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001525
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001526 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001527 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1528 mWorkspace.removeAllViews();
1529 mWorkspace = null;
1530 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001531
Michael Jurka2ecf9952012-06-18 12:52:28 -07001532 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
1534
Adam Cohena9cf38f2011-05-02 15:36:58 -07001535 public DragController getDragController() {
1536 return mDragController;
1537 }
1538
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 @Override
1540 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001541 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 super.startActivityForResult(intent, requestCode);
1543 }
1544
Winson Chung70d72102011-08-12 11:24:35 -07001545 /**
1546 * Indicates that we want global search for this activity by setting the globalSearch
1547 * argument for {@link #startSearch} to true.
1548 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001550 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001552
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001553 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001554
Karl Rosaen138a0412009-04-23 19:00:21 -07001555 if (initialQuery == null) {
1556 // Use any text typed in the launcher as the initial query
1557 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 if (appSearchData == null) {
1560 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001561 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Winson Chungadf0c182012-08-23 14:59:07 -07001563 Rect sourceBounds = new Rect();
1564 if (mSearchDropTargetBar != null) {
1565 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Michael Jurkaa33411c2012-06-14 16:18:21 -07001568 startGlobalSearch(initialQuery, selectInitialQuery,
1569 appSearchData, sourceBounds);
1570 }
1571
1572 /**
1573 * Starts the global search activity. This code is a copied from SearchManager
1574 */
1575 public void startGlobalSearch(String initialQuery,
1576 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001577 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001578 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1579 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1580 if (globalSearchActivity == null) {
1581 Log.w(TAG, "No global search activity found.");
1582 return;
1583 }
1584 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1585 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1586 intent.setComponent(globalSearchActivity);
1587 // Make sure that we have a Bundle to put source in
1588 if (appSearchData == null) {
1589 appSearchData = new Bundle();
1590 } else {
1591 appSearchData = new Bundle(appSearchData);
1592 }
1593 // Set source to package name of app that starts global search, if not set already.
1594 if (!appSearchData.containsKey("source")) {
1595 appSearchData.putString("source", getPackageName());
1596 }
1597 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1598 if (!TextUtils.isEmpty(initialQuery)) {
1599 intent.putExtra(SearchManager.QUERY, initialQuery);
1600 }
1601 if (selectInitialQuery) {
1602 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1603 }
1604 intent.setSourceBounds(sourceBounds);
1605 try {
1606 startActivity(intent);
1607 } catch (ActivityNotFoundException ex) {
1608 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
1611
Winson Chung70d72102011-08-12 11:24:35 -07001612 @Override
1613 public boolean onCreateOptionsMenu(Menu menu) {
1614 if (isWorkspaceLocked()) {
1615 return false;
1616 }
1617
1618 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001619
1620 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1621 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1622 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001623 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1624 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1625 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001626 String helpUrl = getString(R.string.help_url);
1627 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001628 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1629 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1630
Winson Chung70d72102011-08-12 11:24:35 -07001631 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1632 .setIcon(android.R.drawable.ic_menu_gallery)
1633 .setAlphabeticShortcut('W');
1634 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1635 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001636 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001637 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001638 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1639 .setIcon(android.R.drawable.ic_menu_preferences)
1640 .setIntent(settings)
1641 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001642 if (!helpUrl.isEmpty()) {
1643 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1644 .setIcon(android.R.drawable.ic_menu_help)
1645 .setIntent(help)
1646 .setAlphabeticShortcut('H');
1647 }
Winson Chung70d72102011-08-12 11:24:35 -07001648 return true;
1649 }
1650
1651 @Override
1652 public boolean onPrepareOptionsMenu(Menu menu) {
1653 super.onPrepareOptionsMenu(menu);
1654
1655 if (mAppsCustomizeTabHost.isTransitioning()) {
1656 return false;
1657 }
1658 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1659 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1660
1661 return true;
1662 }
1663
1664 @Override
1665 public boolean onOptionsItemSelected(MenuItem item) {
1666 switch (item.getItemId()) {
1667 case MENU_WALLPAPER_SETTINGS:
1668 startWallpaper();
1669 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001670 }
1671
1672 return super.onOptionsItemSelected(item);
1673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001675 @Override
1676 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001677 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001678 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001679 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001680 }
1681
Joe Onorato9c1289c2009-08-17 11:03:03 -04001682 public boolean isWorkspaceLocked() {
1683 return mWorkspaceLoading || mWaitingForResult;
1684 }
1685
Michael Jurka0280c3b2010-09-17 15:00:07 -07001686 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001687 mPendingAddInfo.container = ItemInfo.NO_ID;
1688 mPendingAddInfo.screen = -1;
1689 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1690 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001691 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001693 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001694
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001695 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1696 AppWidgetProviderInfo appWidgetInfo) {
1697 if (appWidgetInfo.configure != null) {
1698 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001699
Bjorn Bringert7984c942009-12-09 15:38:25 +00001700 // Launch over to configure widget, if needed
1701 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001702 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001703 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001704 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001706 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001707 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1708 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001709 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001710 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 }
1712 }
Romain Guycbb89e42009-06-08 15:52:54 -07001713
Adam Cohenfbba09b2011-07-18 21:43:05 -07001714 /**
1715 * Process a shortcut drop.
1716 *
1717 * @param componentName The name of the component
1718 * @param screen The screen where it should be added
1719 * @param cell The cell it should be added to, optional
1720 * @param position The location on the screen where it was dropped, optional
1721 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1723 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001724 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001725 mPendingAddInfo.container = container;
1726 mPendingAddInfo.screen = screen;
1727 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001728
1729 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001730 mPendingAddInfo.cellX = cell[0];
1731 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001732 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001733
1734 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1735 createShortcutIntent.setComponent(componentName);
1736 processShortcut(createShortcutIntent);
1737 }
1738
Adam Cohenfbba09b2011-07-18 21:43:05 -07001739 /**
1740 * Process a widget drop.
1741 *
1742 * @param info The PendingAppWidgetInfo of the widget being added.
1743 * @param screen The screen where it should be added
1744 * @param cell The cell it should be added to, optional
1745 * @param position The location on the screen where it was dropped, optional
1746 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001747 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001748 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001749 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001750 mPendingAddInfo.container = info.container = container;
1751 mPendingAddInfo.screen = info.screen = screen;
1752 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001753 mPendingAddInfo.minSpanX = info.minSpanX;
1754 mPendingAddInfo.minSpanY = info.minSpanY;
1755
Adam Cohenfbba09b2011-07-18 21:43:05 -07001756 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001757 mPendingAddInfo.cellX = cell[0];
1758 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001759 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001760 if (span != null) {
1761 mPendingAddInfo.spanX = span[0];
1762 mPendingAddInfo.spanY = span[1];
1763 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001764
Adam Cohened66b2b2012-01-23 17:28:51 -08001765 AppWidgetHostView hostView = info.boundWidget;
1766 int appWidgetId;
1767 if (hostView != null) {
1768 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001769 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001770 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001771 // In this case, we either need to start an activity to get permission to bind
1772 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001773 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001774 Bundle options = info.bindOptions;
1775
1776 boolean success = false;
1777 if (options != null) {
1778 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1779 info.componentName, options);
1780 } else {
1781 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1782 info.componentName);
1783 }
1784 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001785 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001786 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001787 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001788 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1789 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1790 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001791 // TODO: we need to make sure that this accounts for the options bundle.
1792 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001793 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1794 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001795 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001796 }
1797
Joe Onoratodeb98af2010-02-19 14:59:39 -08001798 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001799 // Handle case where user selected "Applications"
1800 String applicationName = getResources().getString(R.string.group_applications);
1801 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001802
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001803 if (applicationName != null && applicationName.equals(shortcutName)) {
1804 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1805 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001807 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1808 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001809 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001810 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001812 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
1815
Winson Chung24ab2f12010-09-16 14:10:47 -07001816 void processWallpaper(Intent intent) {
1817 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1818 }
1819
Winson Chung3d503fb2011-07-13 17:25:49 -07001820 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1821 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001822 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 folderInfo.title = getText(R.string.folder_name);
1824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001826 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1827 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001828 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829
1830 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 FolderIcon newFolder =
1832 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1833 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1834 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001835 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
Romain Guycbb89e42009-06-08 15:52:54 -07001837
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001839 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001843 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001845 Intent chooser = Intent.createChooser(pickWallpaper,
1846 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001847 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1848 // Removed in Eclair MR1
1849// WallpaperManager wm = (WallpaperManager)
1850// getSystemService(Context.WALLPAPER_SERVICE);
1851// WallpaperInfo wi = wm.getWallpaperInfo();
1852// if (wi != null && wi.getSettingsActivity() != null) {
1853// LabeledIntent li = new LabeledIntent(getPackageName(),
1854// R.string.configure_wallpaper, 0);
1855// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1856// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1857// }
Mike Clerona0618e42009-10-22 13:55:21 -07001858 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
1860
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001861 /**
1862 * Registers various content observers. The current implementation registers
1863 * only a favorites observer to keep track of the favorites applications.
1864 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001865 private void registerContentObservers() {
1866 ContentResolver resolver = getContentResolver();
1867 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1868 true, mWidgetObserver);
1869 }
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 @Override
1872 public boolean dispatchKeyEvent(KeyEvent event) {
1873 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1874 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001876 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001877 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001878 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001879 dumpState();
1880 return true;
1881 }
1882 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001883 }
1884 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1885 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001886 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 return true;
1888 }
1889 }
1890
1891 return super.dispatchKeyEvent(event);
1892 }
1893
Joe Onorato88ec0992009-11-19 13:16:06 -08001894 @Override
1895 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001896 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001897 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001898 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001899 Folder openFolder = mWorkspace.getOpenFolder();
1900 if (openFolder.isEditingName()) {
1901 openFolder.dismissEditingName();
1902 } else {
1903 closeFolder();
1904 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001905 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001906 mWorkspace.exitWidgetResizeMode();
1907
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001908 // Back button is a no-op here, but give at least some feedback for the button press
1909 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001910 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001911 }
1912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001914 * Re-listen when widgets are reset.
1915 */
1916 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001917 if (mAppWidgetHost != null) {
1918 mAppWidgetHost.startListening();
1919 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001920 }
1921
1922 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 * Launches the intent referred by the clicked shortcut.
1924 *
1925 * @param v The view representing the clicked shortcut.
1926 */
1927 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001928 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1929 // view has detached (it's possible for this to happen if the view is removed mid touch).
1930 if (v.getWindowToken() == null) {
1931 return;
1932 }
1933
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001934 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001935 return;
1936 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001939 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001941 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001942 int[] pos = new int[2];
1943 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001944 intent.setSourceBounds(new Rect(pos[0], pos[1],
1945 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001946
1947 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001948
1949 if (success && v instanceof BubbleTextView) {
1950 mWaitingForResume = (BubbleTextView) v;
1951 mWaitingForResume.setStayPressed(true);
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001954 if (v instanceof FolderIcon) {
1955 FolderIcon fi = (FolderIcon) v;
1956 handleFolderClick(fi);
1957 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001958 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001959 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001960 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001961 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001962 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965 }
1966
Michael Jurka0e260592010-06-30 17:07:39 -07001967 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001968 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001969 // clicking anywhere on the workspace causes the customization drawer to slide down
1970 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001971 return false;
1972 }
1973
Michael Jurkaaf442092010-06-10 17:01:57 -07001974 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001975 * Event handler for the search button
1976 *
1977 * @param v The view that was clicked.
1978 */
1979 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001980 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1981
Amith Yamasania135ba82011-08-09 17:42:01 -07001982 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001983 }
1984
1985 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001986 * Event handler for the voice button
1987 *
1988 * @param v The view that was clicked.
1989 */
1990 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001991 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001992
Michael Jurkaae65ee32012-04-30 11:45:54 -07001993 try {
1994 final SearchManager searchManager =
1995 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1996 ComponentName activityName = searchManager.getGlobalSearchActivity();
1997 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001998 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001999 if (activityName != null) {
2000 intent.setPackage(activityName.getPackageName());
2001 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002002 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002003 } catch (ActivityNotFoundException e) {
2004 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002005 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002006 startActivitySafely(null, intent, "onClickVoiceButton");
2007 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002008 }
2009
2010 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002011 * Event handler for the "grid" button that appears on the home screen, which
2012 * enters all apps mode.
2013 *
2014 * @param v The view that was clicked.
2015 */
2016 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002017 showAllApps(true);
2018 }
2019
2020 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002021 // Provide the same haptic feedback that the system offers for virtual keys.
2022 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002023 }
2024
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002025 public void onClickAppMarketButton(View v) {
2026 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002027 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002028 } else {
2029 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002030 }
2031 }
2032
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002033 void startApplicationDetailsActivity(ComponentName componentName) {
2034 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002035 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2036 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002037 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002038 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002039 }
2040
Patrick Dubroy5539af72010-09-07 15:22:01 -07002041 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2042 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2043 // System applications cannot be installed. For now, show a toast explaining that.
2044 // We may give them the option of disabling apps this way.
2045 int messageId = R.string.uninstall_system_app_text;
2046 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2047 } else {
2048 String packageName = appInfo.componentName.getPackageName();
2049 String className = appInfo.componentName.getClassName();
2050 Intent intent = new Intent(
2051 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002052 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2053 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002054 startActivity(intent);
2055 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002056 }
2057
Michael Jurka86a720e2012-05-09 11:23:23 -07002058 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002062 // Only launch using the new animation if the shortcut has not opted out (this is a
2063 // private contract between launcher and may be ignored in the future).
2064 boolean useLaunchAnimation = (v != null) &&
2065 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2066 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002067 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2068 v.getMeasuredWidth(), v.getMeasuredHeight());
2069
2070 startActivity(intent, opts.toBundle());
2071 } else {
2072 startActivity(intent);
2073 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002074 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 } catch (SecurityException e) {
2076 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002077 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002079 "or use the exported attribute for this activity. "
2080 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002082 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002084
Michael Jurka86a720e2012-05-09 11:23:23 -07002085 boolean startActivitySafely(View v, Intent intent, Object tag) {
2086 boolean success = false;
2087 try {
2088 success = startActivity(v, intent, tag);
2089 } catch (ActivityNotFoundException e) {
2090 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2091 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2092 }
2093 return success;
2094 }
2095
Romain Guy8e633c52010-03-04 12:51:36 -08002096 void startActivityForResultSafely(Intent intent, int requestCode) {
2097 try {
2098 startActivityForResult(intent, requestCode);
2099 } catch (ActivityNotFoundException e) {
2100 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2101 } catch (SecurityException e) {
2102 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2103 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2104 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2105 "or use the exported attribute for this activity.", e);
2106 }
2107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108
Adam Cohena9cf38f2011-05-02 15:36:58 -07002109 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002110 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002111 Folder openFolder = mWorkspace.getFolderForTag(info);
2112
2113 // If the folder info reports that the associated folder is open, then verify that
2114 // it is actually opened. There have been a few instances where this gets out of sync.
2115 if (info.opened && openFolder == null) {
2116 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2117 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2118 info.opened = false;
2119 }
2120
Adam Cohenfb91f302012-06-11 15:45:18 -07002121 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 // Close any open folder
2123 closeFolder();
2124 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002125 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 } else {
2127 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 int folderScreen;
2129 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002130 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 // .. and close it
2132 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002133 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 // Close any folder open on the current screen
2135 closeFolder();
2136 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002137 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139 }
2140 }
2141 }
2142
Adam Cohen268c4752012-06-06 17:47:33 -07002143 /**
2144 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2145 * in the DragLayer in the exact absolute location of the original FolderIcon.
2146 */
2147 private void copyFolderIconToImage(FolderIcon fi) {
2148 final int width = fi.getMeasuredWidth();
2149 final int height = fi.getMeasuredHeight();
2150
2151 // Lazy load ImageView, Bitmap and Canvas
2152 if (mFolderIconImageView == null) {
2153 mFolderIconImageView = new ImageView(this);
2154 }
2155 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2156 mFolderIconBitmap.getHeight() != height) {
2157 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2158 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2159 }
2160
2161 DragLayer.LayoutParams lp;
2162 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2163 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2164 } else {
2165 lp = new DragLayer.LayoutParams(width, height);
2166 }
2167
Adam Cohen307fe232012-08-16 17:55:58 -07002168 // The layout from which the folder is being opened may be scaled, adjust the starting
2169 // view size by this scale factor.
2170 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002171 lp.customPosition = true;
2172 lp.x = mRectForFolderAnimation.left;
2173 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002174 lp.width = (int) (scale * width);
2175 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002176
2177 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2178 fi.draw(mFolderIconCanvas);
2179 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002180 if (fi.getFolder() != null) {
2181 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2182 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002183 }
Adam Cohen268c4752012-06-06 17:47:33 -07002184 // Just in case this image view is still in the drag layer from a previous animation,
2185 // we remove it and re-add it.
2186 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2187 mDragLayer.removeView(mFolderIconImageView);
2188 }
2189 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002190 if (fi.getFolder() != null) {
2191 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002192 }
Adam Cohen268c4752012-06-06 17:47:33 -07002193 }
2194
Adam Cohen2801caf2011-05-13 20:57:39 -07002195 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002196 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002197 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2198 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2199 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2200
Adam Cohenc51934b2011-07-26 21:07:43 -07002201 FolderInfo info = (FolderInfo) fi.getTag();
2202 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2203 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002204 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2205 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002206 }
2207
Adam Cohen268c4752012-06-06 17:47:33 -07002208 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2209 copyFolderIconToImage(fi);
2210 fi.setVisibility(View.INVISIBLE);
2211
Michael Jurka2ecf9952012-06-18 12:52:28 -07002212 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002213 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002214 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2215 oa.start();
2216 }
2217
Adam Cohen268c4752012-06-06 17:47:33 -07002218 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002219 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002220 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2221 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2222 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2223
Adam Cohen268c4752012-06-06 17:47:33 -07002224 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002225
Adam Cohen268c4752012-06-06 17:47:33 -07002226 // We remove and re-draw the FolderIcon in-case it has changed
2227 mDragLayer.removeView(mFolderIconImageView);
2228 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002229 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002230 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002231 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002232 oa.addListener(new AnimatorListenerAdapter() {
2233 @Override
2234 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002235 if (cl != null) {
2236 cl.clearFolderLeaveBehind();
2237 // Remove the ImageView copy of the FolderIcon and make the original visible.
2238 mDragLayer.removeView(mFolderIconImageView);
2239 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002240 }
2241 }
2242 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002243 oa.start();
2244 }
2245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002247 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 * is animated relative to the specified View. If the View is null, no animation
2249 * is played.
2250 *
2251 * @param folderInfo The FolderInfo describing the folder to open.
2252 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002253 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002254 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002255 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256
Adam Cohena9cf38f2011-05-02 15:36:58 -07002257 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258
Adam Cohen4554ee12011-08-03 16:13:21 -07002259 // Just verify that the folder hasn't already been added to the DragLayer.
2260 // There was a one-off crash where the folder had a parent already.
2261 if (folder.getParent() == null) {
2262 mDragLayer.addView(folder);
2263 mDragController.addDropTarget((DropTarget) folder);
2264 } else {
2265 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2266 folder.getParent() + ").");
2267 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002268 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002269 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002270 }
2271
2272 public void closeFolder() {
2273 Folder folder = mWorkspace.getOpenFolder();
2274 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002275 if (folder.isEditingName()) {
2276 folder.dismissEditingName();
2277 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002278 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002279
2280 // Dismiss the folder cling
2281 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002282 }
2283 }
2284
2285 void closeFolder(Folder folder) {
2286 folder.getInfo().opened = false;
2287
2288 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2289 if (parent != null) {
2290 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2291 shrinkAndFadeInFolderIcon(fi);
2292 }
2293 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 }
2295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002297 if (!isDraggingEnabled()) return false;
2298 if (isWorkspaceLocked()) return false;
2299 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300
2301 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002302 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
2304
Michael Jurka0280c3b2010-09-17 15:00:07 -07002305 resetAddInfo();
2306 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002308 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 return true;
2310 }
2311
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 // The hotseat touch handling does not go through Workspace, and we always allow long press
2313 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002314 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2316 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002317 if (itemUnderLongClick == null) {
2318 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002319 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2320 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002321 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002323 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002325 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 }
2327 }
2328 }
2329 return true;
2330 }
2331
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 boolean isHotseatLayout(View layout) {
2333 return mHotseat != null && layout != null &&
2334 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2335 }
2336 Hotseat getHotseat() {
2337 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002338 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002339 SearchDropTargetBar getSearchBar() {
2340 return mSearchDropTargetBar;
2341 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002342
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 /**
2344 * Returns the CellLayout of the specified container at the specified screen.
2345 */
2346 CellLayout getCellLayout(long container, int screen) {
2347 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2348 if (mHotseat != null) {
2349 return mHotseat.getLayout();
2350 } else {
2351 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002352 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 } else {
2354 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002355 }
2356 }
2357
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 Workspace getWorkspace() {
2359 return mWorkspace;
2360 }
2361
Daniel Sandler843e8602010-06-07 14:59:01 -04002362 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002363 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002364 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002365 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002366 }
2367
Craig Mautner360310b2012-10-26 15:13:08 -07002368 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002369 public boolean isAllAppsButtonRank(int rank) {
2370 return mHotseat.isAllAppsButtonRank(rank);
2371 }
2372
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002373 /**
2374 * Helper method for the cameraZoomIn/cameraZoomOut animations
2375 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002376 * @param scaleFactor The scale factor used for the zoom
2377 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002378 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002379 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002380 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002381 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002382
Winson Chung18f41f82012-05-09 13:28:10 -07002383 void disableWallpaperIfInAllApps() {
2384 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002385 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002386 if (mAppsCustomizeTabHost != null &&
2387 !mAppsCustomizeTabHost.isTransitioning()) {
2388 updateWallpaperVisibility(false);
2389 }
2390 }
2391 }
2392
Craig Mautner360310b2012-10-26 15:13:08 -07002393 private void setWorkspaceBackground(boolean workspace) {
2394 mLauncherView.setBackground(workspace ?
2395 mWorkspaceBackgroundDrawable : mBlackBackgroundDrawable);
2396 }
2397
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002398 void updateWallpaperVisibility(boolean visible) {
2399 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2400 int curflags = getWindow().getAttributes().flags
2401 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2402 if (wpflags != curflags) {
2403 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2404 }
Craig Mautner360310b2012-10-26 15:13:08 -07002405 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002406 }
2407
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002408 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2409 if (v instanceof LauncherTransitionable) {
2410 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2411 }
2412 }
2413
Michael Jurkabed61d22012-02-14 22:51:29 -08002414 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2415 if (v instanceof LauncherTransitionable) {
2416 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2417 }
Winson Chung70442722012-02-10 15:43:22 -08002418
2419 // Update the workspace transition step as well
2420 dispatchOnLauncherTransitionStep(v, 0f);
2421 }
2422
2423 private void dispatchOnLauncherTransitionStep(View v, float t) {
2424 if (v instanceof LauncherTransitionable) {
2425 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2426 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002427 }
2428
2429 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2430 if (v instanceof LauncherTransitionable) {
2431 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2432 }
Winson Chung70442722012-02-10 15:43:22 -08002433
2434 // Update the workspace transition step as well
2435 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002436 }
2437
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002438 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002439 * Things to test when changing the following seven functions.
2440 * - Home from workspace
2441 * - from center screen
2442 * - from other screens
2443 * - Home from all apps
2444 * - from center screen
2445 * - from other screens
2446 * - Back from all apps
2447 * - from center screen
2448 * - from other screens
2449 * - Launch app from workspace and quit
2450 * - with back
2451 * - with home
2452 * - Launch app from all apps and quit
2453 * - with back
2454 * - with home
2455 * - Go to a screen that's not the default, then all
2456 * apps, and launch and app, and go back
2457 * - with back
2458 * -with home
2459 * - On workspace, long press power and go back
2460 * - with back
2461 * - with home
2462 * - On all apps, long press power and go back
2463 * - with back
2464 * - with home
2465 * - On workspace, power off
2466 * - On all apps, power off
2467 * - Launch an app and turn off the screen while in that app
2468 * - Go back with home key
2469 * - Go back with back key TODO: make this not go to workspace
2470 * - From all apps
2471 * - From workspace
2472 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2473 * - From all apps
2474 * - From the center workspace
2475 * - From another workspace
2476 */
2477
2478 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002479 * Zoom the camera out from the workspace to reveal 'toView'.
2480 * Assumes that the view to show is anchored at either the very top or very bottom
2481 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002482 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002483 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002484 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002485 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002486 mStateAnimation.cancel();
2487 mStateAnimation = null;
2488 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002489 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002490
Winson Chungf0ea4d32011-06-06 14:27:16 -07002491 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2492 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2493 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002494 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002495 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002496 final int startDelay =
2497 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002498
Michael Jurkab3e22d92011-10-31 15:58:33 -07002499 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002500
Michael Jurkad74c9842011-07-10 12:44:21 -07002501 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002502 Animator workspaceAnim =
2503 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002504
2505 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002506 toView.setScaleX(scale);
2507 toView.setScaleY(scale);
2508 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2509 scaleAnim.
2510 scaleX(1f).scaleY(1f).
2511 setDuration(duration).
2512 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002513
Winson Chungf0ea4d32011-06-06 14:27:16 -07002514 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002515 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002516 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002517 .ofFloat(toView, "alpha", 0f, 1f)
2518 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002519 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002520 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2521 @Override
2522 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002523 if (animation == null) {
2524 throw new RuntimeException("animation is null");
2525 }
Winson Chung70442722012-02-10 15:43:22 -08002526 float t = (Float) animation.getAnimatedValue();
2527 dispatchOnLauncherTransitionStep(fromView, t);
2528 dispatchOnLauncherTransitionStep(toView, t);
2529 }
2530 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002531
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002532 // toView should appear right at the end of the workspace shrink
2533 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002534 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002535 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002536 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002537
2538 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002539 boolean animationCancelled = false;
2540
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002541 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002542 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002543 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002544 // Prepare the position
2545 toView.setTranslationX(0.0f);
2546 toView.setTranslationY(0.0f);
2547 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002548 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002549 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002550 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002551 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002552 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2553 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002554
Michael Jurkafa7969f2012-09-21 16:39:14 -07002555 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002556 // Hide the workspace scrollbar
2557 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002558 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002559 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002560 if (!animationCancelled) {
2561 updateWallpaperVisibility(false);
2562 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002563
2564 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002565 if (mSearchDropTargetBar != null) {
2566 mSearchDropTargetBar.hideSearchBar(false);
2567 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002568 }
2569
Adam Cohencff6af82011-09-13 14:51:53 -07002570 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002571 public void onAnimationCancel(Animator animation) {
2572 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002573 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002574 });
2575
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002576 if (workspaceAnim != null) {
2577 mStateAnimation.play(workspaceAnim);
2578 }
Michael Jurka1899a362011-11-03 13:50:45 -07002579
2580 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002581
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002582 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2583 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002584
2585 // If any of the objects being animated haven't been measured/laid out
2586 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002587 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002588 (mWorkspace.getMeasuredWidth() == 0) ||
2589 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002590 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002591 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002592
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002593 final AnimatorSet stateAnimation = mStateAnimation;
2594 final Runnable startAnimRunnable = new Runnable() {
2595 public void run() {
2596 // Check that mStateAnimation hasn't changed while
2597 // we waited for a layout/draw pass
2598 if (mStateAnimation != stateAnimation)
2599 return;
2600 setPivotsForZoom(toView, scale);
2601 dispatchOnLauncherTransitionStart(fromView, animated, false);
2602 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002603 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002604 }
2605 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002606 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002607 final ViewTreeObserver observer = toView.getViewTreeObserver();
2608 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2609 public void onGlobalLayout() {
2610 startAnimRunnable.run();
2611 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2612 }
2613 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002614 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002615 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002616 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002617 } else {
2618 toView.setTranslationX(0.0f);
2619 toView.setTranslationY(0.0f);
2620 toView.setScaleX(1.0f);
2621 toView.setScaleY(1.0f);
2622 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002623 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002624
Michael Jurkabed61d22012-02-14 22:51:29 -08002625 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2626 // Hide the workspace scrollbar
2627 mWorkspace.hideScrollingIndicator(true);
2628 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002629
2630 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002631 if (mSearchDropTargetBar != null) {
2632 mSearchDropTargetBar.hideSearchBar(false);
2633 }
Michael Jurkaabded662011-03-04 12:06:57 -08002634 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002635 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002636 dispatchOnLauncherTransitionStart(fromView, animated, false);
2637 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002638 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002639 dispatchOnLauncherTransitionStart(toView, animated, false);
2640 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002641 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002643 }
2644
2645 /**
2646 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002647 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002649 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002650 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2651 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002652
Michael Jurkab3e22d92011-10-31 15:58:33 -07002653 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002654 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002655 mStateAnimation.cancel();
2656 mStateAnimation = null;
2657 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002659
Winson Chungf0ea4d32011-06-06 14:27:16 -07002660 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002661 final int fadeOutDuration =
2662 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002663 final float scaleFactor = (float)
2664 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2665 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002666 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002667 Animator workspaceAnim = null;
2668
2669 if (toState == State.WORKSPACE) {
2670 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2671 workspaceAnim = mWorkspace.getChangeStateAnimation(
2672 Workspace.State.NORMAL, animated, stagger);
2673 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2674 workspaceAnim = mWorkspace.getChangeStateAnimation(
2675 Workspace.State.SPRING_LOADED, animated);
2676 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002677
Michael Jurkab3e22d92011-10-31 15:58:33 -07002678 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002679 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002680 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002682 final LauncherViewPropertyAnimator scaleAnim =
2683 new LauncherViewPropertyAnimator(fromView);
2684 scaleAnim.
2685 scaleX(scaleFactor).scaleY(scaleFactor).
2686 setDuration(duration).
2687 setInterpolator(new Workspace.ZoomInInterpolator());
2688
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002689 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002690 .ofFloat(fromView, "alpha", 1f, 0f)
2691 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002692 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002693 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2694 @Override
2695 public void onAnimationUpdate(ValueAnimator animation) {
2696 float t = 1f - (Float) animation.getAnimatedValue();
2697 dispatchOnLauncherTransitionStep(fromView, t);
2698 dispatchOnLauncherTransitionStep(toView, t);
2699 }
2700 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002701
Michael Jurka2ecf9952012-06-18 12:52:28 -07002702 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002703
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002704 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2705 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002706 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002707
2708 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002709 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002710 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002711 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002713 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2714 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002715 if (mWorkspace != null) {
2716 mWorkspace.hideScrollingIndicator(false);
2717 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002718 if (onCompleteRunnable != null) {
2719 onCompleteRunnable.run();
2720 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002721 mAppsCustomizeContent.updateCurrentPageScroll();
2722 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002723 }
2724 });
2725
Winson Chungf0ea4d32011-06-06 14:27:16 -07002726 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002727 if (workspaceAnim != null) {
2728 mStateAnimation.play(workspaceAnim);
2729 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002730 dispatchOnLauncherTransitionStart(fromView, animated, true);
2731 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002732 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002733 } else {
2734 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002735 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002736 dispatchOnLauncherTransitionStart(fromView, animated, true);
2737 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002738 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002739 dispatchOnLauncherTransitionStart(toView, animated, true);
2740 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002741 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002743 }
2744
Michael Jurkae326f182011-11-21 14:05:46 -08002745 @Override
2746 public void onTrimMemory(int level) {
2747 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002748 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002749 mAppsCustomizeTabHost.onTrimMemory();
2750 }
2751 }
2752
Winson Chung18f41f82012-05-09 13:28:10 -07002753 @Override
2754 public void onWindowFocusChanged(boolean hasFocus) {
2755 if (!hasFocus) {
2756 // When another window occludes launcher (like the notification shade, or recents),
2757 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2758 updateWallpaperVisibility(true);
2759 } else {
2760 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002761 mWorkspace.postDelayed(new Runnable() {
2762 @Override
2763 public void run() {
2764 disableWallpaperIfInAllApps();
2765 }
2766 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002767 }
2768 }
2769
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002770 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002771 showWorkspace(animated, null);
2772 }
2773
2774 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002776 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002778 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002779
Winson Chungc7d2b602012-05-16 17:02:20 -07002780 // Show the search bar (only animate if we were showing the drop target bar in spring
2781 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002782 if (mSearchDropTargetBar != null) {
2783 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2784 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002785
Michael Jurkab737ee62011-11-15 15:57:22 -08002786 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002787 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002788
2789 // Set focus to the AppsCustomize button
2790 if (mAllAppsButton != null) {
2791 mAllAppsButton.requestFocus();
2792 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002793 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002794
Michael Jurkab737ee62011-11-15 15:57:22 -08002795 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002796
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002797 // Change the state *after* we've called all the transition code
2798 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002799
Winson Chung5fb63472011-02-02 17:03:37 -08002800 // Resume the auto-advance of widgets
2801 mUserPresent = true;
2802 updateRunning();
2803
alanv1d4fde62012-10-17 13:15:47 -07002804 // Send an accessibility event to announce the context change
2805 getWindow().getDecorView()
2806 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Joe Onorato00acb122009-08-04 16:04:30 -04002807 }
2808
Michael Jurkab3e22d92011-10-31 15:58:33 -07002809 void showAllApps(boolean animated) {
2810 if (mState != State.WORKSPACE) return;
2811
2812 showAppsCustomizeHelper(animated, false);
2813 mAppsCustomizeTabHost.requestFocus();
2814
Michael Jurkab3e22d92011-10-31 15:58:33 -07002815 // Change the state *after* we've called all the transition code
2816 mState = State.APPS_CUSTOMIZE;
2817
2818 // Pause the auto-advance of widgets until we are out of AllApps
2819 mUserPresent = false;
2820 updateRunning();
2821 closeFolder();
2822
2823 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002824 getWindow().getDecorView()
2825 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002826 }
2827
Adam Cohen7777d962011-08-18 18:58:38 -07002828 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002829 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002830 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002831 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002832 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002833 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002834 }
Adam Cohen7777d962011-08-18 18:58:38 -07002835
Adam Cohened66b2b2012-01-23 17:28:51 -08002836 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2837 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002838 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2839
Winson Chunge7a03942011-08-05 15:05:12 -07002840 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002841 @Override
2842 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002843 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002844 // Before we show workspace, hide all apps again because
2845 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2846 // clean up our state transition functions
2847 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002848 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002849 } else {
2850 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002851 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002852 }
2853 }, (extendedDelay ?
2854 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2855 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2856 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002857
Michael Jurkad3ef3062010-11-23 16:23:58 -08002858 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002859 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002860 final boolean animated = true;
2861 final boolean springLoaded = true;
2862 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002863 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002864 }
2865 // Otherwise, we are not in spring loaded mode, so don't do anything.
2866 }
2867
Michael Jurkab737ee62011-11-15 15:57:22 -08002868 void hideDockDivider() {
2869 if (mQsbDivider != null && mDockDivider != null) {
2870 mQsbDivider.setVisibility(View.INVISIBLE);
2871 mDockDivider.setVisibility(View.INVISIBLE);
2872 }
2873 }
2874
2875 void showDockDivider(boolean animated) {
2876 if (mQsbDivider != null && mDockDivider != null) {
2877 mQsbDivider.setVisibility(View.VISIBLE);
2878 mDockDivider.setVisibility(View.VISIBLE);
2879 if (mDividerAnimator != null) {
2880 mDividerAnimator.cancel();
2881 mQsbDivider.setAlpha(1f);
2882 mDockDivider.setAlpha(1f);
2883 mDividerAnimator = null;
2884 }
2885 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002886 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2887 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2888 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002889 int duration = 0;
2890 if (mSearchDropTargetBar != null) {
2891 duration = mSearchDropTargetBar.getTransitionInDuration();
2892 }
2893 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002894 mDividerAnimator.start();
2895 }
2896 }
2897 }
2898
Michael Jurkab3e22d92011-10-31 15:58:33 -07002899 void lockAllApps() {
2900 // TODO
2901 }
2902
2903 void unlockAllApps() {
2904 // TODO
2905 }
2906
Winson Chungf0ea4d32011-06-06 14:27:16 -07002907 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002908 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002909 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002910 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002911 if (!LauncherApplication.isScreenLarge()) {
2912 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002913 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002914 int duration = 0;
2915 if (mSearchDropTargetBar != null) {
2916 duration = mSearchDropTargetBar.getTransitionInDuration();
2917 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002918 mHotseat.animate().alpha(1f).setDuration(duration);
2919 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002920 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002921 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002922 }
2923 }
2924 }
2925
2926 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002927 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002928 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002929 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002930 if (!LauncherApplication.isScreenLarge()) {
2931 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002932 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002933 int duration = 0;
2934 if (mSearchDropTargetBar != null) {
2935 duration = mSearchDropTargetBar.getTransitionOutDuration();
2936 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002937 mHotseat.animate().alpha(0f).setDuration(duration);
2938 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002939 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002940 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002941 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002942 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002943 }
2944
Patrick Dubroy5f445422011-02-18 14:35:21 -08002945 /**
2946 * Add an item from all apps or customize onto the given workspace screen.
2947 * If layout is null, add to the current screen.
2948 */
2949 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002950 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002951 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002952 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002953 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002954
Winson Chungdff8ebb2011-09-08 17:25:31 -07002955 /** Maps the current orientation to an index for referencing orientation correct global icons */
2956 private int getCurrentOrientationIndexForGlobalIcons() {
2957 // default - 0, landscape - 1
2958 switch (getResources().getConfiguration().orientation) {
2959 case Configuration.ORIENTATION_LANDSCAPE:
2960 return 1;
2961 default:
2962 return 0;
2963 }
2964 }
2965
Michael Jurkaae65ee32012-04-30 11:45:54 -07002966 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002967 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002968 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002969 // Look for the toolbar icon specified in the activity meta-data
2970 Bundle metaData = packageManager.getActivityInfo(
2971 activityName, PackageManager.GET_META_DATA).metaData;
2972 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002973 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002974 if (iconResId != 0) {
2975 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002976 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002977 }
2978 }
2979 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002980 // This can happen if the activity defines an invalid drawable
2981 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2982 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002983 } catch (Resources.NotFoundException nfe) {
2984 // This can happen if the activity defines an invalid drawable
2985 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2986 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002987 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002988 return null;
2989 }
2990
2991 // if successful in getting icon, return it; otherwise, set button to use default drawable
2992 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002993 int buttonId, ComponentName activityName, int fallbackDrawableId,
2994 String toolbarResourceName) {
2995 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002996 Resources r = getResources();
2997 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2998 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002999
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003000 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003001 // If we were unable to find the icon via the meta-data, use a generic one
3002 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003003 toolbarIcon = r.getDrawable(fallbackDrawableId);
3004 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003005 if (button != null) {
3006 button.setCompoundDrawables(toolbarIcon, null, null, null);
3007 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003008 return null;
3009 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003010 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003011 if (button != null) {
3012 button.setCompoundDrawables(toolbarIcon, null, null, null);
3013 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003014 return toolbarIcon.getConstantState();
3015 }
3016 }
3017
3018 // if successful in getting icon, return it; otherwise, set button to use default drawable
3019 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003020 int buttonId, ComponentName activityName, int fallbackDrawableId,
3021 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003022 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003023 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003024
Michael Jurka19e0fc52011-07-22 18:00:21 -07003025 if (button != null) {
3026 // If we were unable to find the icon via the meta-data, use a
3027 // generic one
3028 if (toolbarIcon == null) {
3029 button.setImageResource(fallbackDrawableId);
3030 } else {
3031 button.setImageDrawable(toolbarIcon);
3032 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003033 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003034
3035 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3036
Michael Jurka0423dcf2010-10-05 14:56:18 -07003037 }
3038
Winson Chung1b884092012-06-08 17:13:02 -07003039 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003040 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003041 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003042 }
3043
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003044 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003045 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003046 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003047 }
3048
Winson Chungbb185bd2011-11-21 12:31:42 -08003049 private void invalidatePressedFocusedStates(View container, View button) {
3050 if (container instanceof HolographicLinearLayout) {
3051 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3052 layout.invalidatePressedFocusedStates();
3053 } else if (button instanceof HolographicImageView) {
3054 HolographicImageView view = (HolographicImageView) button;
3055 view.invalidatePressedFocusedStates();
3056 }
3057 }
3058
Winson Chungc51db6a2011-10-05 11:44:49 -07003059 private boolean updateGlobalSearchIcon() {
3060 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003061 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003062 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003063 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003064 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003065
Winson Chung1cad91e2011-05-25 17:41:01 -07003066 final SearchManager searchManager =
3067 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3068 ComponentName activityName = searchManager.getGlobalSearchActivity();
3069 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003070 int coi = getCurrentOrientationIndexForGlobalIcons();
3071 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003072 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3073 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3074 if (sGlobalSearchIcon[coi] == null) {
3075 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3076 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3077 TOOLBAR_ICON_METADATA_NAME);
3078 }
3079
Winson Chungc51db6a2011-10-05 11:44:49 -07003080 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3081 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003082 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003083 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003084 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003085 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003086 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3087 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3088 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003089 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003090 if (voiceButtonProxy != null) {
3091 voiceButtonProxy.setVisibility(View.GONE);
3092 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003093 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003094 }
3095 }
3096
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003097 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003098 final View searchButtonContainer = findViewById(R.id.search_button_container);
3099 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003100 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003101 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003102 }
3103
Winson Chungc51db6a2011-10-05 11:44:49 -07003104 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003105 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003106 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003107 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003108
Winson Chungc51db6a2011-10-05 11:44:49 -07003109 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003110 final SearchManager searchManager =
3111 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3112 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3113
3114 ComponentName activityName = null;
3115 if (globalSearchActivity != null) {
3116 // Check if the global search activity handles voice search
3117 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3118 intent.setPackage(globalSearchActivity.getPackageName());
3119 activityName = intent.resolveActivity(getPackageManager());
3120 }
3121
3122 if (activityName == null) {
3123 // Fallback: check if an activity other than the global search activity
3124 // resolves this
3125 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3126 activityName = intent.resolveActivity(getPackageManager());
3127 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003128 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003129 int coi = getCurrentOrientationIndexForGlobalIcons();
3130 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003131 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3132 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3133 if (sVoiceSearchIcon[coi] == null) {
3134 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3135 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3136 TOOLBAR_ICON_METADATA_NAME);
3137 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003138 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003139 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003140 if (voiceButtonProxy != null) {
3141 voiceButtonProxy.setVisibility(View.VISIBLE);
3142 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003143 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003144 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003145 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003146 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003147 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003148 if (voiceButtonProxy != null) {
3149 voiceButtonProxy.setVisibility(View.GONE);
3150 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003151 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003152 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003153 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003154
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003155 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003156 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3157 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003158 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003159 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003160 }
3161
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003162 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003163 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003164 */
3165 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003166 final View marketButton = findViewById(R.id.market_button);
3167 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3168 // Find the app market activity by resolving an intent.
3169 // (If multiple app markets are installed, it will return the ResolverActivity.)
3170 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003171 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003172 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003173 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003174 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003175 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3176 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003177 marketButton.setVisibility(View.VISIBLE);
3178 } else {
3179 // We should hide and disable the view so that we don't try and restore the visibility
3180 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3181 marketButton.setVisibility(View.GONE);
3182 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003183 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003184 }
3185
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003186 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003187 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3188 Resources r = getResources();
3189 Drawable marketIconDrawable = d.newDrawable(r);
3190 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3191 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3192 marketIconDrawable.setBounds(0, 0, w, h);
3193
3194 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003195 }
3196
Michael Jurkad7c28052012-04-27 15:43:36 -07003197 @Override
3198 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003199 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003200 final List<CharSequence> text = event.getText();
3201 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003202 // Populate event with a fake title based on the current state.
3203 if (mState == State.APPS_CUSTOMIZE) {
3204 text.add(getString(R.string.all_apps_button_label));
3205 } else {
3206 text.add(getString(R.string.all_apps_home_button_label));
3207 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003208 return result;
3209 }
3210
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003211 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003212 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003213 */
3214 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3215 @Override
3216 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003217 closeSystemDialogs();
3218 }
3219 }
3220
3221 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003222 * Receives notifications whenever the appwidgets are reset.
3223 */
3224 private class AppWidgetResetObserver extends ContentObserver {
3225 public AppWidgetResetObserver() {
3226 super(new Handler());
3227 }
3228
3229 @Override
3230 public void onChange(boolean selfChange) {
3231 onAppWidgetReset();
3232 }
3233 }
3234
3235 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003236 * If the activity is currently paused, signal that we need to re-run the loader
3237 * in onResume.
3238 *
3239 * This needs to be called from incoming places where resources might have been loaded
3240 * while we are paused. That is becaues the Configuration might be wrong
3241 * when we're not running, and if it comes back to what it was when we
3242 * were paused, we are not restarted.
3243 *
3244 * Implementation of the method from LauncherModel.Callbacks.
3245 *
3246 * @return true if we are currently paused. The caller might be able to
3247 * skip some work in that case since we will come back again.
3248 */
3249 public boolean setLoadOnResume() {
3250 if (mPaused) {
3251 Log.i(TAG, "setLoadOnResume");
3252 mOnResumeNeedsLoad = true;
3253 return true;
3254 } else {
3255 return false;
3256 }
3257 }
3258
3259 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003260 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003263 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003264 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003265 } else {
3266 return SCREEN_COUNT / 2;
3267 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003269
Joe Onorato9c1289c2009-08-17 11:03:03 -04003270 /**
3271 * Refreshes the shortcuts shown on the workspace.
3272 *
3273 * Implementation of the method from LauncherModel.Callbacks.
3274 */
3275 public void startBinding() {
3276 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003277
Winson Chungf0c6ae02012-03-21 16:10:31 -07003278 mNewShortcutAnimatePage = -1;
3279 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003280 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 int count = workspace.getChildCount();
3282 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003283 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003284 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3285 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003286 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003287 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003288 if (mHotseat != null) {
3289 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 }
3291 }
3292
3293 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003294 * Bind the items start-end from the list.
3295 *
3296 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003299 setLoadOnResume();
3300
Winson Chungf0c6ae02012-03-21 16:10:31 -07003301 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3302 Set<String> newApps = new HashSet<String>();
3303 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3304
3305 Workspace workspace = mWorkspace;
3306 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003308
3309 // Short circuit if we are loading dock items for a configuration which has no dock
3310 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3311 mHotseat == null) {
3312 continue;
3313 }
3314
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 switch (item.itemType) {
3316 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3317 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003318 ShortcutInfo info = (ShortcutInfo) item;
3319 String uri = info.intent.toUri(0).toString();
3320 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003321 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3322 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003323 boolean animateIconUp = false;
3324 synchronized (newApps) {
3325 if (newApps.contains(uri)) {
3326 animateIconUp = newApps.remove(uri);
3327 }
3328 }
3329 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003330 // Prepare the view to be animated up
3331 shortcut.setAlpha(0f);
3332 shortcut.setScaleX(0f);
3333 shortcut.setScaleY(0f);
3334 mNewShortcutAnimatePage = item.screen;
3335 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3336 mNewShortcutAnimateViews.add(shortcut);
3337 }
3338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003340 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003341 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003342 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003343 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003344 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3345 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003346 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003348 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003349
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003351 }
3352
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353 /**
3354 * Implementation of the method from LauncherModel.Callbacks.
3355 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003356 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003357 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003358 sFolders.clear();
3359 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361
3362 /**
3363 * Add the views for a widget to the workspace.
3364 *
3365 * Implementation of the method from LauncherModel.Callbacks.
3366 */
3367 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003368 setLoadOnResume();
3369
Daniel Sandler843e8602010-06-07 14:59:01 -04003370 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3371 if (DEBUG_WIDGETS) {
3372 Log.d(TAG, "bindAppWidget: " + item);
3373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 final Workspace workspace = mWorkspace;
3375
3376 final int appWidgetId = item.appWidgetId;
3377 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003378 if (DEBUG_WIDGETS) {
3379 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3380 }
3381
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3383
Joe Onorato9c1289c2009-08-17 11:03:03 -04003384 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003385 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386
Winson Chung3d503fb2011-07-13 17:25:49 -07003387 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003389 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 workspace.requestLayout();
3392
Daniel Sandler843e8602010-06-07 14:59:01 -04003393 if (DEBUG_WIDGETS) {
3394 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3395 + (SystemClock.uptimeMillis()-start) + "ms");
3396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003397 }
3398
Adam Cohen1462de32012-07-24 22:34:36 -07003399 public void onPageBoundSynchronously(int page) {
3400 mSynchronouslyBoundPages.add(page);
3401 }
3402
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 /**
3404 * Callback saying that there aren't any more items to bind.
3405 *
3406 * Implementation of the method from LauncherModel.Callbacks.
3407 */
3408 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003409 setLoadOnResume();
3410
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 if (mSavedState != null) {
3412 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003413 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 mSavedState = null;
3416 }
3417
Adam Cohen1462de32012-07-24 22:34:36 -07003418 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003420 // If we received the result of any pending adds while the loader was running (e.g. the
3421 // widget configuration forced an orientation change), process them now.
3422 for (int i = 0; i < sPendingAddList.size(); i++) {
3423 completeAdd(sPendingAddList.get(i));
3424 }
3425 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426
Winson Chungc51db6a2011-10-05 11:44:49 -07003427 // Update the market app icon as necessary (the other icons will be managed in response to
3428 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003429 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003430
Winson Chungf0c6ae02012-03-21 16:10:31 -07003431 // Animate up any icons as necessary
3432 if (mVisible || mWorkspaceLoading) {
3433 Runnable newAppsRunnable = new Runnable() {
3434 @Override
3435 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003436 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003437 }
3438 };
Winson Chunga2413752012-04-03 14:22:34 -07003439
3440 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3441 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3442 if (canRunNewAppsAnimation()) {
3443 // If the user has not interacted recently, then either snap to the new page to show
3444 // the new-apps animation or just run them if they are to appear on the current page
3445 if (willSnapPage) {
3446 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3447 } else {
3448 runNewAppsAnimation(false);
3449 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003450 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003451 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003452 // are on another page (or just normally if they are added to the current page)
3453 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003454 }
3455 }
3456
3457 mWorkspaceLoading = false;
3458 }
3459
Winson Chunga2413752012-04-03 14:22:34 -07003460 private boolean canRunNewAppsAnimation() {
3461 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3462 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3463 }
3464
Winson Chungf0c6ae02012-03-21 16:10:31 -07003465 /**
3466 * Runs a new animation that scales up icons that were added while Launcher was in the
3467 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003468 *
3469 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003470 */
Winson Chunga2413752012-04-03 14:22:34 -07003471 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003472 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003473 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003474
3475 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003476 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3477 @Override
3478 public int compare(View a, View b) {
3479 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3480 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3481 int cellCountX = LauncherModel.getCellCountX();
3482 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3483 }
3484 });
Winson Chunga2413752012-04-03 14:22:34 -07003485
3486 // Animate each of the views in place (or show them immediately if requested)
3487 if (immediate) {
3488 for (View v : mNewShortcutAnimateViews) {
3489 v.setAlpha(1f);
3490 v.setScaleX(1f);
3491 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003492 }
Winson Chunga2413752012-04-03 14:22:34 -07003493 } else {
3494 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3495 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003496 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003497 PropertyValuesHolder.ofFloat("alpha", 1f),
3498 PropertyValuesHolder.ofFloat("scaleX", 1f),
3499 PropertyValuesHolder.ofFloat("scaleY", 1f));
3500 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3501 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3502 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3503 bounceAnims.add(bounceAnim);
3504 }
3505 anim.playTogether(bounceAnims);
3506 anim.addListener(new AnimatorListenerAdapter() {
3507 @Override
3508 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003509 if (mWorkspace != null) {
3510 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3511 }
Winson Chunga2413752012-04-03 14:22:34 -07003512 }
3513 });
3514 anim.start();
3515 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003516
3517 // Clean up
3518 mNewShortcutAnimatePage = -1;
3519 mNewShortcutAnimateViews.clear();
3520 new Thread("clearNewAppsThread") {
3521 public void run() {
3522 mSharedPrefs.edit()
3523 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3524 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3525 .commit();
3526 }
3527 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003528 }
3529
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003530 @Override
3531 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003532 boolean searchVisible = updateGlobalSearchIcon();
3533 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003534 if (mSearchDropTargetBar != null) {
3535 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3536 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003537 }
3538
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003539 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 * Add the icons for all apps.
3541 *
3542 * Implementation of the method from LauncherModel.Callbacks.
3543 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003544 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003545 Runnable setAllAppsRunnable = new Runnable() {
3546 public void run() {
3547 if (mAppsCustomizeContent != null) {
3548 mAppsCustomizeContent.setApps(apps);
3549 }
3550 }
3551 };
3552
Michael Jurkac57b7a82011-08-09 22:02:20 -07003553 // Remove the progress bar entirely; we could also make it GONE
3554 // but better to remove it since we know it's not going to be used
3555 View progressBar = mAppsCustomizeTabHost.
3556 findViewById(R.id.apps_customize_progress_bar);
3557 if (progressBar != null) {
3558 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003559
3560 // We just post the call to setApps so the user sees the progress bar
3561 // disappear-- otherwise, it just looks like the progress bar froze
3562 // which doesn't look great
3563 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3564 } else {
3565 // If we did not initialize the spinner in onCreate, then we can directly set the
3566 // list of applications without waiting for any progress bars views to be hidden.
3567 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003568 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 }
3570
3571 /**
3572 * A package was installed.
3573 *
3574 * Implementation of the method from LauncherModel.Callbacks.
3575 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003576 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003577 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003578
Winson Chung785d2eb2011-04-14 16:08:02 -07003579 if (mAppsCustomizeContent != null) {
3580 mAppsCustomizeContent.addApps(apps);
3581 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 }
3583
3584 /**
3585 * A package was updated.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003589 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003590 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003591 if (mWorkspace != null) {
3592 mWorkspace.updateShortcuts(apps);
3593 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003594
Winson Chung785d2eb2011-04-14 16:08:02 -07003595 if (mAppsCustomizeContent != null) {
3596 mAppsCustomizeContent.updateApps(apps);
3597 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 }
3599
3600 /**
3601 * A package was uninstalled.
3602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 */
Winson Chungcd810732012-06-18 16:45:43 -07003605 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003606 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003607 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003608 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003609
Winson Chung785d2eb2011-04-14 16:08:02 -07003610 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003611 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003612 }
Winson Chunga1820962011-10-03 16:31:06 -07003613
3614 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003615 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 }
Joe Onoratobe386092009-11-17 17:32:16 -08003617
3618 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003619 * A number of packages were updated.
3620 */
3621 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003622 if (mAppsCustomizeContent != null) {
3623 mAppsCustomizeContent.onPackagesUpdated();
3624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003625 }
3626
Winson Chung400438b2011-01-16 17:53:48 -08003627 private int mapConfigurationOriActivityInfoOri(int configOri) {
3628 final Display d = getWindowManager().getDefaultDisplay();
3629 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3630 switch (d.getRotation()) {
3631 case Surface.ROTATION_0:
3632 case Surface.ROTATION_180:
3633 // We are currently in the same basic orientation as the natural orientation
3634 naturalOri = configOri;
3635 break;
3636 case Surface.ROTATION_90:
3637 case Surface.ROTATION_270:
3638 // We are currently in the other basic orientation to the natural orientation
3639 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3640 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3641 break;
3642 }
3643
3644 int[] oriMap = {
3645 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3646 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3647 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3648 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3649 };
3650 // Since the map starts at portrait, we need to offset if this device's natural orientation
3651 // is landscape.
3652 int indexOffset = 0;
3653 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3654 indexOffset = 1;
3655 }
3656 return oriMap[(d.getRotation() + indexOffset) % 4];
3657 }
Adam Cohen446e9402011-09-15 18:21:21 -07003658
Winson Chung4b919f82012-05-01 10:44:08 -07003659 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003660 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003661 getResources().getBoolean(R.bool.allow_rotation);
3662 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003663 }
Winson Chung4b919f82012-05-01 10:44:08 -07003664 public void lockScreenOrientation() {
3665 if (isRotationEnabled()) {
3666 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3667 .getConfiguration().orientation));
3668 }
3669 }
3670 public void unlockScreenOrientation(boolean immediate) {
3671 if (isRotationEnabled()) {
3672 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003673 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003674 } else {
3675 mHandler.postDelayed(new Runnable() {
3676 public void run() {
3677 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3678 }
3679 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003680 }
Winson Chung4b919f82012-05-01 10:44:08 -07003681 }
Winson Chung400438b2011-01-16 17:53:48 -08003682 }
3683
Winson Chung82f55532011-08-09 14:14:23 -07003684 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003685 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003686 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003687 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003688
Winson Chung7d7541e2011-09-16 20:14:36 -07003689 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003690 }
Michael Jurka22143132012-10-04 17:42:38 +02003691
Winson Chung7d7541e2011-09-16 20:14:36 -07003692 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003693 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003694 if (cling != null) {
3695 cling.init(this, positionData);
3696 cling.setVisibility(View.VISIBLE);
3697 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3698 if (animate) {
3699 cling.buildLayer();
3700 cling.setAlpha(0f);
3701 cling.animate()
3702 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003703 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003704 .setDuration(SHOW_CLING_DURATION)
3705 .setStartDelay(delay)
3706 .start();
3707 } else {
3708 cling.setAlpha(1f);
3709 }
Michael Jurka22143132012-10-04 17:42:38 +02003710 cling.setFocusableInTouchMode(true);
3711 cling.post(new Runnable() {
3712 public void run() {
3713 cling.setFocusable(true);
3714 cling.requestFocus();
3715 }
3716 });
3717 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3718 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003719 }
3720 return cling;
3721 }
Michael Jurka22143132012-10-04 17:42:38 +02003722
Winson Chung7d7541e2011-09-16 20:14:36 -07003723 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003724 // To catch cases where siblings of top-level views are made invisible, just check whether
3725 // the cling is directly set to GONE before dismissing it.
3726 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003727 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003728 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003729 anim.addListener(new AnimatorListenerAdapter() {
3730 public void onAnimationEnd(Animator animation) {
3731 cling.setVisibility(View.GONE);
3732 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003733 // We should update the shared preferences on a background thread
3734 new Thread("dismissClingThread") {
3735 public void run() {
3736 SharedPreferences.Editor editor = mSharedPrefs.edit();
3737 editor.putBoolean(flag, true);
3738 editor.commit();
3739 }
3740 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003741 };
3742 });
3743 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003744 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003745 }
3746 }
Michael Jurka22143132012-10-04 17:42:38 +02003747
Winson Chung9d9d74f2011-09-19 11:49:12 -07003748 private void removeCling(int id) {
3749 final View cling = findViewById(id);
3750 if (cling != null) {
3751 final ViewGroup parent = (ViewGroup) cling.getParent();
3752 parent.post(new Runnable() {
3753 @Override
3754 public void run() {
3755 parent.removeView(cling);
3756 }
3757 });
Michael Jurka22143132012-10-04 17:42:38 +02003758 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003759 }
3760 }
Michael Jurka974c3862012-05-22 22:00:31 -07003761
3762 private boolean skipCustomClingIfNoAccounts() {
3763 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3764 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3765 if (customCling) {
3766 AccountManager am = AccountManager.get(this);
3767 Account[] accounts = am.getAccountsByType("com.google");
3768 return accounts.length == 0;
3769 }
3770 return false;
3771 }
3772
Winson Chung7d7541e2011-09-16 20:14:36 -07003773 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003774 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003775 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003776 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3777 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02003778 // If we're not using the default workspace layout, replace workspace cling
3779 // with a custom workspace cling (usually specified in an overlay)
3780 // For now, only do this on tablets
3781 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02003782 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02003783 // Use a custom cling
3784 View cling = findViewById(R.id.workspace_cling);
3785 ViewGroup clingParent = (ViewGroup) cling.getParent();
3786 int clingIndex = clingParent.indexOfChild(cling);
3787 clingParent.removeViewAt(clingIndex);
3788 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3789 clingParent.addView(customCling, clingIndex);
3790 customCling.setId(R.id.workspace_cling);
3791 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003792 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003793 } else {
3794 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003795 }
3796 }
3797 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003798 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003799 if (isClingsEnabled() &&
3800 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003801 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003802 } else {
3803 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003804 }
3805 }
3806 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003807 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003808 if (isClingsEnabled() &&
3809 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3810 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003811 } else {
3812 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003813 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003814 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003815 }
3816 public boolean isFolderClingVisible() {
3817 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003818 if (cling != null) {
3819 return cling.getVisibility() == View.VISIBLE;
3820 }
3821 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003822 }
Winson Chung82f55532011-08-09 14:14:23 -07003823 public void dismissWorkspaceCling(View v) {
3824 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003825 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003826 }
3827 public void dismissAllAppsCling(View v) {
3828 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003829 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3830 }
3831 public void dismissFolderCling(View v) {
3832 Cling cling = (Cling) findViewById(R.id.folder_cling);
3833 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003834 }
3835
Winson Chung80baf5a2010-08-09 16:03:15 -07003836 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003837 * Prints out out state for debugging.
3838 */
3839 public void dumpState() {
3840 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003841 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003842 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3843 Log.d(TAG, "mRestoring=" + mRestoring);
3844 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3845 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003846 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003847 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003848
Winson Chung785d2eb2011-04-14 16:08:02 -07003849 if (mAppsCustomizeContent != null) {
3850 mAppsCustomizeContent.dumpState();
3851 }
Joe Onoratobe386092009-11-17 17:32:16 -08003852 Log.d(TAG, "END launcher2 dump state");
3853 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003854
3855 @Override
3856 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3857 super.dump(prefix, fd, writer, args);
3858 writer.println(" ");
3859 writer.println("Debug logs: ");
3860 for (int i = 0; i < sDumpLogs.size(); i++) {
3861 writer.println(" " + sDumpLogs.get(i));
3862 }
3863 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003864
3865 public static void dumpDebugLogsToConsole() {
3866 Log.d(TAG, "");
3867 Log.d(TAG, "*********************");
3868 Log.d(TAG, "Launcher debug logs: ");
3869 for (int i = 0; i < sDumpLogs.size(); i++) {
3870 Log.d(TAG, " " + sDumpLogs.get(i));
3871 }
3872 Log.d(TAG, "*********************");
3873 Log.d(TAG, "");
3874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003875}
Michael Jurka2763be32011-02-24 11:19:57 -08003876
Michael Jurkaabded662011-03-04 12:06:57 -08003877interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003878 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003879 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003880 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003881 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003882 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003883}