blob: b2f27b960dcab14dcf65fef2ca0a8fadcdf86e86 [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;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080079import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080080import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700103import java.util.Collection;
104import java.util.Collections;
105import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700108import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700122 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
Winson Chung70d72102011-08-12 11:24:35 -0700124 private static final int MENU_GROUP_WALLPAPER = 1;
125 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
126 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700127 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
128 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurka8b805b12012-04-18 14:23:14 -0700137 private static final int REQUEST_BIND_APPWIDGET = 11;
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Romain Guy98d01652009-06-30 16:21:04 -0700144 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800145 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Winson Chung2672ff92012-05-04 16:22:30 -0700147 // The Intent extra that defines whether to ignore the launch animation
148 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
149 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700153 // Type: int
154 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
157 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: boolean
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
165 // Type: long
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
171 // Type: parcelable
172 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700174 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700175 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
176 "com.android.launcher.toolbar_search_icon";
177 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
178 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700180 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700181 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700182 private State mState = State.WORKSPACE;
183 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800184 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700187 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
188 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700189 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700190 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Winson Chunga2413752012-04-03 14:22:34 -0700195 // How long to wait before the new-shortcut animation automatically pans the workspace
196 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
197
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800198 private final BroadcastReceiver mCloseSystemDialogsReceiver
199 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800200 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private LayoutInflater mInflater;
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800205 private View mQsbDivider;
206 private View mDockDivider;
207 private DragLayer mDragLayer;
208 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700209
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private AppWidgetManager mAppWidgetManager;
211 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
Michael Jurkac9d95c52011-08-29 14:03:34 -0700213 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700214 private AppWidgetProviderInfo mPendingAddWidgetInfo;
215
Michael Jurka0280c3b2010-09-17 15:00:07 -0700216 private int[] mTmpAddItemCellCoordinates = new int[2];
217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private FolderInfo mFolderInfo;
219
Winson Chung3d503fb2011-07-13 17:25:49 -0700220 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700222
Winson Chungc51db6a2011-10-05 11:44:49 -0700223 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700224 private AppsCustomizeTabHost mAppsCustomizeTabHost;
225 private AppsCustomizePagedView mAppsCustomizeContent;
226 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private Bundle mSavedInstanceState;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800243 private boolean mUserPresent = true;
244 private boolean mVisible = false;
245 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static LocaleConfiguration sLocaleConfiguration = null;
248
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700249 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Winson Chung400438b2011-01-16 17:53:48 -0800262 // Determines how long to wait after a rotation before restoring the screen orientation to
263 // match the sensor state.
264 private final int mRestoreScreenOrientationDelay = 500;
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700267 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
268 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
269 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700271 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
272
Winson Chung46353de2012-02-16 14:05:10 -0800273 // We only want to get the SharedPreferences once since it does an FS stat each time we get
274 // it from the context.
275 private SharedPreferences mSharedPrefs;
276
Winson Chungf0c6ae02012-03-21 16:10:31 -0700277 // Holds the page that we need to animate to, and the icon views that we need to animate up
278 // when we scroll to that page on resume.
279 private int mNewShortcutAnimatePage = -1;
280 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700281
Michael Jurkaddd62e92011-02-16 17:49:14 -0800282 private BubbleTextView mWaitingForResume;
283
Michael Jurkac1f5d262011-09-30 19:32:27 -0700284 private Runnable mBuildLayersRunnable = new Runnable() {
285 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700286 if (mWorkspace != null) {
287 mWorkspace.buildPageHardwareLayers();
288 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700289 }
290 };
291
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800292 private static ArrayList<PendingAddArguments> sPendingAddList
293 = new ArrayList<PendingAddArguments>();
294
295 private static class PendingAddArguments {
296 int requestCode;
297 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700298 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800299 int screen;
300 int cellX;
301 int cellY;
302 }
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 @Override
305 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700306 if (DEBUG_STRICT_MODE) {
307 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
308 .detectDiskReads()
309 .detectDiskWrites()
310 .detectNetwork() // or .detectAll() for all detectable problems
311 .penaltyLog()
312 .build());
313 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
314 .detectLeakedSqlLiteObjects()
315 .detectLeakedClosableObjects()
316 .penaltyLog()
317 .penaltyDeath()
318 .build());
319 }
320
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800322 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700323 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
324 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800325 mModel = app.setLauncher(this);
326 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400327 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700329
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700330 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700331 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
332 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200335 android.os.Debug.startMethodTracing(
336 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
338
339 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 setContentView(R.layout.launcher);
341 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700342 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800344 registerContentObservers();
345
Joe Onorato7c312c12009-08-13 21:36:53 -0700346 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 mSavedState = savedInstanceState;
349 restoreState(mSavedState);
350
Winson Chunga12a2502010-12-20 14:41:35 -0800351 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700352 if (mAppsCustomizeContent != null) {
Winson Chung7c7a22d2012-06-01 13:46:48 -0700353 Log.d(TAG, "6549598 Launcher.onCreate()");
Winson Chung785d2eb2011-04-14 16:08:02 -0700354 mAppsCustomizeContent.onPackagesUpdated();
355 }
Winson Chunga12a2502010-12-20 14:41:35 -0800356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 if (PROFILE_STARTUP) {
358 android.os.Debug.stopMethodTracing();
359 }
360
361 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700362 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
364
Michael Jurkac57b7a82011-08-09 22:02:20 -0700365 if (!mModel.isAllAppsLoaded()) {
366 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
367 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
368 }
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 // For handling default keys
371 mDefaultKeySsb = new SpannableStringBuilder();
372 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800373
374 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
375 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800376
Winson Chungc51db6a2011-10-05 11:44:49 -0700377 boolean searchVisible = false;
378 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800379 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700380 int coi = getCurrentOrientationIndexForGlobalIcons();
381 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
382 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700383 updateAppMarketIcon();
384 searchVisible = updateGlobalSearchIcon();
385 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700386 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700387 if (sGlobalSearchIcon[coi] != null) {
388 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700389 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700390 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700391 if (sVoiceSearchIcon[coi] != null) {
392 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700393 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700394 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700395 if (sAppMarketIcon[coi] != null) {
396 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800397 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700398 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700399
400 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700401 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
Romain Guycbb89e42009-06-08 15:52:54 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 if (sLocaleConfiguration == null) {
406 new AsyncTask<Void, Void, LocaleConfiguration>() {
407 @Override
408 protected LocaleConfiguration doInBackground(Void... unused) {
409 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
410 readConfiguration(Launcher.this, localeConfiguration);
411 return localeConfiguration;
412 }
413
414 @Override
415 protected void onPostExecute(LocaleConfiguration result) {
416 sLocaleConfiguration = result;
417 checkForLocaleChange(); // recursive, but now with a locale configuration
418 }
419 }.execute();
420 return;
421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 final Configuration configuration = getResources().getConfiguration();
424
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700425 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 final String locale = configuration.locale.toString();
427
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700428 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 final int mcc = configuration.mcc;
430
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700431 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 final int mnc = configuration.mnc;
433
Romain Guy84f296c2009-11-04 15:00:44 -0800434 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435
Romain Guy84f296c2009-11-04 15:00:44 -0800436 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700437 sLocaleConfiguration.locale = locale;
438 sLocaleConfiguration.mcc = mcc;
439 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700440
Joe Onorato0589f0f2010-02-08 13:44:00 -0800441 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700442
443 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
444 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700445 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700446 public void run() {
447 writeConfiguration(Launcher.this, localeConfiguration);
448 }
449 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700450 }
451 }
452
453 private static class LocaleConfiguration {
454 public String locale;
455 public int mcc = -1;
456 public int mnc = -1;
457 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700458
Romain Guy98d01652009-06-30 16:21:04 -0700459 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
460 DataInputStream in = null;
461 try {
462 in = new DataInputStream(context.openFileInput(PREFERENCES));
463 configuration.locale = in.readUTF();
464 configuration.mcc = in.readInt();
465 configuration.mnc = in.readInt();
466 } catch (FileNotFoundException e) {
467 // Ignore
468 } catch (IOException e) {
469 // Ignore
470 } finally {
471 if (in != null) {
472 try {
473 in.close();
474 } catch (IOException e) {
475 // Ignore
476 }
477 }
478 }
479 }
480
481 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
482 DataOutputStream out = null;
483 try {
484 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
485 out.writeUTF(configuration.locale);
486 out.writeInt(configuration.mcc);
487 out.writeInt(configuration.mnc);
488 out.flush();
489 } catch (FileNotFoundException e) {
490 // Ignore
491 } catch (IOException e) {
492 //noinspection ResultOfMethodCallIgnored
493 context.getFileStreamPath(PREFERENCES).delete();
494 } finally {
495 if (out != null) {
496 try {
497 out.close();
498 } catch (IOException e) {
499 // Ignore
500 }
501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 }
503 }
504
Adam Cohen716b51e2011-06-30 12:09:54 -0700505 public DragLayer getDragLayer() {
506 return mDragLayer;
507 }
508
Winson Chung36a62fe2012-05-06 18:04:42 -0700509 boolean isDraggingEnabled() {
510 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
511 // that is subsequently removed from the workspace in startBinding().
512 return !mModel.isLoadingWorkspace();
513 }
514
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 static int getScreen() {
516 synchronized (sLock) {
517 return sScreen;
518 }
519 }
520
521 static void setScreen(int screen) {
522 synchronized (sLock) {
523 sScreen = screen;
524 }
525 }
526
Winson Chung557d6ed2011-07-08 15:34:52 -0700527 /**
528 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
529 * a configuration step, this allows the proper animations to run after other transitions.
530 */
531 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700532 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800533 switch (args.requestCode) {
534 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700535 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
536 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800537 break;
538 case REQUEST_PICK_SHORTCUT:
539 processShortcut(args.intent);
540 break;
541 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700542 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
543 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700544 result = true;
545 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800546 case REQUEST_CREATE_APPWIDGET:
547 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700549 result = true;
550 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800551 case REQUEST_PICK_WALLPAPER:
552 // We just wanted the activity result here so we can clear mWaitingForResult
553 break;
554 }
Michael Jurka27614d22012-04-02 06:40:22 -0700555 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
556 // if you turned the screen off and then back while in All Apps, Launcher would not
557 // return to the workspace. Clearing mAddInfo.container here fixes this issue
558 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700559 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800560 }
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700563 protected void onActivityResult(
564 final int requestCode, final int resultCode, final Intent data) {
565 if (requestCode == REQUEST_BIND_APPWIDGET) {
566 int appWidgetId = data != null ?
567 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
568 if (resultCode == RESULT_CANCELED) {
569 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
570 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700571 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700572 }
573 return;
574 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700575 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800576 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
577 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700578 mWaitingForResult = false;
579
Adam Cohened66b2b2012-01-23 17:28:51 -0800580 // We have special handling for widgets
581 if (isWidgetDrop) {
582 int appWidgetId = data != null ?
583 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700584 if (appWidgetId < 0) {
585 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
586 "widget configuration activity.");
587 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
588 } else {
589 completeTwoStageWidgetDrop(resultCode, appWidgetId);
590 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800591 return;
592 }
593
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 // The pattern used here is that a user PICKs a specific application,
595 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
598 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700599 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800600 final PendingAddArguments args = new PendingAddArguments();
601 args.requestCode = requestCode;
602 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700603 args.container = mPendingAddInfo.container;
604 args.screen = mPendingAddInfo.screen;
605 args.cellX = mPendingAddInfo.cellX;
606 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 if (isWorkspaceLocked()) {
608 sPendingAddList.add(args);
609 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700610 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800613 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700614 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800615 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
616 null);
617 }
618
619 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700621 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 Runnable onCompleteRunnable = null;
623 int animationType = 0;
624
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700625 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800626 if (resultCode == RESULT_OK) {
627 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
628 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700629 mPendingAddWidgetInfo);
630 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800631 onCompleteRunnable = new Runnable() {
632 @Override
633 public void run() {
634 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
635 mPendingAddInfo.screen, layout, null);
636 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
637 null);
638 }
639 };
640 } else if (resultCode == RESULT_CANCELED) {
641 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
642 onCompleteRunnable = new Runnable() {
643 @Override
644 public void run() {
645 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
646 null);
647 }
648 };
649 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700650 if (mDragLayer.getAnimatedView() != null) {
651 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
652 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
653 animationType, boundWidget, true);
654 } else {
655 // The animated view may be null in the case of a rotation during widget configuration
656 onCompleteRunnable.run();
657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659
660 @Override
661 protected void onResume() {
662 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700663
Winson Chungde0fb8f2012-05-08 14:37:08 -0700664 // Process any items that were added while Launcher was away
665 InstallShortcutReceiver.flushInstallQueue(this);
666
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800667 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700668 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700670 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700672 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
Winson Chunge4e50662012-01-23 14:45:13 -0800674
675 // Reset the pressed state of icons that were locked in the press state while activities
676 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800677 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800678 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800679 mWaitingForResume.setStayPressed(false);
680 }
Winson Chunge4e50662012-01-23 14:45:13 -0800681 if (mAppsCustomizeContent != null) {
682 // Resets the previous all apps icon press state
683 mAppsCustomizeContent.resetDrawableState();
684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
686
687 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700689 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
690 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
691 // when Launcher resumes and we are still in AllApps.
692 updateWallpaperVisibility(true);
693
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700695 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800696 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700697 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698 }
Romain Guycbb89e42009-06-08 15:52:54 -0700699
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700700 @Override
701 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 // Flag the loader to stop early before switching
703 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700704 if (mAppsCustomizeContent != null) {
705 mAppsCustomizeContent.surrender();
706 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800707 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700708 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800710 // We can't hide the IME if it was forced open. So don't bother
711 /*
712 @Override
713 public void onWindowFocusChanged(boolean hasFocus) {
714 super.onWindowFocusChanged(hasFocus);
715
716 if (hasFocus) {
717 final InputMethodManager inputManager = (InputMethodManager)
718 getSystemService(Context.INPUT_METHOD_SERVICE);
719 WindowManager.LayoutParams lp = getWindow().getAttributes();
720 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
721 android.os.Handler()) {
722 protected void onReceiveResult(int resultCode, Bundle resultData) {
723 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
724 }
725 });
726 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
727 }
728 }
729 */
730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 private boolean acceptFilter() {
732 final InputMethodManager inputManager = (InputMethodManager)
733 getSystemService(Context.INPUT_METHOD_SERVICE);
734 return !inputManager.isFullscreenMode();
735 }
736
737 @Override
738 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700739 final int uniChar = event.getUnicodeChar();
740 final boolean handled = super.onKeyDown(keyCode, event);
741 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
742 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
744 keyCode, event);
745 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700746 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700747 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700748 // showSearchDialog()
749 // If there are multiple keystrokes before the search dialog takes focus,
750 // onSearchRequested() will be called for every keystroke,
751 // but it is idempotent, so it's fine.
752 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
754 }
755
Joe Onorato8a9625e2010-01-28 15:55:35 -0800756 // Eat the long press event so the keyboard doesn't come up.
757 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
758 return true;
759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 return handled;
762 }
763
Karl Rosaen138a0412009-04-23 19:00:21 -0700764 private String getTypedText() {
765 return mDefaultKeySsb.toString();
766 }
767
768 private void clearTypedText() {
769 mDefaultKeySsb.clear();
770 mDefaultKeySsb.clearSpans();
771 Selection.setSelection(mDefaultKeySsb, 0);
772 }
773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700775 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
776 * State
777 */
778 private static State intToState(int stateOrdinal) {
779 State state = State.WORKSPACE;
780 final State[] stateValues = State.values();
781 for (int i = 0; i < stateValues.length; i++) {
782 if (stateValues[i].ordinal() == stateOrdinal) {
783 state = stateValues[i];
784 break;
785 }
786 }
787 return state;
788 }
789
790 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 * Restores the previous state, if it exists.
792 *
793 * @param savedState The previous state.
794 */
795 private void restoreState(Bundle savedState) {
796 if (savedState == null) {
797 return;
798 }
799
Michael Jurka883f55b2010-10-21 15:47:14 -0700800 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700801 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800802 showAllApps(false);
803 }
804
Winson Chungf0c6ae02012-03-21 16:10:31 -0700805 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700807 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
809
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
811 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700812
Winson Chung3d503fb2011-07-13 17:25:49 -0700813 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
814 mPendingAddInfo.container = pendingAddContainer;
815 mPendingAddInfo.screen = pendingAddScreen;
816 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
817 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700818 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
819 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
820 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700821 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 mRestoring = true;
823 }
824
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
827 if (renameFolder) {
828 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700829 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 mRestoring = true;
831 }
Winson Chunga12a2502010-12-20 14:41:35 -0800832
Winson Chung785d2eb2011-04-14 16:08:02 -0700833
834 // Restore the AppsCustomize tab
835 if (mAppsCustomizeTabHost != null) {
836 String curTab = savedState.getString("apps_customize_currentTab");
837 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700838 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700839 mAppsCustomizeContent.setContentType(
840 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
841 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700842 mAppsCustomizeContent.loadAssociatedPages(
843 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700844 }
845
Winson Chung5afbf7b2011-07-25 11:53:08 -0700846 int currentIndex = savedState.getInt("apps_customize_currentIndex");
847 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
850
851 /**
852 * Finds all the views we need and configure them properly.
853 */
854 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700855 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400856
Winson Chung4c98d922011-05-31 16:50:48 -0700857 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
858 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800859 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
860 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861
Winson Chung4c98d922011-05-31 16:50:48 -0700862 // Setup the drag layer
863 mDragLayer.setup(this, dragController);
864
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 // Setup the hotseat
866 mHotseat = (Hotseat) findViewById(R.id.hotseat);
867 if (mHotseat != null) {
868 mHotseat.setup(this);
869 }
870
Winson Chung4c98d922011-05-31 16:50:48 -0700871 // Setup the workspace
872 mWorkspace.setHapticFeedbackEnabled(false);
873 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700874 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700875 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700876
Winson Chungf0ea4d32011-06-06 14:27:16 -0700877 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700878 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700879
Winson Chungf0ea4d32011-06-06 14:27:16 -0700880 // Setup AppsCustomize
881 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
882 findViewById(R.id.apps_customize_pane);
883 mAppsCustomizeContent = (AppsCustomizePagedView)
884 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700885 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700886 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400887
Michael Jurka5130e402011-10-13 04:55:35 -0700888 // Get the all apps button
889 mAllAppsButton = findViewById(R.id.all_apps_button);
890 if (mAllAppsButton != null) {
891 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
892 @Override
893 public boolean onTouch(View v, MotionEvent event) {
894 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
895 onTouchDownAllAppsButton(v);
896 }
897 return false;
898 }
899 });
900 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700901 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700902 dragController.setDragScoller(mWorkspace);
903 dragController.setScrollView(mDragLayer);
904 dragController.setMoveTarget(mWorkspace);
905 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700906 if (mSearchDropTargetBar != null) {
907 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
910
911 /**
912 * Creates a view representing a shortcut.
913 *
914 * @param info The data structure describing the shortcut.
915 *
916 * @return A View inflated from R.layout.application.
917 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800918 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700920 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 }
922
923 /**
924 * Creates a view representing a shortcut inflated from the specified resource.
925 *
926 * @param layoutResId The id of the XML layout used to create the shortcut.
927 * @param parent The group the shortcut belongs to.
928 * @param info The data structure describing the shortcut.
929 *
930 * @return A View inflated from layoutResId.
931 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800932 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800933 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
934 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 return favorite;
937 }
938
939 /**
940 * Add an application shortcut to the workspace.
941 *
942 * @param data The intent describing the application.
943 * @param cellInfo The position on screen where to create the shortcut.
944 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700948
Adam Cohenfbba09b2011-07-18 21:43:05 -0700949 // First we check if we already know the exact location where we want to add this item.
950 if (cellX >= 0 && cellY >= 0) {
951 cellXY[0] = cellX;
952 cellXY[1] = cellY;
953 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700954 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700955 return;
956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800958 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800959
Romain Guy73b979d2009-06-09 12:57:21 -0700960 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800961 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800963 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700965 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800966 } else {
967 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969 }
Romain Guycbb89e42009-06-08 15:52:54 -0700970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 /**
972 * Add a shortcut to the workspace.
973 *
974 * @param data The intent describing the shortcut.
975 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
978 int cellY) {
979 int[] cellXY = mTmpAddItemCellCoordinates;
980 int[] touchXY = mPendingAddInfo.dropPos;
981 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700982
Michael Jurkad3ef3062010-11-23 16:23:58 -0800983 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700984
Adam Cohen558baaf2011-08-15 15:22:57 -0700985 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800986 if (info == null) {
987 return;
988 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700989 final View view = createShortcut(info);
990
Adam Cohenfbba09b2011-07-18 21:43:05 -0700991 // First we check if we already know the exact location where we want to add this item.
992 if (cellX >= 0 && cellY >= 0) {
993 cellXY[0] = cellX;
994 cellXY[1] = cellY;
995 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700996
997 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800998 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700999 true, null,null)) {
1000 return;
1001 }
1002 DragObject dragObject = new DragObject();
1003 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001004 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1005 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001006 return;
1007 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001008 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001009 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001010 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001011 foundCellSpan = (result != null);
1012 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001013 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001014 }
1015
1016 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001017 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001018 return;
1019 }
1020
Adam Cohen558baaf2011-08-15 15:22:57 -07001021 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022
1023 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001024 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1025 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027 }
1028
Adam Cohen2f093b62012-04-30 18:59:53 -07001029 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1030 int minHeight) {
1031 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001032 // We want to account for the extra amount of padding that we are adding to the widget
1033 // to ensure that it gets the full amount of space that it has requested
1034 int requiredWidth = minWidth + padding.left + padding.right;
1035 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001036 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001037 }
1038
Adam Cohen2f093b62012-04-30 18:59:53 -07001039 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1040 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001041 }
1042
Adam Cohen2f093b62012-04-30 18:59:53 -07001043 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1044 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001045 }
1046
Adam Cohen2f093b62012-04-30 18:59:53 -07001047 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1048 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001049 }
1050
Adam Cohen2f093b62012-04-30 18:59:53 -07001051 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1052 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001053 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001054 }
1055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001059 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001060 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001062 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1063 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1064 if (appWidgetInfo == null) {
1065 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1066 }
Romain Guycbb89e42009-06-08 15:52:54 -07001067
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001069 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001070
Adam Cohen2f093b62012-04-30 18:59:53 -07001071 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1072 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001075 // We have saved the position to which the widget was dragged-- this really only matters
1076 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 int[] cellXY = mTmpAddItemCellCoordinates;
1078 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001079 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001081 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1082 cellXY[0] = mPendingAddInfo.cellX;
1083 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001084 spanXY[0] = mPendingAddInfo.spanX;
1085 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001086 foundCellSpan = true;
1087 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001089 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001090 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1091 spanXY[1], cellXY, finalSpan);
1092 spanXY[0] = finalSpan[0];
1093 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001094 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001096 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001097 }
1098
Michael Jurka0280c3b2010-09-17 15:00:07 -07001099 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001100 if (appWidgetId != -1) {
1101 // Deleting an app widget ID is a void call but writes to disk before returning
1102 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001103 new Thread("deleteAppWidgetId") {
1104 public void run() {
1105 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1106 }
1107 }.start();
1108 }
Winson Chung93eef082012-03-23 15:59:27 -07001109 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001110 return;
1111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001113 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001114 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1115 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 launcherInfo.spanX = spanXY[0];
1117 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001118 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1119 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001122 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123
1124 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001125 if (hostView == null) {
1126 // Perform actual inflation because we're live
1127 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1128 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1129 } else {
1130 // The AppWidgetHostView has already been inflated and instantiated
1131 launcherInfo.hostView = hostView;
1132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001135 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001136 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001139
1140 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001142 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
Romain Guycbb89e42009-06-08 15:52:54 -07001144
Adam Cohended9f8d2010-11-03 13:25:16 -07001145 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1146 @Override
1147 public void onReceive(Context context, Intent intent) {
1148 final String action = intent.getAction();
1149 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1150 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001151 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001152 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001153
Winson Chungc100e8e2011-08-09 16:02:43 -07001154 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001155 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001156 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1157 mAppsCustomizeTabHost.reset();
1158 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001159 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001160 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1161 mUserPresent = true;
1162 updateRunning();
1163 }
1164 }
1165 };
1166
1167 @Override
1168 public void onAttachedToWindow() {
1169 super.onAttachedToWindow();
1170
1171 // Listen for broadcasts related to user-presence
1172 final IntentFilter filter = new IntentFilter();
1173 filter.addAction(Intent.ACTION_SCREEN_OFF);
1174 filter.addAction(Intent.ACTION_USER_PRESENT);
1175 registerReceiver(mReceiver, filter);
1176
Adam Cohend113e0c2010-11-11 10:48:05 -08001177 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001178 mVisible = true;
1179 }
1180
1181 @Override
1182 public void onDetachedFromWindow() {
1183 super.onDetachedFromWindow();
1184 mVisible = false;
1185
Adam Cohend113e0c2010-11-11 10:48:05 -08001186 if (mAttached) {
1187 unregisterReceiver(mReceiver);
1188 mAttached = false;
1189 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001190 updateRunning();
1191 }
1192
1193 public void onWindowVisibilityChanged(int visibility) {
1194 mVisible = visibility == View.VISIBLE;
1195 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001196 // The following code used to be in onResume, but it turns out onResume is called when
1197 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1198 // is a more appropriate event to handle
1199 if (mVisible) {
1200 mAppsCustomizeTabHost.onWindowVisible();
1201 if (!mWorkspaceLoading) {
1202 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001203 // We want to let Launcher draw itself at least once before we force it to build
1204 // layers on all the workspace pages, so that transitioning to Launcher from other
1205 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1206 // a true draw so we wait until the second preDraw call to be safe
1207 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001208 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001209 // We delay the layer building a bit in order to give
1210 // other message processing a time to run. In particular
1211 // this avoids a delay in hiding the IME if it was
1212 // currently shown, because doing that may involve
1213 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001214 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001215
1216 // We had to enable the wallpaper visibility when launching apps from all
1217 // apps (so that the transitions would be the same as when launching from
1218 // workspace) so take this time to see if we need to re-disable the
1219 // wallpaper visibility to ensure performance.
1220 mWorkspace.post(new Runnable() {
1221 @Override
1222 public void run() {
Winson Chung18f41f82012-05-09 13:28:10 -07001223 disableWallpaperIfInAllApps();
Winson Chung31ce0732012-05-06 13:36:43 -07001224 }
1225 });
1226
Michael Jurka6ee21d22012-02-21 18:20:27 -08001227 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001228 return true;
1229 }
1230 });
1231 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001232 // When Launcher comes back to foreground, a different Activity might be responsible for
1233 // the app market intent, so refresh the icon
1234 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001235 clearTypedText();
1236 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001237 }
1238
1239 private void sendAdvanceMessage(long delay) {
1240 mHandler.removeMessages(ADVANCE_MSG);
1241 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1242 mHandler.sendMessageDelayed(msg, delay);
1243 mAutoAdvanceSentTime = System.currentTimeMillis();
1244 }
1245
1246 private void updateRunning() {
1247 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1248 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1249 mAutoAdvanceRunning = autoAdvanceRunning;
1250 if (autoAdvanceRunning) {
1251 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1252 sendAdvanceMessage(delay);
1253 } else {
1254 if (!mWidgetsToAdvance.isEmpty()) {
1255 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1256 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1257 }
1258 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001259 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001260 }
1261 }
1262 }
1263
1264 private final Handler mHandler = new Handler() {
1265 @Override
1266 public void handleMessage(Message msg) {
1267 if (msg.what == ADVANCE_MSG) {
1268 int i = 0;
1269 for (View key: mWidgetsToAdvance.keySet()) {
1270 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1271 final int delay = mAdvanceStagger * i;
1272 if (v instanceof Advanceable) {
1273 postDelayed(new Runnable() {
1274 public void run() {
1275 ((Advanceable) v).advance();
1276 }
1277 }, delay);
1278 }
1279 i++;
1280 }
1281 sendAdvanceMessage(mAdvanceInterval);
1282 }
1283 }
1284 };
1285
1286 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001287 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1289 if (v instanceof Advanceable) {
1290 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001291 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001292 updateRunning();
1293 }
1294 }
1295
1296 void removeWidgetToAutoAdvance(View hostView) {
1297 if (mWidgetsToAdvance.containsKey(hostView)) {
1298 mWidgetsToAdvance.remove(hostView);
1299 updateRunning();
1300 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301 }
1302
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001305 launcherInfo.hostView = null;
1306 }
1307
Winson Chung93eef082012-03-23 15:59:27 -07001308 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1309 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1310 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001311 }
1312
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001313 public LauncherAppWidgetHost getAppWidgetHost() {
1314 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
Romain Guycbb89e42009-06-08 15:52:54 -07001316
Winson Chunga9abd0e2010-10-27 17:18:37 -07001317 public LauncherModel getModel() {
1318 return mModel;
1319 }
1320
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001321 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001322 getWindow().closeAllPanels();
1323
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001324 // Whatever we were doing is hereby canceled.
1325 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001326 }
1327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 @Override
1329 protected void onNewIntent(Intent intent) {
1330 super.onNewIntent(intent);
1331
1332 // Close the menu
1333 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001334 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001335 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001336
Joe Onorato14f122b2009-11-19 14:06:36 -08001337 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1338 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001339
Adam Cohenac56cff2011-09-28 20:45:37 -07001340 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001341 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001342 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001343 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1344 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001345 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001346 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001347
1348 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001349 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001350 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001351
Joe Onorato3a8820b2009-11-10 15:06:42 -08001352 final View v = getWindow().peekDecorView();
1353 if (v != null && v.getWindowToken() != null) {
1354 InputMethodManager imm = (InputMethodManager)getSystemService(
1355 INPUT_METHOD_SERVICE);
1356 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001358
Michael Jurka35aa14d2011-07-07 17:01:08 -07001359 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001360 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001361 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364 }
1365
1366 @Override
1367 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1368 // Do not call super here
1369 mSavedInstanceState = savedInstanceState;
1370 }
1371
1372 @Override
1373 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001374 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001375 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376
Michael Jurka883f55b2010-10-21 15:47:14 -07001377 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001378 // We close any open folder since it will not be re-opened, and we need to make sure
1379 // this state is reflected.
1380 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1383 mWaitingForResult) {
1384 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1385 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1386 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1387 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001388 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1389 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1390 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392
1393 if (mFolderInfo != null && mWaitingForResult) {
1394 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1395 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1396 }
Michael Jurka946ad472010-07-09 18:05:18 -07001397
Winson Chung785d2eb2011-04-14 16:08:02 -07001398 // Save the current AppsCustomize tab
1399 if (mAppsCustomizeTabHost != null) {
1400 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1401 if (currentTabTag != null) {
1402 outState.putString("apps_customize_currentTab", currentTabTag);
1403 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001404 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1405 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
1409 @Override
1410 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001412
Winson Chunge7a03942011-08-05 15:05:12 -07001413 // Remove all pending runnables
1414 mHandler.removeMessages(ADVANCE_MSG);
1415 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001416 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001417
Winson Chungcd2b0142011-06-08 16:02:26 -07001418 // Stop callbacks from LauncherModel
1419 LauncherApplication app = ((LauncherApplication) getApplication());
1420 mModel.stopLoader();
1421 app.setLauncher(null);
1422
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001424 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001426 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001428 mAppWidgetHost = null;
1429
1430 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
1432 TextKeyListener.getInstance().release();
1433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001436
1437 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001438 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001439
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001440 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001441 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1442 mWorkspace.removeAllViews();
1443 mWorkspace = null;
1444 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001445
1446 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448
Adam Cohena9cf38f2011-05-02 15:36:58 -07001449 public DragController getDragController() {
1450 return mDragController;
1451 }
1452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 @Override
1454 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001455 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 super.startActivityForResult(intent, requestCode);
1457 }
1458
Winson Chung70d72102011-08-12 11:24:35 -07001459 /**
1460 * Indicates that we want global search for this activity by setting the globalSearch
1461 * argument for {@link #startSearch} to true.
1462 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001464 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001466
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001467 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001468
Karl Rosaen138a0412009-04-23 19:00:21 -07001469 if (initialQuery == null) {
1470 // Use any text typed in the launcher as the initial query
1471 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 if (appSearchData == null) {
1474 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001475 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001477 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Karl Rosaen138a0412009-04-23 19:00:21 -07001479 final SearchManager searchManager =
1480 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001481 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001482 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
1484
Winson Chung70d72102011-08-12 11:24:35 -07001485 @Override
1486 public boolean onCreateOptionsMenu(Menu menu) {
1487 if (isWorkspaceLocked()) {
1488 return false;
1489 }
1490
1491 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001492
1493 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1494 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1495 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001496 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1497 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1498 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001499 String helpUrl = getString(R.string.help_url);
1500 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001501 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1502 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1503
Winson Chung70d72102011-08-12 11:24:35 -07001504 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1505 .setIcon(android.R.drawable.ic_menu_gallery)
1506 .setAlphabeticShortcut('W');
1507 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1508 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001509 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001510 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001511 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1512 .setIcon(android.R.drawable.ic_menu_preferences)
1513 .setIntent(settings)
1514 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001515 if (!helpUrl.isEmpty()) {
1516 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1517 .setIcon(android.R.drawable.ic_menu_help)
1518 .setIntent(help)
1519 .setAlphabeticShortcut('H');
1520 }
Winson Chung70d72102011-08-12 11:24:35 -07001521 return true;
1522 }
1523
1524 @Override
1525 public boolean onPrepareOptionsMenu(Menu menu) {
1526 super.onPrepareOptionsMenu(menu);
1527
1528 if (mAppsCustomizeTabHost.isTransitioning()) {
1529 return false;
1530 }
1531 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1532 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1533
1534 return true;
1535 }
1536
1537 @Override
1538 public boolean onOptionsItemSelected(MenuItem item) {
1539 switch (item.getItemId()) {
1540 case MENU_WALLPAPER_SETTINGS:
1541 startWallpaper();
1542 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001543 }
1544
1545 return super.onOptionsItemSelected(item);
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001548 @Override
1549 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001550 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001551 // Use a custom animation for launching search
1552 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001554 }
1555
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 public boolean isWorkspaceLocked() {
1557 return mWorkspaceLoading || mWaitingForResult;
1558 }
1559
Michael Jurka0280c3b2010-09-17 15:00:07 -07001560 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 mPendingAddInfo.container = ItemInfo.NO_ID;
1562 mPendingAddInfo.screen = -1;
1563 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1564 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001565 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001566 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001567 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001568
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001569 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1570 AppWidgetProviderInfo appWidgetInfo) {
1571 if (appWidgetInfo.configure != null) {
1572 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001573
Bjorn Bringert7984c942009-12-09 15:38:25 +00001574 // Launch over to configure widget, if needed
1575 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001576 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001577 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001578 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001580 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001581 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1582 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001583 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001584 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586 }
Romain Guycbb89e42009-06-08 15:52:54 -07001587
Adam Cohenfbba09b2011-07-18 21:43:05 -07001588 /**
1589 * Process a shortcut drop.
1590 *
1591 * @param componentName The name of the component
1592 * @param screen The screen where it should be added
1593 * @param cell The cell it should be added to, optional
1594 * @param position The location on the screen where it was dropped, optional
1595 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001596 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1597 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001598 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001599 mPendingAddInfo.container = container;
1600 mPendingAddInfo.screen = screen;
1601 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001602
1603 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001604 mPendingAddInfo.cellX = cell[0];
1605 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001606 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001607
1608 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1609 createShortcutIntent.setComponent(componentName);
1610 processShortcut(createShortcutIntent);
1611 }
1612
Adam Cohenfbba09b2011-07-18 21:43:05 -07001613 /**
1614 * Process a widget drop.
1615 *
1616 * @param info The PendingAppWidgetInfo of the widget being added.
1617 * @param screen The screen where it should be added
1618 * @param cell The cell it should be added to, optional
1619 * @param position The location on the screen where it was dropped, optional
1620 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001621 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001622 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001623 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001624 mPendingAddInfo.container = info.container = container;
1625 mPendingAddInfo.screen = info.screen = screen;
1626 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001627 mPendingAddInfo.minSpanX = info.minSpanX;
1628 mPendingAddInfo.minSpanY = info.minSpanY;
1629
Adam Cohenfbba09b2011-07-18 21:43:05 -07001630 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001631 mPendingAddInfo.cellX = cell[0];
1632 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001633 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001634 if (span != null) {
1635 mPendingAddInfo.spanX = span[0];
1636 mPendingAddInfo.spanY = span[1];
1637 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001638
Adam Cohened66b2b2012-01-23 17:28:51 -08001639 AppWidgetHostView hostView = info.boundWidget;
1640 int appWidgetId;
1641 if (hostView != null) {
1642 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001643 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001644 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001645 // In this case, we either need to start an activity to get permission to bind
1646 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001647 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001648 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001649 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001650 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001651 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001652 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1653 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1654 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1655 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1656 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001657 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001658 }
1659
Joe Onoratodeb98af2010-02-19 14:59:39 -08001660 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001661 // Handle case where user selected "Applications"
1662 String applicationName = getResources().getString(R.string.group_applications);
1663 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001664
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001665 if (applicationName != null && applicationName.equals(shortcutName)) {
1666 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1667 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001668
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001669 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1670 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001671 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001672 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001673 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001674 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
1677
Winson Chung24ab2f12010-09-16 14:10:47 -07001678 void processWallpaper(Intent intent) {
1679 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1680 }
1681
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1683 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001684 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 folderInfo.title = getText(R.string.folder_name);
1686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001688 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1689 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001690 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691
1692 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001693 FolderIcon newFolder =
1694 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1695 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1696 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001697 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
Romain Guycbb89e42009-06-08 15:52:54 -07001699
Joe Onorato9c1289c2009-08-17 11:03:03 -04001700 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001701 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001702 }
1703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001705 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001707 Intent chooser = Intent.createChooser(pickWallpaper,
1708 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001709 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1710 // Removed in Eclair MR1
1711// WallpaperManager wm = (WallpaperManager)
1712// getSystemService(Context.WALLPAPER_SERVICE);
1713// WallpaperInfo wi = wm.getWallpaperInfo();
1714// if (wi != null && wi.getSettingsActivity() != null) {
1715// LabeledIntent li = new LabeledIntent(getPackageName(),
1716// R.string.configure_wallpaper, 0);
1717// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1718// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1719// }
Mike Clerona0618e42009-10-22 13:55:21 -07001720 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001723 /**
1724 * Registers various content observers. The current implementation registers
1725 * only a favorites observer to keep track of the favorites applications.
1726 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001727 private void registerContentObservers() {
1728 ContentResolver resolver = getContentResolver();
1729 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1730 true, mWidgetObserver);
1731 }
1732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 @Override
1734 public boolean dispatchKeyEvent(KeyEvent event) {
1735 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1736 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001738 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001739 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001740 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001741 dumpState();
1742 return true;
1743 }
1744 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001745 }
1746 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1747 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001748 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 return true;
1750 }
1751 }
1752
1753 return super.dispatchKeyEvent(event);
1754 }
1755
Joe Onorato88ec0992009-11-19 13:16:06 -08001756 @Override
1757 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001758 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001759 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001760 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001761 Folder openFolder = mWorkspace.getOpenFolder();
1762 if (openFolder.isEditingName()) {
1763 openFolder.dismissEditingName();
1764 } else {
1765 closeFolder();
1766 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001767 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001768 mWorkspace.exitWidgetResizeMode();
1769
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001770 // Back button is a no-op here, but give at least some feedback for the button press
1771 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001772 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001773 }
1774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001776 * Re-listen when widgets are reset.
1777 */
1778 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001779 if (mAppWidgetHost != null) {
1780 mAppWidgetHost.startListening();
1781 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001782 }
1783
1784 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1786 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001788 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001789 if (mModel != null) {
1790 mModel.unbindWorkspaceItems();
1791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 /**
1795 * Launches the intent referred by the clicked shortcut.
1796 *
1797 * @param v The view representing the clicked shortcut.
1798 */
1799 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001800 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1801 // view has detached (it's possible for this to happen if the view is removed mid touch).
1802 if (v.getWindowToken() == null) {
1803 return;
1804 }
1805
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001806 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001807 return;
1808 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001811 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001813 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001814 int[] pos = new int[2];
1815 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001816 intent.setSourceBounds(new Rect(pos[0], pos[1],
1817 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001818
1819 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001820
1821 if (success && v instanceof BubbleTextView) {
1822 mWaitingForResume = (BubbleTextView) v;
1823 mWaitingForResume.setStayPressed(true);
1824 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001826 if (v instanceof FolderIcon) {
1827 FolderIcon fi = (FolderIcon) v;
1828 handleFolderClick(fi);
1829 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001830 } else if (v == mAllAppsButton) {
1831 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001832 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001833 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001834 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837 }
1838
Michael Jurka0e260592010-06-30 17:07:39 -07001839 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001840 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001841 // clicking anywhere on the workspace causes the customization drawer to slide down
1842 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001843 return false;
1844 }
1845
Michael Jurkaaf442092010-06-10 17:01:57 -07001846 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001847 * Event handler for the search button
1848 *
1849 * @param v The view that was clicked.
1850 */
1851 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001852 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1853
Amith Yamasania135ba82011-08-09 17:42:01 -07001854 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001855 }
1856
1857 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001858 * Event handler for the voice button
1859 *
1860 * @param v The view that was clicked.
1861 */
1862 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001863 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001864
Michael Jurkaae65ee32012-04-30 11:45:54 -07001865 try {
1866 final SearchManager searchManager =
1867 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1868 ComponentName activityName = searchManager.getGlobalSearchActivity();
1869 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1870 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1871 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1872 if (activityName != null) {
1873 intent.setPackage(activityName.getPackageName());
1874 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001875 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001876 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001877 } catch (ActivityNotFoundException e) {
1878 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1879 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1880 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1881 startActivitySafely(null, intent, "onClickVoiceButton");
1882 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001883 }
1884
1885 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001886 * Event handler for the "grid" button that appears on the home screen, which
1887 * enters all apps mode.
1888 *
1889 * @param v The view that was clicked.
1890 */
1891 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001892 showAllApps(true);
1893 }
1894
1895 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001896 // Provide the same haptic feedback that the system offers for virtual keys.
1897 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001898 }
1899
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001900 public void onClickAppMarketButton(View v) {
1901 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001902 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001903 } else {
1904 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001905 }
1906 }
1907
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001908 void startApplicationDetailsActivity(ComponentName componentName) {
1909 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001910 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1911 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001912 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001913 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001914 }
1915
Patrick Dubroy5539af72010-09-07 15:22:01 -07001916 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1917 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1918 // System applications cannot be installed. For now, show a toast explaining that.
1919 // We may give them the option of disabling apps this way.
1920 int messageId = R.string.uninstall_system_app_text;
1921 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1922 } else {
1923 String packageName = appInfo.componentName.getPackageName();
1924 String className = appInfo.componentName.getClassName();
1925 Intent intent = new Intent(
1926 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001927 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1928 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001929 startActivity(intent);
1930 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001931 }
1932
Michael Jurka86a720e2012-05-09 11:23:23 -07001933 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001937 // Only launch using the new animation if the shortcut has not opted out (this is a
1938 // private contract between launcher and may be ignored in the future).
1939 boolean useLaunchAnimation = (v != null) &&
1940 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1941 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001942 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1943 v.getMeasuredWidth(), v.getMeasuredHeight());
1944
1945 startActivity(intent, opts.toBundle());
1946 } else {
1947 startActivity(intent);
1948 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001949 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 } catch (SecurityException e) {
1951 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001952 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001954 "or use the exported attribute for this activity. "
1955 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001957 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001959
Michael Jurka86a720e2012-05-09 11:23:23 -07001960 boolean startActivitySafely(View v, Intent intent, Object tag) {
1961 boolean success = false;
1962 try {
1963 success = startActivity(v, intent, tag);
1964 } catch (ActivityNotFoundException e) {
1965 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1966 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1967 }
1968 return success;
1969 }
1970
Romain Guy8e633c52010-03-04 12:51:36 -08001971 void startActivityForResultSafely(Intent intent, int requestCode) {
1972 try {
1973 startActivityForResult(intent, requestCode);
1974 } catch (ActivityNotFoundException e) {
1975 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1976 } catch (SecurityException e) {
1977 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1978 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1979 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1980 "or use the exported attribute for this activity.", e);
1981 }
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
Adam Cohena9cf38f2011-05-02 15:36:58 -07001984 private void handleFolderClick(FolderIcon folderIcon) {
1985 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001986 Folder openFolder = mWorkspace.getFolderForTag(info);
1987
1988 // If the folder info reports that the associated folder is open, then verify that
1989 // it is actually opened. There have been a few instances where this gets out of sync.
1990 if (info.opened && openFolder == null) {
1991 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1992 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1993 info.opened = false;
1994 }
1995
Adam Cohena9cf38f2011-05-02 15:36:58 -07001996 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 // Close any open folder
1998 closeFolder();
1999 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002000 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 } else {
2002 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 int folderScreen;
2004 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002005 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 // .. and close it
2007 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002008 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 // Close any folder open on the current screen
2010 closeFolder();
2011 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002012 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014 }
2015 }
2016 }
2017
Adam Cohen2801caf2011-05-13 20:57:39 -07002018 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002019 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002020 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2021 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2022 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2023
Adam Cohenc51934b2011-07-26 21:07:43 -07002024 FolderInfo info = (FolderInfo) fi.getTag();
2025 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2026 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002027 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2028 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002029 }
2030
Adam Cohen2801caf2011-05-13 20:57:39 -07002031 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2032 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2033 oa.start();
2034 }
2035
2036 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002037 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002038 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2039 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2040 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2041
Adam Cohenc51934b2011-07-26 21:07:43 -07002042 FolderInfo info = (FolderInfo) fi.getTag();
2043 CellLayout cl = null;
2044 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2045 cl = (CellLayout) fi.getParent().getParent();
2046 }
2047
2048 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002049 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2050 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002051 oa.addListener(new AnimatorListenerAdapter() {
2052 @Override
2053 public void onAnimationEnd(Animator animation) {
2054 if (layout != null) {
2055 layout.clearFolderLeaveBehind();
2056 }
2057 }
2058 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002059 oa.start();
2060 }
2061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002063 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 * is animated relative to the specified View. If the View is null, no animation
2065 * is played.
2066 *
2067 * @param folderInfo The FolderInfo describing the folder to open.
2068 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002069 public void openFolder(FolderIcon folderIcon) {
2070 Folder folder = folderIcon.mFolder;
2071 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072
Adam Cohen2801caf2011-05-13 20:57:39 -07002073 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002074 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075
Adam Cohen4554ee12011-08-03 16:13:21 -07002076 // Just verify that the folder hasn't already been added to the DragLayer.
2077 // There was a one-off crash where the folder had a parent already.
2078 if (folder.getParent() == null) {
2079 mDragLayer.addView(folder);
2080 mDragController.addDropTarget((DropTarget) folder);
2081 } else {
2082 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2083 folder.getParent() + ").");
2084 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002085 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002086 }
2087
2088 public void closeFolder() {
2089 Folder folder = mWorkspace.getOpenFolder();
2090 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002091 if (folder.isEditingName()) {
2092 folder.dismissEditingName();
2093 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002094 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002095
2096 // Dismiss the folder cling
2097 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002098 }
2099 }
2100
2101 void closeFolder(Folder folder) {
2102 folder.getInfo().opened = false;
2103
2104 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2105 if (parent != null) {
2106 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2107 shrinkAndFadeInFolderIcon(fi);
2108 }
2109 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002113 if (!isDraggingEnabled()) return false;
2114 if (isWorkspaceLocked()) return false;
2115 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116
2117 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002118 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
2120
Michael Jurka0280c3b2010-09-17 15:00:07 -07002121 resetAddInfo();
2122 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002124 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 return true;
2126 }
2127
Winson Chung3d503fb2011-07-13 17:25:49 -07002128 // The hotseat touch handling does not go through Workspace, and we always allow long press
2129 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002130 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002131 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2132 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002133 if (itemUnderLongClick == null) {
2134 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002135 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2136 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002137 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002139 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002141 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 }
2143 }
2144 }
2145 return true;
2146 }
2147
Winson Chung3d503fb2011-07-13 17:25:49 -07002148 boolean isHotseatLayout(View layout) {
2149 return mHotseat != null && layout != null &&
2150 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2151 }
2152 Hotseat getHotseat() {
2153 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002154 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002155 SearchDropTargetBar getSearchBar() {
2156 return mSearchDropTargetBar;
2157 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002158
Winson Chung3d503fb2011-07-13 17:25:49 -07002159 /**
2160 * Returns the CellLayout of the specified container at the specified screen.
2161 */
2162 CellLayout getCellLayout(long container, int screen) {
2163 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2164 if (mHotseat != null) {
2165 return mHotseat.getLayout();
2166 } else {
2167 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002168 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002169 } else {
2170 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002171 }
2172 }
2173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 Workspace getWorkspace() {
2175 return mWorkspace;
2176 }
2177
Daniel Sandler843e8602010-06-07 14:59:01 -04002178 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2179 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002180 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002181 }
2182
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002183 public boolean isAllAppsButtonRank(int rank) {
2184 return mHotseat.isAllAppsButtonRank(rank);
2185 }
2186
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002187 // AllAppsView.Watcher
2188 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002189 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002190 mWorkspace.setVisibility(View.GONE);
2191 }
2192 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002193
2194 /**
2195 * Helper method for the cameraZoomIn/cameraZoomOut animations
2196 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002197 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002198 * @param scaleFactor The scale factor used for the zoom
2199 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002200 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002201 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002202 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002203 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002204
Winson Chung18f41f82012-05-09 13:28:10 -07002205 void disableWallpaperIfInAllApps() {
2206 // Only disable it if we are in all apps
2207 if (mState == State.APPS_CUSTOMIZE) {
2208 if (mAppsCustomizeTabHost != null &&
2209 !mAppsCustomizeTabHost.isTransitioning()) {
2210 updateWallpaperVisibility(false);
2211 }
2212 }
2213 }
2214
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002215 void updateWallpaperVisibility(boolean visible) {
2216 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2217 int curflags = getWindow().getAttributes().flags
2218 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2219 if (wpflags != curflags) {
2220 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2221 }
2222 }
2223
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002224 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2225 if (v instanceof LauncherTransitionable) {
2226 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2227 }
2228 }
2229
Michael Jurkabed61d22012-02-14 22:51:29 -08002230 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07002231 if (toWorkspace) {
2232 Log.d(TAG, "6549598 Start animation to workspace");
2233 } else {
2234 Log.d(TAG, "6549598 Start animation to all apps");
2235 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002236 if (v instanceof LauncherTransitionable) {
2237 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2238 }
Winson Chung70442722012-02-10 15:43:22 -08002239
2240 // Update the workspace transition step as well
2241 dispatchOnLauncherTransitionStep(v, 0f);
2242 }
2243
2244 private void dispatchOnLauncherTransitionStep(View v, float t) {
2245 if (v instanceof LauncherTransitionable) {
2246 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2247 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002248 }
2249
2250 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
Winson Chung7c7a22d2012-06-01 13:46:48 -07002251 if (toWorkspace) {
2252 Log.d(TAG, "6549598 End animation to workspace");
2253 } else {
2254 Log.d(TAG, "6549598 End animation to all apps");
2255 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002256 if (v instanceof LauncherTransitionable) {
2257 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2258 }
Winson Chung70442722012-02-10 15:43:22 -08002259
2260 // Update the workspace transition step as well
2261 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002262 }
2263
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002264 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002265 * Things to test when changing the following seven functions.
2266 * - Home from workspace
2267 * - from center screen
2268 * - from other screens
2269 * - Home from all apps
2270 * - from center screen
2271 * - from other screens
2272 * - Back from all apps
2273 * - from center screen
2274 * - from other screens
2275 * - Launch app from workspace and quit
2276 * - with back
2277 * - with home
2278 * - Launch app from all apps and quit
2279 * - with back
2280 * - with home
2281 * - Go to a screen that's not the default, then all
2282 * apps, and launch and app, and go back
2283 * - with back
2284 * -with home
2285 * - On workspace, long press power and go back
2286 * - with back
2287 * - with home
2288 * - On all apps, long press power and go back
2289 * - with back
2290 * - with home
2291 * - On workspace, power off
2292 * - On all apps, power off
2293 * - Launch an app and turn off the screen while in that app
2294 * - Go back with home key
2295 * - Go back with back key TODO: make this not go to workspace
2296 * - From all apps
2297 * - From workspace
2298 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2299 * - From all apps
2300 * - From the center workspace
2301 * - From another workspace
2302 */
2303
2304 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002305 * Zoom the camera out from the workspace to reveal 'toView'.
2306 * Assumes that the view to show is anchored at either the very top or very bottom
2307 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002308 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002309 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002310 if (mStateAnimation != null) {
2311 mStateAnimation.cancel();
2312 mStateAnimation = null;
2313 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002314 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002315
Winson Chungf0ea4d32011-06-06 14:27:16 -07002316 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2317 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2318 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002319 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002320 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002321 final int startDelay =
2322 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002323
Michael Jurkab3e22d92011-10-31 15:58:33 -07002324 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002325
Michael Jurkad74c9842011-07-10 12:44:21 -07002326 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002327 Animator workspaceAnim =
2328 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002329
2330 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002331 toView.setScaleX(scale);
2332 toView.setScaleY(scale);
2333 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2334 scaleAnim.
2335 scaleX(1f).scaleY(1f).
2336 setDuration(duration).
2337 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002338
Winson Chungf0ea4d32011-06-06 14:27:16 -07002339 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002340 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002341 final ObjectAnimator alphaAnim = ObjectAnimator
2342 .ofFloat(toView, "alpha", 0f, 1f)
2343 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002344 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002345 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2346 @Override
2347 public void onAnimationUpdate(ValueAnimator animation) {
2348 float t = (Float) animation.getAnimatedValue();
2349 dispatchOnLauncherTransitionStep(fromView, t);
2350 dispatchOnLauncherTransitionStep(toView, t);
2351 }
2352 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002353
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002354 // toView should appear right at the end of the workspace shrink
2355 // animation
2356 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002357 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002358 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002359
2360 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002361 boolean animationCancelled = false;
2362
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002363 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002364 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002365 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002366 // Prepare the position
2367 toView.setTranslationX(0.0f);
2368 toView.setTranslationY(0.0f);
2369 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002370 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002371 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002372 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002373 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002374 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2375 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002376
2377 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2378 // Hide the workspace scrollbar
2379 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002380 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002381 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002382 if (!animationCancelled) {
2383 updateWallpaperVisibility(false);
2384 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002385
2386 // Hide the search bar
2387 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002388 }
2389
Adam Cohencff6af82011-09-13 14:51:53 -07002390 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002391 public void onAnimationCancel(Animator animation) {
2392 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002393 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002394 });
2395
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002396 if (workspaceAnim != null) {
2397 mStateAnimation.play(workspaceAnim);
2398 }
Michael Jurka1899a362011-11-03 13:50:45 -07002399
2400 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002401 final ViewTreeObserver observer;
2402
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002403 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2404 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002405
2406 // If any of the objects being animated haven't been measured/laid out
2407 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002408 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002409 (mWorkspace.getMeasuredWidth() == 0) ||
2410 (toView.getMeasuredWidth() == 0)) {
2411 observer = mWorkspace.getViewTreeObserver();
2412 delayAnim = true;
2413 } else {
2414 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002415 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002416
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002417 final AnimatorSet stateAnimation = mStateAnimation;
2418 final Runnable startAnimRunnable = new Runnable() {
2419 public void run() {
2420 // Check that mStateAnimation hasn't changed while
2421 // we waited for a layout/draw pass
2422 if (mStateAnimation != stateAnimation)
2423 return;
2424 setPivotsForZoom(toView, scale);
2425 dispatchOnLauncherTransitionStart(fromView, animated, false);
2426 dispatchOnLauncherTransitionStart(toView, animated, false);
2427 mWorkspace.post(new Runnable() {
2428 public void run() {
2429 // Check that mStateAnimation hasn't changed while
2430 // we waited for a layout/draw pass
2431 if (mStateAnimation != stateAnimation)
2432 return;
2433 mStateAnimation.start();
2434 }
2435 });
2436 }
2437 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002438 if (delayAnim) {
2439 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2440 public void onGlobalLayout() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002441 mWorkspace.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002442 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002443 }
2444 };
2445 observer.addOnGlobalLayoutListener(delayedStart);
2446 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002447 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002448 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002449 } else {
2450 toView.setTranslationX(0.0f);
2451 toView.setTranslationY(0.0f);
2452 toView.setScaleX(1.0f);
2453 toView.setScaleY(1.0f);
2454 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002455 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002456
Michael Jurkabed61d22012-02-14 22:51:29 -08002457 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2458 // Hide the workspace scrollbar
2459 mWorkspace.hideScrollingIndicator(true);
2460 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002461
2462 // Hide the search bar
2463 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002464 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002465 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002466 dispatchOnLauncherTransitionStart(fromView, animated, false);
2467 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002468 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002469 dispatchOnLauncherTransitionStart(toView, animated, false);
2470 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002471 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002473 }
2474
2475 /**
2476 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002477 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002478 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002479 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002480 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2481 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002482
Michael Jurkab3e22d92011-10-31 15:58:33 -07002483 if (mStateAnimation != null) {
2484 mStateAnimation.cancel();
2485 mStateAnimation = null;
2486 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002487 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002488
Winson Chungf0ea4d32011-06-06 14:27:16 -07002489 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002490 final int fadeOutDuration =
2491 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002492 final float scaleFactor = (float)
2493 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2494 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002495 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002496 Animator workspaceAnim = null;
2497
2498 if (toState == State.WORKSPACE) {
2499 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2500 workspaceAnim = mWorkspace.getChangeStateAnimation(
2501 Workspace.State.NORMAL, animated, stagger);
2502 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2503 workspaceAnim = mWorkspace.getChangeStateAnimation(
2504 Workspace.State.SPRING_LOADED, animated);
2505 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002506
Michael Jurkab3e22d92011-10-31 15:58:33 -07002507 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002508 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002509 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002510 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002511 final LauncherViewPropertyAnimator scaleAnim =
2512 new LauncherViewPropertyAnimator(fromView);
2513 scaleAnim.
2514 scaleX(scaleFactor).scaleY(scaleFactor).
2515 setDuration(duration).
2516 setInterpolator(new Workspace.ZoomInInterpolator());
2517
2518 final ObjectAnimator alphaAnim = ObjectAnimator
2519 .ofFloat(fromView, "alpha", 1f, 0f)
2520 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002521 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002522 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2523 @Override
2524 public void onAnimationUpdate(ValueAnimator animation) {
2525 float t = 1f - (Float) animation.getAnimatedValue();
2526 dispatchOnLauncherTransitionStep(fromView, t);
2527 dispatchOnLauncherTransitionStep(toView, t);
2528 }
2529 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002530
Michael Jurkabed61d22012-02-14 22:51:29 -08002531 mStateAnimation = new AnimatorSet();
2532
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002533 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2534 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002535
2536 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002537 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002538 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002539 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002540 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002541 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2542 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002543 if (mWorkspace != null) {
2544 mWorkspace.hideScrollingIndicator(false);
2545 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002546 if (onCompleteRunnable != null) {
2547 onCompleteRunnable.run();
2548 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002549 }
2550 });
2551
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002553 if (workspaceAnim != null) {
2554 mStateAnimation.play(workspaceAnim);
2555 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002556 dispatchOnLauncherTransitionStart(fromView, animated, true);
2557 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002558 final Animator stateAnimation = mStateAnimation;
2559 mWorkspace.post(new Runnable() {
2560 public void run() {
2561 if (stateAnimation != mStateAnimation)
2562 return;
2563 mStateAnimation.start();
2564 }
2565 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 } else {
2567 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002568 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002569 dispatchOnLauncherTransitionStart(fromView, animated, true);
2570 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002571 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002572 dispatchOnLauncherTransitionStart(toView, animated, true);
2573 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002574 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002576 }
2577
Michael Jurkae326f182011-11-21 14:05:46 -08002578 @Override
2579 public void onTrimMemory(int level) {
2580 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002581 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002582 mAppsCustomizeTabHost.onTrimMemory();
2583 }
2584 }
2585
Winson Chung18f41f82012-05-09 13:28:10 -07002586 @Override
2587 public void onWindowFocusChanged(boolean hasFocus) {
2588 if (!hasFocus) {
2589 // When another window occludes launcher (like the notification shade, or recents),
2590 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2591 updateWallpaperVisibility(true);
2592 } else {
2593 // When launcher has focus again, disable the wallpaper if we are in AllApps
2594 disableWallpaperIfInAllApps();
2595 }
2596 }
2597
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002598 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002599 showWorkspace(animated, null);
2600 }
2601
2602 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002603 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002604 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002605 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002606 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002607
Winson Chungc7d2b602012-05-16 17:02:20 -07002608 // Show the search bar (only animate if we were showing the drop target bar in spring
2609 // loaded mode)
2610 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2611
Michael Jurkab737ee62011-11-15 15:57:22 -08002612 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002613 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002614
2615 // Set focus to the AppsCustomize button
2616 if (mAllAppsButton != null) {
2617 mAllAppsButton.requestFocus();
2618 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002619 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002620
Michael Jurkab737ee62011-11-15 15:57:22 -08002621 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002622
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002623 // Change the state *after* we've called all the transition code
2624 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002625
Winson Chung5fb63472011-02-02 17:03:37 -08002626 // Resume the auto-advance of widgets
2627 mUserPresent = true;
2628 updateRunning();
2629
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002630 // send an accessibility event to announce the context change
2631 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002632 }
2633
Michael Jurkab3e22d92011-10-31 15:58:33 -07002634 void showAllApps(boolean animated) {
2635 if (mState != State.WORKSPACE) return;
2636
2637 showAppsCustomizeHelper(animated, false);
2638 mAppsCustomizeTabHost.requestFocus();
2639
Michael Jurkab3e22d92011-10-31 15:58:33 -07002640 // Change the state *after* we've called all the transition code
2641 mState = State.APPS_CUSTOMIZE;
2642
2643 // Pause the auto-advance of widgets until we are out of AllApps
2644 mUserPresent = false;
2645 updateRunning();
2646 closeFolder();
2647
2648 // Send an accessibility event to announce the context change
2649 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2650 }
2651
Adam Cohen7777d962011-08-18 18:58:38 -07002652 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002653 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002654 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002655 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002656 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002657 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002658 }
Adam Cohen7777d962011-08-18 18:58:38 -07002659
Adam Cohened66b2b2012-01-23 17:28:51 -08002660 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2661 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002662 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2663
Winson Chunge7a03942011-08-05 15:05:12 -07002664 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002665 @Override
2666 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002667 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002668 // Before we show workspace, hide all apps again because
2669 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2670 // clean up our state transition functions
2671 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002672 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002673 } else {
2674 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002675 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002676 }
2677 }, (extendedDelay ?
2678 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2679 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2680 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002681
Michael Jurkad3ef3062010-11-23 16:23:58 -08002682 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002683 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002684 final boolean animated = true;
2685 final boolean springLoaded = true;
2686 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002687 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002688 }
2689 // Otherwise, we are not in spring loaded mode, so don't do anything.
2690 }
2691
Michael Jurkab737ee62011-11-15 15:57:22 -08002692 void hideDockDivider() {
2693 if (mQsbDivider != null && mDockDivider != null) {
2694 mQsbDivider.setVisibility(View.INVISIBLE);
2695 mDockDivider.setVisibility(View.INVISIBLE);
2696 }
2697 }
2698
2699 void showDockDivider(boolean animated) {
2700 if (mQsbDivider != null && mDockDivider != null) {
2701 mQsbDivider.setVisibility(View.VISIBLE);
2702 mDockDivider.setVisibility(View.VISIBLE);
2703 if (mDividerAnimator != null) {
2704 mDividerAnimator.cancel();
2705 mQsbDivider.setAlpha(1f);
2706 mDockDivider.setAlpha(1f);
2707 mDividerAnimator = null;
2708 }
2709 if (animated) {
2710 mDividerAnimator = new AnimatorSet();
2711 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2712 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2713 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2714 mDividerAnimator.start();
2715 }
2716 }
2717 }
2718
Michael Jurkab3e22d92011-10-31 15:58:33 -07002719 void lockAllApps() {
2720 // TODO
2721 }
2722
2723 void unlockAllApps() {
2724 // TODO
2725 }
2726
Adam Cohenfc53cd22011-07-20 15:45:11 -07002727 public boolean isAllAppsCustomizeOpen() {
2728 return mState == State.APPS_CUSTOMIZE;
2729 }
2730
Winson Chungf0ea4d32011-06-06 14:27:16 -07002731 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002732 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002733 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002734 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002735 if (!LauncherApplication.isScreenLarge()) {
2736 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002737 if (mHotseat.getAlpha() != 1f) {
2738 int duration = mSearchDropTargetBar.getTransitionInDuration();
2739 mHotseat.animate().alpha(1f).setDuration(duration);
2740 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002741 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002742 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002743 }
2744 }
2745 }
2746
2747 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002748 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002749 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002750 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 if (!LauncherApplication.isScreenLarge()) {
2752 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002753 if (mHotseat.getAlpha() != 0f) {
2754 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2755 mHotseat.animate().alpha(0f).setDuration(duration);
2756 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002757 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002758 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002759 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002760 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002761 }
2762
Patrick Dubroy5f445422011-02-18 14:35:21 -08002763 /**
2764 * Add an item from all apps or customize onto the given workspace screen.
2765 * If layout is null, add to the current screen.
2766 */
2767 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002768 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002769 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002770 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002771 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002772
Winson Chungdff8ebb2011-09-08 17:25:31 -07002773 /** Maps the current orientation to an index for referencing orientation correct global icons */
2774 private int getCurrentOrientationIndexForGlobalIcons() {
2775 // default - 0, landscape - 1
2776 switch (getResources().getConfiguration().orientation) {
2777 case Configuration.ORIENTATION_LANDSCAPE:
2778 return 1;
2779 default:
2780 return 0;
2781 }
2782 }
2783
Michael Jurkaae65ee32012-04-30 11:45:54 -07002784 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002785 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002786 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002787 // Look for the toolbar icon specified in the activity meta-data
2788 Bundle metaData = packageManager.getActivityInfo(
2789 activityName, PackageManager.GET_META_DATA).metaData;
2790 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002791 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002792 if (iconResId != 0) {
2793 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002794 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002795 }
2796 }
2797 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002798 // This can happen if the activity defines an invalid drawable
2799 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2800 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002801 } catch (Resources.NotFoundException nfe) {
2802 // This can happen if the activity defines an invalid drawable
2803 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2804 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002805 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806 return null;
2807 }
2808
2809 // if successful in getting icon, return it; otherwise, set button to use default drawable
2810 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002811 int buttonId, ComponentName activityName, int fallbackDrawableId,
2812 String toolbarResourceName) {
2813 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002814 Resources r = getResources();
2815 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2816 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002817
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002818 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002819 // If we were unable to find the icon via the meta-data, use a generic one
2820 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002821 toolbarIcon = r.getDrawable(fallbackDrawableId);
2822 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002823 if (button != null) {
2824 button.setCompoundDrawables(toolbarIcon, null, null, null);
2825 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002826 return null;
2827 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002828 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002829 if (button != null) {
2830 button.setCompoundDrawables(toolbarIcon, null, null, null);
2831 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002832 return toolbarIcon.getConstantState();
2833 }
2834 }
2835
2836 // if successful in getting icon, return it; otherwise, set button to use default drawable
2837 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002838 int buttonId, ComponentName activityName, int fallbackDrawableId,
2839 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002840 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002841 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002842
Michael Jurka19e0fc52011-07-22 18:00:21 -07002843 if (button != null) {
2844 // If we were unable to find the icon via the meta-data, use a
2845 // generic one
2846 if (toolbarIcon == null) {
2847 button.setImageResource(fallbackDrawableId);
2848 } else {
2849 button.setImageDrawable(toolbarIcon);
2850 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002851 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002852
2853 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2854
Michael Jurka0423dcf2010-10-05 14:56:18 -07002855 }
2856
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002857 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2858 TextView button = (TextView) findViewById(buttonId);
2859 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2860 }
2861
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002862 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002863 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002864 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002865 }
2866
Winson Chungbb185bd2011-11-21 12:31:42 -08002867 private void invalidatePressedFocusedStates(View container, View button) {
2868 if (container instanceof HolographicLinearLayout) {
2869 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2870 layout.invalidatePressedFocusedStates();
2871 } else if (button instanceof HolographicImageView) {
2872 HolographicImageView view = (HolographicImageView) button;
2873 view.invalidatePressedFocusedStates();
2874 }
2875 }
2876
Winson Chungc51db6a2011-10-05 11:44:49 -07002877 private boolean updateGlobalSearchIcon() {
2878 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002879 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2880 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002881 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002882 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002883 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002884
Winson Chung1cad91e2011-05-25 17:41:01 -07002885 final SearchManager searchManager =
2886 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2887 ComponentName activityName = searchManager.getGlobalSearchActivity();
2888 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002889 int coi = getCurrentOrientationIndexForGlobalIcons();
2890 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002891 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2892 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2893 if (sGlobalSearchIcon[coi] == null) {
2894 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2895 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2896 TOOLBAR_ICON_METADATA_NAME);
2897 }
2898
Winson Chung649723c2011-07-06 20:41:23 -07002899 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002900 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2901 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002902 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002903 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002904 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002905 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002906 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002907 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2908 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2909 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002910 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002911 if (voiceButtonProxy != null) {
2912 voiceButtonProxy.setVisibility(View.GONE);
2913 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002914 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002915 }
2916 }
2917
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002918 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002919 final View searchButtonContainer = findViewById(R.id.search_button_container);
2920 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002921 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002922 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002923 }
2924
Winson Chungc51db6a2011-10-05 11:44:49 -07002925 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002926 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002927 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002928 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002929 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002930
Winson Chungc51db6a2011-10-05 11:44:49 -07002931 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002932 final SearchManager searchManager =
2933 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2934 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2935
2936 ComponentName activityName = null;
2937 if (globalSearchActivity != null) {
2938 // Check if the global search activity handles voice search
2939 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2940 intent.setPackage(globalSearchActivity.getPackageName());
2941 activityName = intent.resolveActivity(getPackageManager());
2942 }
2943
2944 if (activityName == null) {
2945 // Fallback: check if an activity other than the global search activity
2946 // resolves this
2947 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2948 activityName = intent.resolveActivity(getPackageManager());
2949 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002950 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002951 int coi = getCurrentOrientationIndexForGlobalIcons();
2952 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002953 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2954 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2955 if (sVoiceSearchIcon[coi] == null) {
2956 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2957 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2958 TOOLBAR_ICON_METADATA_NAME);
2959 }
Winson Chung649723c2011-07-06 20:41:23 -07002960 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002961 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002962 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002963 if (voiceButtonProxy != null) {
2964 voiceButtonProxy.setVisibility(View.VISIBLE);
2965 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002966 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002967 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002968 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002969 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002970 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002971 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002972 if (voiceButtonProxy != null) {
2973 voiceButtonProxy.setVisibility(View.GONE);
2974 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002975 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002976 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002977 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002978
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002979 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002980 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2981 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002982 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002983 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002984 }
2985
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002986 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002987 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002988 */
2989 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002990 final View marketButton = findViewById(R.id.market_button);
2991 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2992 // Find the app market activity by resolving an intent.
2993 // (If multiple app markets are installed, it will return the ResolverActivity.)
2994 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002995 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002996 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002997 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002998 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002999 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3000 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003001 marketButton.setVisibility(View.VISIBLE);
3002 } else {
3003 // We should hide and disable the view so that we don't try and restore the visibility
3004 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3005 marketButton.setVisibility(View.GONE);
3006 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003007 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003008 }
3009
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003010 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003011 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003012 }
3013
Michael Jurkad7c28052012-04-27 15:43:36 -07003014 @Override
3015 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3016 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3017 final List<CharSequence> text = event.getText();
3018 text.clear();
3019 text.add(getString(R.string.home));
3020 return result;
3021 }
3022
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003023 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003024 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003025 */
3026 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3027 @Override
3028 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003029 closeSystemDialogs();
3030 }
3031 }
3032
3033 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003034 * Receives notifications whenever the appwidgets are reset.
3035 */
3036 private class AppWidgetResetObserver extends ContentObserver {
3037 public AppWidgetResetObserver() {
3038 super(new Handler());
3039 }
3040
3041 @Override
3042 public void onChange(boolean selfChange) {
3043 onAppWidgetReset();
3044 }
3045 }
3046
3047 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003048 * If the activity is currently paused, signal that we need to re-run the loader
3049 * in onResume.
3050 *
3051 * This needs to be called from incoming places where resources might have been loaded
3052 * while we are paused. That is becaues the Configuration might be wrong
3053 * when we're not running, and if it comes back to what it was when we
3054 * were paused, we are not restarted.
3055 *
3056 * Implementation of the method from LauncherModel.Callbacks.
3057 *
3058 * @return true if we are currently paused. The caller might be able to
3059 * skip some work in that case since we will come back again.
3060 */
3061 public boolean setLoadOnResume() {
3062 if (mPaused) {
3063 Log.i(TAG, "setLoadOnResume");
3064 mOnResumeNeedsLoad = true;
3065 return true;
3066 } else {
3067 return false;
3068 }
3069 }
3070
3071 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003075 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003076 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003077 } else {
3078 return SCREEN_COUNT / 2;
3079 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003081
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 /**
3083 * Refreshes the shortcuts shown on the workspace.
3084 *
3085 * Implementation of the method from LauncherModel.Callbacks.
3086 */
3087 public void startBinding() {
3088 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003089
Winson Chungf0c6ae02012-03-21 16:10:31 -07003090 mNewShortcutAnimatePage = -1;
3091 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003092 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 int count = workspace.getChildCount();
3094 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003095 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003096 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3097 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003099 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003100 if (mHotseat != null) {
3101 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 }
3103 }
3104
3105 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 * Bind the items start-end from the list.
3107 *
3108 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003111 setLoadOnResume();
3112
Winson Chungf0c6ae02012-03-21 16:10:31 -07003113 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3114 Set<String> newApps = new HashSet<String>();
3115 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3116
3117 Workspace workspace = mWorkspace;
3118 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003120
3121 // Short circuit if we are loading dock items for a configuration which has no dock
3122 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3123 mHotseat == null) {
3124 continue;
3125 }
3126
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 switch (item.itemType) {
3128 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3129 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003130 ShortcutInfo info = (ShortcutInfo) item;
3131 String uri = info.intent.toUri(0).toString();
3132 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003133 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3134 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003135 if (newApps.contains(uri)) {
3136 newApps.remove(uri);
3137
3138 // Prepare the view to be animated up
3139 shortcut.setAlpha(0f);
3140 shortcut.setScaleX(0f);
3141 shortcut.setScaleY(0f);
3142 mNewShortcutAnimatePage = item.screen;
3143 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3144 mNewShortcutAnimateViews.add(shortcut);
3145 }
3146 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003148 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003150 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003151 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3153 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003157
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159 }
3160
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 /**
3162 * Implementation of the method from LauncherModel.Callbacks.
3163 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003164 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003165 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003166 sFolders.clear();
3167 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169
3170 /**
3171 * Add the views for a widget to the workspace.
3172 *
3173 * Implementation of the method from LauncherModel.Callbacks.
3174 */
3175 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003176 setLoadOnResume();
3177
Daniel Sandler843e8602010-06-07 14:59:01 -04003178 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3179 if (DEBUG_WIDGETS) {
3180 Log.d(TAG, "bindAppWidget: " + item);
3181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 final Workspace workspace = mWorkspace;
3183
3184 final int appWidgetId = item.appWidgetId;
3185 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003186 if (DEBUG_WIDGETS) {
3187 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3188 }
3189
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3191
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3193 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003194 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003195
Winson Chung3d503fb2011-07-13 17:25:49 -07003196 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003198 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3199
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 workspace.requestLayout();
3201
Daniel Sandler843e8602010-06-07 14:59:01 -04003202 if (DEBUG_WIDGETS) {
3203 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3204 + (SystemClock.uptimeMillis()-start) + "ms");
3205 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003206 }
3207
3208 /**
3209 * Callback saying that there aren't any more items to bind.
3210 *
3211 * Implementation of the method from LauncherModel.Callbacks.
3212 */
3213 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003214 setLoadOnResume();
3215
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 if (mSavedState != null) {
3217 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003218 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003220 mSavedState = null;
3221 }
3222
3223 if (mSavedInstanceState != null) {
3224 super.onRestoreInstanceState(mSavedInstanceState);
3225 mSavedInstanceState = null;
3226 }
3227
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003228 // If we received the result of any pending adds while the loader was running (e.g. the
3229 // widget configuration forced an orientation change), process them now.
3230 for (int i = 0; i < sPendingAddList.size(); i++) {
3231 completeAdd(sPendingAddList.get(i));
3232 }
3233 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003234
Winson Chungc51db6a2011-10-05 11:44:49 -07003235 // Update the market app icon as necessary (the other icons will be managed in response to
3236 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003237 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003238
Winson Chungf0c6ae02012-03-21 16:10:31 -07003239 // Animate up any icons as necessary
3240 if (mVisible || mWorkspaceLoading) {
3241 Runnable newAppsRunnable = new Runnable() {
3242 @Override
3243 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003244 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003245 }
3246 };
Winson Chunga2413752012-04-03 14:22:34 -07003247
3248 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3249 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3250 if (canRunNewAppsAnimation()) {
3251 // If the user has not interacted recently, then either snap to the new page to show
3252 // the new-apps animation or just run them if they are to appear on the current page
3253 if (willSnapPage) {
3254 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3255 } else {
3256 runNewAppsAnimation(false);
3257 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003258 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003259 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003260 // are on another page (or just normally if they are added to the current page)
3261 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003262 }
3263 }
3264
3265 mWorkspaceLoading = false;
3266 }
3267
Winson Chunga2413752012-04-03 14:22:34 -07003268 private boolean canRunNewAppsAnimation() {
3269 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3270 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3271 }
3272
Winson Chungf0c6ae02012-03-21 16:10:31 -07003273 /**
3274 * Runs a new animation that scales up icons that were added while Launcher was in the
3275 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003276 *
3277 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003278 */
Winson Chunga2413752012-04-03 14:22:34 -07003279 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003280 AnimatorSet anim = new AnimatorSet();
3281 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003282
3283 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003284 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3285 @Override
3286 public int compare(View a, View b) {
3287 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3288 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3289 int cellCountX = LauncherModel.getCellCountX();
3290 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3291 }
3292 });
Winson Chunga2413752012-04-03 14:22:34 -07003293
3294 // Animate each of the views in place (or show them immediately if requested)
3295 if (immediate) {
3296 for (View v : mNewShortcutAnimateViews) {
3297 v.setAlpha(1f);
3298 v.setScaleX(1f);
3299 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003300 }
Winson Chunga2413752012-04-03 14:22:34 -07003301 } else {
3302 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3303 View v = mNewShortcutAnimateViews.get(i);
3304 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3305 PropertyValuesHolder.ofFloat("alpha", 1f),
3306 PropertyValuesHolder.ofFloat("scaleX", 1f),
3307 PropertyValuesHolder.ofFloat("scaleY", 1f));
3308 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3309 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3310 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3311 bounceAnims.add(bounceAnim);
3312 }
3313 anim.playTogether(bounceAnims);
3314 anim.addListener(new AnimatorListenerAdapter() {
3315 @Override
3316 public void onAnimationEnd(Animator animation) {
3317 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3318 }
3319 });
3320 anim.start();
3321 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003322
3323 // Clean up
3324 mNewShortcutAnimatePage = -1;
3325 mNewShortcutAnimateViews.clear();
3326 new Thread("clearNewAppsThread") {
3327 public void run() {
3328 mSharedPrefs.edit()
3329 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3330 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3331 .commit();
3332 }
3333 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003334 }
3335
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003336 @Override
3337 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003338 boolean searchVisible = updateGlobalSearchIcon();
3339 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3340 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003341 }
3342
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003343 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003344 * Add the icons for all apps.
3345 *
3346 * Implementation of the method from LauncherModel.Callbacks.
3347 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003348 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3349 // Remove the progress bar entirely; we could also make it GONE
3350 // but better to remove it since we know it's not going to be used
3351 View progressBar = mAppsCustomizeTabHost.
3352 findViewById(R.id.apps_customize_progress_bar);
3353 if (progressBar != null) {
3354 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003355 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003356 // We just post the call to setApps so the user sees the progress bar
3357 // disappear-- otherwise, it just looks like the progress bar froze
3358 // which doesn't look great
3359 mAppsCustomizeTabHost.post(new Runnable() {
3360 public void run() {
3361 if (mAppsCustomizeContent != null) {
3362 mAppsCustomizeContent.setApps(apps);
3363 }
3364 }
3365 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 }
3367
3368 /**
3369 * A package was installed.
3370 *
3371 * Implementation of the method from LauncherModel.Callbacks.
3372 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003373 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003374 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003375
Winson Chung785d2eb2011-04-14 16:08:02 -07003376 if (mAppsCustomizeContent != null) {
3377 mAppsCustomizeContent.addApps(apps);
3378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003379 }
3380
3381 /**
3382 * A package was updated.
3383 *
3384 * Implementation of the method from LauncherModel.Callbacks.
3385 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003386 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003387 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003388 if (mWorkspace != null) {
3389 mWorkspace.updateShortcuts(apps);
3390 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003391
Winson Chung785d2eb2011-04-14 16:08:02 -07003392 if (mAppsCustomizeContent != null) {
3393 mAppsCustomizeContent.updateApps(apps);
3394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 }
3396
3397 /**
3398 * A package was uninstalled.
3399 *
3400 * Implementation of the method from LauncherModel.Callbacks.
3401 */
Joe Onorato36115782010-06-17 13:28:48 -04003402 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003403 if (permanent) {
3404 mWorkspace.removeItems(apps);
3405 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003406
Winson Chung785d2eb2011-04-14 16:08:02 -07003407 if (mAppsCustomizeContent != null) {
3408 mAppsCustomizeContent.removeApps(apps);
3409 }
Winson Chunga1820962011-10-03 16:31:06 -07003410
3411 // Notify the drag controller
3412 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 }
Joe Onoratobe386092009-11-17 17:32:16 -08003414
3415 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003416 * A number of packages were updated.
3417 */
3418 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003419 if (mAppsCustomizeContent != null) {
3420 mAppsCustomizeContent.onPackagesUpdated();
3421 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003422 }
3423
Winson Chung400438b2011-01-16 17:53:48 -08003424 private int mapConfigurationOriActivityInfoOri(int configOri) {
3425 final Display d = getWindowManager().getDefaultDisplay();
3426 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3427 switch (d.getRotation()) {
3428 case Surface.ROTATION_0:
3429 case Surface.ROTATION_180:
3430 // We are currently in the same basic orientation as the natural orientation
3431 naturalOri = configOri;
3432 break;
3433 case Surface.ROTATION_90:
3434 case Surface.ROTATION_270:
3435 // We are currently in the other basic orientation to the natural orientation
3436 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3437 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3438 break;
3439 }
3440
3441 int[] oriMap = {
3442 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3443 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3444 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3445 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3446 };
3447 // Since the map starts at portrait, we need to offset if this device's natural orientation
3448 // is landscape.
3449 int indexOffset = 0;
3450 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3451 indexOffset = 1;
3452 }
3453 return oriMap[(d.getRotation() + indexOffset) % 4];
3454 }
Adam Cohen446e9402011-09-15 18:21:21 -07003455
Winson Chung4b919f82012-05-01 10:44:08 -07003456 public boolean isRotationEnabled() {
3457 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3458 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3459 boolean enableRotation = forceEnableRotation ||
3460 getResources().getBoolean(R.bool.allow_rotation);
3461 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003462 }
Winson Chung4b919f82012-05-01 10:44:08 -07003463 public void lockScreenOrientation() {
3464 if (isRotationEnabled()) {
3465 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3466 .getConfiguration().orientation));
3467 }
3468 }
3469 public void unlockScreenOrientation(boolean immediate) {
3470 if (isRotationEnabled()) {
3471 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003472 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003473 } else {
3474 mHandler.postDelayed(new Runnable() {
3475 public void run() {
3476 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3477 }
3478 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003479 }
Winson Chung4b919f82012-05-01 10:44:08 -07003480 }
Winson Chung400438b2011-01-16 17:53:48 -08003481 }
3482
Winson Chung82f55532011-08-09 14:14:23 -07003483 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003484 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003485 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003486 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003487
Winson Chung7d7541e2011-09-16 20:14:36 -07003488 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003489 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003490 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3491 Cling cling = (Cling) findViewById(clingId);
3492 if (cling != null) {
3493 cling.init(this, positionData);
3494 cling.setVisibility(View.VISIBLE);
3495 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -07003496 cling.requestAccessibilityFocus();
Winson Chung7d7541e2011-09-16 20:14:36 -07003497 if (animate) {
3498 cling.buildLayer();
3499 cling.setAlpha(0f);
3500 cling.animate()
3501 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003502 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003503 .setDuration(SHOW_CLING_DURATION)
3504 .setStartDelay(delay)
3505 .start();
3506 } else {
3507 cling.setAlpha(1f);
3508 }
3509 }
3510 return cling;
3511 }
3512 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003513 if (cling != null) {
3514 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003515 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003516 anim.addListener(new AnimatorListenerAdapter() {
3517 public void onAnimationEnd(Animator animation) {
3518 cling.setVisibility(View.GONE);
3519 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003520 // We should update the shared preferences on a background thread
3521 new Thread("dismissClingThread") {
3522 public void run() {
3523 SharedPreferences.Editor editor = mSharedPrefs.edit();
3524 editor.putBoolean(flag, true);
3525 editor.commit();
3526 }
3527 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003528 };
3529 });
3530 anim.start();
3531 }
3532 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003533 private void removeCling(int id) {
3534 final View cling = findViewById(id);
3535 if (cling != null) {
3536 final ViewGroup parent = (ViewGroup) cling.getParent();
3537 parent.post(new Runnable() {
3538 @Override
3539 public void run() {
3540 parent.removeView(cling);
3541 }
3542 });
3543 }
3544 }
Michael Jurka974c3862012-05-22 22:00:31 -07003545
3546 private boolean skipCustomClingIfNoAccounts() {
3547 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3548 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3549 if (customCling) {
3550 AccountManager am = AccountManager.get(this);
3551 Account[] accounts = am.getAccountsByType("com.google");
3552 return accounts.length == 0;
3553 }
3554 return false;
3555 }
3556
Winson Chung7d7541e2011-09-16 20:14:36 -07003557 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003558 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003559 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003560 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3561 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003562 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003563 } else {
3564 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003565 }
3566 }
3567 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003568 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003569 if (isClingsEnabled() &&
3570 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003571 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003572 } else {
3573 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003574 }
3575 }
3576 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003577 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003578 if (isClingsEnabled() &&
3579 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3580 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003581 } else {
3582 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003583 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003584 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003585 }
3586 public boolean isFolderClingVisible() {
3587 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003588 if (cling != null) {
3589 return cling.getVisibility() == View.VISIBLE;
3590 }
3591 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003592 }
Winson Chung82f55532011-08-09 14:14:23 -07003593 public void dismissWorkspaceCling(View v) {
3594 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003595 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003596 }
3597 public void dismissAllAppsCling(View v) {
3598 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003599 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3600 }
3601 public void dismissFolderCling(View v) {
3602 Cling cling = (Cling) findViewById(R.id.folder_cling);
3603 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003604 }
3605
Winson Chung80baf5a2010-08-09 16:03:15 -07003606 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003607 * Prints out out state for debugging.
3608 */
3609 public void dumpState() {
3610 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003611 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003612 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3613 Log.d(TAG, "mRestoring=" + mRestoring);
3614 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3615 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003616 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003617 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003618
Winson Chung785d2eb2011-04-14 16:08:02 -07003619 if (mAppsCustomizeContent != null) {
3620 mAppsCustomizeContent.dumpState();
3621 }
Joe Onoratobe386092009-11-17 17:32:16 -08003622 Log.d(TAG, "END launcher2 dump state");
3623 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003624
3625 @Override
3626 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3627 super.dump(prefix, fd, writer, args);
3628 writer.println(" ");
3629 writer.println("Debug logs: ");
3630 for (int i = 0; i < sDumpLogs.size(); i++) {
3631 writer.println(" " + sDumpLogs.get(i));
3632 }
3633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003634}
Michael Jurka2763be32011-02-24 11:19:57 -08003635
Michael Jurkaabded662011-03-04 12:06:57 -08003636interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003637 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003638 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003639 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003640 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003641 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003642}