blob: 7437eacffe67fcdc0079d6cb74f6d9e8aafe6a90 [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;
Michael Jurkae233a8b2013-03-19 13:49:20 +010066import android.os.UserManager;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080068import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080081import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070084import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080085import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070090import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070094import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070095import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Adam Cohendf2cc412011-04-27 16:56:57 -070098import com.android.common.Search;
99import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -0700100import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
110import java.util.Collections;
111import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Michael Jurka946ad472010-07-09 18:05:18 -0700120public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
Winson Chung70d72102011-08-12 11:24:35 -0700130 private static final int MENU_GROUP_WALLPAPER = 1;
131 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
132 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700133 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
134 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 private static final int REQUEST_PICK_APPLICATION = 6;
139 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Michael Jurka8b805b12012-04-18 14:23:14 -0700143 private static final int REQUEST_BIND_APPWIDGET = 11;
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
146
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800147 static final int SCREEN_COUNT = 5;
148 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800151 // To turn on these properties, type
152 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
153 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
154 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Winson Chung2672ff92012-05-04 16:22:30 -0700156 // The Intent extra that defines whether to ignore the launch animation
157 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
158 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: int
161 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700162 // Type: int
163 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700165 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
166 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
168 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700169 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: boolean
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
174 // Type: long
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700176 // Type: int
177 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
178 // Type: int
179 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
180 // Type: parcelable
181 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700183 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700184 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
185 "com.android.launcher.toolbar_search_icon";
186 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
187 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700188
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700190 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700191 private State mState = State.WORKSPACE;
192 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800193 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700196 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
197 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700198 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700199 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800202 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Winson Chunga2413752012-04-03 14:22:34 -0700204 // How long to wait before the new-shortcut animation automatically pans the workspace
205 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
206
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800207 private final BroadcastReceiver mCloseSystemDialogsReceiver
208 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800209 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private LayoutInflater mInflater;
212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800214 private View mQsbDivider;
215 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700216 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800217 private DragLayer mDragLayer;
218 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700219
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700220 private AppWidgetManager mAppWidgetManager;
221 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700222
Michael Jurkac9d95c52011-08-29 14:03:34 -0700223 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700224 private AppWidgetProviderInfo mPendingAddWidgetInfo;
225
Michael Jurka0280c3b2010-09-17 15:00:07 -0700226 private int[] mTmpAddItemCellCoordinates = new int[2];
227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private FolderInfo mFolderInfo;
229
Winson Chung3d503fb2011-07-13 17:25:49 -0700230 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800231 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700232
Winson Chungc51db6a2011-10-05 11:44:49 -0700233 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700234 private AppsCustomizeTabHost mAppsCustomizeTabHost;
235 private AppsCustomizePagedView mAppsCustomizeContent;
236 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700239 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
240 // scroll issues (because the workspace may not have been measured yet) and extra work.
241 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
242 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
244 private SpannableStringBuilder mDefaultKeySsb = null;
245
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 private boolean mWorkspaceLoading = true;
247
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800248 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private boolean mRestoring;
250 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700251 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252
Michael Jurka7607c2f2013-04-03 14:33:19 -0700253 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
254
Winson Chung4a2afa32012-07-19 14:53:05 -0700255 // Keep track of whether the user has left launcher
256 private static boolean sPausedFromUserAction = false;
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private Bundle mSavedInstanceState;
259
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800261 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800262 private boolean mUserPresent = true;
263 private boolean mVisible = false;
264 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700266 private static LocaleConfiguration sLocaleConfiguration = null;
267
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700268 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700269
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700270 private Intent mAppMarketIntent = null;
271
Adam Cohended9f8d2010-11-03 13:25:16 -0700272 // Related to the auto-advancing of widgets
273 private final int ADVANCE_MSG = 1;
274 private final int mAdvanceInterval = 20000;
275 private final int mAdvanceStagger = 250;
276 private long mAutoAdvanceSentTime;
277 private long mAutoAdvanceTimeLeft = -1;
278 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
279 new HashMap<View, AppWidgetProviderInfo>();
280
Winson Chung400438b2011-01-16 17:53:48 -0800281 // Determines how long to wait after a rotation before restoring the screen orientation to
282 // match the sensor state.
283 private final int mRestoreScreenOrientationDelay = 500;
284
Michael Jurka4ef207b2010-11-29 17:05:45 -0800285 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700286 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
287 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
288 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800289
Craig Mautner360310b2012-10-26 15:13:08 -0700290 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700291
Adam Cohen1462de32012-07-24 22:34:36 -0700292 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
293
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700294 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
295
Winson Chung46353de2012-02-16 14:05:10 -0800296 // We only want to get the SharedPreferences once since it does an FS stat each time we get
297 // it from the context.
298 private SharedPreferences mSharedPrefs;
299
Winson Chungf0c6ae02012-03-21 16:10:31 -0700300 // Holds the page that we need to animate to, and the icon views that we need to animate up
301 // when we scroll to that page on resume.
302 private int mNewShortcutAnimatePage = -1;
303 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700304 private ImageView mFolderIconImageView;
305 private Bitmap mFolderIconBitmap;
306 private Canvas mFolderIconCanvas;
307 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700308
Michael Jurkaddd62e92011-02-16 17:49:14 -0800309 private BubbleTextView mWaitingForResume;
310
Michael Jurka22143132012-10-04 17:42:38 +0200311 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
312 = new HideFromAccessibilityHelper();
313
Michael Jurkac1f5d262011-09-30 19:32:27 -0700314 private Runnable mBuildLayersRunnable = new Runnable() {
315 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700316 if (mWorkspace != null) {
317 mWorkspace.buildPageHardwareLayers();
318 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700319 }
320 };
321
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800322 private static ArrayList<PendingAddArguments> sPendingAddList
323 = new ArrayList<PendingAddArguments>();
324
Michael Jurka0a457bf2012-11-19 14:05:05 -0800325 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
326
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800327 private static class PendingAddArguments {
328 int requestCode;
329 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700330 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800331 int screen;
332 int cellX;
333 int cellY;
334 }
335
Michael Jurka0a457bf2012-11-19 14:05:05 -0800336 private static boolean isPropertyEnabled(String propertyName) {
337 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700338 }
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 @Override
341 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700342 if (DEBUG_STRICT_MODE) {
343 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
344 .detectDiskReads()
345 .detectDiskWrites()
346 .detectNetwork() // or .detectAll() for all detectable problems
347 .penaltyLog()
348 .build());
349 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
350 .detectLeakedSqlLiteObjects()
351 .detectLeakedClosableObjects()
352 .penaltyLog()
353 .penaltyDeath()
354 .build());
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700359 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
360 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800361 mModel = app.setLauncher(this);
362 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400363 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700367 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
368 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700369
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700370 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
371 // this also ensures that any synchronous binding below doesn't re-trigger another
372 // LauncherModel load.
373 mPaused = false;
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200376 android.os.Debug.startMethodTracing(
377 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
379
380 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setContentView(R.layout.launcher);
382 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700383 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800385 registerContentObservers();
386
Joe Onorato7c312c12009-08-13 21:36:53 -0700387 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 mSavedState = savedInstanceState;
390 restoreState(mSavedState);
391
Winson Chunga12a2502010-12-20 14:41:35 -0800392 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700393 if (mAppsCustomizeContent != null) {
394 mAppsCustomizeContent.onPackagesUpdated();
395 }
Winson Chunga12a2502010-12-20 14:41:35 -0800396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 if (PROFILE_STARTUP) {
398 android.os.Debug.stopMethodTracing();
399 }
400
401 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700402 if (sPausedFromUserAction) {
403 // If the user leaves launcher, then we should just load items asynchronously when
404 // they return.
405 mModel.startLoader(true, -1);
406 } else {
407 // We only load the page synchronously if the user rotates (or triggers a
408 // configuration change) while launcher is in the foreground
409 mModel.startLoader(true, mWorkspace.getCurrentPage());
410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
Michael Jurkac57b7a82011-08-09 22:02:20 -0700413 if (!mModel.isAllAppsLoaded()) {
414 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
415 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
416 }
417
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 // For handling default keys
419 mDefaultKeySsb = new SpannableStringBuilder();
420 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800421
422 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
423 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800424
Adam Cohenf9426d52012-06-04 17:26:21 -0700425 updateGlobalIcons();
426
427 // On large interfaces, we want the screen to auto-rotate based on the current orientation
428 unlockScreenOrientation(true);
429 }
430
Winson Chung4a2afa32012-07-19 14:53:05 -0700431 protected void onUserLeaveHint() {
432 super.onUserLeaveHint();
433 sPausedFromUserAction = true;
434 }
435
Adam Cohenf9426d52012-06-04 17:26:21 -0700436 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700437 boolean searchVisible = false;
438 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800439 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700440 int coi = getCurrentOrientationIndexForGlobalIcons();
441 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
442 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700443 updateAppMarketIcon();
444 searchVisible = updateGlobalSearchIcon();
445 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sGlobalSearchIcon[coi] != null) {
448 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700449 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 if (sVoiceSearchIcon[coi] != null) {
452 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700453 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700454 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700455 if (sAppMarketIcon[coi] != null) {
456 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800457 }
Winson Chungadf0c182012-08-23 14:59:07 -0700458 if (mSearchDropTargetBar != null) {
459 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
Romain Guycbb89e42009-06-08 15:52:54 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700464 if (sLocaleConfiguration == null) {
465 new AsyncTask<Void, Void, LocaleConfiguration>() {
466 @Override
467 protected LocaleConfiguration doInBackground(Void... unused) {
468 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
469 readConfiguration(Launcher.this, localeConfiguration);
470 return localeConfiguration;
471 }
472
473 @Override
474 protected void onPostExecute(LocaleConfiguration result) {
475 sLocaleConfiguration = result;
476 checkForLocaleChange(); // recursive, but now with a locale configuration
477 }
478 }.execute();
479 return;
480 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final Configuration configuration = getResources().getConfiguration();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final String locale = configuration.locale.toString();
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mcc = configuration.mcc;
489
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 final int mnc = configuration.mnc;
492
Romain Guy84f296c2009-11-04 15:00:44 -0800493 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
Romain Guy84f296c2009-11-04 15:00:44 -0800495 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700496 sLocaleConfiguration.locale = locale;
497 sLocaleConfiguration.mcc = mcc;
498 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700499
Joe Onorato0589f0f2010-02-08 13:44:00 -0800500 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501
502 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
503 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700504 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700505 public void run() {
506 writeConfiguration(Launcher.this, localeConfiguration);
507 }
508 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700509 }
510 }
511
512 private static class LocaleConfiguration {
513 public String locale;
514 public int mcc = -1;
515 public int mnc = -1;
516 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700517
Romain Guy98d01652009-06-30 16:21:04 -0700518 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
519 DataInputStream in = null;
520 try {
521 in = new DataInputStream(context.openFileInput(PREFERENCES));
522 configuration.locale = in.readUTF();
523 configuration.mcc = in.readInt();
524 configuration.mnc = in.readInt();
525 } catch (FileNotFoundException e) {
526 // Ignore
527 } catch (IOException e) {
528 // Ignore
529 } finally {
530 if (in != null) {
531 try {
532 in.close();
533 } catch (IOException e) {
534 // Ignore
535 }
536 }
537 }
538 }
539
540 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
541 DataOutputStream out = null;
542 try {
543 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
544 out.writeUTF(configuration.locale);
545 out.writeInt(configuration.mcc);
546 out.writeInt(configuration.mnc);
547 out.flush();
548 } catch (FileNotFoundException e) {
549 // Ignore
550 } catch (IOException e) {
551 //noinspection ResultOfMethodCallIgnored
552 context.getFileStreamPath(PREFERENCES).delete();
553 } finally {
554 if (out != null) {
555 try {
556 out.close();
557 } catch (IOException e) {
558 // Ignore
559 }
560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 }
562 }
563
Adam Cohen716b51e2011-06-30 12:09:54 -0700564 public DragLayer getDragLayer() {
565 return mDragLayer;
566 }
567
Winson Chung36a62fe2012-05-06 18:04:42 -0700568 boolean isDraggingEnabled() {
569 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
570 // that is subsequently removed from the workspace in startBinding().
571 return !mModel.isLoadingWorkspace();
572 }
573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 static int getScreen() {
575 synchronized (sLock) {
576 return sScreen;
577 }
578 }
579
580 static void setScreen(int screen) {
581 synchronized (sLock) {
582 sScreen = screen;
583 }
584 }
585
Winson Chung557d6ed2011-07-08 15:34:52 -0700586 /**
587 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
588 * a configuration step, this allows the proper animations to run after other transitions.
589 */
590 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700591 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 switch (args.requestCode) {
593 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700594 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
595 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800596 break;
597 case REQUEST_PICK_SHORTCUT:
598 processShortcut(args.intent);
599 break;
600 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700601 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
602 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 result = true;
604 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 case REQUEST_CREATE_APPWIDGET:
606 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700608 result = true;
609 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_WALLPAPER:
611 // We just wanted the activity result here so we can clear mWaitingForResult
612 break;
613 }
Michael Jurka27614d22012-04-02 06:40:22 -0700614 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
615 // if you turned the screen off and then back while in All Apps, Launcher would not
616 // return to the workspace. Clearing mAddInfo.container here fixes this issue
617 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 }
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 protected void onActivityResult(
623 final int requestCode, final int resultCode, final Intent data) {
624 if (requestCode == REQUEST_BIND_APPWIDGET) {
625 int appWidgetId = data != null ?
626 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
627 if (resultCode == RESULT_CANCELED) {
628 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
629 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700630 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
632 return;
633 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700634 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
636 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700637 mWaitingForResult = false;
638
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 // We have special handling for widgets
640 if (isWidgetDrop) {
641 int appWidgetId = data != null ?
642 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700643 if (appWidgetId < 0) {
644 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
645 "widget configuration activity.");
646 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
647 } else {
648 completeTwoStageWidgetDrop(resultCode, appWidgetId);
649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 return;
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700658 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 final PendingAddArguments args = new PendingAddArguments();
660 args.requestCode = requestCode;
661 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 args.container = mPendingAddInfo.container;
663 args.screen = mPendingAddInfo.screen;
664 args.cellX = mPendingAddInfo.cellX;
665 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 if (isWorkspaceLocked()) {
667 sPendingAddList.add(args);
668 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
675 null);
676 }
677
678 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 Runnable onCompleteRunnable = null;
682 int animationType = 0;
683
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 if (resultCode == RESULT_OK) {
686 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
687 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700688 mPendingAddWidgetInfo);
689 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 onCompleteRunnable = new Runnable() {
691 @Override
692 public void run() {
693 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
694 mPendingAddInfo.screen, layout, null);
695 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
696 null);
697 }
698 };
699 } else if (resultCode == RESULT_CANCELED) {
700 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
705 null);
706 }
707 };
708 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700709 if (mDragLayer.getAnimatedView() != null) {
710 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
711 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
712 animationType, boundWidget, true);
713 } else {
714 // The animated view may be null in the case of a rotation during widget configuration
715 onCompleteRunnable.run();
716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
719 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700720 protected void onStop() {
721 super.onStop();
722 FirstFrameAnimatorHelper.setIsVisible(false);
723 }
724
725 @Override
726 protected void onStart() {
727 super.onStart();
728 FirstFrameAnimatorHelper.setIsVisible(true);
729 }
730
731 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 protected void onResume() {
733 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700734
Winson Chung4a2afa32012-07-19 14:53:05 -0700735 // Restore the previous launcher state
736 if (mOnResumeState == State.WORKSPACE) {
737 showWorkspace(false);
738 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
739 showAllApps(false);
740 }
741 mOnResumeState = State.NONE;
742
Craig Mautner360310b2012-10-26 15:13:08 -0700743 // Background was set to gradient in onPause(), restore to black if in all apps.
744 setWorkspaceBackground(mState == State.WORKSPACE);
745
Winson Chungde0fb8f2012-05-08 14:37:08 -0700746 // Process any items that were added while Launcher was away
747 InstallShortcutReceiver.flushInstallQueue(this);
748
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800749 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700750 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700751 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700753 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700755 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
Michael Jurka7607c2f2013-04-03 14:33:19 -0700757 // We might have postponed some bind calls until onResume (see waitUntilResume) --
758 // execute them here
759 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
760 mOnResumeCallbacks.get(i).run();
761 }
762 mOnResumeCallbacks.clear();
Winson Chunge4e50662012-01-23 14:45:13 -0800763
764 // Reset the pressed state of icons that were locked in the press state while activities
765 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800766 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800767 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800768 mWaitingForResume.setStayPressed(false);
769 }
Winson Chunge4e50662012-01-23 14:45:13 -0800770 if (mAppsCustomizeContent != null) {
771 // Resets the previous all apps icon press state
772 mAppsCustomizeContent.resetDrawableState();
773 }
Adam Cohen06dff352012-06-01 17:17:08 -0700774 // It is possible that widgets can receive updates while launcher is not in the foreground.
775 // Consequently, the widgets will be inflated in the orientation of the foreground activity
776 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
777 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700778 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700779
780 // Again, as with the above scenario, it's possible that one or more of the global icons
781 // were updated in the wrong orientation.
782 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700786 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700787 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
788 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
789 // when Launcher resumes and we are still in AllApps.
790 updateWallpaperVisibility(true);
791
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700792 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700793 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800794 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700795 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700796 }
Romain Guycbb89e42009-06-08 15:52:54 -0700797
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700798 @Override
799 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 // Flag the loader to stop early before switching
801 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700802 if (mAppsCustomizeContent != null) {
803 mAppsCustomizeContent.surrender();
804 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800805 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700806 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700807
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800808 // We can't hide the IME if it was forced open. So don't bother
809 /*
810 @Override
811 public void onWindowFocusChanged(boolean hasFocus) {
812 super.onWindowFocusChanged(hasFocus);
813
814 if (hasFocus) {
815 final InputMethodManager inputManager = (InputMethodManager)
816 getSystemService(Context.INPUT_METHOD_SERVICE);
817 WindowManager.LayoutParams lp = getWindow().getAttributes();
818 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
819 android.os.Handler()) {
820 protected void onReceiveResult(int resultCode, Bundle resultData) {
821 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
822 }
823 });
824 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
825 }
826 }
827 */
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 private boolean acceptFilter() {
830 final InputMethodManager inputManager = (InputMethodManager)
831 getSystemService(Context.INPUT_METHOD_SERVICE);
832 return !inputManager.isFullscreenMode();
833 }
834
835 @Override
836 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700837 final int uniChar = event.getUnicodeChar();
838 final boolean handled = super.onKeyDown(keyCode, event);
839 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
840 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
842 keyCode, event);
843 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700844 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700845 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700846 // showSearchDialog()
847 // If there are multiple keystrokes before the search dialog takes focus,
848 // onSearchRequested() will be called for every keystroke,
849 // but it is idempotent, so it's fine.
850 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
852 }
853
Joe Onorato8a9625e2010-01-28 15:55:35 -0800854 // Eat the long press event so the keyboard doesn't come up.
855 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
856 return true;
857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 return handled;
860 }
861
Karl Rosaen138a0412009-04-23 19:00:21 -0700862 private String getTypedText() {
863 return mDefaultKeySsb.toString();
864 }
865
866 private void clearTypedText() {
867 mDefaultKeySsb.clear();
868 mDefaultKeySsb.clearSpans();
869 Selection.setSelection(mDefaultKeySsb, 0);
870 }
871
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700873 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
874 * State
875 */
876 private static State intToState(int stateOrdinal) {
877 State state = State.WORKSPACE;
878 final State[] stateValues = State.values();
879 for (int i = 0; i < stateValues.length; i++) {
880 if (stateValues[i].ordinal() == stateOrdinal) {
881 state = stateValues[i];
882 break;
883 }
884 }
885 return state;
886 }
887
888 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 * Restores the previous state, if it exists.
890 *
891 * @param savedState The previous state.
892 */
893 private void restoreState(Bundle savedState) {
894 if (savedState == null) {
895 return;
896 }
897
Michael Jurka883f55b2010-10-21 15:47:14 -0700898 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700899 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700900 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800901 }
902
Winson Chungf0c6ae02012-03-21 16:10:31 -0700903 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700905 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
Winson Chung3d503fb2011-07-13 17:25:49 -0700908 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
909 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700910
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
912 mPendingAddInfo.container = pendingAddContainer;
913 mPendingAddInfo.screen = pendingAddScreen;
914 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
915 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
917 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
918 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700919 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 mRestoring = true;
921 }
922
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
925 if (renameFolder) {
926 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700927 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 mRestoring = true;
929 }
Winson Chunga12a2502010-12-20 14:41:35 -0800930
Winson Chung785d2eb2011-04-14 16:08:02 -0700931
932 // Restore the AppsCustomize tab
933 if (mAppsCustomizeTabHost != null) {
934 String curTab = savedState.getString("apps_customize_currentTab");
935 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700936 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700937 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700938 mAppsCustomizeContent.loadAssociatedPages(
939 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700940 }
941
Winson Chung5afbf7b2011-07-25 11:53:08 -0700942 int currentIndex = savedState.getInt("apps_customize_currentIndex");
943 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 }
946
947 /**
948 * Finds all the views we need and configure them properly.
949 */
950 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700951 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400952
Craig Mautner360310b2012-10-26 15:13:08 -0700953 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700954 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
955 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700956 mQsbDivider = findViewById(R.id.qsb_divider);
957 mDockDivider = findViewById(R.id.dock_divider);
958
959 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
960 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961
Winson Chung4c98d922011-05-31 16:50:48 -0700962 // Setup the drag layer
963 mDragLayer.setup(this, dragController);
964
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 // Setup the hotseat
966 mHotseat = (Hotseat) findViewById(R.id.hotseat);
967 if (mHotseat != null) {
968 mHotseat.setup(this);
969 }
970
Winson Chung4c98d922011-05-31 16:50:48 -0700971 // Setup the workspace
972 mWorkspace.setHapticFeedbackEnabled(false);
973 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700974 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700975 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700976
Winson Chungf0ea4d32011-06-06 14:27:16 -0700977 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700978 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700979
Winson Chungf0ea4d32011-06-06 14:27:16 -0700980 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -0700981 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700982 mAppsCustomizeContent = (AppsCustomizePagedView)
983 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
984 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -0700985
Winson Chung3d503fb2011-07-13 17:25:49 -0700986 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700987 dragController.setDragScoller(mWorkspace);
988 dragController.setScrollView(mDragLayer);
989 dragController.setMoveTarget(mWorkspace);
990 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700991 if (mSearchDropTargetBar != null) {
992 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
995
996 /**
997 * Creates a view representing a shortcut.
998 *
999 * @param info The data structure describing the shortcut.
1000 *
1001 * @return A View inflated from R.layout.application.
1002 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001003 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001005 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007
1008 /**
1009 * Creates a view representing a shortcut inflated from the specified resource.
1010 *
1011 * @param layoutResId The id of the XML layout used to create the shortcut.
1012 * @param parent The group the shortcut belongs to.
1013 * @param info The data structure describing the shortcut.
1014 *
1015 * @return A View inflated from layoutResId.
1016 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001017 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001018 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1019 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 return favorite;
1022 }
1023
1024 /**
1025 * Add an application shortcut to the workspace.
1026 *
1027 * @param data The intent describing the application.
1028 * @param cellInfo The position on screen where to create the shortcut.
1029 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001030 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001031 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001033
Adam Cohenfbba09b2011-07-18 21:43:05 -07001034 // First we check if we already know the exact location where we want to add this item.
1035 if (cellX >= 0 && cellY >= 0) {
1036 cellXY[0] = cellX;
1037 cellXY[1] = cellY;
1038 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001039 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001040 return;
1041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001043 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001044
Romain Guy73b979d2009-06-09 12:57:21 -07001045 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001046 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001048 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001049 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001050 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001051 } else {
1052 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054 }
Romain Guycbb89e42009-06-08 15:52:54 -07001055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 /**
1057 * Add a shortcut to the workspace.
1058 *
1059 * @param data The intent describing the shortcut.
1060 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001062 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1063 int cellY) {
1064 int[] cellXY = mTmpAddItemCellCoordinates;
1065 int[] touchXY = mPendingAddInfo.dropPos;
1066 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001067
Michael Jurkad3ef3062010-11-23 16:23:58 -08001068 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001069
Adam Cohen558baaf2011-08-15 15:22:57 -07001070 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001071 if (info == null) {
1072 return;
1073 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001074 final View view = createShortcut(info);
1075
Adam Cohenfbba09b2011-07-18 21:43:05 -07001076 // First we check if we already know the exact location where we want to add this item.
1077 if (cellX >= 0 && cellY >= 0) {
1078 cellXY[0] = cellX;
1079 cellXY[1] = cellY;
1080 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001081
1082 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001083 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001084 true, null,null)) {
1085 return;
1086 }
1087 DragObject dragObject = new DragObject();
1088 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001089 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1090 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001091 return;
1092 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001093 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001094 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001095 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001096 foundCellSpan = (result != null);
1097 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001098 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001099 }
1100
1101 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001102 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001103 return;
1104 }
1105
Adam Cohen558baaf2011-08-15 15:22:57 -07001106 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107
1108 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001109 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1110 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112 }
1113
Adam Cohen2f093b62012-04-30 18:59:53 -07001114 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1115 int minHeight) {
1116 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001117 // We want to account for the extra amount of padding that we are adding to the widget
1118 // to ensure that it gets the full amount of space that it has requested
1119 int requiredWidth = minWidth + padding.left + padding.right;
1120 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001121 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001122 }
1123
Adam Cohen2f093b62012-04-30 18:59:53 -07001124 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1125 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001126 }
1127
Adam Cohen2f093b62012-04-30 18:59:53 -07001128 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1129 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001130 }
1131
Adam Cohen2f093b62012-04-30 18:59:53 -07001132 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1133 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001134 }
1135
Adam Cohen2f093b62012-04-30 18:59:53 -07001136 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1137 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001138 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 }
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001142 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001144 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001145 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001147 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1148 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1149 if (appWidgetInfo == null) {
1150 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1151 }
Romain Guycbb89e42009-06-08 15:52:54 -07001152
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001153 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001155
Adam Cohen2f093b62012-04-30 18:59:53 -07001156 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1157 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001160 // We have saved the position to which the widget was dragged-- this really only matters
1161 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001162 int[] cellXY = mTmpAddItemCellCoordinates;
1163 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001166 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1167 cellXY[0] = mPendingAddInfo.cellX;
1168 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 spanXY[0] = mPendingAddInfo.spanX;
1170 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001171 foundCellSpan = true;
1172 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001173 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001174 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001175 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1176 spanXY[1], cellXY, finalSpan);
1177 spanXY[0] = finalSpan[0];
1178 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001179 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001181 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001182 }
1183
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001185 if (appWidgetId != -1) {
1186 // Deleting an app widget ID is a void call but writes to disk before returning
1187 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001188 new Thread("deleteAppWidgetId") {
1189 public void run() {
1190 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1191 }
1192 }.start();
1193 }
Winson Chung93eef082012-03-23 15:59:27 -07001194 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001195 return;
1196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001199 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1200 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001201 launcherInfo.spanX = spanXY[0];
1202 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1204 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001207 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208
1209 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001210 if (hostView == null) {
1211 // Perform actual inflation because we're live
1212 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1213 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1214 } else {
1215 // The AppWidgetHostView has already been inflated and instantiated
1216 launcherInfo.hostView = hostView;
1217 }
Romain Guycbb89e42009-06-08 15:52:54 -07001218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001220 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001221 launcherInfo.notifyWidgetSizeChanged(this);
1222
Winson Chung3d503fb2011-07-13 17:25:49 -07001223 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001225
1226 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001228 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
Romain Guycbb89e42009-06-08 15:52:54 -07001230
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1232 @Override
1233 public void onReceive(Context context, Intent intent) {
1234 final String action = intent.getAction();
1235 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1236 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001237 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001238 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001239
Winson Chungc100e8e2011-08-09 16:02:43 -07001240 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001241 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001242 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1243 mAppsCustomizeTabHost.reset();
1244 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001245 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1247 mUserPresent = true;
1248 updateRunning();
1249 }
1250 }
1251 };
1252
1253 @Override
1254 public void onAttachedToWindow() {
1255 super.onAttachedToWindow();
1256
1257 // Listen for broadcasts related to user-presence
1258 final IntentFilter filter = new IntentFilter();
1259 filter.addAction(Intent.ACTION_SCREEN_OFF);
1260 filter.addAction(Intent.ACTION_USER_PRESENT);
1261 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001262 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001263 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001264 mVisible = true;
1265 }
1266
1267 @Override
1268 public void onDetachedFromWindow() {
1269 super.onDetachedFromWindow();
1270 mVisible = false;
1271
Adam Cohend113e0c2010-11-11 10:48:05 -08001272 if (mAttached) {
1273 unregisterReceiver(mReceiver);
1274 mAttached = false;
1275 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001276 updateRunning();
1277 }
1278
1279 public void onWindowVisibilityChanged(int visibility) {
1280 mVisible = visibility == View.VISIBLE;
1281 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001282 // The following code used to be in onResume, but it turns out onResume is called when
1283 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1284 // is a more appropriate event to handle
1285 if (mVisible) {
1286 mAppsCustomizeTabHost.onWindowVisible();
1287 if (!mWorkspaceLoading) {
1288 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001289 // We want to let Launcher draw itself at least once before we force it to build
1290 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001291 // apps is nice and speedy.
1292 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001293 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001294 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001295 if (mStarted) return;
1296 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001297 // We delay the layer building a bit in order to give
1298 // other message processing a time to run. In particular
1299 // this avoids a delay in hiding the IME if it was
1300 // currently shown, because doing that may involve
1301 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001302 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001303 final ViewTreeObserver.OnDrawListener listener = this;
1304 mWorkspace.post(new Runnable() {
1305 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001306 if (mWorkspace != null &&
1307 mWorkspace.getViewTreeObserver() != null) {
1308 mWorkspace.getViewTreeObserver().
1309 removeOnDrawListener(listener);
1310 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001311 }
1312 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001313 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001314 }
1315 });
1316 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001317 // When Launcher comes back to foreground, a different Activity might be responsible for
1318 // the app market intent, so refresh the icon
1319 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001320 clearTypedText();
1321 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001322 }
1323
1324 private void sendAdvanceMessage(long delay) {
1325 mHandler.removeMessages(ADVANCE_MSG);
1326 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1327 mHandler.sendMessageDelayed(msg, delay);
1328 mAutoAdvanceSentTime = System.currentTimeMillis();
1329 }
1330
1331 private void updateRunning() {
1332 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1333 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1334 mAutoAdvanceRunning = autoAdvanceRunning;
1335 if (autoAdvanceRunning) {
1336 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1337 sendAdvanceMessage(delay);
1338 } else {
1339 if (!mWidgetsToAdvance.isEmpty()) {
1340 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1341 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1342 }
1343 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001344 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001345 }
1346 }
1347 }
1348
1349 private final Handler mHandler = new Handler() {
1350 @Override
1351 public void handleMessage(Message msg) {
1352 if (msg.what == ADVANCE_MSG) {
1353 int i = 0;
1354 for (View key: mWidgetsToAdvance.keySet()) {
1355 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1356 final int delay = mAdvanceStagger * i;
1357 if (v instanceof Advanceable) {
1358 postDelayed(new Runnable() {
1359 public void run() {
1360 ((Advanceable) v).advance();
1361 }
1362 }, delay);
1363 }
1364 i++;
1365 }
1366 sendAdvanceMessage(mAdvanceInterval);
1367 }
1368 }
1369 };
1370
1371 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001372 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001373 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1374 if (v instanceof Advanceable) {
1375 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001376 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001377 updateRunning();
1378 }
1379 }
1380
1381 void removeWidgetToAutoAdvance(View hostView) {
1382 if (mWidgetsToAdvance.containsKey(hostView)) {
1383 mWidgetsToAdvance.remove(hostView);
1384 updateRunning();
1385 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001386 }
1387
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001389 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 launcherInfo.hostView = null;
1391 }
1392
Winson Chung93eef082012-03-23 15:59:27 -07001393 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1394 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1395 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001396 }
1397
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001398 public LauncherAppWidgetHost getAppWidgetHost() {
1399 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
Romain Guycbb89e42009-06-08 15:52:54 -07001401
Winson Chunga9abd0e2010-10-27 17:18:37 -07001402 public LauncherModel getModel() {
1403 return mModel;
1404 }
1405
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001406 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001407 getWindow().closeAllPanels();
1408
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001409 // Whatever we were doing is hereby canceled.
1410 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001411 }
1412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 @Override
1414 protected void onNewIntent(Intent intent) {
1415 super.onNewIntent(intent);
1416
1417 // Close the menu
1418 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001419 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001420 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001421
Jamie Genniscb222e82012-10-23 15:44:26 -07001422 final boolean alreadyOnHome =
1423 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001424 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001425
Jamie Genniscb222e82012-10-23 15:44:26 -07001426 Runnable processIntent = new Runnable() {
1427 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001428 if (mWorkspace == null) {
1429 // Can be cases where mWorkspace is null, this prevents a NPE
1430 return;
1431 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001432 Folder openFolder = mWorkspace.getOpenFolder();
1433 // In all these cases, only animate if we're already on home
1434 mWorkspace.exitWidgetResizeMode();
1435 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1436 openFolder == null) {
1437 mWorkspace.moveToDefaultScreen(true);
1438 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001439
Jamie Genniscb222e82012-10-23 15:44:26 -07001440 closeFolder();
1441 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001442
Jamie Genniscb222e82012-10-23 15:44:26 -07001443 // If we are already on home, then just animate back to the workspace,
1444 // otherwise, just wait until onResume to set the state back to Workspace
1445 if (alreadyOnHome) {
1446 showWorkspace(true);
1447 } else {
1448 mOnResumeState = State.WORKSPACE;
1449 }
1450
1451 final View v = getWindow().peekDecorView();
1452 if (v != null && v.getWindowToken() != null) {
1453 InputMethodManager imm = (InputMethodManager)getSystemService(
1454 INPUT_METHOD_SERVICE);
1455 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1456 }
1457
1458 // Reset AllApps to its initial state
1459 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1460 mAppsCustomizeTabHost.reset();
1461 }
1462 }
1463 };
1464
1465 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1466 // Delay processing of the intent to allow the status bar animation to finish
1467 // first in order to avoid janky animations.
1468 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001469 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001470 // Process the intent immediately.
1471 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001472 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475 }
1476
1477 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001478 public void onRestoreInstanceState(Bundle state) {
1479 super.onRestoreInstanceState(state);
1480 for (int page: mSynchronouslyBoundPages) {
1481 mWorkspace.restoreInstanceStateForChild(page);
1482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
1484
1485 @Override
1486 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001487 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001488 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
Michael Jurka883f55b2010-10-21 15:47:14 -07001490 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001491 // We close any open folder since it will not be re-opened, and we need to make sure
1492 // this state is reflected.
1493 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
Winson Chung3d503fb2011-07-13 17:25:49 -07001495 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1496 mWaitingForResult) {
1497 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1498 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1499 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1500 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001501 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1502 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1503 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505
1506 if (mFolderInfo != null && mWaitingForResult) {
1507 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1508 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1509 }
Michael Jurka946ad472010-07-09 18:05:18 -07001510
Winson Chung785d2eb2011-04-14 16:08:02 -07001511 // Save the current AppsCustomize tab
1512 if (mAppsCustomizeTabHost != null) {
1513 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1514 if (currentTabTag != null) {
1515 outState.putString("apps_customize_currentTab", currentTabTag);
1516 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001517 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1518 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 }
1521
1522 @Override
1523 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Winson Chunge7a03942011-08-05 15:05:12 -07001526 // Remove all pending runnables
1527 mHandler.removeMessages(ADVANCE_MSG);
1528 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001529 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001530
Winson Chungcd2b0142011-06-08 16:02:26 -07001531 // Stop callbacks from LauncherModel
1532 LauncherApplication app = ((LauncherApplication) getApplication());
1533 mModel.stopLoader();
1534 app.setLauncher(null);
1535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001537 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001539 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001541 mAppWidgetHost = null;
1542
1543 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544
1545 TextKeyListener.getInstance().release();
1546
Winson Chung81b52252012-08-27 15:34:29 -07001547 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1548 // to prevent leaking Launcher activities on orientation change.
1549 if (mModel != null) {
1550 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1551 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001552
1553 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001554 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001555
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001556 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001557 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1558 mWorkspace.removeAllViews();
1559 mWorkspace = null;
1560 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001561
Michael Jurka2ecf9952012-06-18 12:52:28 -07001562 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564
Adam Cohena9cf38f2011-05-02 15:36:58 -07001565 public DragController getDragController() {
1566 return mDragController;
1567 }
1568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 @Override
1570 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001571 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 super.startActivityForResult(intent, requestCode);
1573 }
1574
Winson Chung70d72102011-08-12 11:24:35 -07001575 /**
1576 * Indicates that we want global search for this activity by setting the globalSearch
1577 * argument for {@link #startSearch} to true.
1578 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001580 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001582
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001583 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Karl Rosaen138a0412009-04-23 19:00:21 -07001585 if (initialQuery == null) {
1586 // Use any text typed in the launcher as the initial query
1587 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 if (appSearchData == null) {
1590 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001591 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
Winson Chungadf0c182012-08-23 14:59:07 -07001593 Rect sourceBounds = new Rect();
1594 if (mSearchDropTargetBar != null) {
1595 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1596 }
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Michael Jurkaa33411c2012-06-14 16:18:21 -07001598 startGlobalSearch(initialQuery, selectInitialQuery,
1599 appSearchData, sourceBounds);
1600 }
1601
1602 /**
1603 * Starts the global search activity. This code is a copied from SearchManager
1604 */
1605 public void startGlobalSearch(String initialQuery,
1606 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001607 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001608 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1609 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1610 if (globalSearchActivity == null) {
1611 Log.w(TAG, "No global search activity found.");
1612 return;
1613 }
1614 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1615 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1616 intent.setComponent(globalSearchActivity);
1617 // Make sure that we have a Bundle to put source in
1618 if (appSearchData == null) {
1619 appSearchData = new Bundle();
1620 } else {
1621 appSearchData = new Bundle(appSearchData);
1622 }
1623 // Set source to package name of app that starts global search, if not set already.
1624 if (!appSearchData.containsKey("source")) {
1625 appSearchData.putString("source", getPackageName());
1626 }
1627 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1628 if (!TextUtils.isEmpty(initialQuery)) {
1629 intent.putExtra(SearchManager.QUERY, initialQuery);
1630 }
1631 if (selectInitialQuery) {
1632 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1633 }
1634 intent.setSourceBounds(sourceBounds);
1635 try {
1636 startActivity(intent);
1637 } catch (ActivityNotFoundException ex) {
1638 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
1641
Winson Chung70d72102011-08-12 11:24:35 -07001642 @Override
1643 public boolean onCreateOptionsMenu(Menu menu) {
1644 if (isWorkspaceLocked()) {
1645 return false;
1646 }
1647
1648 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001649
1650 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1651 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1652 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001653 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1654 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1655 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001656 String helpUrl = getString(R.string.help_url);
1657 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001658 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1659 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1660
Winson Chung70d72102011-08-12 11:24:35 -07001661 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1662 .setIcon(android.R.drawable.ic_menu_gallery)
1663 .setAlphabeticShortcut('W');
1664 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1665 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001666 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001667 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001668 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1669 .setIcon(android.R.drawable.ic_menu_preferences)
1670 .setIntent(settings)
1671 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001672 if (!helpUrl.isEmpty()) {
1673 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1674 .setIcon(android.R.drawable.ic_menu_help)
1675 .setIntent(help)
1676 .setAlphabeticShortcut('H');
1677 }
Winson Chung70d72102011-08-12 11:24:35 -07001678 return true;
1679 }
1680
1681 @Override
1682 public boolean onPrepareOptionsMenu(Menu menu) {
1683 super.onPrepareOptionsMenu(menu);
1684
1685 if (mAppsCustomizeTabHost.isTransitioning()) {
1686 return false;
1687 }
1688 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1689 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1690
1691 return true;
1692 }
1693
1694 @Override
1695 public boolean onOptionsItemSelected(MenuItem item) {
1696 switch (item.getItemId()) {
1697 case MENU_WALLPAPER_SETTINGS:
1698 startWallpaper();
1699 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001700 }
1701
1702 return super.onOptionsItemSelected(item);
1703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001705 @Override
1706 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001707 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001708 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001709 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001710 }
1711
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 public boolean isWorkspaceLocked() {
1713 return mWorkspaceLoading || mWaitingForResult;
1714 }
1715
Michael Jurka0280c3b2010-09-17 15:00:07 -07001716 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001717 mPendingAddInfo.container = ItemInfo.NO_ID;
1718 mPendingAddInfo.screen = -1;
1719 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1720 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001721 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001723 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001724
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001725 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1726 AppWidgetProviderInfo appWidgetInfo) {
1727 if (appWidgetInfo.configure != null) {
1728 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001729
Bjorn Bringert7984c942009-12-09 15:38:25 +00001730 // Launch over to configure widget, if needed
1731 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001732 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001733 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001734 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001736 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001737 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1738 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001739 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001740 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
1742 }
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Adam Cohenfbba09b2011-07-18 21:43:05 -07001744 /**
1745 * Process a shortcut drop.
1746 *
1747 * @param componentName The name of the component
1748 * @param screen The screen where it should be added
1749 * @param cell The cell it should be added to, optional
1750 * @param position The location on the screen where it was dropped, optional
1751 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001752 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1753 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001754 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001755 mPendingAddInfo.container = container;
1756 mPendingAddInfo.screen = screen;
1757 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001758
1759 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001760 mPendingAddInfo.cellX = cell[0];
1761 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001762 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763
1764 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1765 createShortcutIntent.setComponent(componentName);
1766 processShortcut(createShortcutIntent);
1767 }
1768
Adam Cohenfbba09b2011-07-18 21:43:05 -07001769 /**
1770 * Process a widget drop.
1771 *
1772 * @param info The PendingAppWidgetInfo of the widget being added.
1773 * @param screen The screen where it should be added
1774 * @param cell The cell it should be added to, optional
1775 * @param position The location on the screen where it was dropped, optional
1776 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001777 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001778 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001779 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001780 mPendingAddInfo.container = info.container = container;
1781 mPendingAddInfo.screen = info.screen = screen;
1782 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001783 mPendingAddInfo.minSpanX = info.minSpanX;
1784 mPendingAddInfo.minSpanY = info.minSpanY;
1785
Adam Cohenfbba09b2011-07-18 21:43:05 -07001786 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001787 mPendingAddInfo.cellX = cell[0];
1788 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001789 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001790 if (span != null) {
1791 mPendingAddInfo.spanX = span[0];
1792 mPendingAddInfo.spanY = span[1];
1793 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001794
Adam Cohened66b2b2012-01-23 17:28:51 -08001795 AppWidgetHostView hostView = info.boundWidget;
1796 int appWidgetId;
1797 if (hostView != null) {
1798 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001799 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001800 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001801 // In this case, we either need to start an activity to get permission to bind
1802 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001803 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001804 Bundle options = info.bindOptions;
1805
1806 boolean success = false;
1807 if (options != null) {
1808 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1809 info.componentName, options);
1810 } else {
1811 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1812 info.componentName);
1813 }
1814 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001815 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001816 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001817 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001818 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1819 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1820 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001821 // TODO: we need to make sure that this accounts for the options bundle.
1822 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001823 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1824 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001825 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001826 }
1827
Joe Onoratodeb98af2010-02-19 14:59:39 -08001828 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001829 // Handle case where user selected "Applications"
1830 String applicationName = getResources().getString(R.string.group_applications);
1831 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001832
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001833 if (applicationName != null && applicationName.equals(shortcutName)) {
1834 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1835 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001836
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001837 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1838 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001839 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001840 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001841 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001842 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845
Winson Chung24ab2f12010-09-16 14:10:47 -07001846 void processWallpaper(Intent intent) {
1847 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1848 }
1849
Winson Chung3d503fb2011-07-13 17:25:49 -07001850 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1851 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001852 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 folderInfo.title = getText(R.string.folder_name);
1854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001856 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1857 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001858 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
1860 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001861 FolderIcon newFolder =
1862 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1863 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1864 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001865 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
Romain Guycbb89e42009-06-08 15:52:54 -07001867
Joe Onorato9c1289c2009-08-17 11:03:03 -04001868 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001869 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001870 }
1871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001873 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001875 Intent chooser = Intent.createChooser(pickWallpaper,
1876 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001877 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1878 // Removed in Eclair MR1
1879// WallpaperManager wm = (WallpaperManager)
1880// getSystemService(Context.WALLPAPER_SERVICE);
1881// WallpaperInfo wi = wm.getWallpaperInfo();
1882// if (wi != null && wi.getSettingsActivity() != null) {
1883// LabeledIntent li = new LabeledIntent(getPackageName(),
1884// R.string.configure_wallpaper, 0);
1885// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1886// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1887// }
Mike Clerona0618e42009-10-22 13:55:21 -07001888 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001891 /**
1892 * Registers various content observers. The current implementation registers
1893 * only a favorites observer to keep track of the favorites applications.
1894 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001895 private void registerContentObservers() {
1896 ContentResolver resolver = getContentResolver();
1897 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1898 true, mWidgetObserver);
1899 }
1900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 @Override
1902 public boolean dispatchKeyEvent(KeyEvent event) {
1903 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1904 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001906 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001907 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001908 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001909 dumpState();
1910 return true;
1911 }
1912 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001913 }
1914 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1915 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001916 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 return true;
1918 }
1919 }
1920
1921 return super.dispatchKeyEvent(event);
1922 }
1923
Joe Onorato88ec0992009-11-19 13:16:06 -08001924 @Override
1925 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001926 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001927 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001928 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001929 Folder openFolder = mWorkspace.getOpenFolder();
1930 if (openFolder.isEditingName()) {
1931 openFolder.dismissEditingName();
1932 } else {
1933 closeFolder();
1934 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001935 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001936 mWorkspace.exitWidgetResizeMode();
1937
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001938 // Back button is a no-op here, but give at least some feedback for the button press
1939 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001940 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001944 * Re-listen when widgets are reset.
1945 */
1946 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001947 if (mAppWidgetHost != null) {
1948 mAppWidgetHost.startListening();
1949 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001950 }
1951
1952 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 * Launches the intent referred by the clicked shortcut.
1954 *
1955 * @param v The view representing the clicked shortcut.
1956 */
1957 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001958 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1959 // view has detached (it's possible for this to happen if the view is removed mid touch).
1960 if (v.getWindowToken() == null) {
1961 return;
1962 }
1963
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001964 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001965 return;
1966 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001969 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001971 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001972 int[] pos = new int[2];
1973 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001974 intent.setSourceBounds(new Rect(pos[0], pos[1],
1975 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001976
1977 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001978
1979 if (success && v instanceof BubbleTextView) {
1980 mWaitingForResume = (BubbleTextView) v;
1981 mWaitingForResume.setStayPressed(true);
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001984 if (v instanceof FolderIcon) {
1985 FolderIcon fi = (FolderIcon) v;
1986 handleFolderClick(fi);
1987 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001988 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001989 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001990 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001991 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001992 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995 }
1996
Michael Jurka0e260592010-06-30 17:07:39 -07001997 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001998 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001999 // clicking anywhere on the workspace causes the customization drawer to slide down
2000 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002001 return false;
2002 }
2003
Michael Jurkaaf442092010-06-10 17:01:57 -07002004 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002005 * Event handler for the search button
2006 *
2007 * @param v The view that was clicked.
2008 */
2009 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002010 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2011
Amith Yamasania135ba82011-08-09 17:42:01 -07002012 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002013 }
2014
2015 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002016 * Event handler for the voice button
2017 *
2018 * @param v The view that was clicked.
2019 */
2020 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002021 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002022
Michael Jurkaae65ee32012-04-30 11:45:54 -07002023 try {
2024 final SearchManager searchManager =
2025 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2026 ComponentName activityName = searchManager.getGlobalSearchActivity();
2027 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002028 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002029 if (activityName != null) {
2030 intent.setPackage(activityName.getPackageName());
2031 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002032 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002033 } catch (ActivityNotFoundException e) {
2034 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002035 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002036 startActivitySafely(null, intent, "onClickVoiceButton");
2037 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002038 }
2039
2040 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002041 * Event handler for the "grid" button that appears on the home screen, which
2042 * enters all apps mode.
2043 *
2044 * @param v The view that was clicked.
2045 */
2046 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002047 showAllApps(true);
2048 }
2049
2050 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002051 // Provide the same haptic feedback that the system offers for virtual keys.
2052 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002053 }
2054
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002055 public void onClickAppMarketButton(View v) {
2056 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002057 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002058 } else {
2059 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002060 }
2061 }
2062
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002063 void startApplicationDetailsActivity(ComponentName componentName) {
2064 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002065 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2066 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002067 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002068 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002069 }
2070
Patrick Dubroy5539af72010-09-07 15:22:01 -07002071 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2072 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2073 // System applications cannot be installed. For now, show a toast explaining that.
2074 // We may give them the option of disabling apps this way.
2075 int messageId = R.string.uninstall_system_app_text;
2076 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2077 } else {
2078 String packageName = appInfo.componentName.getPackageName();
2079 String className = appInfo.componentName.getClassName();
2080 Intent intent = new Intent(
2081 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002082 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2083 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002084 startActivity(intent);
2085 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002086 }
2087
Michael Jurka86a720e2012-05-09 11:23:23 -07002088 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002092 // Only launch using the new animation if the shortcut has not opted out (this is a
2093 // private contract between launcher and may be ignored in the future).
2094 boolean useLaunchAnimation = (v != null) &&
2095 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2096 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002097 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2098 v.getMeasuredWidth(), v.getMeasuredHeight());
2099
2100 startActivity(intent, opts.toBundle());
2101 } else {
2102 startActivity(intent);
2103 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002104 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 } catch (SecurityException e) {
2106 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002107 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002109 "or use the exported attribute for this activity. "
2110 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002112 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002114
Michael Jurka86a720e2012-05-09 11:23:23 -07002115 boolean startActivitySafely(View v, Intent intent, Object tag) {
2116 boolean success = false;
2117 try {
2118 success = startActivity(v, intent, tag);
2119 } catch (ActivityNotFoundException e) {
2120 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2121 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2122 }
2123 return success;
2124 }
2125
Romain Guy8e633c52010-03-04 12:51:36 -08002126 void startActivityForResultSafely(Intent intent, int requestCode) {
2127 try {
2128 startActivityForResult(intent, requestCode);
2129 } catch (ActivityNotFoundException e) {
2130 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2131 } catch (SecurityException e) {
2132 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2133 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2134 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2135 "or use the exported attribute for this activity.", e);
2136 }
2137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138
Adam Cohena9cf38f2011-05-02 15:36:58 -07002139 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002140 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002141 Folder openFolder = mWorkspace.getFolderForTag(info);
2142
2143 // If the folder info reports that the associated folder is open, then verify that
2144 // it is actually opened. There have been a few instances where this gets out of sync.
2145 if (info.opened && openFolder == null) {
2146 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2147 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2148 info.opened = false;
2149 }
2150
Adam Cohenfb91f302012-06-11 15:45:18 -07002151 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 // Close any open folder
2153 closeFolder();
2154 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002155 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 } else {
2157 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 int folderScreen;
2159 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002160 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 // .. and close it
2162 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002163 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 // Close any folder open on the current screen
2165 closeFolder();
2166 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002167 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 }
2169 }
2170 }
2171 }
2172
Adam Cohen268c4752012-06-06 17:47:33 -07002173 /**
2174 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2175 * in the DragLayer in the exact absolute location of the original FolderIcon.
2176 */
2177 private void copyFolderIconToImage(FolderIcon fi) {
2178 final int width = fi.getMeasuredWidth();
2179 final int height = fi.getMeasuredHeight();
2180
2181 // Lazy load ImageView, Bitmap and Canvas
2182 if (mFolderIconImageView == null) {
2183 mFolderIconImageView = new ImageView(this);
2184 }
2185 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2186 mFolderIconBitmap.getHeight() != height) {
2187 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2188 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2189 }
2190
2191 DragLayer.LayoutParams lp;
2192 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2193 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2194 } else {
2195 lp = new DragLayer.LayoutParams(width, height);
2196 }
2197
Adam Cohen307fe232012-08-16 17:55:58 -07002198 // The layout from which the folder is being opened may be scaled, adjust the starting
2199 // view size by this scale factor.
2200 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002201 lp.customPosition = true;
2202 lp.x = mRectForFolderAnimation.left;
2203 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002204 lp.width = (int) (scale * width);
2205 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002206
2207 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2208 fi.draw(mFolderIconCanvas);
2209 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002210 if (fi.getFolder() != null) {
2211 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2212 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002213 }
Adam Cohen268c4752012-06-06 17:47:33 -07002214 // Just in case this image view is still in the drag layer from a previous animation,
2215 // we remove it and re-add it.
2216 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2217 mDragLayer.removeView(mFolderIconImageView);
2218 }
2219 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002220 if (fi.getFolder() != null) {
2221 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002222 }
Adam Cohen268c4752012-06-06 17:47:33 -07002223 }
2224
Adam Cohen2801caf2011-05-13 20:57:39 -07002225 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002226 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002227 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2228 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2229 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2230
Adam Cohenc51934b2011-07-26 21:07:43 -07002231 FolderInfo info = (FolderInfo) fi.getTag();
2232 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2233 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002234 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2235 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002236 }
2237
Adam Cohen268c4752012-06-06 17:47:33 -07002238 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2239 copyFolderIconToImage(fi);
2240 fi.setVisibility(View.INVISIBLE);
2241
Michael Jurka2ecf9952012-06-18 12:52:28 -07002242 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002243 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002244 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2245 oa.start();
2246 }
2247
Adam Cohen268c4752012-06-06 17:47:33 -07002248 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002249 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002250 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2251 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2252 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2253
Adam Cohen268c4752012-06-06 17:47:33 -07002254 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002255
Adam Cohen268c4752012-06-06 17:47:33 -07002256 // We remove and re-draw the FolderIcon in-case it has changed
2257 mDragLayer.removeView(mFolderIconImageView);
2258 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002259 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002260 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002261 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002262 oa.addListener(new AnimatorListenerAdapter() {
2263 @Override
2264 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002265 if (cl != null) {
2266 cl.clearFolderLeaveBehind();
2267 // Remove the ImageView copy of the FolderIcon and make the original visible.
2268 mDragLayer.removeView(mFolderIconImageView);
2269 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002270 }
2271 }
2272 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002273 oa.start();
2274 }
2275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002277 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 * is animated relative to the specified View. If the View is null, no animation
2279 * is played.
2280 *
2281 * @param folderInfo The FolderInfo describing the folder to open.
2282 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002283 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002284 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002285 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286
Adam Cohena9cf38f2011-05-02 15:36:58 -07002287 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288
Adam Cohen4554ee12011-08-03 16:13:21 -07002289 // Just verify that the folder hasn't already been added to the DragLayer.
2290 // There was a one-off crash where the folder had a parent already.
2291 if (folder.getParent() == null) {
2292 mDragLayer.addView(folder);
2293 mDragController.addDropTarget((DropTarget) folder);
2294 } else {
2295 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2296 folder.getParent() + ").");
2297 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002298 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002299 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002300
2301 // Notify the accessibility manager that this folder "window" has appeared and occluded
2302 // the workspace items
2303 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2304 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002305 }
2306
2307 public void closeFolder() {
2308 Folder folder = mWorkspace.getOpenFolder();
2309 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002310 if (folder.isEditingName()) {
2311 folder.dismissEditingName();
2312 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002313 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002314
2315 // Dismiss the folder cling
2316 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002317 }
2318 }
2319
2320 void closeFolder(Folder folder) {
2321 folder.getInfo().opened = false;
2322
2323 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2324 if (parent != null) {
2325 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2326 shrinkAndFadeInFolderIcon(fi);
2327 }
2328 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002329
2330 // Notify the accessibility manager that this folder "window" has disappeard and no
2331 // longer occludeds the workspace items
2332 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 }
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002336 if (!isDraggingEnabled()) return false;
2337 if (isWorkspaceLocked()) return false;
2338 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339
2340 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002341 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
2343
Michael Jurka0280c3b2010-09-17 15:00:07 -07002344 resetAddInfo();
2345 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002347 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 return true;
2349 }
2350
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 // The hotseat touch handling does not go through Workspace, and we always allow long press
2352 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002353 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2355 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002356 if (itemUnderLongClick == null) {
2357 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002358 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2359 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002360 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002362 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002364 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 }
2366 }
2367 }
2368 return true;
2369 }
2370
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 boolean isHotseatLayout(View layout) {
2372 return mHotseat != null && layout != null &&
2373 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2374 }
2375 Hotseat getHotseat() {
2376 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002377 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002378 SearchDropTargetBar getSearchBar() {
2379 return mSearchDropTargetBar;
2380 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002381
Winson Chung3d503fb2011-07-13 17:25:49 -07002382 /**
2383 * Returns the CellLayout of the specified container at the specified screen.
2384 */
2385 CellLayout getCellLayout(long container, int screen) {
2386 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2387 if (mHotseat != null) {
2388 return mHotseat.getLayout();
2389 } else {
2390 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002391 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002392 } else {
2393 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002394 }
2395 }
2396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 Workspace getWorkspace() {
2398 return mWorkspace;
2399 }
2400
Daniel Sandler843e8602010-06-07 14:59:01 -04002401 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002402 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002403 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002404 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002405 }
2406
Craig Mautner360310b2012-10-26 15:13:08 -07002407 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002408 public boolean isAllAppsButtonRank(int rank) {
2409 return mHotseat.isAllAppsButtonRank(rank);
2410 }
2411
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002412 /**
2413 * Helper method for the cameraZoomIn/cameraZoomOut animations
2414 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002415 * @param scaleFactor The scale factor used for the zoom
2416 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002417 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002418 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002419 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002420 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002421
Winson Chung18f41f82012-05-09 13:28:10 -07002422 void disableWallpaperIfInAllApps() {
2423 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002424 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002425 if (mAppsCustomizeTabHost != null &&
2426 !mAppsCustomizeTabHost.isTransitioning()) {
2427 updateWallpaperVisibility(false);
2428 }
2429 }
2430 }
2431
Craig Mautner360310b2012-10-26 15:13:08 -07002432 private void setWorkspaceBackground(boolean workspace) {
2433 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002434 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002435 }
2436
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002437 void updateWallpaperVisibility(boolean visible) {
2438 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2439 int curflags = getWindow().getAttributes().flags
2440 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2441 if (wpflags != curflags) {
2442 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2443 }
Craig Mautner360310b2012-10-26 15:13:08 -07002444 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002445 }
2446
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002447 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2448 if (v instanceof LauncherTransitionable) {
2449 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2450 }
2451 }
2452
Michael Jurkabed61d22012-02-14 22:51:29 -08002453 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2454 if (v instanceof LauncherTransitionable) {
2455 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2456 }
Winson Chung70442722012-02-10 15:43:22 -08002457
2458 // Update the workspace transition step as well
2459 dispatchOnLauncherTransitionStep(v, 0f);
2460 }
2461
2462 private void dispatchOnLauncherTransitionStep(View v, float t) {
2463 if (v instanceof LauncherTransitionable) {
2464 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2465 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002466 }
2467
2468 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2469 if (v instanceof LauncherTransitionable) {
2470 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2471 }
Winson Chung70442722012-02-10 15:43:22 -08002472
2473 // Update the workspace transition step as well
2474 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002475 }
2476
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002477 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002478 * Things to test when changing the following seven functions.
2479 * - Home from workspace
2480 * - from center screen
2481 * - from other screens
2482 * - Home from all apps
2483 * - from center screen
2484 * - from other screens
2485 * - Back from all apps
2486 * - from center screen
2487 * - from other screens
2488 * - Launch app from workspace and quit
2489 * - with back
2490 * - with home
2491 * - Launch app from all apps and quit
2492 * - with back
2493 * - with home
2494 * - Go to a screen that's not the default, then all
2495 * apps, and launch and app, and go back
2496 * - with back
2497 * -with home
2498 * - On workspace, long press power and go back
2499 * - with back
2500 * - with home
2501 * - On all apps, long press power and go back
2502 * - with back
2503 * - with home
2504 * - On workspace, power off
2505 * - On all apps, power off
2506 * - Launch an app and turn off the screen while in that app
2507 * - Go back with home key
2508 * - Go back with back key TODO: make this not go to workspace
2509 * - From all apps
2510 * - From workspace
2511 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2512 * - From all apps
2513 * - From the center workspace
2514 * - From another workspace
2515 */
2516
2517 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002518 * Zoom the camera out from the workspace to reveal 'toView'.
2519 * Assumes that the view to show is anchored at either the very top or very bottom
2520 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002521 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002522 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002523 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002524 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002525 mStateAnimation.cancel();
2526 mStateAnimation = null;
2527 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002528 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002529
Winson Chungf0ea4d32011-06-06 14:27:16 -07002530 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2531 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2532 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002533 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002534 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002535 final int startDelay =
2536 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002537
Michael Jurkab3e22d92011-10-31 15:58:33 -07002538 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002539
Michael Jurkad74c9842011-07-10 12:44:21 -07002540 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002541 Animator workspaceAnim =
2542 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002543
2544 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002545 toView.setScaleX(scale);
2546 toView.setScaleY(scale);
2547 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2548 scaleAnim.
2549 scaleX(1f).scaleY(1f).
2550 setDuration(duration).
2551 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002552
Winson Chungf0ea4d32011-06-06 14:27:16 -07002553 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002554 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002555 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002556 .ofFloat(toView, "alpha", 0f, 1f)
2557 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002558 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002559 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2560 @Override
2561 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002562 if (animation == null) {
2563 throw new RuntimeException("animation is null");
2564 }
Winson Chung70442722012-02-10 15:43:22 -08002565 float t = (Float) animation.getAnimatedValue();
2566 dispatchOnLauncherTransitionStep(fromView, t);
2567 dispatchOnLauncherTransitionStep(toView, t);
2568 }
2569 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002570
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002571 // toView should appear right at the end of the workspace shrink
2572 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002573 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002574 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002575 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002576
2577 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002578 boolean animationCancelled = false;
2579
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002580 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002581 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002582 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002583 // Prepare the position
2584 toView.setTranslationX(0.0f);
2585 toView.setTranslationY(0.0f);
2586 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002587 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002588 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002589 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002590 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002591 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2592 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002593
Michael Jurkafa7969f2012-09-21 16:39:14 -07002594 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002595 // Hide the workspace scrollbar
2596 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002597 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002598 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002599 if (!animationCancelled) {
2600 updateWallpaperVisibility(false);
2601 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002602
2603 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002604 if (mSearchDropTargetBar != null) {
2605 mSearchDropTargetBar.hideSearchBar(false);
2606 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002607 }
2608
Adam Cohencff6af82011-09-13 14:51:53 -07002609 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002610 public void onAnimationCancel(Animator animation) {
2611 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002612 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002613 });
2614
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002615 if (workspaceAnim != null) {
2616 mStateAnimation.play(workspaceAnim);
2617 }
Michael Jurka1899a362011-11-03 13:50:45 -07002618
2619 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002620
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002621 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2622 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002623
2624 // If any of the objects being animated haven't been measured/laid out
2625 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002626 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002627 (mWorkspace.getMeasuredWidth() == 0) ||
2628 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002629 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002630 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002631
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002632 final AnimatorSet stateAnimation = mStateAnimation;
2633 final Runnable startAnimRunnable = new Runnable() {
2634 public void run() {
2635 // Check that mStateAnimation hasn't changed while
2636 // we waited for a layout/draw pass
2637 if (mStateAnimation != stateAnimation)
2638 return;
2639 setPivotsForZoom(toView, scale);
2640 dispatchOnLauncherTransitionStart(fromView, animated, false);
2641 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002642 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002643 }
2644 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002645 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002646 final ViewTreeObserver observer = toView.getViewTreeObserver();
2647 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2648 public void onGlobalLayout() {
2649 startAnimRunnable.run();
2650 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2651 }
2652 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002653 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002654 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002655 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002656 } else {
2657 toView.setTranslationX(0.0f);
2658 toView.setTranslationY(0.0f);
2659 toView.setScaleX(1.0f);
2660 toView.setScaleY(1.0f);
2661 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002662 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002663
Michael Jurkabed61d22012-02-14 22:51:29 -08002664 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2665 // Hide the workspace scrollbar
2666 mWorkspace.hideScrollingIndicator(true);
2667 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002668
2669 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002670 if (mSearchDropTargetBar != null) {
2671 mSearchDropTargetBar.hideSearchBar(false);
2672 }
Michael Jurkaabded662011-03-04 12:06:57 -08002673 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002674 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002675 dispatchOnLauncherTransitionStart(fromView, animated, false);
2676 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002677 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002678 dispatchOnLauncherTransitionStart(toView, animated, false);
2679 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002680 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002682 }
2683
2684 /**
2685 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002686 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002688 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002689 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2690 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002691
Michael Jurkab3e22d92011-10-31 15:58:33 -07002692 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002693 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002694 mStateAnimation.cancel();
2695 mStateAnimation = null;
2696 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002697 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002698
Winson Chungf0ea4d32011-06-06 14:27:16 -07002699 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002700 final int fadeOutDuration =
2701 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002702 final float scaleFactor = (float)
2703 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2704 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002705 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002706 Animator workspaceAnim = null;
2707
2708 if (toState == State.WORKSPACE) {
2709 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2710 workspaceAnim = mWorkspace.getChangeStateAnimation(
2711 Workspace.State.NORMAL, animated, stagger);
2712 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2713 workspaceAnim = mWorkspace.getChangeStateAnimation(
2714 Workspace.State.SPRING_LOADED, animated);
2715 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002716
Michael Jurkab3e22d92011-10-31 15:58:33 -07002717 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002718 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002719 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002720 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002721 final LauncherViewPropertyAnimator scaleAnim =
2722 new LauncherViewPropertyAnimator(fromView);
2723 scaleAnim.
2724 scaleX(scaleFactor).scaleY(scaleFactor).
2725 setDuration(duration).
2726 setInterpolator(new Workspace.ZoomInInterpolator());
2727
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002728 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002729 .ofFloat(fromView, "alpha", 1f, 0f)
2730 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002731 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002732 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2733 @Override
2734 public void onAnimationUpdate(ValueAnimator animation) {
2735 float t = 1f - (Float) animation.getAnimatedValue();
2736 dispatchOnLauncherTransitionStep(fromView, t);
2737 dispatchOnLauncherTransitionStep(toView, t);
2738 }
2739 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002740
Michael Jurka2ecf9952012-06-18 12:52:28 -07002741 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002742
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002743 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2744 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002745 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002746
2747 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002748 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002749 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002750 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002751 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002752 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2753 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002754 if (mWorkspace != null) {
2755 mWorkspace.hideScrollingIndicator(false);
2756 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002757 if (onCompleteRunnable != null) {
2758 onCompleteRunnable.run();
2759 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002760 mAppsCustomizeContent.updateCurrentPageScroll();
2761 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002762 }
2763 });
2764
Winson Chungf0ea4d32011-06-06 14:27:16 -07002765 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002766 if (workspaceAnim != null) {
2767 mStateAnimation.play(workspaceAnim);
2768 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002769 dispatchOnLauncherTransitionStart(fromView, animated, true);
2770 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002771 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 } else {
2773 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002774 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002775 dispatchOnLauncherTransitionStart(fromView, animated, true);
2776 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002777 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002778 dispatchOnLauncherTransitionStart(toView, animated, true);
2779 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002780 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002781 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002782 }
2783
Michael Jurkae326f182011-11-21 14:05:46 -08002784 @Override
2785 public void onTrimMemory(int level) {
2786 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002787 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002788 mAppsCustomizeTabHost.onTrimMemory();
2789 }
2790 }
2791
Winson Chung18f41f82012-05-09 13:28:10 -07002792 @Override
2793 public void onWindowFocusChanged(boolean hasFocus) {
2794 if (!hasFocus) {
2795 // When another window occludes launcher (like the notification shade, or recents),
2796 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2797 updateWallpaperVisibility(true);
2798 } else {
2799 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002800 mWorkspace.postDelayed(new Runnable() {
2801 @Override
2802 public void run() {
2803 disableWallpaperIfInAllApps();
2804 }
2805 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002806 }
2807 }
2808
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002809 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002810 showWorkspace(animated, null);
2811 }
2812
2813 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002814 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002815 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002816 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002817 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002818
Winson Chungc7d2b602012-05-16 17:02:20 -07002819 // Show the search bar (only animate if we were showing the drop target bar in spring
2820 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002821 if (mSearchDropTargetBar != null) {
2822 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2823 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002824
Michael Jurkab737ee62011-11-15 15:57:22 -08002825 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002826 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002827
2828 // Set focus to the AppsCustomize button
2829 if (mAllAppsButton != null) {
2830 mAllAppsButton.requestFocus();
2831 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002832 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002833
Michael Jurkab737ee62011-11-15 15:57:22 -08002834 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002835
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002836 // Change the state *after* we've called all the transition code
2837 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002838
Winson Chung5fb63472011-02-02 17:03:37 -08002839 // Resume the auto-advance of widgets
2840 mUserPresent = true;
2841 updateRunning();
2842
alanv1d4fde62012-10-17 13:15:47 -07002843 // Send an accessibility event to announce the context change
2844 getWindow().getDecorView()
2845 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Joe Onorato00acb122009-08-04 16:04:30 -04002846 }
2847
Michael Jurkab3e22d92011-10-31 15:58:33 -07002848 void showAllApps(boolean animated) {
2849 if (mState != State.WORKSPACE) return;
2850
2851 showAppsCustomizeHelper(animated, false);
2852 mAppsCustomizeTabHost.requestFocus();
2853
Michael Jurkab3e22d92011-10-31 15:58:33 -07002854 // Change the state *after* we've called all the transition code
2855 mState = State.APPS_CUSTOMIZE;
2856
2857 // Pause the auto-advance of widgets until we are out of AllApps
2858 mUserPresent = false;
2859 updateRunning();
2860 closeFolder();
2861
2862 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002863 getWindow().getDecorView()
2864 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002865 }
2866
Adam Cohen7777d962011-08-18 18:58:38 -07002867 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002868 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002869 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002870 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002871 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002872 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002873 }
Adam Cohen7777d962011-08-18 18:58:38 -07002874
Adam Cohened66b2b2012-01-23 17:28:51 -08002875 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2876 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002877 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2878
Winson Chunge7a03942011-08-05 15:05:12 -07002879 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002880 @Override
2881 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002882 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002883 // Before we show workspace, hide all apps again because
2884 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2885 // clean up our state transition functions
2886 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002887 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002888 } else {
2889 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002890 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002891 }
2892 }, (extendedDelay ?
2893 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2894 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2895 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002896
Michael Jurkad3ef3062010-11-23 16:23:58 -08002897 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002898 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002899 final boolean animated = true;
2900 final boolean springLoaded = true;
2901 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002902 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002903 }
2904 // Otherwise, we are not in spring loaded mode, so don't do anything.
2905 }
2906
Michael Jurkab737ee62011-11-15 15:57:22 -08002907 void hideDockDivider() {
2908 if (mQsbDivider != null && mDockDivider != null) {
2909 mQsbDivider.setVisibility(View.INVISIBLE);
2910 mDockDivider.setVisibility(View.INVISIBLE);
2911 }
2912 }
2913
2914 void showDockDivider(boolean animated) {
2915 if (mQsbDivider != null && mDockDivider != null) {
2916 mQsbDivider.setVisibility(View.VISIBLE);
2917 mDockDivider.setVisibility(View.VISIBLE);
2918 if (mDividerAnimator != null) {
2919 mDividerAnimator.cancel();
2920 mQsbDivider.setAlpha(1f);
2921 mDockDivider.setAlpha(1f);
2922 mDividerAnimator = null;
2923 }
2924 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002925 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2926 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2927 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002928 int duration = 0;
2929 if (mSearchDropTargetBar != null) {
2930 duration = mSearchDropTargetBar.getTransitionInDuration();
2931 }
2932 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002933 mDividerAnimator.start();
2934 }
2935 }
2936 }
2937
Michael Jurkab3e22d92011-10-31 15:58:33 -07002938 void lockAllApps() {
2939 // TODO
2940 }
2941
2942 void unlockAllApps() {
2943 // TODO
2944 }
2945
Winson Chungf0ea4d32011-06-06 14:27:16 -07002946 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002947 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002948 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002949 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002950 if (!LauncherApplication.isScreenLarge()) {
2951 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002952 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002953 int duration = 0;
2954 if (mSearchDropTargetBar != null) {
2955 duration = mSearchDropTargetBar.getTransitionInDuration();
2956 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002957 mHotseat.animate().alpha(1f).setDuration(duration);
2958 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002959 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002960 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002961 }
2962 }
2963 }
2964
2965 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002966 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002967 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002968 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002969 if (!LauncherApplication.isScreenLarge()) {
2970 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002971 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002972 int duration = 0;
2973 if (mSearchDropTargetBar != null) {
2974 duration = mSearchDropTargetBar.getTransitionOutDuration();
2975 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002976 mHotseat.animate().alpha(0f).setDuration(duration);
2977 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002978 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002979 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002980 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002981 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002982 }
2983
Patrick Dubroy5f445422011-02-18 14:35:21 -08002984 /**
2985 * Add an item from all apps or customize onto the given workspace screen.
2986 * If layout is null, add to the current screen.
2987 */
2988 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002989 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002990 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002991 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002992 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002993
Winson Chungdff8ebb2011-09-08 17:25:31 -07002994 /** Maps the current orientation to an index for referencing orientation correct global icons */
2995 private int getCurrentOrientationIndexForGlobalIcons() {
2996 // default - 0, landscape - 1
2997 switch (getResources().getConfiguration().orientation) {
2998 case Configuration.ORIENTATION_LANDSCAPE:
2999 return 1;
3000 default:
3001 return 0;
3002 }
3003 }
3004
Michael Jurkaae65ee32012-04-30 11:45:54 -07003005 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003006 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003007 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003008 // Look for the toolbar icon specified in the activity meta-data
3009 Bundle metaData = packageManager.getActivityInfo(
3010 activityName, PackageManager.GET_META_DATA).metaData;
3011 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003012 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003013 if (iconResId != 0) {
3014 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003015 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003016 }
3017 }
3018 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003019 // This can happen if the activity defines an invalid drawable
3020 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3021 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003022 } catch (Resources.NotFoundException nfe) {
3023 // This can happen if the activity defines an invalid drawable
3024 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3025 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003026 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003027 return null;
3028 }
3029
3030 // if successful in getting icon, return it; otherwise, set button to use default drawable
3031 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003032 int buttonId, ComponentName activityName, int fallbackDrawableId,
3033 String toolbarResourceName) {
3034 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003035 Resources r = getResources();
3036 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3037 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003038
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003039 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003040 // If we were unable to find the icon via the meta-data, use a generic one
3041 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003042 toolbarIcon = r.getDrawable(fallbackDrawableId);
3043 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003044 if (button != null) {
3045 button.setCompoundDrawables(toolbarIcon, null, null, null);
3046 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003047 return null;
3048 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003049 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003050 if (button != null) {
3051 button.setCompoundDrawables(toolbarIcon, null, null, null);
3052 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003053 return toolbarIcon.getConstantState();
3054 }
3055 }
3056
3057 // if successful in getting icon, return it; otherwise, set button to use default drawable
3058 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003059 int buttonId, ComponentName activityName, int fallbackDrawableId,
3060 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003061 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003062 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003063
Michael Jurka19e0fc52011-07-22 18:00:21 -07003064 if (button != null) {
3065 // If we were unable to find the icon via the meta-data, use a
3066 // generic one
3067 if (toolbarIcon == null) {
3068 button.setImageResource(fallbackDrawableId);
3069 } else {
3070 button.setImageDrawable(toolbarIcon);
3071 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003072 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003073
3074 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3075
Michael Jurka0423dcf2010-10-05 14:56:18 -07003076 }
3077
Winson Chung1b884092012-06-08 17:13:02 -07003078 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003079 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003080 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003081 }
3082
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003083 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003084 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003085 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003086 }
3087
Winson Chungbb185bd2011-11-21 12:31:42 -08003088 private void invalidatePressedFocusedStates(View container, View button) {
3089 if (container instanceof HolographicLinearLayout) {
3090 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3091 layout.invalidatePressedFocusedStates();
3092 } else if (button instanceof HolographicImageView) {
3093 HolographicImageView view = (HolographicImageView) button;
3094 view.invalidatePressedFocusedStates();
3095 }
3096 }
3097
Winson Chungc51db6a2011-10-05 11:44:49 -07003098 private boolean updateGlobalSearchIcon() {
3099 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003100 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003101 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003102 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003103 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003104
Winson Chung1cad91e2011-05-25 17:41:01 -07003105 final SearchManager searchManager =
3106 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3107 ComponentName activityName = searchManager.getGlobalSearchActivity();
3108 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003109 int coi = getCurrentOrientationIndexForGlobalIcons();
3110 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003111 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3112 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3113 if (sGlobalSearchIcon[coi] == null) {
3114 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3115 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3116 TOOLBAR_ICON_METADATA_NAME);
3117 }
3118
Winson Chungc51db6a2011-10-05 11:44:49 -07003119 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3120 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003121 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003122 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003123 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003124 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003125 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3126 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3127 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003128 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003129 if (voiceButtonProxy != null) {
3130 voiceButtonProxy.setVisibility(View.GONE);
3131 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003132 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003133 }
3134 }
3135
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003136 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003137 final View searchButtonContainer = findViewById(R.id.search_button_container);
3138 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003139 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003140 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 }
3142
Winson Chungc51db6a2011-10-05 11:44:49 -07003143 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003144 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003145 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003146 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003147
Winson Chungc51db6a2011-10-05 11:44:49 -07003148 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003149 final SearchManager searchManager =
3150 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3151 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3152
3153 ComponentName activityName = null;
3154 if (globalSearchActivity != null) {
3155 // Check if the global search activity handles voice search
3156 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3157 intent.setPackage(globalSearchActivity.getPackageName());
3158 activityName = intent.resolveActivity(getPackageManager());
3159 }
3160
3161 if (activityName == null) {
3162 // Fallback: check if an activity other than the global search activity
3163 // resolves this
3164 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3165 activityName = intent.resolveActivity(getPackageManager());
3166 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003167 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003168 int coi = getCurrentOrientationIndexForGlobalIcons();
3169 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003170 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3171 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3172 if (sVoiceSearchIcon[coi] == null) {
3173 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3174 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3175 TOOLBAR_ICON_METADATA_NAME);
3176 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003177 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003178 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003179 if (voiceButtonProxy != null) {
3180 voiceButtonProxy.setVisibility(View.VISIBLE);
3181 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003182 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003183 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003184 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003185 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003186 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003187 if (voiceButtonProxy != null) {
3188 voiceButtonProxy.setVisibility(View.GONE);
3189 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003190 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003191 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003192 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003193
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003194 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003195 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3196 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003197 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003198 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003199 }
3200
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003201 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003202 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003203 */
3204 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003205 final View marketButton = findViewById(R.id.market_button);
3206 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3207 // Find the app market activity by resolving an intent.
3208 // (If multiple app markets are installed, it will return the ResolverActivity.)
3209 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003210 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003211 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003212 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003213 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003214 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3215 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003216 marketButton.setVisibility(View.VISIBLE);
3217 } else {
3218 // We should hide and disable the view so that we don't try and restore the visibility
3219 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3220 marketButton.setVisibility(View.GONE);
3221 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003222 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003223 }
3224
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003225 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003226 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3227 Resources r = getResources();
3228 Drawable marketIconDrawable = d.newDrawable(r);
3229 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3230 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3231 marketIconDrawable.setBounds(0, 0, w, h);
3232
3233 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003234 }
3235
Michael Jurkad7c28052012-04-27 15:43:36 -07003236 @Override
3237 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003238 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003239 final List<CharSequence> text = event.getText();
3240 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003241 // Populate event with a fake title based on the current state.
3242 if (mState == State.APPS_CUSTOMIZE) {
3243 text.add(getString(R.string.all_apps_button_label));
3244 } else {
3245 text.add(getString(R.string.all_apps_home_button_label));
3246 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003247 return result;
3248 }
3249
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003250 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003251 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003252 */
3253 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3254 @Override
3255 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003256 closeSystemDialogs();
3257 }
3258 }
3259
3260 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003261 * Receives notifications whenever the appwidgets are reset.
3262 */
3263 private class AppWidgetResetObserver extends ContentObserver {
3264 public AppWidgetResetObserver() {
3265 super(new Handler());
3266 }
3267
3268 @Override
3269 public void onChange(boolean selfChange) {
3270 onAppWidgetReset();
3271 }
3272 }
3273
3274 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003275 * If the activity is currently paused, signal that we need to run the passed Runnable
3276 * in onResume.
3277 *
3278 * This needs to be called from incoming places where resources might have been loaded
3279 * while we are paused. That is becaues the Configuration might be wrong
3280 * when we're not running, and if it comes back to what it was when we
3281 * were paused, we are not restarted.
3282 *
3283 * Implementation of the method from LauncherModel.Callbacks.
3284 *
3285 * @return true if we are currently paused. The caller might be able to
3286 * skip some work in that case since we will come back again.
3287 */
3288 private boolean waitUntilResume(Runnable run) {
3289 if (mPaused) {
3290 Log.i(TAG, "Deferring update until onResume");
3291 mOnResumeCallbacks.add(run);
3292 return true;
3293 } else {
3294 return false;
3295 }
3296 }
3297
3298 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003299 * If the activity is currently paused, signal that we need to re-run the loader
3300 * in onResume.
3301 *
3302 * This needs to be called from incoming places where resources might have been loaded
3303 * while we are paused. That is becaues the Configuration might be wrong
3304 * when we're not running, and if it comes back to what it was when we
3305 * were paused, we are not restarted.
3306 *
3307 * Implementation of the method from LauncherModel.Callbacks.
3308 *
3309 * @return true if we are currently paused. The caller might be able to
3310 * skip some work in that case since we will come back again.
3311 */
3312 public boolean setLoadOnResume() {
3313 if (mPaused) {
3314 Log.i(TAG, "setLoadOnResume");
3315 mOnResumeNeedsLoad = true;
3316 return true;
3317 } else {
3318 return false;
3319 }
3320 }
3321
3322 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003326 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003327 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003328 } else {
3329 return SCREEN_COUNT / 2;
3330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003332
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 /**
3334 * Refreshes the shortcuts shown on the workspace.
3335 *
3336 * Implementation of the method from LauncherModel.Callbacks.
3337 */
3338 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003339 // If we're starting binding all over again, clear any bind calls we'd postponed in
3340 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3341 // from scratch again
3342 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003343
Michael Jurka7607c2f2013-04-03 14:33:19 -07003344 final Workspace workspace = mWorkspace;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003345 mNewShortcutAnimatePage = -1;
3346 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003347 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 int count = workspace.getChildCount();
3349 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003350 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003351 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3352 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003354 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003355 if (mHotseat != null) {
3356 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003357 }
3358 }
3359
3360 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 * Bind the items start-end from the list.
3362 *
3363 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003364 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003365 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3366 if (waitUntilResume(new Runnable() {
3367 public void run() {
3368 bindItems(shortcuts, start, end);
3369 }
3370 })) {
3371 return;
3372 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003373
Winson Chungf0c6ae02012-03-21 16:10:31 -07003374 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3375 Set<String> newApps = new HashSet<String>();
3376 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3377
3378 Workspace workspace = mWorkspace;
3379 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003381
3382 // Short circuit if we are loading dock items for a configuration which has no dock
3383 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3384 mHotseat == null) {
3385 continue;
3386 }
3387
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 switch (item.itemType) {
3389 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3390 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003391 ShortcutInfo info = (ShortcutInfo) item;
3392 String uri = info.intent.toUri(0).toString();
3393 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003394 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3395 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003396 boolean animateIconUp = false;
3397 synchronized (newApps) {
3398 if (newApps.contains(uri)) {
3399 animateIconUp = newApps.remove(uri);
3400 }
3401 }
3402 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003403 // Prepare the view to be animated up
3404 shortcut.setAlpha(0f);
3405 shortcut.setScaleX(0f);
3406 shortcut.setScaleY(0f);
3407 mNewShortcutAnimatePage = item.screen;
3408 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3409 mNewShortcutAnimateViews.add(shortcut);
3410 }
3411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003413 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003414 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003415 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003416 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003417 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3418 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003421 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003422
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003424 }
3425
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 /**
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003429 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3430 if (waitUntilResume(new Runnable() {
3431 public void run() {
3432 bindFolders(folders);
3433 }
3434 })) {
3435 return;
3436 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003437 sFolders.clear();
3438 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003439 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440
3441 /**
3442 * Add the views for a widget to the workspace.
3443 *
3444 * Implementation of the method from LauncherModel.Callbacks.
3445 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003446 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3447 if (waitUntilResume(new Runnable() {
3448 public void run() {
3449 bindAppWidget(item);
3450 }
3451 })) {
3452 return;
3453 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003454
Daniel Sandler843e8602010-06-07 14:59:01 -04003455 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3456 if (DEBUG_WIDGETS) {
3457 Log.d(TAG, "bindAppWidget: " + item);
3458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 final Workspace workspace = mWorkspace;
3460
3461 final int appWidgetId = item.appWidgetId;
3462 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003463 if (DEBUG_WIDGETS) {
3464 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3465 }
3466
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3468
Joe Onorato9c1289c2009-08-17 11:03:03 -04003469 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003470 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471
Winson Chung3d503fb2011-07-13 17:25:49 -07003472 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003474 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3475
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 workspace.requestLayout();
3477
Daniel Sandler843e8602010-06-07 14:59:01 -04003478 if (DEBUG_WIDGETS) {
3479 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3480 + (SystemClock.uptimeMillis()-start) + "ms");
3481 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 }
3483
Adam Cohen1462de32012-07-24 22:34:36 -07003484 public void onPageBoundSynchronously(int page) {
3485 mSynchronouslyBoundPages.add(page);
3486 }
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 /**
3489 * Callback saying that there aren't any more items to bind.
3490 *
3491 * Implementation of the method from LauncherModel.Callbacks.
3492 */
3493 public void finishBindingItems() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003494 if (waitUntilResume(new Runnable() {
3495 public void run() {
3496 finishBindingItems();
3497 }
3498 })) {
3499 return;
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 if (mSavedState != null) {
3502 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003503 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 mSavedState = null;
3506 }
3507
Adam Cohen1462de32012-07-24 22:34:36 -07003508 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003510 // If we received the result of any pending adds while the loader was running (e.g. the
3511 // widget configuration forced an orientation change), process them now.
3512 for (int i = 0; i < sPendingAddList.size(); i++) {
3513 completeAdd(sPendingAddList.get(i));
3514 }
3515 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516
Winson Chungc51db6a2011-10-05 11:44:49 -07003517 // Update the market app icon as necessary (the other icons will be managed in response to
3518 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003519 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003520
Winson Chungf0c6ae02012-03-21 16:10:31 -07003521 // Animate up any icons as necessary
3522 if (mVisible || mWorkspaceLoading) {
3523 Runnable newAppsRunnable = new Runnable() {
3524 @Override
3525 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003526 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003527 }
3528 };
Winson Chunga2413752012-04-03 14:22:34 -07003529
3530 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3531 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3532 if (canRunNewAppsAnimation()) {
3533 // If the user has not interacted recently, then either snap to the new page to show
3534 // the new-apps animation or just run them if they are to appear on the current page
3535 if (willSnapPage) {
3536 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3537 } else {
3538 runNewAppsAnimation(false);
3539 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003540 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003541 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003542 // are on another page (or just normally if they are added to the current page)
3543 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003544 }
3545 }
3546
3547 mWorkspaceLoading = false;
3548 }
3549
Winson Chunga2413752012-04-03 14:22:34 -07003550 private boolean canRunNewAppsAnimation() {
3551 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3552 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3553 }
3554
Winson Chungf0c6ae02012-03-21 16:10:31 -07003555 /**
3556 * Runs a new animation that scales up icons that were added while Launcher was in the
3557 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003558 *
3559 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003560 */
Winson Chunga2413752012-04-03 14:22:34 -07003561 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003562 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003563 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003564
3565 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003566 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3567 @Override
3568 public int compare(View a, View b) {
3569 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3570 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3571 int cellCountX = LauncherModel.getCellCountX();
3572 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3573 }
3574 });
Winson Chunga2413752012-04-03 14:22:34 -07003575
3576 // Animate each of the views in place (or show them immediately if requested)
3577 if (immediate) {
3578 for (View v : mNewShortcutAnimateViews) {
3579 v.setAlpha(1f);
3580 v.setScaleX(1f);
3581 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003582 }
Winson Chunga2413752012-04-03 14:22:34 -07003583 } else {
3584 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3585 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003586 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003587 PropertyValuesHolder.ofFloat("alpha", 1f),
3588 PropertyValuesHolder.ofFloat("scaleX", 1f),
3589 PropertyValuesHolder.ofFloat("scaleY", 1f));
3590 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3591 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3592 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3593 bounceAnims.add(bounceAnim);
3594 }
3595 anim.playTogether(bounceAnims);
3596 anim.addListener(new AnimatorListenerAdapter() {
3597 @Override
3598 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003599 if (mWorkspace != null) {
3600 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3601 }
Winson Chunga2413752012-04-03 14:22:34 -07003602 }
3603 });
3604 anim.start();
3605 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003606
3607 // Clean up
3608 mNewShortcutAnimatePage = -1;
3609 mNewShortcutAnimateViews.clear();
3610 new Thread("clearNewAppsThread") {
3611 public void run() {
3612 mSharedPrefs.edit()
3613 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3614 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3615 .commit();
3616 }
3617 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003618 }
3619
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003620 @Override
3621 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003622 boolean searchVisible = updateGlobalSearchIcon();
3623 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003624 if (mSearchDropTargetBar != null) {
3625 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3626 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003627 }
3628
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 * Add the icons for all apps.
3631 *
3632 * Implementation of the method from LauncherModel.Callbacks.
3633 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003634 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003635 Runnable setAllAppsRunnable = new Runnable() {
3636 public void run() {
3637 if (mAppsCustomizeContent != null) {
3638 mAppsCustomizeContent.setApps(apps);
3639 }
3640 }
3641 };
3642
Michael Jurkac57b7a82011-08-09 22:02:20 -07003643 // Remove the progress bar entirely; we could also make it GONE
3644 // but better to remove it since we know it's not going to be used
3645 View progressBar = mAppsCustomizeTabHost.
3646 findViewById(R.id.apps_customize_progress_bar);
3647 if (progressBar != null) {
3648 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003649
3650 // We just post the call to setApps so the user sees the progress bar
3651 // disappear-- otherwise, it just looks like the progress bar froze
3652 // which doesn't look great
3653 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3654 } else {
3655 // If we did not initialize the spinner in onCreate, then we can directly set the
3656 // list of applications without waiting for any progress bars views to be hidden.
3657 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003658 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 }
3660
3661 /**
3662 * A package was installed.
3663 *
3664 * Implementation of the method from LauncherModel.Callbacks.
3665 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003666 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3667 if (waitUntilResume(new Runnable() {
3668 public void run() {
3669 bindAppsAdded(apps);
3670 }
3671 })) {
3672 return;
3673 }
3674
Winson Chungf0ea4d32011-06-06 14:27:16 -07003675
Winson Chung785d2eb2011-04-14 16:08:02 -07003676 if (mAppsCustomizeContent != null) {
3677 mAppsCustomizeContent.addApps(apps);
3678 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003679 }
3680
3681 /**
3682 * A package was updated.
3683 *
3684 * Implementation of the method from LauncherModel.Callbacks.
3685 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003686 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3687 if (waitUntilResume(new Runnable() {
3688 public void run() {
3689 bindAppsUpdated(apps);
3690 }
3691 })) {
3692 return;
3693 }
3694
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003695 if (mWorkspace != null) {
3696 mWorkspace.updateShortcuts(apps);
3697 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003698
Winson Chung785d2eb2011-04-14 16:08:02 -07003699 if (mAppsCustomizeContent != null) {
3700 mAppsCustomizeContent.updateApps(apps);
3701 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003702 }
3703
3704 /**
3705 * A package was uninstalled.
3706 *
3707 * Implementation of the method from LauncherModel.Callbacks.
3708 */
Winson Chungcd810732012-06-18 16:45:43 -07003709 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003710 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003711 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003712 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003713
Winson Chung785d2eb2011-04-14 16:08:02 -07003714 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003715 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003716 }
Winson Chunga1820962011-10-03 16:31:06 -07003717
3718 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003719 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 }
Joe Onoratobe386092009-11-17 17:32:16 -08003721
3722 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003723 * A number of packages were updated.
3724 */
3725 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003726 if (mAppsCustomizeContent != null) {
3727 mAppsCustomizeContent.onPackagesUpdated();
3728 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003729 }
3730
Winson Chung400438b2011-01-16 17:53:48 -08003731 private int mapConfigurationOriActivityInfoOri(int configOri) {
3732 final Display d = getWindowManager().getDefaultDisplay();
3733 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3734 switch (d.getRotation()) {
3735 case Surface.ROTATION_0:
3736 case Surface.ROTATION_180:
3737 // We are currently in the same basic orientation as the natural orientation
3738 naturalOri = configOri;
3739 break;
3740 case Surface.ROTATION_90:
3741 case Surface.ROTATION_270:
3742 // We are currently in the other basic orientation to the natural orientation
3743 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3744 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3745 break;
3746 }
3747
3748 int[] oriMap = {
3749 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3750 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3751 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3752 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3753 };
3754 // Since the map starts at portrait, we need to offset if this device's natural orientation
3755 // is landscape.
3756 int indexOffset = 0;
3757 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3758 indexOffset = 1;
3759 }
3760 return oriMap[(d.getRotation() + indexOffset) % 4];
3761 }
Adam Cohen446e9402011-09-15 18:21:21 -07003762
Winson Chung4b919f82012-05-01 10:44:08 -07003763 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003764 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003765 getResources().getBoolean(R.bool.allow_rotation);
3766 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003767 }
Winson Chung4b919f82012-05-01 10:44:08 -07003768 public void lockScreenOrientation() {
3769 if (isRotationEnabled()) {
3770 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3771 .getConfiguration().orientation));
3772 }
3773 }
3774 public void unlockScreenOrientation(boolean immediate) {
3775 if (isRotationEnabled()) {
3776 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003777 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003778 } else {
3779 mHandler.postDelayed(new Runnable() {
3780 public void run() {
3781 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3782 }
3783 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003784 }
Winson Chung4b919f82012-05-01 10:44:08 -07003785 }
Winson Chung400438b2011-01-16 17:53:48 -08003786 }
3787
Winson Chung82f55532011-08-09 14:14:23 -07003788 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003789 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003790 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003791 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003792
Michael Jurkae233a8b2013-03-19 13:49:20 +01003793 // Restricted secondary users (child mode) will potentially have very few apps
3794 // seeded when they start up for the first time. Clings won't work well with that
Amith Yamasani9178ed82013-04-12 15:16:23 -07003795 boolean supportsLimitedUsers =
Michael Jurkae233a8b2013-03-19 13:49:20 +01003796 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
Amith Yamasanid59b0642013-04-25 15:18:12 -07003797 Account[] accounts = AccountManager.get(this).getAccounts();
3798 if (supportsLimitedUsers && accounts.length == 0) {
3799 UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3800 Bundle restrictions = um.getUserRestrictions();
3801 if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3802 return false;
3803 }
Michael Jurkae233a8b2013-03-19 13:49:20 +01003804 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003805 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003806 }
Michael Jurka22143132012-10-04 17:42:38 +02003807
Winson Chung7d7541e2011-09-16 20:14:36 -07003808 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003809 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003810 if (cling != null) {
3811 cling.init(this, positionData);
3812 cling.setVisibility(View.VISIBLE);
3813 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3814 if (animate) {
3815 cling.buildLayer();
3816 cling.setAlpha(0f);
3817 cling.animate()
3818 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003819 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003820 .setDuration(SHOW_CLING_DURATION)
3821 .setStartDelay(delay)
3822 .start();
3823 } else {
3824 cling.setAlpha(1f);
3825 }
Michael Jurka22143132012-10-04 17:42:38 +02003826 cling.setFocusableInTouchMode(true);
3827 cling.post(new Runnable() {
3828 public void run() {
3829 cling.setFocusable(true);
3830 cling.requestFocus();
3831 }
3832 });
3833 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3834 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003835 }
3836 return cling;
3837 }
Michael Jurka22143132012-10-04 17:42:38 +02003838
Winson Chung7d7541e2011-09-16 20:14:36 -07003839 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003840 // To catch cases where siblings of top-level views are made invisible, just check whether
3841 // the cling is directly set to GONE before dismissing it.
3842 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003843 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003844 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003845 anim.addListener(new AnimatorListenerAdapter() {
3846 public void onAnimationEnd(Animator animation) {
3847 cling.setVisibility(View.GONE);
3848 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003849 // We should update the shared preferences on a background thread
3850 new Thread("dismissClingThread") {
3851 public void run() {
3852 SharedPreferences.Editor editor = mSharedPrefs.edit();
3853 editor.putBoolean(flag, true);
3854 editor.commit();
3855 }
3856 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003857 };
3858 });
3859 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003860 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003861 }
3862 }
Michael Jurka22143132012-10-04 17:42:38 +02003863
Winson Chung9d9d74f2011-09-19 11:49:12 -07003864 private void removeCling(int id) {
3865 final View cling = findViewById(id);
3866 if (cling != null) {
3867 final ViewGroup parent = (ViewGroup) cling.getParent();
3868 parent.post(new Runnable() {
3869 @Override
3870 public void run() {
3871 parent.removeView(cling);
3872 }
3873 });
Michael Jurka22143132012-10-04 17:42:38 +02003874 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003875 }
3876 }
Michael Jurka974c3862012-05-22 22:00:31 -07003877
3878 private boolean skipCustomClingIfNoAccounts() {
3879 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3880 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3881 if (customCling) {
3882 AccountManager am = AccountManager.get(this);
3883 Account[] accounts = am.getAccountsByType("com.google");
3884 return accounts.length == 0;
3885 }
3886 return false;
3887 }
3888
Winson Chung7d7541e2011-09-16 20:14:36 -07003889 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003890 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003891 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003892 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3893 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02003894 // If we're not using the default workspace layout, replace workspace cling
3895 // with a custom workspace cling (usually specified in an overlay)
3896 // For now, only do this on tablets
3897 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02003898 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02003899 // Use a custom cling
3900 View cling = findViewById(R.id.workspace_cling);
3901 ViewGroup clingParent = (ViewGroup) cling.getParent();
3902 int clingIndex = clingParent.indexOfChild(cling);
3903 clingParent.removeViewAt(clingIndex);
3904 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3905 clingParent.addView(customCling, clingIndex);
3906 customCling.setId(R.id.workspace_cling);
3907 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003908 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003909 } else {
3910 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003911 }
3912 }
3913 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003914 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003915 if (isClingsEnabled() &&
3916 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003917 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003918 } else {
3919 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003920 }
3921 }
3922 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003923 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003924 if (isClingsEnabled() &&
3925 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3926 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003927 } else {
3928 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003929 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003930 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003931 }
3932 public boolean isFolderClingVisible() {
3933 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003934 if (cling != null) {
3935 return cling.getVisibility() == View.VISIBLE;
3936 }
3937 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003938 }
Winson Chung82f55532011-08-09 14:14:23 -07003939 public void dismissWorkspaceCling(View v) {
3940 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003941 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003942 }
3943 public void dismissAllAppsCling(View v) {
3944 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003945 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3946 }
3947 public void dismissFolderCling(View v) {
3948 Cling cling = (Cling) findViewById(R.id.folder_cling);
3949 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003950 }
3951
Winson Chung80baf5a2010-08-09 16:03:15 -07003952 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003953 * Prints out out state for debugging.
3954 */
3955 public void dumpState() {
3956 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003957 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003958 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3959 Log.d(TAG, "mRestoring=" + mRestoring);
3960 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3961 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003962 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003963 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003964
Winson Chung785d2eb2011-04-14 16:08:02 -07003965 if (mAppsCustomizeContent != null) {
3966 mAppsCustomizeContent.dumpState();
3967 }
Joe Onoratobe386092009-11-17 17:32:16 -08003968 Log.d(TAG, "END launcher2 dump state");
3969 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003970
3971 @Override
3972 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3973 super.dump(prefix, fd, writer, args);
3974 writer.println(" ");
3975 writer.println("Debug logs: ");
3976 for (int i = 0; i < sDumpLogs.size(); i++) {
3977 writer.println(" " + sDumpLogs.get(i));
3978 }
3979 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003980
3981 public static void dumpDebugLogsToConsole() {
3982 Log.d(TAG, "");
3983 Log.d(TAG, "*********************");
3984 Log.d(TAG, "Launcher debug logs: ");
3985 for (int i = 0; i < sDumpLogs.size(); i++) {
3986 Log.d(TAG, " " + sDumpLogs.get(i));
3987 }
3988 Log.d(TAG, "*********************");
3989 Log.d(TAG, "");
3990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003991}
Michael Jurka2763be32011-02-24 11:19:57 -08003992
Michael Jurkaabded662011-03-04 12:06:57 -08003993interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003994 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003995 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003996 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003997 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003998 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003999}