blob: 56a854c006fa5687adf0458ff1b1b81aaea8f05b [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
Winson Chung4a2afa32012-07-19 14:53:05 -0700253 // Keep track of whether the user has left launcher
254 private static boolean sPausedFromUserAction = false;
255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 private Bundle mSavedInstanceState;
257
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800259 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800260 private boolean mUserPresent = true;
261 private boolean mVisible = false;
262 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700264 private static LocaleConfiguration sLocaleConfiguration = null;
265
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700266 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700267
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700268 private Intent mAppMarketIntent = null;
269
Adam Cohended9f8d2010-11-03 13:25:16 -0700270 // Related to the auto-advancing of widgets
271 private final int ADVANCE_MSG = 1;
272 private final int mAdvanceInterval = 20000;
273 private final int mAdvanceStagger = 250;
274 private long mAutoAdvanceSentTime;
275 private long mAutoAdvanceTimeLeft = -1;
276 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
277 new HashMap<View, AppWidgetProviderInfo>();
278
Winson Chung400438b2011-01-16 17:53:48 -0800279 // Determines how long to wait after a rotation before restoring the screen orientation to
280 // match the sensor state.
281 private final int mRestoreScreenOrientationDelay = 500;
282
Michael Jurka4ef207b2010-11-29 17:05:45 -0800283 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700284 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
285 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
286 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800287
Craig Mautner360310b2012-10-26 15:13:08 -0700288 private Drawable mWorkspaceBackgroundDrawable;
289 private Drawable mBlackBackgroundDrawable;
290
Adam Cohen1462de32012-07-24 22:34:36 -0700291 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
292
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700293 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
294
Winson Chung46353de2012-02-16 14:05:10 -0800295 // We only want to get the SharedPreferences once since it does an FS stat each time we get
296 // it from the context.
297 private SharedPreferences mSharedPrefs;
298
Winson Chungf0c6ae02012-03-21 16:10:31 -0700299 // Holds the page that we need to animate to, and the icon views that we need to animate up
300 // when we scroll to that page on resume.
301 private int mNewShortcutAnimatePage = -1;
302 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700303 private ImageView mFolderIconImageView;
304 private Bitmap mFolderIconBitmap;
305 private Canvas mFolderIconCanvas;
306 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700307
Michael Jurkaddd62e92011-02-16 17:49:14 -0800308 private BubbleTextView mWaitingForResume;
309
Michael Jurka22143132012-10-04 17:42:38 +0200310 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
311 = new HideFromAccessibilityHelper();
312
Michael Jurkac1f5d262011-09-30 19:32:27 -0700313 private Runnable mBuildLayersRunnable = new Runnable() {
314 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700315 if (mWorkspace != null) {
316 mWorkspace.buildPageHardwareLayers();
317 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700318 }
319 };
320
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800321 private static ArrayList<PendingAddArguments> sPendingAddList
322 = new ArrayList<PendingAddArguments>();
323
Michael Jurka0a457bf2012-11-19 14:05:05 -0800324 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
325
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800326 private static class PendingAddArguments {
327 int requestCode;
328 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700329 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800330 int screen;
331 int cellX;
332 int cellY;
333 }
334
Michael Jurka0a457bf2012-11-19 14:05:05 -0800335 private static boolean isPropertyEnabled(String propertyName) {
336 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700337 }
338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 @Override
340 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700341 if (DEBUG_STRICT_MODE) {
342 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
343 .detectDiskReads()
344 .detectDiskWrites()
345 .detectNetwork() // or .detectAll() for all detectable problems
346 .penaltyLog()
347 .build());
348 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
349 .detectLeakedSqlLiteObjects()
350 .detectLeakedClosableObjects()
351 .penaltyLog()
352 .penaltyDeath()
353 .build());
354 }
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700358 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
359 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800360 mModel = app.setLauncher(this);
361 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400362 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700364
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700365 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
367 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700368
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700369 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
370 // this also ensures that any synchronous binding below doesn't re-trigger another
371 // LauncherModel load.
372 mPaused = false;
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200375 android.os.Debug.startMethodTracing(
376 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
378
379 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 setContentView(R.layout.launcher);
381 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700382 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800384 registerContentObservers();
385
Joe Onorato7c312c12009-08-13 21:36:53 -0700386 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 mSavedState = savedInstanceState;
389 restoreState(mSavedState);
390
Winson Chunga12a2502010-12-20 14:41:35 -0800391 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700392 if (mAppsCustomizeContent != null) {
393 mAppsCustomizeContent.onPackagesUpdated();
394 }
Winson Chunga12a2502010-12-20 14:41:35 -0800395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 if (PROFILE_STARTUP) {
397 android.os.Debug.stopMethodTracing();
398 }
399
400 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700401 if (sPausedFromUserAction) {
402 // If the user leaves launcher, then we should just load items asynchronously when
403 // they return.
404 mModel.startLoader(true, -1);
405 } else {
406 // We only load the page synchronously if the user rotates (or triggers a
407 // configuration change) while launcher is in the foreground
408 mModel.startLoader(true, mWorkspace.getCurrentPage());
409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
411
Michael Jurkac57b7a82011-08-09 22:02:20 -0700412 if (!mModel.isAllAppsLoaded()) {
413 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
414 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
415 }
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 // For handling default keys
418 mDefaultKeySsb = new SpannableStringBuilder();
419 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800420
421 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
422 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800423
Adam Cohenf9426d52012-06-04 17:26:21 -0700424 updateGlobalIcons();
425
426 // On large interfaces, we want the screen to auto-rotate based on the current orientation
427 unlockScreenOrientation(true);
428 }
429
Winson Chung4a2afa32012-07-19 14:53:05 -0700430 protected void onUserLeaveHint() {
431 super.onUserLeaveHint();
432 sPausedFromUserAction = true;
433 }
434
Adam Cohenf9426d52012-06-04 17:26:21 -0700435 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700436 boolean searchVisible = false;
437 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800438 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700439 int coi = getCurrentOrientationIndexForGlobalIcons();
440 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
441 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700442 updateAppMarketIcon();
443 searchVisible = updateGlobalSearchIcon();
444 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700445 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700446 if (sGlobalSearchIcon[coi] != null) {
447 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700450 if (sVoiceSearchIcon[coi] != null) {
451 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700452 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700454 if (sAppMarketIcon[coi] != null) {
455 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800456 }
Winson Chungadf0c182012-08-23 14:59:07 -0700457 if (mSearchDropTargetBar != null) {
458 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
Romain Guycbb89e42009-06-08 15:52:54 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 if (sLocaleConfiguration == null) {
464 new AsyncTask<Void, Void, LocaleConfiguration>() {
465 @Override
466 protected LocaleConfiguration doInBackground(Void... unused) {
467 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
468 readConfiguration(Launcher.this, localeConfiguration);
469 return localeConfiguration;
470 }
471
472 @Override
473 protected void onPostExecute(LocaleConfiguration result) {
474 sLocaleConfiguration = result;
475 checkForLocaleChange(); // recursive, but now with a locale configuration
476 }
477 }.execute();
478 return;
479 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final Configuration configuration = getResources().getConfiguration();
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final String locale = configuration.locale.toString();
485
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final int mcc = configuration.mcc;
488
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700489 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 final int mnc = configuration.mnc;
491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
Romain Guy84f296c2009-11-04 15:00:44 -0800494 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 sLocaleConfiguration.locale = locale;
496 sLocaleConfiguration.mcc = mcc;
497 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700498
Joe Onorato0589f0f2010-02-08 13:44:00 -0800499 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500
501 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
502 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700503 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 public void run() {
505 writeConfiguration(Launcher.this, localeConfiguration);
506 }
507 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700508 }
509 }
510
511 private static class LocaleConfiguration {
512 public String locale;
513 public int mcc = -1;
514 public int mnc = -1;
515 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700516
Romain Guy98d01652009-06-30 16:21:04 -0700517 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
518 DataInputStream in = null;
519 try {
520 in = new DataInputStream(context.openFileInput(PREFERENCES));
521 configuration.locale = in.readUTF();
522 configuration.mcc = in.readInt();
523 configuration.mnc = in.readInt();
524 } catch (FileNotFoundException e) {
525 // Ignore
526 } catch (IOException e) {
527 // Ignore
528 } finally {
529 if (in != null) {
530 try {
531 in.close();
532 } catch (IOException e) {
533 // Ignore
534 }
535 }
536 }
537 }
538
539 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
540 DataOutputStream out = null;
541 try {
542 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
543 out.writeUTF(configuration.locale);
544 out.writeInt(configuration.mcc);
545 out.writeInt(configuration.mnc);
546 out.flush();
547 } catch (FileNotFoundException e) {
548 // Ignore
549 } catch (IOException e) {
550 //noinspection ResultOfMethodCallIgnored
551 context.getFileStreamPath(PREFERENCES).delete();
552 } finally {
553 if (out != null) {
554 try {
555 out.close();
556 } catch (IOException e) {
557 // Ignore
558 }
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561 }
562
Adam Cohen716b51e2011-06-30 12:09:54 -0700563 public DragLayer getDragLayer() {
564 return mDragLayer;
565 }
566
Winson Chung36a62fe2012-05-06 18:04:42 -0700567 boolean isDraggingEnabled() {
568 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
569 // that is subsequently removed from the workspace in startBinding().
570 return !mModel.isLoadingWorkspace();
571 }
572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 static int getScreen() {
574 synchronized (sLock) {
575 return sScreen;
576 }
577 }
578
579 static void setScreen(int screen) {
580 synchronized (sLock) {
581 sScreen = screen;
582 }
583 }
584
Winson Chung557d6ed2011-07-08 15:34:52 -0700585 /**
586 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
587 * a configuration step, this allows the proper animations to run after other transitions.
588 */
589 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700590 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800591 switch (args.requestCode) {
592 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700593 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
594 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 break;
596 case REQUEST_PICK_SHORTCUT:
597 processShortcut(args.intent);
598 break;
599 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700600 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
601 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 result = true;
603 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 case REQUEST_CREATE_APPWIDGET:
605 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700607 result = true;
608 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800609 case REQUEST_PICK_WALLPAPER:
610 // We just wanted the activity result here so we can clear mWaitingForResult
611 break;
612 }
Michael Jurka27614d22012-04-02 06:40:22 -0700613 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
614 // if you turned the screen off and then back while in All Apps, Launcher would not
615 // return to the workspace. Clearing mAddInfo.container here fixes this issue
616 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700617 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 protected void onActivityResult(
622 final int requestCode, final int resultCode, final Intent data) {
623 if (requestCode == REQUEST_BIND_APPWIDGET) {
624 int appWidgetId = data != null ?
625 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
626 if (resultCode == RESULT_CANCELED) {
627 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
628 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700629 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 }
631 return;
632 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700633 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800634 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
635 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700636 mWaitingForResult = false;
637
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 // We have special handling for widgets
639 if (isWidgetDrop) {
640 int appWidgetId = data != null ?
641 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700642 if (appWidgetId < 0) {
643 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
644 "widget configuration activity.");
645 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
646 } else {
647 completeTwoStageWidgetDrop(resultCode, appWidgetId);
648 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 return;
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // The pattern used here is that a user PICKs a specific application,
653 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
656 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700657 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 final PendingAddArguments args = new PendingAddArguments();
659 args.requestCode = requestCode;
660 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700661 args.container = mPendingAddInfo.container;
662 args.screen = mPendingAddInfo.screen;
663 args.cellX = mPendingAddInfo.cellX;
664 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 if (isWorkspaceLocked()) {
666 sPendingAddList.add(args);
667 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700668 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
674 null);
675 }
676
677 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700678 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700679 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 Runnable onCompleteRunnable = null;
681 int animationType = 0;
682
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700683 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 if (resultCode == RESULT_OK) {
685 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
686 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 mPendingAddWidgetInfo);
688 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 onCompleteRunnable = new Runnable() {
690 @Override
691 public void run() {
692 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
693 mPendingAddInfo.screen, layout, null);
694 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
695 null);
696 }
697 };
698 } else if (resultCode == RESULT_CANCELED) {
699 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
700 onCompleteRunnable = new Runnable() {
701 @Override
702 public void run() {
703 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
704 null);
705 }
706 };
707 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700708 if (mDragLayer.getAnimatedView() != null) {
709 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
710 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
711 animationType, boundWidget, true);
712 } else {
713 // The animated view may be null in the case of a rotation during widget configuration
714 onCompleteRunnable.run();
715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
718 @Override
719 protected void onResume() {
720 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700721
Winson Chung4a2afa32012-07-19 14:53:05 -0700722 // Restore the previous launcher state
723 if (mOnResumeState == State.WORKSPACE) {
724 showWorkspace(false);
725 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
726 showAllApps(false);
727 }
728 mOnResumeState = State.NONE;
729
Craig Mautner360310b2012-10-26 15:13:08 -0700730 // Background was set to gradient in onPause(), restore to black if in all apps.
731 setWorkspaceBackground(mState == State.WORKSPACE);
732
Winson Chungde0fb8f2012-05-08 14:37:08 -0700733 // Process any items that were added while Launcher was away
734 InstallShortcutReceiver.flushInstallQueue(this);
735
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800736 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700737 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700738 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700740 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700742 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
Winson Chunge4e50662012-01-23 14:45:13 -0800744
745 // Reset the pressed state of icons that were locked in the press state while activities
746 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800747 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800748 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800749 mWaitingForResume.setStayPressed(false);
750 }
Winson Chunge4e50662012-01-23 14:45:13 -0800751 if (mAppsCustomizeContent != null) {
752 // Resets the previous all apps icon press state
753 mAppsCustomizeContent.resetDrawableState();
754 }
Adam Cohen06dff352012-06-01 17:17:08 -0700755 // It is possible that widgets can receive updates while launcher is not in the foreground.
756 // Consequently, the widgets will be inflated in the orientation of the foreground activity
757 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
758 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700759 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700760
761 // Again, as with the above scenario, it's possible that one or more of the global icons
762 // were updated in the wrong orientation.
763 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700768 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
769 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
770 // when Launcher resumes and we are still in AllApps.
771 updateWallpaperVisibility(true);
772
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700774 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800775 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700776 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700777 }
Romain Guycbb89e42009-06-08 15:52:54 -0700778
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700779 @Override
780 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781 // Flag the loader to stop early before switching
782 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700783 if (mAppsCustomizeContent != null) {
784 mAppsCustomizeContent.surrender();
785 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800786 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700787 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700788
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800789 // We can't hide the IME if it was forced open. So don't bother
790 /*
791 @Override
792 public void onWindowFocusChanged(boolean hasFocus) {
793 super.onWindowFocusChanged(hasFocus);
794
795 if (hasFocus) {
796 final InputMethodManager inputManager = (InputMethodManager)
797 getSystemService(Context.INPUT_METHOD_SERVICE);
798 WindowManager.LayoutParams lp = getWindow().getAttributes();
799 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
800 android.os.Handler()) {
801 protected void onReceiveResult(int resultCode, Bundle resultData) {
802 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
803 }
804 });
805 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
806 }
807 }
808 */
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 private boolean acceptFilter() {
811 final InputMethodManager inputManager = (InputMethodManager)
812 getSystemService(Context.INPUT_METHOD_SERVICE);
813 return !inputManager.isFullscreenMode();
814 }
815
816 @Override
817 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700818 final int uniChar = event.getUnicodeChar();
819 final boolean handled = super.onKeyDown(keyCode, event);
820 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
821 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
823 keyCode, event);
824 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700825 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700826 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700827 // showSearchDialog()
828 // If there are multiple keystrokes before the search dialog takes focus,
829 // onSearchRequested() will be called for every keystroke,
830 // but it is idempotent, so it's fine.
831 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833 }
834
Joe Onorato8a9625e2010-01-28 15:55:35 -0800835 // Eat the long press event so the keyboard doesn't come up.
836 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
837 return true;
838 }
839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 return handled;
841 }
842
Karl Rosaen138a0412009-04-23 19:00:21 -0700843 private String getTypedText() {
844 return mDefaultKeySsb.toString();
845 }
846
847 private void clearTypedText() {
848 mDefaultKeySsb.clear();
849 mDefaultKeySsb.clearSpans();
850 Selection.setSelection(mDefaultKeySsb, 0);
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700854 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
855 * State
856 */
857 private static State intToState(int stateOrdinal) {
858 State state = State.WORKSPACE;
859 final State[] stateValues = State.values();
860 for (int i = 0; i < stateValues.length; i++) {
861 if (stateValues[i].ordinal() == stateOrdinal) {
862 state = stateValues[i];
863 break;
864 }
865 }
866 return state;
867 }
868
869 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 * Restores the previous state, if it exists.
871 *
872 * @param savedState The previous state.
873 */
874 private void restoreState(Bundle savedState) {
875 if (savedState == null) {
876 return;
877 }
878
Michael Jurka883f55b2010-10-21 15:47:14 -0700879 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700880 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700881 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800882 }
883
Winson Chungf0c6ae02012-03-21 16:10:31 -0700884 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700886 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
890 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700891
Winson Chung3d503fb2011-07-13 17:25:49 -0700892 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
893 mPendingAddInfo.container = pendingAddContainer;
894 mPendingAddInfo.screen = pendingAddScreen;
895 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
896 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
898 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
899 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700900 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 mRestoring = true;
902 }
903
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
906 if (renameFolder) {
907 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700908 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 mRestoring = true;
910 }
Winson Chunga12a2502010-12-20 14:41:35 -0800911
Winson Chung785d2eb2011-04-14 16:08:02 -0700912
913 // Restore the AppsCustomize tab
914 if (mAppsCustomizeTabHost != null) {
915 String curTab = savedState.getString("apps_customize_currentTab");
916 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700917 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700919 mAppsCustomizeContent.loadAssociatedPages(
920 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700921 }
922
Winson Chung5afbf7b2011-07-25 11:53:08 -0700923 int currentIndex = savedState.getInt("apps_customize_currentIndex");
924 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927
928 /**
929 * Finds all the views we need and configure them properly.
930 */
931 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700932 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400933
Craig Mautner360310b2012-10-26 15:13:08 -0700934 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700935 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
936 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700937 mQsbDivider = findViewById(R.id.qsb_divider);
938 mDockDivider = findViewById(R.id.dock_divider);
939
940 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
941 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
942 mBlackBackgroundDrawable = new ColorDrawable(Color.BLACK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943
Winson Chung4c98d922011-05-31 16:50:48 -0700944 // Setup the drag layer
945 mDragLayer.setup(this, dragController);
946
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 // Setup the hotseat
948 mHotseat = (Hotseat) findViewById(R.id.hotseat);
949 if (mHotseat != null) {
950 mHotseat.setup(this);
951 }
952
Winson Chung4c98d922011-05-31 16:50:48 -0700953 // Setup the workspace
954 mWorkspace.setHapticFeedbackEnabled(false);
955 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700956 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700957 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700958
Winson Chungf0ea4d32011-06-06 14:27:16 -0700959 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700960 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700961
Winson Chungf0ea4d32011-06-06 14:27:16 -0700962 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -0700963 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700964 mAppsCustomizeContent = (AppsCustomizePagedView)
965 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
966 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -0700967
Winson Chung3d503fb2011-07-13 17:25:49 -0700968 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700969 dragController.setDragScoller(mWorkspace);
970 dragController.setScrollView(mDragLayer);
971 dragController.setMoveTarget(mWorkspace);
972 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700973 if (mSearchDropTargetBar != null) {
974 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 /**
979 * Creates a view representing a shortcut.
980 *
981 * @param info The data structure describing the shortcut.
982 *
983 * @return A View inflated from R.layout.application.
984 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800985 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700987 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
989
990 /**
991 * Creates a view representing a shortcut inflated from the specified resource.
992 *
993 * @param layoutResId The id of the XML layout used to create the shortcut.
994 * @param parent The group the shortcut belongs to.
995 * @param info The data structure describing the shortcut.
996 *
997 * @return A View inflated from layoutResId.
998 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001000 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1001 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 return favorite;
1004 }
1005
1006 /**
1007 * Add an application shortcut to the workspace.
1008 *
1009 * @param data The intent describing the application.
1010 * @param cellInfo The position on screen where to create the shortcut.
1011 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001012 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001013 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001015
Adam Cohenfbba09b2011-07-18 21:43:05 -07001016 // First we check if we already know the exact location where we want to add this item.
1017 if (cellX >= 0 && cellY >= 0) {
1018 cellXY[0] = cellX;
1019 cellXY[1] = cellY;
1020 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001021 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001022 return;
1023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001025 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026
Romain Guy73b979d2009-06-09 12:57:21 -07001027 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001028 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001030 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001031 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001032 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001033 } else {
1034 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 /**
1039 * Add a shortcut to the workspace.
1040 *
1041 * @param data The intent describing the shortcut.
1042 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001044 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1045 int cellY) {
1046 int[] cellXY = mTmpAddItemCellCoordinates;
1047 int[] touchXY = mPendingAddInfo.dropPos;
1048 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001049
Michael Jurkad3ef3062010-11-23 16:23:58 -08001050 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001051
Adam Cohen558baaf2011-08-15 15:22:57 -07001052 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001053 if (info == null) {
1054 return;
1055 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001056 final View view = createShortcut(info);
1057
Adam Cohenfbba09b2011-07-18 21:43:05 -07001058 // First we check if we already know the exact location where we want to add this item.
1059 if (cellX >= 0 && cellY >= 0) {
1060 cellXY[0] = cellX;
1061 cellXY[1] = cellY;
1062 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001063
1064 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001065 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001066 true, null,null)) {
1067 return;
1068 }
1069 DragObject dragObject = new DragObject();
1070 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001071 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1072 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001073 return;
1074 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001075 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001076 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001078 foundCellSpan = (result != null);
1079 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001080 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001081 }
1082
1083 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001084 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 return;
1086 }
1087
Adam Cohen558baaf2011-08-15 15:22:57 -07001088 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089
1090 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1092 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094 }
1095
Adam Cohen2f093b62012-04-30 18:59:53 -07001096 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1097 int minHeight) {
1098 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001099 // We want to account for the extra amount of padding that we are adding to the widget
1100 // to ensure that it gets the full amount of space that it has requested
1101 int requiredWidth = minWidth + padding.left + padding.right;
1102 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001103 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001104 }
1105
Adam Cohen2f093b62012-04-30 18:59:53 -07001106 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1107 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001108 }
1109
Adam Cohen2f093b62012-04-30 18:59:53 -07001110 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1111 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001112 }
1113
Adam Cohen2f093b62012-04-30 18:59:53 -07001114 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1115 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001116 }
1117
Adam Cohen2f093b62012-04-30 18:59:53 -07001118 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1119 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001120 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001121 }
1122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001124 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001126 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001127 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001129 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1130 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1131 if (appWidgetInfo == null) {
1132 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1133 }
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001135 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001136 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001137
Adam Cohen2f093b62012-04-30 18:59:53 -07001138 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1139 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001142 // We have saved the position to which the widget was dragged-- this really only matters
1143 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001144 int[] cellXY = mTmpAddItemCellCoordinates;
1145 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001146 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001148 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1149 cellXY[0] = mPendingAddInfo.cellX;
1150 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 spanXY[0] = mPendingAddInfo.spanX;
1152 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001153 foundCellSpan = true;
1154 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001155 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001156 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1158 spanXY[1], cellXY, finalSpan);
1159 spanXY[0] = finalSpan[0];
1160 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001161 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001162 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001164 }
1165
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001167 if (appWidgetId != -1) {
1168 // Deleting an app widget ID is a void call but writes to disk before returning
1169 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001170 new Thread("deleteAppWidgetId") {
1171 public void run() {
1172 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1173 }
1174 }.start();
1175 }
Winson Chung93eef082012-03-23 15:59:27 -07001176 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001177 return;
1178 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001181 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1182 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183 launcherInfo.spanX = spanXY[0];
1184 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001185 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1186 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190
1191 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001192 if (hostView == null) {
1193 // Perform actual inflation because we're live
1194 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1195 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1196 } else {
1197 // The AppWidgetHostView has already been inflated and instantiated
1198 launcherInfo.hostView = hostView;
1199 }
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001202 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001203 launcherInfo.notifyWidgetSizeChanged(this);
1204
Winson Chung3d503fb2011-07-13 17:25:49 -07001205 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001207
1208 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001210 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
Romain Guycbb89e42009-06-08 15:52:54 -07001212
Adam Cohended9f8d2010-11-03 13:25:16 -07001213 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1214 @Override
1215 public void onReceive(Context context, Intent intent) {
1216 final String action = intent.getAction();
1217 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1218 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001219 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001220 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001221
Winson Chungc100e8e2011-08-09 16:02:43 -07001222 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001223 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001224 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1225 mAppsCustomizeTabHost.reset();
1226 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001227 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001228 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1229 mUserPresent = true;
1230 updateRunning();
1231 }
1232 }
1233 };
1234
1235 @Override
1236 public void onAttachedToWindow() {
1237 super.onAttachedToWindow();
1238
1239 // Listen for broadcasts related to user-presence
1240 final IntentFilter filter = new IntentFilter();
1241 filter.addAction(Intent.ACTION_SCREEN_OFF);
1242 filter.addAction(Intent.ACTION_USER_PRESENT);
1243 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001244 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001245 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 mVisible = true;
1247 }
1248
1249 @Override
1250 public void onDetachedFromWindow() {
1251 super.onDetachedFromWindow();
1252 mVisible = false;
1253
Adam Cohend113e0c2010-11-11 10:48:05 -08001254 if (mAttached) {
1255 unregisterReceiver(mReceiver);
1256 mAttached = false;
1257 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001258 updateRunning();
1259 }
1260
1261 public void onWindowVisibilityChanged(int visibility) {
1262 mVisible = visibility == View.VISIBLE;
1263 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001264 // The following code used to be in onResume, but it turns out onResume is called when
1265 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1266 // is a more appropriate event to handle
1267 if (mVisible) {
1268 mAppsCustomizeTabHost.onWindowVisible();
1269 if (!mWorkspaceLoading) {
1270 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001271 // We want to let Launcher draw itself at least once before we force it to build
1272 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001273 // apps is nice and speedy.
1274 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001275 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001276 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001277 if (mStarted) return;
1278 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001279 // We delay the layer building a bit in order to give
1280 // other message processing a time to run. In particular
1281 // this avoids a delay in hiding the IME if it was
1282 // currently shown, because doing that may involve
1283 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001284 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001285 final ViewTreeObserver.OnDrawListener listener = this;
1286 mWorkspace.post(new Runnable() {
1287 public void run() {
1288 mWorkspace.getViewTreeObserver().removeOnDrawListener(listener);
1289 }
1290 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001291 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001292 }
1293 });
1294 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001295 // When Launcher comes back to foreground, a different Activity might be responsible for
1296 // the app market intent, so refresh the icon
1297 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001298 clearTypedText();
1299 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001300 }
1301
1302 private void sendAdvanceMessage(long delay) {
1303 mHandler.removeMessages(ADVANCE_MSG);
1304 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1305 mHandler.sendMessageDelayed(msg, delay);
1306 mAutoAdvanceSentTime = System.currentTimeMillis();
1307 }
1308
1309 private void updateRunning() {
1310 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1311 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1312 mAutoAdvanceRunning = autoAdvanceRunning;
1313 if (autoAdvanceRunning) {
1314 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1315 sendAdvanceMessage(delay);
1316 } else {
1317 if (!mWidgetsToAdvance.isEmpty()) {
1318 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1319 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1320 }
1321 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001322 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001323 }
1324 }
1325 }
1326
1327 private final Handler mHandler = new Handler() {
1328 @Override
1329 public void handleMessage(Message msg) {
1330 if (msg.what == ADVANCE_MSG) {
1331 int i = 0;
1332 for (View key: mWidgetsToAdvance.keySet()) {
1333 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1334 final int delay = mAdvanceStagger * i;
1335 if (v instanceof Advanceable) {
1336 postDelayed(new Runnable() {
1337 public void run() {
1338 ((Advanceable) v).advance();
1339 }
1340 }, delay);
1341 }
1342 i++;
1343 }
1344 sendAdvanceMessage(mAdvanceInterval);
1345 }
1346 }
1347 };
1348
1349 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001350 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001351 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1352 if (v instanceof Advanceable) {
1353 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001354 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 updateRunning();
1356 }
1357 }
1358
1359 void removeWidgetToAutoAdvance(View hostView) {
1360 if (mWidgetsToAdvance.containsKey(hostView)) {
1361 mWidgetsToAdvance.remove(hostView);
1362 updateRunning();
1363 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 }
1365
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 launcherInfo.hostView = null;
1369 }
1370
Winson Chung93eef082012-03-23 15:59:27 -07001371 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1372 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1373 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 }
1375
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001376 public LauncherAppWidgetHost getAppWidgetHost() {
1377 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
Romain Guycbb89e42009-06-08 15:52:54 -07001379
Winson Chunga9abd0e2010-10-27 17:18:37 -07001380 public LauncherModel getModel() {
1381 return mModel;
1382 }
1383
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001384 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001385 getWindow().closeAllPanels();
1386
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001387 // Whatever we were doing is hereby canceled.
1388 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 @Override
1392 protected void onNewIntent(Intent intent) {
1393 super.onNewIntent(intent);
1394
1395 // Close the menu
1396 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001397 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001398 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001399
Jamie Genniscb222e82012-10-23 15:44:26 -07001400 final boolean alreadyOnHome =
1401 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001402 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001403
Jamie Genniscb222e82012-10-23 15:44:26 -07001404 Runnable processIntent = new Runnable() {
1405 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001406 if (mWorkspace == null) {
1407 // Can be cases where mWorkspace is null, this prevents a NPE
1408 return;
1409 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001410 Folder openFolder = mWorkspace.getOpenFolder();
1411 // In all these cases, only animate if we're already on home
1412 mWorkspace.exitWidgetResizeMode();
1413 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1414 openFolder == null) {
1415 mWorkspace.moveToDefaultScreen(true);
1416 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001417
Jamie Genniscb222e82012-10-23 15:44:26 -07001418 closeFolder();
1419 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001420
Jamie Genniscb222e82012-10-23 15:44:26 -07001421 // If we are already on home, then just animate back to the workspace,
1422 // otherwise, just wait until onResume to set the state back to Workspace
1423 if (alreadyOnHome) {
1424 showWorkspace(true);
1425 } else {
1426 mOnResumeState = State.WORKSPACE;
1427 }
1428
1429 final View v = getWindow().peekDecorView();
1430 if (v != null && v.getWindowToken() != null) {
1431 InputMethodManager imm = (InputMethodManager)getSystemService(
1432 INPUT_METHOD_SERVICE);
1433 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1434 }
1435
1436 // Reset AllApps to its initial state
1437 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1438 mAppsCustomizeTabHost.reset();
1439 }
1440 }
1441 };
1442
1443 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1444 // Delay processing of the intent to allow the status bar animation to finish
1445 // first in order to avoid janky animations.
1446 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001447 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001448 // Process the intent immediately.
1449 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001450 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453 }
1454
1455 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001456 public void onRestoreInstanceState(Bundle state) {
1457 super.onRestoreInstanceState(state);
1458 for (int page: mSynchronouslyBoundPages) {
1459 mWorkspace.restoreInstanceStateForChild(page);
1460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462
1463 @Override
1464 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001465 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001466 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467
Michael Jurka883f55b2010-10-21 15:47:14 -07001468 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001469 // We close any open folder since it will not be re-opened, and we need to make sure
1470 // this state is reflected.
1471 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
Winson Chung3d503fb2011-07-13 17:25:49 -07001473 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1474 mWaitingForResult) {
1475 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1476 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1477 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1478 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001479 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1480 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1481 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
1484 if (mFolderInfo != null && mWaitingForResult) {
1485 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1486 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1487 }
Michael Jurka946ad472010-07-09 18:05:18 -07001488
Winson Chung785d2eb2011-04-14 16:08:02 -07001489 // Save the current AppsCustomize tab
1490 if (mAppsCustomizeTabHost != null) {
1491 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1492 if (currentTabTag != null) {
1493 outState.putString("apps_customize_currentTab", currentTabTag);
1494 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001495 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1496 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
1500 @Override
1501 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001503
Winson Chunge7a03942011-08-05 15:05:12 -07001504 // Remove all pending runnables
1505 mHandler.removeMessages(ADVANCE_MSG);
1506 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001507 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001508
Winson Chungcd2b0142011-06-08 16:02:26 -07001509 // Stop callbacks from LauncherModel
1510 LauncherApplication app = ((LauncherApplication) getApplication());
1511 mModel.stopLoader();
1512 app.setLauncher(null);
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001517 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001519 mAppWidgetHost = null;
1520
1521 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
1523 TextKeyListener.getInstance().release();
1524
Winson Chung81b52252012-08-27 15:34:29 -07001525 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1526 // to prevent leaking Launcher activities on orientation change.
1527 if (mModel != null) {
1528 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1529 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001530
1531 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001532 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001533
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001534 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001535 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1536 mWorkspace.removeAllViews();
1537 mWorkspace = null;
1538 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001539
Michael Jurka2ecf9952012-06-18 12:52:28 -07001540 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542
Adam Cohena9cf38f2011-05-02 15:36:58 -07001543 public DragController getDragController() {
1544 return mDragController;
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 @Override
1548 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001549 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 super.startActivityForResult(intent, requestCode);
1551 }
1552
Winson Chung70d72102011-08-12 11:24:35 -07001553 /**
1554 * Indicates that we want global search for this activity by setting the globalSearch
1555 * argument for {@link #startSearch} to true.
1556 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001558 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001560
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001561 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Karl Rosaen138a0412009-04-23 19:00:21 -07001563 if (initialQuery == null) {
1564 // Use any text typed in the launcher as the initial query
1565 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 if (appSearchData == null) {
1568 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001569 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Winson Chungadf0c182012-08-23 14:59:07 -07001571 Rect sourceBounds = new Rect();
1572 if (mSearchDropTargetBar != null) {
1573 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1574 }
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Michael Jurkaa33411c2012-06-14 16:18:21 -07001576 startGlobalSearch(initialQuery, selectInitialQuery,
1577 appSearchData, sourceBounds);
1578 }
1579
1580 /**
1581 * Starts the global search activity. This code is a copied from SearchManager
1582 */
1583 public void startGlobalSearch(String initialQuery,
1584 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001585 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001586 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1587 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1588 if (globalSearchActivity == null) {
1589 Log.w(TAG, "No global search activity found.");
1590 return;
1591 }
1592 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1593 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1594 intent.setComponent(globalSearchActivity);
1595 // Make sure that we have a Bundle to put source in
1596 if (appSearchData == null) {
1597 appSearchData = new Bundle();
1598 } else {
1599 appSearchData = new Bundle(appSearchData);
1600 }
1601 // Set source to package name of app that starts global search, if not set already.
1602 if (!appSearchData.containsKey("source")) {
1603 appSearchData.putString("source", getPackageName());
1604 }
1605 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1606 if (!TextUtils.isEmpty(initialQuery)) {
1607 intent.putExtra(SearchManager.QUERY, initialQuery);
1608 }
1609 if (selectInitialQuery) {
1610 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1611 }
1612 intent.setSourceBounds(sourceBounds);
1613 try {
1614 startActivity(intent);
1615 } catch (ActivityNotFoundException ex) {
1616 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1617 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 }
1619
Winson Chung70d72102011-08-12 11:24:35 -07001620 @Override
1621 public boolean onCreateOptionsMenu(Menu menu) {
1622 if (isWorkspaceLocked()) {
1623 return false;
1624 }
1625
1626 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001627
1628 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1629 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1630 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001631 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1632 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1633 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001634 String helpUrl = getString(R.string.help_url);
1635 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001636 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1637 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1638
Winson Chung70d72102011-08-12 11:24:35 -07001639 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1640 .setIcon(android.R.drawable.ic_menu_gallery)
1641 .setAlphabeticShortcut('W');
1642 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1643 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001644 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001645 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001646 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1647 .setIcon(android.R.drawable.ic_menu_preferences)
1648 .setIntent(settings)
1649 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001650 if (!helpUrl.isEmpty()) {
1651 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1652 .setIcon(android.R.drawable.ic_menu_help)
1653 .setIntent(help)
1654 .setAlphabeticShortcut('H');
1655 }
Winson Chung70d72102011-08-12 11:24:35 -07001656 return true;
1657 }
1658
1659 @Override
1660 public boolean onPrepareOptionsMenu(Menu menu) {
1661 super.onPrepareOptionsMenu(menu);
1662
1663 if (mAppsCustomizeTabHost.isTransitioning()) {
1664 return false;
1665 }
1666 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1667 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1668
1669 return true;
1670 }
1671
1672 @Override
1673 public boolean onOptionsItemSelected(MenuItem item) {
1674 switch (item.getItemId()) {
1675 case MENU_WALLPAPER_SETTINGS:
1676 startWallpaper();
1677 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001678 }
1679
1680 return super.onOptionsItemSelected(item);
1681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001683 @Override
1684 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001685 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001686 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001687 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001688 }
1689
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 public boolean isWorkspaceLocked() {
1691 return mWorkspaceLoading || mWaitingForResult;
1692 }
1693
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001695 mPendingAddInfo.container = ItemInfo.NO_ID;
1696 mPendingAddInfo.screen = -1;
1697 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1698 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001699 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001700 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001701 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001702
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001703 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1704 AppWidgetProviderInfo appWidgetInfo) {
1705 if (appWidgetInfo.configure != null) {
1706 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001707
Bjorn Bringert7984c942009-12-09 15:38:25 +00001708 // Launch over to configure widget, if needed
1709 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001710 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001711 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001712 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001714 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001715 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1716 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001717 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001718 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
1720 }
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Adam Cohenfbba09b2011-07-18 21:43:05 -07001722 /**
1723 * Process a shortcut drop.
1724 *
1725 * @param componentName The name of the component
1726 * @param screen The screen where it should be added
1727 * @param cell The cell it should be added to, optional
1728 * @param position The location on the screen where it was dropped, optional
1729 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001730 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1731 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001732 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001733 mPendingAddInfo.container = container;
1734 mPendingAddInfo.screen = screen;
1735 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001736
1737 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001738 mPendingAddInfo.cellX = cell[0];
1739 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001740 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741
1742 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1743 createShortcutIntent.setComponent(componentName);
1744 processShortcut(createShortcutIntent);
1745 }
1746
Adam Cohenfbba09b2011-07-18 21:43:05 -07001747 /**
1748 * Process a widget drop.
1749 *
1750 * @param info The PendingAppWidgetInfo of the widget being added.
1751 * @param screen The screen where it should be added
1752 * @param cell The cell it should be added to, optional
1753 * @param position The location on the screen where it was dropped, optional
1754 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001755 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001756 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001757 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001758 mPendingAddInfo.container = info.container = container;
1759 mPendingAddInfo.screen = info.screen = screen;
1760 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001761 mPendingAddInfo.minSpanX = info.minSpanX;
1762 mPendingAddInfo.minSpanY = info.minSpanY;
1763
Adam Cohenfbba09b2011-07-18 21:43:05 -07001764 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001765 mPendingAddInfo.cellX = cell[0];
1766 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001767 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001768 if (span != null) {
1769 mPendingAddInfo.spanX = span[0];
1770 mPendingAddInfo.spanY = span[1];
1771 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001772
Adam Cohened66b2b2012-01-23 17:28:51 -08001773 AppWidgetHostView hostView = info.boundWidget;
1774 int appWidgetId;
1775 if (hostView != null) {
1776 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001777 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001778 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001779 // In this case, we either need to start an activity to get permission to bind
1780 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001781 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001782 Bundle options = info.bindOptions;
1783
1784 boolean success = false;
1785 if (options != null) {
1786 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1787 info.componentName, options);
1788 } else {
1789 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1790 info.componentName);
1791 }
1792 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001793 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001794 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001795 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001796 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1797 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1798 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001799 // TODO: we need to make sure that this accounts for the options bundle.
1800 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001801 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1802 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001803 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001804 }
1805
Joe Onoratodeb98af2010-02-19 14:59:39 -08001806 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001807 // Handle case where user selected "Applications"
1808 String applicationName = getResources().getString(R.string.group_applications);
1809 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 if (applicationName != null && applicationName.equals(shortcutName)) {
1812 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1813 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1816 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001817 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001818 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001819 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001820 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 }
1823
Winson Chung24ab2f12010-09-16 14:10:47 -07001824 void processWallpaper(Intent intent) {
1825 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1826 }
1827
Winson Chung3d503fb2011-07-13 17:25:49 -07001828 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1829 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001830 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 folderInfo.title = getText(R.string.folder_name);
1832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001834 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1835 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001836 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837
1838 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001839 FolderIcon newFolder =
1840 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1841 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1842 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001843 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
Romain Guycbb89e42009-06-08 15:52:54 -07001845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001847 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001851 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001853 Intent chooser = Intent.createChooser(pickWallpaper,
1854 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001855 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1856 // Removed in Eclair MR1
1857// WallpaperManager wm = (WallpaperManager)
1858// getSystemService(Context.WALLPAPER_SERVICE);
1859// WallpaperInfo wi = wm.getWallpaperInfo();
1860// if (wi != null && wi.getSettingsActivity() != null) {
1861// LabeledIntent li = new LabeledIntent(getPackageName(),
1862// R.string.configure_wallpaper, 0);
1863// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1864// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1865// }
Mike Clerona0618e42009-10-22 13:55:21 -07001866 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
1868
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001869 /**
1870 * Registers various content observers. The current implementation registers
1871 * only a favorites observer to keep track of the favorites applications.
1872 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001873 private void registerContentObservers() {
1874 ContentResolver resolver = getContentResolver();
1875 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1876 true, mWidgetObserver);
1877 }
1878
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 @Override
1880 public boolean dispatchKeyEvent(KeyEvent event) {
1881 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1882 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001884 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001885 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001886 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001887 dumpState();
1888 return true;
1889 }
1890 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001891 }
1892 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1893 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001894 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 return true;
1896 }
1897 }
1898
1899 return super.dispatchKeyEvent(event);
1900 }
1901
Joe Onorato88ec0992009-11-19 13:16:06 -08001902 @Override
1903 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001904 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001905 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001906 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001907 Folder openFolder = mWorkspace.getOpenFolder();
1908 if (openFolder.isEditingName()) {
1909 openFolder.dismissEditingName();
1910 } else {
1911 closeFolder();
1912 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001913 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001914 mWorkspace.exitWidgetResizeMode();
1915
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001916 // Back button is a no-op here, but give at least some feedback for the button press
1917 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001918 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001919 }
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001922 * Re-listen when widgets are reset.
1923 */
1924 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001925 if (mAppWidgetHost != null) {
1926 mAppWidgetHost.startListening();
1927 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001928 }
1929
1930 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 * Launches the intent referred by the clicked shortcut.
1932 *
1933 * @param v The view representing the clicked shortcut.
1934 */
1935 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001936 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1937 // view has detached (it's possible for this to happen if the view is removed mid touch).
1938 if (v.getWindowToken() == null) {
1939 return;
1940 }
1941
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001942 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001943 return;
1944 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001947 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001949 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001950 int[] pos = new int[2];
1951 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001952 intent.setSourceBounds(new Rect(pos[0], pos[1],
1953 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001954
1955 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001956
1957 if (success && v instanceof BubbleTextView) {
1958 mWaitingForResume = (BubbleTextView) v;
1959 mWaitingForResume.setStayPressed(true);
1960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001962 if (v instanceof FolderIcon) {
1963 FolderIcon fi = (FolderIcon) v;
1964 handleFolderClick(fi);
1965 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001966 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001967 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001968 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001969 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001970 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973 }
1974
Michael Jurka0e260592010-06-30 17:07:39 -07001975 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001976 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001977 // clicking anywhere on the workspace causes the customization drawer to slide down
1978 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001979 return false;
1980 }
1981
Michael Jurkaaf442092010-06-10 17:01:57 -07001982 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001983 * Event handler for the search button
1984 *
1985 * @param v The view that was clicked.
1986 */
1987 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001988 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1989
Amith Yamasania135ba82011-08-09 17:42:01 -07001990 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001991 }
1992
1993 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001994 * Event handler for the voice button
1995 *
1996 * @param v The view that was clicked.
1997 */
1998 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001999 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002000
Michael Jurkaae65ee32012-04-30 11:45:54 -07002001 try {
2002 final SearchManager searchManager =
2003 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2004 ComponentName activityName = searchManager.getGlobalSearchActivity();
2005 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002006 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002007 if (activityName != null) {
2008 intent.setPackage(activityName.getPackageName());
2009 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002010 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002011 } catch (ActivityNotFoundException e) {
2012 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002013 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002014 startActivitySafely(null, intent, "onClickVoiceButton");
2015 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002016 }
2017
2018 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002019 * Event handler for the "grid" button that appears on the home screen, which
2020 * enters all apps mode.
2021 *
2022 * @param v The view that was clicked.
2023 */
2024 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002025 showAllApps(true);
2026 }
2027
2028 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002029 // Provide the same haptic feedback that the system offers for virtual keys.
2030 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002031 }
2032
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002033 public void onClickAppMarketButton(View v) {
2034 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002035 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002036 } else {
2037 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002038 }
2039 }
2040
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002041 void startApplicationDetailsActivity(ComponentName componentName) {
2042 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002043 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2044 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002045 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002046 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002047 }
2048
Patrick Dubroy5539af72010-09-07 15:22:01 -07002049 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2050 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2051 // System applications cannot be installed. For now, show a toast explaining that.
2052 // We may give them the option of disabling apps this way.
2053 int messageId = R.string.uninstall_system_app_text;
2054 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2055 } else {
2056 String packageName = appInfo.componentName.getPackageName();
2057 String className = appInfo.componentName.getClassName();
2058 Intent intent = new Intent(
2059 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002060 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2061 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002062 startActivity(intent);
2063 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002064 }
2065
Michael Jurka86a720e2012-05-09 11:23:23 -07002066 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002070 // Only launch using the new animation if the shortcut has not opted out (this is a
2071 // private contract between launcher and may be ignored in the future).
2072 boolean useLaunchAnimation = (v != null) &&
2073 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2074 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002075 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2076 v.getMeasuredWidth(), v.getMeasuredHeight());
2077
2078 startActivity(intent, opts.toBundle());
2079 } else {
2080 startActivity(intent);
2081 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002082 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 } catch (SecurityException e) {
2084 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002085 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002087 "or use the exported attribute for this activity. "
2088 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002090 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002092
Michael Jurka86a720e2012-05-09 11:23:23 -07002093 boolean startActivitySafely(View v, Intent intent, Object tag) {
2094 boolean success = false;
2095 try {
2096 success = startActivity(v, intent, tag);
2097 } catch (ActivityNotFoundException e) {
2098 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2099 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2100 }
2101 return success;
2102 }
2103
Romain Guy8e633c52010-03-04 12:51:36 -08002104 void startActivityForResultSafely(Intent intent, int requestCode) {
2105 try {
2106 startActivityForResult(intent, requestCode);
2107 } catch (ActivityNotFoundException e) {
2108 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2109 } catch (SecurityException e) {
2110 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2111 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2112 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2113 "or use the exported attribute for this activity.", e);
2114 }
2115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116
Adam Cohena9cf38f2011-05-02 15:36:58 -07002117 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002118 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002119 Folder openFolder = mWorkspace.getFolderForTag(info);
2120
2121 // If the folder info reports that the associated folder is open, then verify that
2122 // it is actually opened. There have been a few instances where this gets out of sync.
2123 if (info.opened && openFolder == null) {
2124 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2125 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2126 info.opened = false;
2127 }
2128
Adam Cohenfb91f302012-06-11 15:45:18 -07002129 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 // Close any open folder
2131 closeFolder();
2132 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002133 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 } else {
2135 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 int folderScreen;
2137 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002138 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 // .. and close it
2140 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002141 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 // Close any folder open on the current screen
2143 closeFolder();
2144 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002145 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 }
2147 }
2148 }
2149 }
2150
Adam Cohen268c4752012-06-06 17:47:33 -07002151 /**
2152 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2153 * in the DragLayer in the exact absolute location of the original FolderIcon.
2154 */
2155 private void copyFolderIconToImage(FolderIcon fi) {
2156 final int width = fi.getMeasuredWidth();
2157 final int height = fi.getMeasuredHeight();
2158
2159 // Lazy load ImageView, Bitmap and Canvas
2160 if (mFolderIconImageView == null) {
2161 mFolderIconImageView = new ImageView(this);
2162 }
2163 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2164 mFolderIconBitmap.getHeight() != height) {
2165 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2166 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2167 }
2168
2169 DragLayer.LayoutParams lp;
2170 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2171 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2172 } else {
2173 lp = new DragLayer.LayoutParams(width, height);
2174 }
2175
Adam Cohen307fe232012-08-16 17:55:58 -07002176 // The layout from which the folder is being opened may be scaled, adjust the starting
2177 // view size by this scale factor.
2178 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002179 lp.customPosition = true;
2180 lp.x = mRectForFolderAnimation.left;
2181 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002182 lp.width = (int) (scale * width);
2183 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002184
2185 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2186 fi.draw(mFolderIconCanvas);
2187 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002188 if (fi.getFolder() != null) {
2189 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2190 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002191 }
Adam Cohen268c4752012-06-06 17:47:33 -07002192 // Just in case this image view is still in the drag layer from a previous animation,
2193 // we remove it and re-add it.
2194 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2195 mDragLayer.removeView(mFolderIconImageView);
2196 }
2197 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002198 if (fi.getFolder() != null) {
2199 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002200 }
Adam Cohen268c4752012-06-06 17:47:33 -07002201 }
2202
Adam Cohen2801caf2011-05-13 20:57:39 -07002203 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002204 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002205 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2206 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2207 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2208
Adam Cohenc51934b2011-07-26 21:07:43 -07002209 FolderInfo info = (FolderInfo) fi.getTag();
2210 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2211 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002212 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2213 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002214 }
2215
Adam Cohen268c4752012-06-06 17:47:33 -07002216 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2217 copyFolderIconToImage(fi);
2218 fi.setVisibility(View.INVISIBLE);
2219
Michael Jurka2ecf9952012-06-18 12:52:28 -07002220 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002221 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002222 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2223 oa.start();
2224 }
2225
Adam Cohen268c4752012-06-06 17:47:33 -07002226 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002227 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002228 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2229 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2230 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2231
Adam Cohen268c4752012-06-06 17:47:33 -07002232 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002233
Adam Cohen268c4752012-06-06 17:47:33 -07002234 // We remove and re-draw the FolderIcon in-case it has changed
2235 mDragLayer.removeView(mFolderIconImageView);
2236 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002237 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002238 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002239 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002240 oa.addListener(new AnimatorListenerAdapter() {
2241 @Override
2242 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002243 if (cl != null) {
2244 cl.clearFolderLeaveBehind();
2245 // Remove the ImageView copy of the FolderIcon and make the original visible.
2246 mDragLayer.removeView(mFolderIconImageView);
2247 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002248 }
2249 }
2250 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002251 oa.start();
2252 }
2253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002255 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 * is animated relative to the specified View. If the View is null, no animation
2257 * is played.
2258 *
2259 * @param folderInfo The FolderInfo describing the folder to open.
2260 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002261 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002262 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002263 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264
Adam Cohena9cf38f2011-05-02 15:36:58 -07002265 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266
Adam Cohen4554ee12011-08-03 16:13:21 -07002267 // Just verify that the folder hasn't already been added to the DragLayer.
2268 // There was a one-off crash where the folder had a parent already.
2269 if (folder.getParent() == null) {
2270 mDragLayer.addView(folder);
2271 mDragController.addDropTarget((DropTarget) folder);
2272 } else {
2273 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2274 folder.getParent() + ").");
2275 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002276 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002277 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002278 }
2279
2280 public void closeFolder() {
2281 Folder folder = mWorkspace.getOpenFolder();
2282 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002283 if (folder.isEditingName()) {
2284 folder.dismissEditingName();
2285 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002286 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002287
2288 // Dismiss the folder cling
2289 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002290 }
2291 }
2292
2293 void closeFolder(Folder folder) {
2294 folder.getInfo().opened = false;
2295
2296 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2297 if (parent != null) {
2298 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2299 shrinkAndFadeInFolderIcon(fi);
2300 }
2301 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 }
2303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002305 if (!isDraggingEnabled()) return false;
2306 if (isWorkspaceLocked()) return false;
2307 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308
2309 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002310 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
2312
Michael Jurka0280c3b2010-09-17 15:00:07 -07002313 resetAddInfo();
2314 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002316 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 return true;
2318 }
2319
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 // The hotseat touch handling does not go through Workspace, and we always allow long press
2321 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002322 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2324 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002325 if (itemUnderLongClick == null) {
2326 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002327 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2328 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002329 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002331 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002333 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 }
2335 }
2336 }
2337 return true;
2338 }
2339
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 boolean isHotseatLayout(View layout) {
2341 return mHotseat != null && layout != null &&
2342 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2343 }
2344 Hotseat getHotseat() {
2345 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002346 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002347 SearchDropTargetBar getSearchBar() {
2348 return mSearchDropTargetBar;
2349 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002350
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 /**
2352 * Returns the CellLayout of the specified container at the specified screen.
2353 */
2354 CellLayout getCellLayout(long container, int screen) {
2355 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2356 if (mHotseat != null) {
2357 return mHotseat.getLayout();
2358 } else {
2359 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002360 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 } else {
2362 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002363 }
2364 }
2365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 Workspace getWorkspace() {
2367 return mWorkspace;
2368 }
2369
Daniel Sandler843e8602010-06-07 14:59:01 -04002370 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002371 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002372 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002373 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002374 }
2375
Craig Mautner360310b2012-10-26 15:13:08 -07002376 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002377 public boolean isAllAppsButtonRank(int rank) {
2378 return mHotseat.isAllAppsButtonRank(rank);
2379 }
2380
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002381 /**
2382 * Helper method for the cameraZoomIn/cameraZoomOut animations
2383 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002384 * @param scaleFactor The scale factor used for the zoom
2385 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002386 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002387 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002388 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002389 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002390
Winson Chung18f41f82012-05-09 13:28:10 -07002391 void disableWallpaperIfInAllApps() {
2392 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002393 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002394 if (mAppsCustomizeTabHost != null &&
2395 !mAppsCustomizeTabHost.isTransitioning()) {
2396 updateWallpaperVisibility(false);
2397 }
2398 }
2399 }
2400
Craig Mautner360310b2012-10-26 15:13:08 -07002401 private void setWorkspaceBackground(boolean workspace) {
2402 mLauncherView.setBackground(workspace ?
2403 mWorkspaceBackgroundDrawable : mBlackBackgroundDrawable);
2404 }
2405
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002406 void updateWallpaperVisibility(boolean visible) {
2407 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2408 int curflags = getWindow().getAttributes().flags
2409 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2410 if (wpflags != curflags) {
2411 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2412 }
Craig Mautner360310b2012-10-26 15:13:08 -07002413 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002414 }
2415
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002416 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2417 if (v instanceof LauncherTransitionable) {
2418 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2419 }
2420 }
2421
Michael Jurkabed61d22012-02-14 22:51:29 -08002422 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2423 if (v instanceof LauncherTransitionable) {
2424 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2425 }
Winson Chung70442722012-02-10 15:43:22 -08002426
2427 // Update the workspace transition step as well
2428 dispatchOnLauncherTransitionStep(v, 0f);
2429 }
2430
2431 private void dispatchOnLauncherTransitionStep(View v, float t) {
2432 if (v instanceof LauncherTransitionable) {
2433 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2434 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002435 }
2436
2437 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2438 if (v instanceof LauncherTransitionable) {
2439 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2440 }
Winson Chung70442722012-02-10 15:43:22 -08002441
2442 // Update the workspace transition step as well
2443 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002444 }
2445
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002446 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002447 * Things to test when changing the following seven functions.
2448 * - Home from workspace
2449 * - from center screen
2450 * - from other screens
2451 * - Home from all apps
2452 * - from center screen
2453 * - from other screens
2454 * - Back from all apps
2455 * - from center screen
2456 * - from other screens
2457 * - Launch app from workspace and quit
2458 * - with back
2459 * - with home
2460 * - Launch app from all apps and quit
2461 * - with back
2462 * - with home
2463 * - Go to a screen that's not the default, then all
2464 * apps, and launch and app, and go back
2465 * - with back
2466 * -with home
2467 * - On workspace, long press power and go back
2468 * - with back
2469 * - with home
2470 * - On all apps, long press power and go back
2471 * - with back
2472 * - with home
2473 * - On workspace, power off
2474 * - On all apps, power off
2475 * - Launch an app and turn off the screen while in that app
2476 * - Go back with home key
2477 * - Go back with back key TODO: make this not go to workspace
2478 * - From all apps
2479 * - From workspace
2480 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2481 * - From all apps
2482 * - From the center workspace
2483 * - From another workspace
2484 */
2485
2486 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002487 * Zoom the camera out from the workspace to reveal 'toView'.
2488 * Assumes that the view to show is anchored at either the very top or very bottom
2489 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002490 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002491 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002492 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002493 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002494 mStateAnimation.cancel();
2495 mStateAnimation = null;
2496 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002497 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002498
Winson Chungf0ea4d32011-06-06 14:27:16 -07002499 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2500 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2501 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002502 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002503 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002504 final int startDelay =
2505 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002506
Michael Jurkab3e22d92011-10-31 15:58:33 -07002507 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002508
Michael Jurkad74c9842011-07-10 12:44:21 -07002509 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002510 Animator workspaceAnim =
2511 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512
2513 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002514 toView.setScaleX(scale);
2515 toView.setScaleY(scale);
2516 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2517 scaleAnim.
2518 scaleX(1f).scaleY(1f).
2519 setDuration(duration).
2520 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002521
Winson Chungf0ea4d32011-06-06 14:27:16 -07002522 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002523 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002524 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002525 .ofFloat(toView, "alpha", 0f, 1f)
2526 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002527 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002528 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2529 @Override
2530 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002531 if (animation == null) {
2532 throw new RuntimeException("animation is null");
2533 }
Winson Chung70442722012-02-10 15:43:22 -08002534 float t = (Float) animation.getAnimatedValue();
2535 dispatchOnLauncherTransitionStep(fromView, t);
2536 dispatchOnLauncherTransitionStep(toView, t);
2537 }
2538 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002539
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002540 // toView should appear right at the end of the workspace shrink
2541 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002542 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002543 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002544 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002545
2546 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002547 boolean animationCancelled = false;
2548
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002549 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002550 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002551 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002552 // Prepare the position
2553 toView.setTranslationX(0.0f);
2554 toView.setTranslationY(0.0f);
2555 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002556 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002558 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002559 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002560 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2561 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002562
Michael Jurkafa7969f2012-09-21 16:39:14 -07002563 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002564 // Hide the workspace scrollbar
2565 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002566 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002567 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002568 if (!animationCancelled) {
2569 updateWallpaperVisibility(false);
2570 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002571
2572 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002573 if (mSearchDropTargetBar != null) {
2574 mSearchDropTargetBar.hideSearchBar(false);
2575 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002576 }
2577
Adam Cohencff6af82011-09-13 14:51:53 -07002578 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002579 public void onAnimationCancel(Animator animation) {
2580 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002581 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 });
2583
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002584 if (workspaceAnim != null) {
2585 mStateAnimation.play(workspaceAnim);
2586 }
Michael Jurka1899a362011-11-03 13:50:45 -07002587
2588 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002589
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002590 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2591 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002592
2593 // If any of the objects being animated haven't been measured/laid out
2594 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002595 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002596 (mWorkspace.getMeasuredWidth() == 0) ||
2597 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002598 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002599 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002600
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002601 final AnimatorSet stateAnimation = mStateAnimation;
2602 final Runnable startAnimRunnable = new Runnable() {
2603 public void run() {
2604 // Check that mStateAnimation hasn't changed while
2605 // we waited for a layout/draw pass
2606 if (mStateAnimation != stateAnimation)
2607 return;
2608 setPivotsForZoom(toView, scale);
2609 dispatchOnLauncherTransitionStart(fromView, animated, false);
2610 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002611 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002612 }
2613 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002614 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002615 final ViewTreeObserver observer = toView.getViewTreeObserver();
2616 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2617 public void onGlobalLayout() {
2618 startAnimRunnable.run();
2619 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2620 }
2621 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002622 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002623 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002624 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002625 } else {
2626 toView.setTranslationX(0.0f);
2627 toView.setTranslationY(0.0f);
2628 toView.setScaleX(1.0f);
2629 toView.setScaleY(1.0f);
2630 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002631 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002632
Michael Jurkabed61d22012-02-14 22:51:29 -08002633 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2634 // Hide the workspace scrollbar
2635 mWorkspace.hideScrollingIndicator(true);
2636 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002637
2638 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002639 if (mSearchDropTargetBar != null) {
2640 mSearchDropTargetBar.hideSearchBar(false);
2641 }
Michael Jurkaabded662011-03-04 12:06:57 -08002642 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002643 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002644 dispatchOnLauncherTransitionStart(fromView, animated, false);
2645 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002646 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002647 dispatchOnLauncherTransitionStart(toView, animated, false);
2648 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002649 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002651 }
2652
2653 /**
2654 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002655 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002657 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002658 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2659 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002660
Michael Jurkab3e22d92011-10-31 15:58:33 -07002661 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002662 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002663 mStateAnimation.cancel();
2664 mStateAnimation = null;
2665 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002666 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002667
Winson Chungf0ea4d32011-06-06 14:27:16 -07002668 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002669 final int fadeOutDuration =
2670 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002671 final float scaleFactor = (float)
2672 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2673 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002674 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002675 Animator workspaceAnim = null;
2676
2677 if (toState == State.WORKSPACE) {
2678 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2679 workspaceAnim = mWorkspace.getChangeStateAnimation(
2680 Workspace.State.NORMAL, animated, stagger);
2681 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2682 workspaceAnim = mWorkspace.getChangeStateAnimation(
2683 Workspace.State.SPRING_LOADED, animated);
2684 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002685
Michael Jurkab3e22d92011-10-31 15:58:33 -07002686 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002687 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002688 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002689 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002690 final LauncherViewPropertyAnimator scaleAnim =
2691 new LauncherViewPropertyAnimator(fromView);
2692 scaleAnim.
2693 scaleX(scaleFactor).scaleY(scaleFactor).
2694 setDuration(duration).
2695 setInterpolator(new Workspace.ZoomInInterpolator());
2696
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002697 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002698 .ofFloat(fromView, "alpha", 1f, 0f)
2699 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002700 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002701 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2702 @Override
2703 public void onAnimationUpdate(ValueAnimator animation) {
2704 float t = 1f - (Float) animation.getAnimatedValue();
2705 dispatchOnLauncherTransitionStep(fromView, t);
2706 dispatchOnLauncherTransitionStep(toView, t);
2707 }
2708 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002709
Michael Jurka2ecf9952012-06-18 12:52:28 -07002710 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002711
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002712 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2713 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002714 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002715
2716 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002717 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002718 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002719 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002720 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002721 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2722 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002723 if (mWorkspace != null) {
2724 mWorkspace.hideScrollingIndicator(false);
2725 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002726 if (onCompleteRunnable != null) {
2727 onCompleteRunnable.run();
2728 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002729 mAppsCustomizeContent.updateCurrentPageScroll();
2730 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002731 }
2732 });
2733
Winson Chungf0ea4d32011-06-06 14:27:16 -07002734 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002735 if (workspaceAnim != null) {
2736 mStateAnimation.play(workspaceAnim);
2737 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002738 dispatchOnLauncherTransitionStart(fromView, animated, true);
2739 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002740 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741 } else {
2742 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002743 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002744 dispatchOnLauncherTransitionStart(fromView, animated, true);
2745 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002746 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002747 dispatchOnLauncherTransitionStart(toView, animated, true);
2748 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002749 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002750 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002751 }
2752
Michael Jurkae326f182011-11-21 14:05:46 -08002753 @Override
2754 public void onTrimMemory(int level) {
2755 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002756 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002757 mAppsCustomizeTabHost.onTrimMemory();
2758 }
2759 }
2760
Winson Chung18f41f82012-05-09 13:28:10 -07002761 @Override
2762 public void onWindowFocusChanged(boolean hasFocus) {
2763 if (!hasFocus) {
2764 // When another window occludes launcher (like the notification shade, or recents),
2765 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2766 updateWallpaperVisibility(true);
2767 } else {
2768 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002769 mWorkspace.postDelayed(new Runnable() {
2770 @Override
2771 public void run() {
2772 disableWallpaperIfInAllApps();
2773 }
2774 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002775 }
2776 }
2777
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002778 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002779 showWorkspace(animated, null);
2780 }
2781
2782 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002783 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002784 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002786 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002787
Winson Chungc7d2b602012-05-16 17:02:20 -07002788 // Show the search bar (only animate if we were showing the drop target bar in spring
2789 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002790 if (mSearchDropTargetBar != null) {
2791 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2792 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002793
Michael Jurkab737ee62011-11-15 15:57:22 -08002794 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002795 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002796
2797 // Set focus to the AppsCustomize button
2798 if (mAllAppsButton != null) {
2799 mAllAppsButton.requestFocus();
2800 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002801 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002802
Michael Jurkab737ee62011-11-15 15:57:22 -08002803 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002804
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002805 // Change the state *after* we've called all the transition code
2806 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002807
Winson Chung5fb63472011-02-02 17:03:37 -08002808 // Resume the auto-advance of widgets
2809 mUserPresent = true;
2810 updateRunning();
2811
alanv1d4fde62012-10-17 13:15:47 -07002812 // Send an accessibility event to announce the context change
2813 getWindow().getDecorView()
2814 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Joe Onorato00acb122009-08-04 16:04:30 -04002815 }
2816
Michael Jurkab3e22d92011-10-31 15:58:33 -07002817 void showAllApps(boolean animated) {
2818 if (mState != State.WORKSPACE) return;
2819
2820 showAppsCustomizeHelper(animated, false);
2821 mAppsCustomizeTabHost.requestFocus();
2822
Michael Jurkab3e22d92011-10-31 15:58:33 -07002823 // Change the state *after* we've called all the transition code
2824 mState = State.APPS_CUSTOMIZE;
2825
2826 // Pause the auto-advance of widgets until we are out of AllApps
2827 mUserPresent = false;
2828 updateRunning();
2829 closeFolder();
2830
2831 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002832 getWindow().getDecorView()
2833 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002834 }
2835
Adam Cohen7777d962011-08-18 18:58:38 -07002836 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002837 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002838 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002839 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002840 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002841 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002842 }
Adam Cohen7777d962011-08-18 18:58:38 -07002843
Adam Cohened66b2b2012-01-23 17:28:51 -08002844 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2845 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002846 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2847
Winson Chunge7a03942011-08-05 15:05:12 -07002848 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002849 @Override
2850 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002851 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002852 // Before we show workspace, hide all apps again because
2853 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2854 // clean up our state transition functions
2855 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002856 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002857 } else {
2858 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002859 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002860 }
2861 }, (extendedDelay ?
2862 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2863 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2864 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002865
Michael Jurkad3ef3062010-11-23 16:23:58 -08002866 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002867 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002868 final boolean animated = true;
2869 final boolean springLoaded = true;
2870 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002871 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002872 }
2873 // Otherwise, we are not in spring loaded mode, so don't do anything.
2874 }
2875
Michael Jurkab737ee62011-11-15 15:57:22 -08002876 void hideDockDivider() {
2877 if (mQsbDivider != null && mDockDivider != null) {
2878 mQsbDivider.setVisibility(View.INVISIBLE);
2879 mDockDivider.setVisibility(View.INVISIBLE);
2880 }
2881 }
2882
2883 void showDockDivider(boolean animated) {
2884 if (mQsbDivider != null && mDockDivider != null) {
2885 mQsbDivider.setVisibility(View.VISIBLE);
2886 mDockDivider.setVisibility(View.VISIBLE);
2887 if (mDividerAnimator != null) {
2888 mDividerAnimator.cancel();
2889 mQsbDivider.setAlpha(1f);
2890 mDockDivider.setAlpha(1f);
2891 mDividerAnimator = null;
2892 }
2893 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002894 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2895 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2896 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002897 int duration = 0;
2898 if (mSearchDropTargetBar != null) {
2899 duration = mSearchDropTargetBar.getTransitionInDuration();
2900 }
2901 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002902 mDividerAnimator.start();
2903 }
2904 }
2905 }
2906
Michael Jurkab3e22d92011-10-31 15:58:33 -07002907 void lockAllApps() {
2908 // TODO
2909 }
2910
2911 void unlockAllApps() {
2912 // TODO
2913 }
2914
Winson Chungf0ea4d32011-06-06 14:27:16 -07002915 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002916 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002917 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002918 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002919 if (!LauncherApplication.isScreenLarge()) {
2920 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002921 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002922 int duration = 0;
2923 if (mSearchDropTargetBar != null) {
2924 duration = mSearchDropTargetBar.getTransitionInDuration();
2925 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002926 mHotseat.animate().alpha(1f).setDuration(duration);
2927 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002928 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002929 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002930 }
2931 }
2932 }
2933
2934 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002935 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002936 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002937 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002938 if (!LauncherApplication.isScreenLarge()) {
2939 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002940 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002941 int duration = 0;
2942 if (mSearchDropTargetBar != null) {
2943 duration = mSearchDropTargetBar.getTransitionOutDuration();
2944 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002945 mHotseat.animate().alpha(0f).setDuration(duration);
2946 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002947 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002948 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002949 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002950 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002951 }
2952
Patrick Dubroy5f445422011-02-18 14:35:21 -08002953 /**
2954 * Add an item from all apps or customize onto the given workspace screen.
2955 * If layout is null, add to the current screen.
2956 */
2957 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002958 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002959 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002960 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002961 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002962
Winson Chungdff8ebb2011-09-08 17:25:31 -07002963 /** Maps the current orientation to an index for referencing orientation correct global icons */
2964 private int getCurrentOrientationIndexForGlobalIcons() {
2965 // default - 0, landscape - 1
2966 switch (getResources().getConfiguration().orientation) {
2967 case Configuration.ORIENTATION_LANDSCAPE:
2968 return 1;
2969 default:
2970 return 0;
2971 }
2972 }
2973
Michael Jurkaae65ee32012-04-30 11:45:54 -07002974 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002975 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002976 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002977 // Look for the toolbar icon specified in the activity meta-data
2978 Bundle metaData = packageManager.getActivityInfo(
2979 activityName, PackageManager.GET_META_DATA).metaData;
2980 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002981 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002982 if (iconResId != 0) {
2983 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002984 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002985 }
2986 }
2987 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002988 // This can happen if the activity defines an invalid drawable
2989 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2990 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002991 } catch (Resources.NotFoundException nfe) {
2992 // This can happen if the activity defines an invalid drawable
2993 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2994 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002995 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002996 return null;
2997 }
2998
2999 // if successful in getting icon, return it; otherwise, set button to use default drawable
3000 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003001 int buttonId, ComponentName activityName, int fallbackDrawableId,
3002 String toolbarResourceName) {
3003 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003004 Resources r = getResources();
3005 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3006 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003007
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003008 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003009 // If we were unable to find the icon via the meta-data, use a generic one
3010 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003011 toolbarIcon = r.getDrawable(fallbackDrawableId);
3012 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003013 if (button != null) {
3014 button.setCompoundDrawables(toolbarIcon, null, null, null);
3015 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003016 return null;
3017 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003018 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003019 if (button != null) {
3020 button.setCompoundDrawables(toolbarIcon, null, null, null);
3021 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003022 return toolbarIcon.getConstantState();
3023 }
3024 }
3025
3026 // if successful in getting icon, return it; otherwise, set button to use default drawable
3027 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003028 int buttonId, ComponentName activityName, int fallbackDrawableId,
3029 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003030 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003031 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003032
Michael Jurka19e0fc52011-07-22 18:00:21 -07003033 if (button != null) {
3034 // If we were unable to find the icon via the meta-data, use a
3035 // generic one
3036 if (toolbarIcon == null) {
3037 button.setImageResource(fallbackDrawableId);
3038 } else {
3039 button.setImageDrawable(toolbarIcon);
3040 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003041 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003042
3043 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3044
Michael Jurka0423dcf2010-10-05 14:56:18 -07003045 }
3046
Winson Chung1b884092012-06-08 17:13:02 -07003047 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003048 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003049 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003050 }
3051
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003052 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003053 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003054 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003055 }
3056
Winson Chungbb185bd2011-11-21 12:31:42 -08003057 private void invalidatePressedFocusedStates(View container, View button) {
3058 if (container instanceof HolographicLinearLayout) {
3059 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3060 layout.invalidatePressedFocusedStates();
3061 } else if (button instanceof HolographicImageView) {
3062 HolographicImageView view = (HolographicImageView) button;
3063 view.invalidatePressedFocusedStates();
3064 }
3065 }
3066
Winson Chungc51db6a2011-10-05 11:44:49 -07003067 private boolean updateGlobalSearchIcon() {
3068 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003069 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003070 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003071 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003072 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003073
Winson Chung1cad91e2011-05-25 17:41:01 -07003074 final SearchManager searchManager =
3075 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3076 ComponentName activityName = searchManager.getGlobalSearchActivity();
3077 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003078 int coi = getCurrentOrientationIndexForGlobalIcons();
3079 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003080 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3081 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3082 if (sGlobalSearchIcon[coi] == null) {
3083 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3084 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3085 TOOLBAR_ICON_METADATA_NAME);
3086 }
3087
Winson Chungc51db6a2011-10-05 11:44:49 -07003088 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3089 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003090 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003091 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003092 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003093 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003094 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3095 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3096 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003097 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003098 if (voiceButtonProxy != null) {
3099 voiceButtonProxy.setVisibility(View.GONE);
3100 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003101 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003102 }
3103 }
3104
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003105 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003106 final View searchButtonContainer = findViewById(R.id.search_button_container);
3107 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003108 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003109 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003110 }
3111
Winson Chungc51db6a2011-10-05 11:44:49 -07003112 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003113 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003114 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003115 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003116
Winson Chungc51db6a2011-10-05 11:44:49 -07003117 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003118 final SearchManager searchManager =
3119 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3120 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3121
3122 ComponentName activityName = null;
3123 if (globalSearchActivity != null) {
3124 // Check if the global search activity handles voice search
3125 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3126 intent.setPackage(globalSearchActivity.getPackageName());
3127 activityName = intent.resolveActivity(getPackageManager());
3128 }
3129
3130 if (activityName == null) {
3131 // Fallback: check if an activity other than the global search activity
3132 // resolves this
3133 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3134 activityName = intent.resolveActivity(getPackageManager());
3135 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003136 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003137 int coi = getCurrentOrientationIndexForGlobalIcons();
3138 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003139 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3140 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3141 if (sVoiceSearchIcon[coi] == null) {
3142 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3143 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3144 TOOLBAR_ICON_METADATA_NAME);
3145 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003146 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003147 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003148 if (voiceButtonProxy != null) {
3149 voiceButtonProxy.setVisibility(View.VISIBLE);
3150 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003151 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003152 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003153 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003154 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003155 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003156 if (voiceButtonProxy != null) {
3157 voiceButtonProxy.setVisibility(View.GONE);
3158 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003159 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003160 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003161 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003162
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003163 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003164 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3165 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003166 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003167 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003168 }
3169
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003170 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003171 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003172 */
3173 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003174 final View marketButton = findViewById(R.id.market_button);
3175 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3176 // Find the app market activity by resolving an intent.
3177 // (If multiple app markets are installed, it will return the ResolverActivity.)
3178 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003179 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003180 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003181 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003182 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003183 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3184 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003185 marketButton.setVisibility(View.VISIBLE);
3186 } else {
3187 // We should hide and disable the view so that we don't try and restore the visibility
3188 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3189 marketButton.setVisibility(View.GONE);
3190 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003191 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003192 }
3193
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003194 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003195 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3196 Resources r = getResources();
3197 Drawable marketIconDrawable = d.newDrawable(r);
3198 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3199 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3200 marketIconDrawable.setBounds(0, 0, w, h);
3201
3202 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003203 }
3204
Michael Jurkad7c28052012-04-27 15:43:36 -07003205 @Override
3206 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003207 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003208 final List<CharSequence> text = event.getText();
3209 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003210 // Populate event with a fake title based on the current state.
3211 if (mState == State.APPS_CUSTOMIZE) {
3212 text.add(getString(R.string.all_apps_button_label));
3213 } else {
3214 text.add(getString(R.string.all_apps_home_button_label));
3215 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003216 return result;
3217 }
3218
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003219 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003220 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003221 */
3222 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3223 @Override
3224 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003225 closeSystemDialogs();
3226 }
3227 }
3228
3229 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003230 * Receives notifications whenever the appwidgets are reset.
3231 */
3232 private class AppWidgetResetObserver extends ContentObserver {
3233 public AppWidgetResetObserver() {
3234 super(new Handler());
3235 }
3236
3237 @Override
3238 public void onChange(boolean selfChange) {
3239 onAppWidgetReset();
3240 }
3241 }
3242
3243 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003244 * If the activity is currently paused, signal that we need to re-run the loader
3245 * in onResume.
3246 *
3247 * This needs to be called from incoming places where resources might have been loaded
3248 * while we are paused. That is becaues the Configuration might be wrong
3249 * when we're not running, and if it comes back to what it was when we
3250 * were paused, we are not restarted.
3251 *
3252 * Implementation of the method from LauncherModel.Callbacks.
3253 *
3254 * @return true if we are currently paused. The caller might be able to
3255 * skip some work in that case since we will come back again.
3256 */
3257 public boolean setLoadOnResume() {
3258 if (mPaused) {
3259 Log.i(TAG, "setLoadOnResume");
3260 mOnResumeNeedsLoad = true;
3261 return true;
3262 } else {
3263 return false;
3264 }
3265 }
3266
3267 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003269 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003270 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003271 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003272 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003273 } else {
3274 return SCREEN_COUNT / 2;
3275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003276 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003277
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 /**
3279 * Refreshes the shortcuts shown on the workspace.
3280 *
3281 * Implementation of the method from LauncherModel.Callbacks.
3282 */
3283 public void startBinding() {
3284 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003285
Winson Chungf0c6ae02012-03-21 16:10:31 -07003286 mNewShortcutAnimatePage = -1;
3287 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003288 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003289 int count = workspace.getChildCount();
3290 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003291 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003292 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3293 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003294 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003295 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003296 if (mHotseat != null) {
3297 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298 }
3299 }
3300
3301 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 * Bind the items start-end from the list.
3303 *
3304 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003305 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003307 setLoadOnResume();
3308
Winson Chungf0c6ae02012-03-21 16:10:31 -07003309 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3310 Set<String> newApps = new HashSet<String>();
3311 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3312
3313 Workspace workspace = mWorkspace;
3314 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003316
3317 // Short circuit if we are loading dock items for a configuration which has no dock
3318 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3319 mHotseat == null) {
3320 continue;
3321 }
3322
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 switch (item.itemType) {
3324 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3325 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003326 ShortcutInfo info = (ShortcutInfo) item;
3327 String uri = info.intent.toUri(0).toString();
3328 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003329 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3330 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003331 boolean animateIconUp = false;
3332 synchronized (newApps) {
3333 if (newApps.contains(uri)) {
3334 animateIconUp = newApps.remove(uri);
3335 }
3336 }
3337 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003338 // Prepare the view to be animated up
3339 shortcut.setAlpha(0f);
3340 shortcut.setScaleX(0f);
3341 shortcut.setScaleY(0f);
3342 mNewShortcutAnimatePage = item.screen;
3343 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3344 mNewShortcutAnimateViews.add(shortcut);
3345 }
3346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003348 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003349 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003350 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003351 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003352 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3353 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003354 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003356 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003357
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003359 }
3360
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 /**
3362 * Implementation of the method from LauncherModel.Callbacks.
3363 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003364 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003365 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003366 sFolders.clear();
3367 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369
3370 /**
3371 * Add the views for a widget to the workspace.
3372 *
3373 * Implementation of the method from LauncherModel.Callbacks.
3374 */
3375 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003376 setLoadOnResume();
3377
Daniel Sandler843e8602010-06-07 14:59:01 -04003378 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3379 if (DEBUG_WIDGETS) {
3380 Log.d(TAG, "bindAppWidget: " + item);
3381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 final Workspace workspace = mWorkspace;
3383
3384 final int appWidgetId = item.appWidgetId;
3385 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003386 if (DEBUG_WIDGETS) {
3387 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3388 }
3389
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3391
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003393 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394
Winson Chung3d503fb2011-07-13 17:25:49 -07003395 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003397 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3398
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 workspace.requestLayout();
3400
Daniel Sandler843e8602010-06-07 14:59:01 -04003401 if (DEBUG_WIDGETS) {
3402 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3403 + (SystemClock.uptimeMillis()-start) + "ms");
3404 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 }
3406
Adam Cohen1462de32012-07-24 22:34:36 -07003407 public void onPageBoundSynchronously(int page) {
3408 mSynchronouslyBoundPages.add(page);
3409 }
3410
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 /**
3412 * Callback saying that there aren't any more items to bind.
3413 *
3414 * Implementation of the method from LauncherModel.Callbacks.
3415 */
3416 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003417 setLoadOnResume();
3418
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 if (mSavedState != null) {
3420 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003421 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 mSavedState = null;
3424 }
3425
Adam Cohen1462de32012-07-24 22:34:36 -07003426 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003428 // If we received the result of any pending adds while the loader was running (e.g. the
3429 // widget configuration forced an orientation change), process them now.
3430 for (int i = 0; i < sPendingAddList.size(); i++) {
3431 completeAdd(sPendingAddList.get(i));
3432 }
3433 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434
Winson Chungc51db6a2011-10-05 11:44:49 -07003435 // Update the market app icon as necessary (the other icons will be managed in response to
3436 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003437 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003438
Winson Chungf0c6ae02012-03-21 16:10:31 -07003439 // Animate up any icons as necessary
3440 if (mVisible || mWorkspaceLoading) {
3441 Runnable newAppsRunnable = new Runnable() {
3442 @Override
3443 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003444 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003445 }
3446 };
Winson Chunga2413752012-04-03 14:22:34 -07003447
3448 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3449 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3450 if (canRunNewAppsAnimation()) {
3451 // If the user has not interacted recently, then either snap to the new page to show
3452 // the new-apps animation or just run them if they are to appear on the current page
3453 if (willSnapPage) {
3454 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3455 } else {
3456 runNewAppsAnimation(false);
3457 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003458 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003459 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003460 // are on another page (or just normally if they are added to the current page)
3461 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003462 }
3463 }
3464
3465 mWorkspaceLoading = false;
3466 }
3467
Winson Chunga2413752012-04-03 14:22:34 -07003468 private boolean canRunNewAppsAnimation() {
3469 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3470 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3471 }
3472
Winson Chungf0c6ae02012-03-21 16:10:31 -07003473 /**
3474 * Runs a new animation that scales up icons that were added while Launcher was in the
3475 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003476 *
3477 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003478 */
Winson Chunga2413752012-04-03 14:22:34 -07003479 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003480 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003481 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003482
3483 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003484 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3485 @Override
3486 public int compare(View a, View b) {
3487 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3488 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3489 int cellCountX = LauncherModel.getCellCountX();
3490 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3491 }
3492 });
Winson Chunga2413752012-04-03 14:22:34 -07003493
3494 // Animate each of the views in place (or show them immediately if requested)
3495 if (immediate) {
3496 for (View v : mNewShortcutAnimateViews) {
3497 v.setAlpha(1f);
3498 v.setScaleX(1f);
3499 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003500 }
Winson Chunga2413752012-04-03 14:22:34 -07003501 } else {
3502 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3503 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003504 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003505 PropertyValuesHolder.ofFloat("alpha", 1f),
3506 PropertyValuesHolder.ofFloat("scaleX", 1f),
3507 PropertyValuesHolder.ofFloat("scaleY", 1f));
3508 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3509 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3510 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3511 bounceAnims.add(bounceAnim);
3512 }
3513 anim.playTogether(bounceAnims);
3514 anim.addListener(new AnimatorListenerAdapter() {
3515 @Override
3516 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003517 if (mWorkspace != null) {
3518 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3519 }
Winson Chunga2413752012-04-03 14:22:34 -07003520 }
3521 });
3522 anim.start();
3523 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003524
3525 // Clean up
3526 mNewShortcutAnimatePage = -1;
3527 mNewShortcutAnimateViews.clear();
3528 new Thread("clearNewAppsThread") {
3529 public void run() {
3530 mSharedPrefs.edit()
3531 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3532 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3533 .commit();
3534 }
3535 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003536 }
3537
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003538 @Override
3539 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003540 boolean searchVisible = updateGlobalSearchIcon();
3541 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003542 if (mSearchDropTargetBar != null) {
3543 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3544 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003545 }
3546
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003547 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 * Add the icons for all apps.
3549 *
3550 * Implementation of the method from LauncherModel.Callbacks.
3551 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003552 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003553 Runnable setAllAppsRunnable = new Runnable() {
3554 public void run() {
3555 if (mAppsCustomizeContent != null) {
3556 mAppsCustomizeContent.setApps(apps);
3557 }
3558 }
3559 };
3560
Michael Jurkac57b7a82011-08-09 22:02:20 -07003561 // Remove the progress bar entirely; we could also make it GONE
3562 // but better to remove it since we know it's not going to be used
3563 View progressBar = mAppsCustomizeTabHost.
3564 findViewById(R.id.apps_customize_progress_bar);
3565 if (progressBar != null) {
3566 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003567
3568 // We just post the call to setApps so the user sees the progress bar
3569 // disappear-- otherwise, it just looks like the progress bar froze
3570 // which doesn't look great
3571 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3572 } else {
3573 // If we did not initialize the spinner in onCreate, then we can directly set the
3574 // list of applications without waiting for any progress bars views to be hidden.
3575 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003576 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 }
3578
3579 /**
3580 * A package was installed.
3581 *
3582 * Implementation of the method from LauncherModel.Callbacks.
3583 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003584 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003585 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003586
Winson Chung785d2eb2011-04-14 16:08:02 -07003587 if (mAppsCustomizeContent != null) {
3588 mAppsCustomizeContent.addApps(apps);
3589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 }
3591
3592 /**
3593 * A package was updated.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
3596 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003597 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003598 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003599 if (mWorkspace != null) {
3600 mWorkspace.updateShortcuts(apps);
3601 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003602
Winson Chung785d2eb2011-04-14 16:08:02 -07003603 if (mAppsCustomizeContent != null) {
3604 mAppsCustomizeContent.updateApps(apps);
3605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
3607
3608 /**
3609 * A package was uninstalled.
3610 *
3611 * Implementation of the method from LauncherModel.Callbacks.
3612 */
Winson Chungcd810732012-06-18 16:45:43 -07003613 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003614 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003615 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003616 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003617
Winson Chung785d2eb2011-04-14 16:08:02 -07003618 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003619 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003620 }
Winson Chunga1820962011-10-03 16:31:06 -07003621
3622 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003623 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 }
Joe Onoratobe386092009-11-17 17:32:16 -08003625
3626 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003627 * A number of packages were updated.
3628 */
3629 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003630 if (mAppsCustomizeContent != null) {
3631 mAppsCustomizeContent.onPackagesUpdated();
3632 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003633 }
3634
Winson Chung400438b2011-01-16 17:53:48 -08003635 private int mapConfigurationOriActivityInfoOri(int configOri) {
3636 final Display d = getWindowManager().getDefaultDisplay();
3637 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3638 switch (d.getRotation()) {
3639 case Surface.ROTATION_0:
3640 case Surface.ROTATION_180:
3641 // We are currently in the same basic orientation as the natural orientation
3642 naturalOri = configOri;
3643 break;
3644 case Surface.ROTATION_90:
3645 case Surface.ROTATION_270:
3646 // We are currently in the other basic orientation to the natural orientation
3647 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3648 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3649 break;
3650 }
3651
3652 int[] oriMap = {
3653 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3654 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3655 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3656 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3657 };
3658 // Since the map starts at portrait, we need to offset if this device's natural orientation
3659 // is landscape.
3660 int indexOffset = 0;
3661 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3662 indexOffset = 1;
3663 }
3664 return oriMap[(d.getRotation() + indexOffset) % 4];
3665 }
Adam Cohen446e9402011-09-15 18:21:21 -07003666
Winson Chung4b919f82012-05-01 10:44:08 -07003667 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003668 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003669 getResources().getBoolean(R.bool.allow_rotation);
3670 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003671 }
Winson Chung4b919f82012-05-01 10:44:08 -07003672 public void lockScreenOrientation() {
3673 if (isRotationEnabled()) {
3674 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3675 .getConfiguration().orientation));
3676 }
3677 }
3678 public void unlockScreenOrientation(boolean immediate) {
3679 if (isRotationEnabled()) {
3680 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003681 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003682 } else {
3683 mHandler.postDelayed(new Runnable() {
3684 public void run() {
3685 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3686 }
3687 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003688 }
Winson Chung4b919f82012-05-01 10:44:08 -07003689 }
Winson Chung400438b2011-01-16 17:53:48 -08003690 }
3691
Winson Chung82f55532011-08-09 14:14:23 -07003692 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003693 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003694 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003695 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003696
Michael Jurkae233a8b2013-03-19 13:49:20 +01003697 // Restricted secondary users (child mode) will potentially have very few apps
3698 // seeded when they start up for the first time. Clings won't work well with that
3699 boolean supportsRestrictedUsers =
3700 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3701 if (supportsRestrictedUsers) {
3702 final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3703 if (um.isUserRestricted()) return false;
3704 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003705 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003706 }
Michael Jurka22143132012-10-04 17:42:38 +02003707
Winson Chung7d7541e2011-09-16 20:14:36 -07003708 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003709 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003710 if (cling != null) {
3711 cling.init(this, positionData);
3712 cling.setVisibility(View.VISIBLE);
3713 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3714 if (animate) {
3715 cling.buildLayer();
3716 cling.setAlpha(0f);
3717 cling.animate()
3718 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003719 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003720 .setDuration(SHOW_CLING_DURATION)
3721 .setStartDelay(delay)
3722 .start();
3723 } else {
3724 cling.setAlpha(1f);
3725 }
Michael Jurka22143132012-10-04 17:42:38 +02003726 cling.setFocusableInTouchMode(true);
3727 cling.post(new Runnable() {
3728 public void run() {
3729 cling.setFocusable(true);
3730 cling.requestFocus();
3731 }
3732 });
3733 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3734 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003735 }
3736 return cling;
3737 }
Michael Jurka22143132012-10-04 17:42:38 +02003738
Winson Chung7d7541e2011-09-16 20:14:36 -07003739 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003740 // To catch cases where siblings of top-level views are made invisible, just check whether
3741 // the cling is directly set to GONE before dismissing it.
3742 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003743 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003744 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003745 anim.addListener(new AnimatorListenerAdapter() {
3746 public void onAnimationEnd(Animator animation) {
3747 cling.setVisibility(View.GONE);
3748 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003749 // We should update the shared preferences on a background thread
3750 new Thread("dismissClingThread") {
3751 public void run() {
3752 SharedPreferences.Editor editor = mSharedPrefs.edit();
3753 editor.putBoolean(flag, true);
3754 editor.commit();
3755 }
3756 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003757 };
3758 });
3759 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003760 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003761 }
3762 }
Michael Jurka22143132012-10-04 17:42:38 +02003763
Winson Chung9d9d74f2011-09-19 11:49:12 -07003764 private void removeCling(int id) {
3765 final View cling = findViewById(id);
3766 if (cling != null) {
3767 final ViewGroup parent = (ViewGroup) cling.getParent();
3768 parent.post(new Runnable() {
3769 @Override
3770 public void run() {
3771 parent.removeView(cling);
3772 }
3773 });
Michael Jurka22143132012-10-04 17:42:38 +02003774 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003775 }
3776 }
Michael Jurka974c3862012-05-22 22:00:31 -07003777
3778 private boolean skipCustomClingIfNoAccounts() {
3779 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3780 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3781 if (customCling) {
3782 AccountManager am = AccountManager.get(this);
3783 Account[] accounts = am.getAccountsByType("com.google");
3784 return accounts.length == 0;
3785 }
3786 return false;
3787 }
3788
Winson Chung7d7541e2011-09-16 20:14:36 -07003789 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003790 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003791 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003792 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3793 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02003794 // If we're not using the default workspace layout, replace workspace cling
3795 // with a custom workspace cling (usually specified in an overlay)
3796 // For now, only do this on tablets
3797 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02003798 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02003799 // Use a custom cling
3800 View cling = findViewById(R.id.workspace_cling);
3801 ViewGroup clingParent = (ViewGroup) cling.getParent();
3802 int clingIndex = clingParent.indexOfChild(cling);
3803 clingParent.removeViewAt(clingIndex);
3804 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3805 clingParent.addView(customCling, clingIndex);
3806 customCling.setId(R.id.workspace_cling);
3807 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003808 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003809 } else {
3810 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003811 }
3812 }
3813 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003814 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003815 if (isClingsEnabled() &&
3816 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003817 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003818 } else {
3819 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003820 }
3821 }
3822 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003823 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003824 if (isClingsEnabled() &&
3825 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3826 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003827 } else {
3828 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003829 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003830 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003831 }
3832 public boolean isFolderClingVisible() {
3833 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003834 if (cling != null) {
3835 return cling.getVisibility() == View.VISIBLE;
3836 }
3837 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003838 }
Winson Chung82f55532011-08-09 14:14:23 -07003839 public void dismissWorkspaceCling(View v) {
3840 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003841 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003842 }
3843 public void dismissAllAppsCling(View v) {
3844 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003845 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3846 }
3847 public void dismissFolderCling(View v) {
3848 Cling cling = (Cling) findViewById(R.id.folder_cling);
3849 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003850 }
3851
Winson Chung80baf5a2010-08-09 16:03:15 -07003852 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003853 * Prints out out state for debugging.
3854 */
3855 public void dumpState() {
3856 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003857 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003858 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3859 Log.d(TAG, "mRestoring=" + mRestoring);
3860 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3861 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003862 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003863 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003864
Winson Chung785d2eb2011-04-14 16:08:02 -07003865 if (mAppsCustomizeContent != null) {
3866 mAppsCustomizeContent.dumpState();
3867 }
Joe Onoratobe386092009-11-17 17:32:16 -08003868 Log.d(TAG, "END launcher2 dump state");
3869 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003870
3871 @Override
3872 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3873 super.dump(prefix, fd, writer, args);
3874 writer.println(" ");
3875 writer.println("Debug logs: ");
3876 for (int i = 0; i < sDumpLogs.size(); i++) {
3877 writer.println(" " + sDumpLogs.get(i));
3878 }
3879 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003880
3881 public static void dumpDebugLogsToConsole() {
3882 Log.d(TAG, "");
3883 Log.d(TAG, "*********************");
3884 Log.d(TAG, "Launcher debug logs: ");
3885 for (int i = 0; i < sDumpLogs.size(); i++) {
3886 Log.d(TAG, " " + sDumpLogs.get(i));
3887 }
3888 Log.d(TAG, "*********************");
3889 Log.d(TAG, "");
3890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003891}
Michael Jurka2763be32011-02-24 11:19:57 -08003892
Michael Jurkaabded662011-03-04 12:06:57 -08003893interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003894 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003895 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003896 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003897 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003898 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003899}