blob: 9fbf50ef0531ba2774ea1184a9095df62579a402 [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) {
353 mAppsCustomizeContent.onPackagesUpdated();
354 }
Winson Chunga12a2502010-12-20 14:41:35 -0800355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 if (PROFILE_STARTUP) {
357 android.os.Debug.stopMethodTracing();
358 }
359
360 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700361 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 }
363
Michael Jurkac57b7a82011-08-09 22:02:20 -0700364 if (!mModel.isAllAppsLoaded()) {
365 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
366 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
367 }
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 // For handling default keys
370 mDefaultKeySsb = new SpannableStringBuilder();
371 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800372
373 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
374 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800375
Winson Chungc51db6a2011-10-05 11:44:49 -0700376 boolean searchVisible = false;
377 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800378 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 int coi = getCurrentOrientationIndexForGlobalIcons();
380 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
381 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700382 updateAppMarketIcon();
383 searchVisible = updateGlobalSearchIcon();
384 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700385 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700386 if (sGlobalSearchIcon[coi] != null) {
387 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700388 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700389 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700390 if (sVoiceSearchIcon[coi] != null) {
391 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700392 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700393 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700394 if (sAppMarketIcon[coi] != null) {
395 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800396 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700397 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700398
399 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Winson Chung4b919f82012-05-01 10:44:08 -0700400 unlockScreenOrientation(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
Romain Guycbb89e42009-06-08 15:52:54 -0700402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404 if (sLocaleConfiguration == null) {
405 new AsyncTask<Void, Void, LocaleConfiguration>() {
406 @Override
407 protected LocaleConfiguration doInBackground(Void... unused) {
408 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
409 readConfiguration(Launcher.this, localeConfiguration);
410 return localeConfiguration;
411 }
412
413 @Override
414 protected void onPostExecute(LocaleConfiguration result) {
415 sLocaleConfiguration = result;
416 checkForLocaleChange(); // recursive, but now with a locale configuration
417 }
418 }.execute();
419 return;
420 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final Configuration configuration = getResources().getConfiguration();
423
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 final String locale = configuration.locale.toString();
426
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 final int mcc = configuration.mcc;
429
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 final int mnc = configuration.mnc;
432
Romain Guy84f296c2009-11-04 15:00:44 -0800433 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434
Romain Guy84f296c2009-11-04 15:00:44 -0800435 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 sLocaleConfiguration.locale = locale;
437 sLocaleConfiguration.mcc = mcc;
438 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700439
Joe Onorato0589f0f2010-02-08 13:44:00 -0800440 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700441
442 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
443 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700444 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700445 public void run() {
446 writeConfiguration(Launcher.this, localeConfiguration);
447 }
448 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700449 }
450 }
451
452 private static class LocaleConfiguration {
453 public String locale;
454 public int mcc = -1;
455 public int mnc = -1;
456 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700457
Romain Guy98d01652009-06-30 16:21:04 -0700458 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
459 DataInputStream in = null;
460 try {
461 in = new DataInputStream(context.openFileInput(PREFERENCES));
462 configuration.locale = in.readUTF();
463 configuration.mcc = in.readInt();
464 configuration.mnc = in.readInt();
465 } catch (FileNotFoundException e) {
466 // Ignore
467 } catch (IOException e) {
468 // Ignore
469 } finally {
470 if (in != null) {
471 try {
472 in.close();
473 } catch (IOException e) {
474 // Ignore
475 }
476 }
477 }
478 }
479
480 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
481 DataOutputStream out = null;
482 try {
483 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
484 out.writeUTF(configuration.locale);
485 out.writeInt(configuration.mcc);
486 out.writeInt(configuration.mnc);
487 out.flush();
488 } catch (FileNotFoundException e) {
489 // Ignore
490 } catch (IOException e) {
491 //noinspection ResultOfMethodCallIgnored
492 context.getFileStreamPath(PREFERENCES).delete();
493 } finally {
494 if (out != null) {
495 try {
496 out.close();
497 } catch (IOException e) {
498 // Ignore
499 }
500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 }
502 }
503
Adam Cohen716b51e2011-06-30 12:09:54 -0700504 public DragLayer getDragLayer() {
505 return mDragLayer;
506 }
507
Winson Chung36a62fe2012-05-06 18:04:42 -0700508 boolean isDraggingEnabled() {
509 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
510 // that is subsequently removed from the workspace in startBinding().
511 return !mModel.isLoadingWorkspace();
512 }
513
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 static int getScreen() {
515 synchronized (sLock) {
516 return sScreen;
517 }
518 }
519
520 static void setScreen(int screen) {
521 synchronized (sLock) {
522 sScreen = screen;
523 }
524 }
525
Winson Chung557d6ed2011-07-08 15:34:52 -0700526 /**
527 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
528 * a configuration step, this allows the proper animations to run after other transitions.
529 */
530 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800532 switch (args.requestCode) {
533 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700534 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
535 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800536 break;
537 case REQUEST_PICK_SHORTCUT:
538 processShortcut(args.intent);
539 break;
540 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700541 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
542 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700543 result = true;
544 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800545 case REQUEST_CREATE_APPWIDGET:
546 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800547 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700548 result = true;
549 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800550 case REQUEST_PICK_WALLPAPER:
551 // We just wanted the activity result here so we can clear mWaitingForResult
552 break;
553 }
Michael Jurka27614d22012-04-02 06:40:22 -0700554 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
555 // if you turned the screen off and then back while in All Apps, Launcher would not
556 // return to the workspace. Clearing mAddInfo.container here fixes this issue
557 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700558 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800559 }
560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700562 protected void onActivityResult(
563 final int requestCode, final int resultCode, final Intent data) {
564 if (requestCode == REQUEST_BIND_APPWIDGET) {
565 int appWidgetId = data != null ?
566 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
567 if (resultCode == RESULT_CANCELED) {
568 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
569 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700570 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700571 }
572 return;
573 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700574 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800575 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
576 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700577 mWaitingForResult = false;
578
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 // We have special handling for widgets
580 if (isWidgetDrop) {
581 int appWidgetId = data != null ?
582 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700583 if (appWidgetId < 0) {
584 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
585 "widget configuration activity.");
586 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
587 } else {
588 completeTwoStageWidgetDrop(resultCode, appWidgetId);
589 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800590 return;
591 }
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 // The pattern used here is that a user PICKs a specific application,
594 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
597 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700598 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 final PendingAddArguments args = new PendingAddArguments();
600 args.requestCode = requestCode;
601 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700602 args.container = mPendingAddInfo.container;
603 args.screen = mPendingAddInfo.screen;
604 args.cellX = mPendingAddInfo.cellX;
605 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 if (isWorkspaceLocked()) {
607 sPendingAddList.add(args);
608 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700609 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800612 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700613 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800614 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
615 null);
616 }
617
618 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700620 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800621 Runnable onCompleteRunnable = null;
622 int animationType = 0;
623
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700624 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800625 if (resultCode == RESULT_OK) {
626 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
627 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700628 mPendingAddWidgetInfo);
629 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800630 onCompleteRunnable = new Runnable() {
631 @Override
632 public void run() {
633 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
634 mPendingAddInfo.screen, layout, null);
635 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
636 null);
637 }
638 };
639 } else if (resultCode == RESULT_CANCELED) {
640 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
641 onCompleteRunnable = new Runnable() {
642 @Override
643 public void run() {
644 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
645 null);
646 }
647 };
648 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700649 if (mDragLayer.getAnimatedView() != null) {
650 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
651 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
652 animationType, boundWidget, true);
653 } else {
654 // The animated view may be null in the case of a rotation during widget configuration
655 onCompleteRunnable.run();
656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
658
659 @Override
660 protected void onResume() {
661 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700662
Winson Chungde0fb8f2012-05-08 14:37:08 -0700663 // Process any items that were added while Launcher was away
664 InstallShortcutReceiver.flushInstallQueue(this);
665
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800666 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700667 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700669 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700671 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Winson Chunge4e50662012-01-23 14:45:13 -0800673
674 // Reset the pressed state of icons that were locked in the press state while activities
675 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800676 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800677 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800678 mWaitingForResume.setStayPressed(false);
679 }
Winson Chunge4e50662012-01-23 14:45:13 -0800680 if (mAppsCustomizeContent != null) {
681 // Resets the previous all apps icon press state
682 mAppsCustomizeContent.resetDrawableState();
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685
686 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700688 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
689 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
690 // when Launcher resumes and we are still in AllApps.
691 updateWallpaperVisibility(true);
692
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700693 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700694 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800695 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700696 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 }
Romain Guycbb89e42009-06-08 15:52:54 -0700698
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700699 @Override
700 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 // Flag the loader to stop early before switching
702 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700703 if (mAppsCustomizeContent != null) {
704 mAppsCustomizeContent.surrender();
705 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800706 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700707 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800709 // We can't hide the IME if it was forced open. So don't bother
710 /*
711 @Override
712 public void onWindowFocusChanged(boolean hasFocus) {
713 super.onWindowFocusChanged(hasFocus);
714
715 if (hasFocus) {
716 final InputMethodManager inputManager = (InputMethodManager)
717 getSystemService(Context.INPUT_METHOD_SERVICE);
718 WindowManager.LayoutParams lp = getWindow().getAttributes();
719 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
720 android.os.Handler()) {
721 protected void onReceiveResult(int resultCode, Bundle resultData) {
722 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
723 }
724 });
725 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
726 }
727 }
728 */
729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 private boolean acceptFilter() {
731 final InputMethodManager inputManager = (InputMethodManager)
732 getSystemService(Context.INPUT_METHOD_SERVICE);
733 return !inputManager.isFullscreenMode();
734 }
735
736 @Override
737 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700738 final int uniChar = event.getUnicodeChar();
739 final boolean handled = super.onKeyDown(keyCode, event);
740 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
741 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
743 keyCode, event);
744 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700745 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700746 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700747 // showSearchDialog()
748 // If there are multiple keystrokes before the search dialog takes focus,
749 // onSearchRequested() will be called for every keystroke,
750 // but it is idempotent, so it's fine.
751 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753 }
754
Joe Onorato8a9625e2010-01-28 15:55:35 -0800755 // Eat the long press event so the keyboard doesn't come up.
756 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
757 return true;
758 }
759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 return handled;
761 }
762
Karl Rosaen138a0412009-04-23 19:00:21 -0700763 private String getTypedText() {
764 return mDefaultKeySsb.toString();
765 }
766
767 private void clearTypedText() {
768 mDefaultKeySsb.clear();
769 mDefaultKeySsb.clearSpans();
770 Selection.setSelection(mDefaultKeySsb, 0);
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700774 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
775 * State
776 */
777 private static State intToState(int stateOrdinal) {
778 State state = State.WORKSPACE;
779 final State[] stateValues = State.values();
780 for (int i = 0; i < stateValues.length; i++) {
781 if (stateValues[i].ordinal() == stateOrdinal) {
782 state = stateValues[i];
783 break;
784 }
785 }
786 return state;
787 }
788
789 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 * Restores the previous state, if it exists.
791 *
792 * @param savedState The previous state.
793 */
794 private void restoreState(Bundle savedState) {
795 if (savedState == null) {
796 return;
797 }
798
Michael Jurka883f55b2010-10-21 15:47:14 -0700799 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700800 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800801 showAllApps(false);
802 }
803
Winson Chungf0c6ae02012-03-21 16:10:31 -0700804 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700806 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808
Winson Chung3d503fb2011-07-13 17:25:49 -0700809 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
810 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700811
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
813 mPendingAddInfo.container = pendingAddContainer;
814 mPendingAddInfo.screen = pendingAddScreen;
815 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
816 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700817 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
818 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
819 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700820 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 mRestoring = true;
822 }
823
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700824
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
826 if (renameFolder) {
827 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700828 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 mRestoring = true;
830 }
Winson Chunga12a2502010-12-20 14:41:35 -0800831
Winson Chung785d2eb2011-04-14 16:08:02 -0700832
833 // Restore the AppsCustomize tab
834 if (mAppsCustomizeTabHost != null) {
835 String curTab = savedState.getString("apps_customize_currentTab");
836 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700837 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700838 mAppsCustomizeContent.setContentType(
839 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
840 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700841 mAppsCustomizeContent.loadAssociatedPages(
842 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700843 }
844
Winson Chung5afbf7b2011-07-25 11:53:08 -0700845 int currentIndex = savedState.getInt("apps_customize_currentIndex");
846 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
849
850 /**
851 * Finds all the views we need and configure them properly.
852 */
853 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700854 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400855
Winson Chung4c98d922011-05-31 16:50:48 -0700856 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
857 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800858 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
859 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860
Winson Chung4c98d922011-05-31 16:50:48 -0700861 // Setup the drag layer
862 mDragLayer.setup(this, dragController);
863
Winson Chung3d503fb2011-07-13 17:25:49 -0700864 // Setup the hotseat
865 mHotseat = (Hotseat) findViewById(R.id.hotseat);
866 if (mHotseat != null) {
867 mHotseat.setup(this);
868 }
869
Winson Chung4c98d922011-05-31 16:50:48 -0700870 // Setup the workspace
871 mWorkspace.setHapticFeedbackEnabled(false);
872 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700873 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700874 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700875
Winson Chungf0ea4d32011-06-06 14:27:16 -0700876 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700877 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700878
Winson Chungf0ea4d32011-06-06 14:27:16 -0700879 // Setup AppsCustomize
880 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
881 findViewById(R.id.apps_customize_pane);
882 mAppsCustomizeContent = (AppsCustomizePagedView)
883 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700884 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700885 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400886
Michael Jurka5130e402011-10-13 04:55:35 -0700887 // Get the all apps button
888 mAllAppsButton = findViewById(R.id.all_apps_button);
889 if (mAllAppsButton != null) {
890 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
891 @Override
892 public boolean onTouch(View v, MotionEvent event) {
893 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
894 onTouchDownAllAppsButton(v);
895 }
896 return false;
897 }
898 });
899 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700900 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700901 dragController.setDragScoller(mWorkspace);
902 dragController.setScrollView(mDragLayer);
903 dragController.setMoveTarget(mWorkspace);
904 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700905 if (mSearchDropTargetBar != null) {
906 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
909
910 /**
911 * Creates a view representing a shortcut.
912 *
913 * @param info The data structure describing the shortcut.
914 *
915 * @return A View inflated from R.layout.application.
916 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800917 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700919 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
921
922 /**
923 * Creates a view representing a shortcut inflated from the specified resource.
924 *
925 * @param layoutResId The id of the XML layout used to create the shortcut.
926 * @param parent The group the shortcut belongs to.
927 * @param info The data structure describing the shortcut.
928 *
929 * @return A View inflated from layoutResId.
930 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800931 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800932 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
933 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 return favorite;
936 }
937
938 /**
939 * Add an application shortcut to the workspace.
940 *
941 * @param data The intent describing the application.
942 * @param cellInfo The position on screen where to create the shortcut.
943 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700945 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700947
Adam Cohenfbba09b2011-07-18 21:43:05 -0700948 // First we check if we already know the exact location where we want to add this item.
949 if (cellX >= 0 && cellY >= 0) {
950 cellXY[0] = cellX;
951 cellXY[1] = cellY;
952 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700953 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700954 return;
955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800957 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800958
Romain Guy73b979d2009-06-09 12:57:21 -0700959 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800960 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800962 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700964 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800965 } else {
966 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968 }
Romain Guycbb89e42009-06-08 15:52:54 -0700969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 /**
971 * Add a shortcut to the workspace.
972 *
973 * @param data The intent describing the shortcut.
974 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700976 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
977 int cellY) {
978 int[] cellXY = mTmpAddItemCellCoordinates;
979 int[] touchXY = mPendingAddInfo.dropPos;
980 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700981
Michael Jurkad3ef3062010-11-23 16:23:58 -0800982 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700983
Adam Cohen558baaf2011-08-15 15:22:57 -0700984 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800985 if (info == null) {
986 return;
987 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700988 final View view = createShortcut(info);
989
Adam Cohenfbba09b2011-07-18 21:43:05 -0700990 // First we check if we already know the exact location where we want to add this item.
991 if (cellX >= 0 && cellY >= 0) {
992 cellXY[0] = cellX;
993 cellXY[1] = cellY;
994 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700995
996 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800997 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700998 true, null,null)) {
999 return;
1000 }
1001 DragObject dragObject = new DragObject();
1002 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001003 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1004 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001005 return;
1006 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001007 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001008 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001009 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001010 foundCellSpan = (result != null);
1011 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001012 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001013 }
1014
1015 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001016 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 return;
1018 }
1019
Adam Cohen558baaf2011-08-15 15:22:57 -07001020 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021
1022 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001023 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1024 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026 }
1027
Adam Cohen2f093b62012-04-30 18:59:53 -07001028 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1029 int minHeight) {
1030 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001031 // We want to account for the extra amount of padding that we are adding to the widget
1032 // to ensure that it gets the full amount of space that it has requested
1033 int requiredWidth = minWidth + padding.left + padding.right;
1034 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001035 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001036 }
1037
Adam Cohen2f093b62012-04-30 18:59:53 -07001038 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1039 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001040 }
1041
Adam Cohen2f093b62012-04-30 18:59:53 -07001042 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1043 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001044 }
1045
Adam Cohen2f093b62012-04-30 18:59:53 -07001046 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1047 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001048 }
1049
Adam Cohen2f093b62012-04-30 18:59:53 -07001050 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1051 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001052 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 }
1054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001058 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001061 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1062 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1063 if (appWidgetInfo == null) {
1064 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1065 }
Romain Guycbb89e42009-06-08 15:52:54 -07001066
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001069
Adam Cohen2f093b62012-04-30 18:59:53 -07001070 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1071 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074 // We have saved the position to which the widget was dragged-- this really only matters
1075 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001076 int[] cellXY = mTmpAddItemCellCoordinates;
1077 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001078 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001079 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001080 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1081 cellXY[0] = mPendingAddInfo.cellX;
1082 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001083 spanXY[0] = mPendingAddInfo.spanX;
1084 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001085 foundCellSpan = true;
1086 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001088 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001089 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1090 spanXY[1], cellXY, finalSpan);
1091 spanXY[0] = finalSpan[0];
1092 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001093 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001095 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001096 }
1097
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001099 if (appWidgetId != -1) {
1100 // Deleting an app widget ID is a void call but writes to disk before returning
1101 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001102 new Thread("deleteAppWidgetId") {
1103 public void run() {
1104 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1105 }
1106 }.start();
1107 }
Winson Chung93eef082012-03-23 15:59:27 -07001108 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001109 return;
1110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001113 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1114 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001115 launcherInfo.spanX = spanXY[0];
1116 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001117 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1118 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122
1123 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001124 if (hostView == null) {
1125 // Perform actual inflation because we're live
1126 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1127 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1128 } else {
1129 // The AppWidgetHostView has already been inflated and instantiated
1130 launcherInfo.hostView = hostView;
1131 }
Romain Guycbb89e42009-06-08 15:52:54 -07001132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001134 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001135 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001136 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001138
1139 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001141 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
Romain Guycbb89e42009-06-08 15:52:54 -07001143
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1145 @Override
1146 public void onReceive(Context context, Intent intent) {
1147 final String action = intent.getAction();
1148 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1149 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001150 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001151 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001152
Winson Chungc100e8e2011-08-09 16:02:43 -07001153 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001154 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001155 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1156 mAppsCustomizeTabHost.reset();
1157 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001158 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001159 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1160 mUserPresent = true;
1161 updateRunning();
1162 }
1163 }
1164 };
1165
1166 @Override
1167 public void onAttachedToWindow() {
1168 super.onAttachedToWindow();
1169
1170 // Listen for broadcasts related to user-presence
1171 final IntentFilter filter = new IntentFilter();
1172 filter.addAction(Intent.ACTION_SCREEN_OFF);
1173 filter.addAction(Intent.ACTION_USER_PRESENT);
1174 registerReceiver(mReceiver, filter);
1175
Adam Cohend113e0c2010-11-11 10:48:05 -08001176 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001177 mVisible = true;
1178 }
1179
1180 @Override
1181 public void onDetachedFromWindow() {
1182 super.onDetachedFromWindow();
1183 mVisible = false;
1184
Adam Cohend113e0c2010-11-11 10:48:05 -08001185 if (mAttached) {
1186 unregisterReceiver(mReceiver);
1187 mAttached = false;
1188 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001189 updateRunning();
1190 }
1191
1192 public void onWindowVisibilityChanged(int visibility) {
1193 mVisible = visibility == View.VISIBLE;
1194 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001195 // The following code used to be in onResume, but it turns out onResume is called when
1196 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1197 // is a more appropriate event to handle
1198 if (mVisible) {
1199 mAppsCustomizeTabHost.onWindowVisible();
1200 if (!mWorkspaceLoading) {
1201 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001202 // We want to let Launcher draw itself at least once before we force it to build
1203 // layers on all the workspace pages, so that transitioning to Launcher from other
1204 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1205 // a true draw so we wait until the second preDraw call to be safe
1206 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001207 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001208 // We delay the layer building a bit in order to give
1209 // other message processing a time to run. In particular
1210 // this avoids a delay in hiding the IME if it was
1211 // currently shown, because doing that may involve
1212 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001213 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001214
1215 // We had to enable the wallpaper visibility when launching apps from all
1216 // apps (so that the transitions would be the same as when launching from
1217 // workspace) so take this time to see if we need to re-disable the
1218 // wallpaper visibility to ensure performance.
1219 mWorkspace.post(new Runnable() {
1220 @Override
1221 public void run() {
Winson Chung18f41f82012-05-09 13:28:10 -07001222 disableWallpaperIfInAllApps();
Winson Chung31ce0732012-05-06 13:36:43 -07001223 }
1224 });
1225
Michael Jurka6ee21d22012-02-21 18:20:27 -08001226 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001227 return true;
1228 }
1229 });
1230 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001231 // When Launcher comes back to foreground, a different Activity might be responsible for
1232 // the app market intent, so refresh the icon
1233 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001234 clearTypedText();
1235 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001236 }
1237
1238 private void sendAdvanceMessage(long delay) {
1239 mHandler.removeMessages(ADVANCE_MSG);
1240 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1241 mHandler.sendMessageDelayed(msg, delay);
1242 mAutoAdvanceSentTime = System.currentTimeMillis();
1243 }
1244
1245 private void updateRunning() {
1246 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1247 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1248 mAutoAdvanceRunning = autoAdvanceRunning;
1249 if (autoAdvanceRunning) {
1250 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1251 sendAdvanceMessage(delay);
1252 } else {
1253 if (!mWidgetsToAdvance.isEmpty()) {
1254 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1255 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1256 }
1257 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001258 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001259 }
1260 }
1261 }
1262
1263 private final Handler mHandler = new Handler() {
1264 @Override
1265 public void handleMessage(Message msg) {
1266 if (msg.what == ADVANCE_MSG) {
1267 int i = 0;
1268 for (View key: mWidgetsToAdvance.keySet()) {
1269 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1270 final int delay = mAdvanceStagger * i;
1271 if (v instanceof Advanceable) {
1272 postDelayed(new Runnable() {
1273 public void run() {
1274 ((Advanceable) v).advance();
1275 }
1276 }, delay);
1277 }
1278 i++;
1279 }
1280 sendAdvanceMessage(mAdvanceInterval);
1281 }
1282 }
1283 };
1284
1285 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001286 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001287 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1288 if (v instanceof Advanceable) {
1289 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001290 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001291 updateRunning();
1292 }
1293 }
1294
1295 void removeWidgetToAutoAdvance(View hostView) {
1296 if (mWidgetsToAdvance.containsKey(hostView)) {
1297 mWidgetsToAdvance.remove(hostView);
1298 updateRunning();
1299 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001300 }
1301
Joe Onorato9c1289c2009-08-17 11:03:03 -04001302 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001303 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304 launcherInfo.hostView = null;
1305 }
1306
Winson Chung93eef082012-03-23 15:59:27 -07001307 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1308 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1309 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 }
1311
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001312 public LauncherAppWidgetHost getAppWidgetHost() {
1313 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Winson Chunga9abd0e2010-10-27 17:18:37 -07001316 public LauncherModel getModel() {
1317 return mModel;
1318 }
1319
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001320 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001321 getWindow().closeAllPanels();
1322
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001323 // Whatever we were doing is hereby canceled.
1324 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001325 }
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 @Override
1328 protected void onNewIntent(Intent intent) {
1329 super.onNewIntent(intent);
1330
1331 // Close the menu
1332 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001333 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001334 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001335
Joe Onorato14f122b2009-11-19 14:06:36 -08001336 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1337 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001338
Adam Cohenac56cff2011-09-28 20:45:37 -07001339 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001340 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001341 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001342 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1343 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001344 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001345 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001346
1347 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001348 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001349 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001350
Joe Onorato3a8820b2009-11-10 15:06:42 -08001351 final View v = getWindow().peekDecorView();
1352 if (v != null && v.getWindowToken() != null) {
1353 InputMethodManager imm = (InputMethodManager)getSystemService(
1354 INPUT_METHOD_SERVICE);
1355 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001357
Michael Jurka35aa14d2011-07-07 17:01:08 -07001358 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001359 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001360 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363 }
1364
1365 @Override
1366 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1367 // Do not call super here
1368 mSavedInstanceState = savedInstanceState;
1369 }
1370
1371 @Override
1372 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001373 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001374 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375
Michael Jurka883f55b2010-10-21 15:47:14 -07001376 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001377 // We close any open folder since it will not be re-opened, and we need to make sure
1378 // this state is reflected.
1379 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380
Winson Chung3d503fb2011-07-13 17:25:49 -07001381 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1382 mWaitingForResult) {
1383 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1384 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1385 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1386 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001387 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1388 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1389 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391
1392 if (mFolderInfo != null && mWaitingForResult) {
1393 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1394 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1395 }
Michael Jurka946ad472010-07-09 18:05:18 -07001396
Winson Chung785d2eb2011-04-14 16:08:02 -07001397 // Save the current AppsCustomize tab
1398 if (mAppsCustomizeTabHost != null) {
1399 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1400 if (currentTabTag != null) {
1401 outState.putString("apps_customize_currentTab", currentTabTag);
1402 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001403 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1404 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407
1408 @Override
1409 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Winson Chunge7a03942011-08-05 15:05:12 -07001412 // Remove all pending runnables
1413 mHandler.removeMessages(ADVANCE_MSG);
1414 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001415 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001416
Winson Chungcd2b0142011-06-08 16:02:26 -07001417 // Stop callbacks from LauncherModel
1418 LauncherApplication app = ((LauncherApplication) getApplication());
1419 mModel.stopLoader();
1420 app.setLauncher(null);
1421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001423 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001425 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001427 mAppWidgetHost = null;
1428
1429 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430
1431 TextKeyListener.getInstance().release();
1432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433
Winson Chung3d503fb2011-07-13 17:25:49 -07001434 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001435
1436 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001437 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001438
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001439 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001440 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1441 mWorkspace.removeAllViews();
1442 mWorkspace = null;
1443 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001444
1445 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447
Adam Cohena9cf38f2011-05-02 15:36:58 -07001448 public DragController getDragController() {
1449 return mDragController;
1450 }
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 @Override
1453 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001454 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 super.startActivityForResult(intent, requestCode);
1456 }
1457
Winson Chung70d72102011-08-12 11:24:35 -07001458 /**
1459 * Indicates that we want global search for this activity by setting the globalSearch
1460 * argument for {@link #startSearch} to true.
1461 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001463 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001465
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001466 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001467
Karl Rosaen138a0412009-04-23 19:00:21 -07001468 if (initialQuery == null) {
1469 // Use any text typed in the launcher as the initial query
1470 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 if (appSearchData == null) {
1473 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001474 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001476 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001477
Karl Rosaen138a0412009-04-23 19:00:21 -07001478 final SearchManager searchManager =
1479 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001480 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001481 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
1483
Winson Chung70d72102011-08-12 11:24:35 -07001484 @Override
1485 public boolean onCreateOptionsMenu(Menu menu) {
1486 if (isWorkspaceLocked()) {
1487 return false;
1488 }
1489
1490 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001491
1492 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1493 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1494 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001495 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1496 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1497 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001498 String helpUrl = getString(R.string.help_url);
1499 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001500 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1501 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1502
Winson Chung70d72102011-08-12 11:24:35 -07001503 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1504 .setIcon(android.R.drawable.ic_menu_gallery)
1505 .setAlphabeticShortcut('W');
1506 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1507 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001508 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001509 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001510 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1511 .setIcon(android.R.drawable.ic_menu_preferences)
1512 .setIntent(settings)
1513 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001514 if (!helpUrl.isEmpty()) {
1515 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1516 .setIcon(android.R.drawable.ic_menu_help)
1517 .setIntent(help)
1518 .setAlphabeticShortcut('H');
1519 }
Winson Chung70d72102011-08-12 11:24:35 -07001520 return true;
1521 }
1522
1523 @Override
1524 public boolean onPrepareOptionsMenu(Menu menu) {
1525 super.onPrepareOptionsMenu(menu);
1526
1527 if (mAppsCustomizeTabHost.isTransitioning()) {
1528 return false;
1529 }
1530 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1531 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1532
1533 return true;
1534 }
1535
1536 @Override
1537 public boolean onOptionsItemSelected(MenuItem item) {
1538 switch (item.getItemId()) {
1539 case MENU_WALLPAPER_SETTINGS:
1540 startWallpaper();
1541 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001542 }
1543
1544 return super.onOptionsItemSelected(item);
1545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001547 @Override
1548 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001549 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001550 // Use a custom animation for launching search
1551 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001552 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001553 }
1554
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 public boolean isWorkspaceLocked() {
1556 return mWorkspaceLoading || mWaitingForResult;
1557 }
1558
Michael Jurka0280c3b2010-09-17 15:00:07 -07001559 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001560 mPendingAddInfo.container = ItemInfo.NO_ID;
1561 mPendingAddInfo.screen = -1;
1562 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1563 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001564 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001565 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001567
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001568 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1569 AppWidgetProviderInfo appWidgetInfo) {
1570 if (appWidgetInfo.configure != null) {
1571 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001572
Bjorn Bringert7984c942009-12-09 15:38:25 +00001573 // Launch over to configure widget, if needed
1574 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001575 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001576 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001577 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001579 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001580 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1581 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001582 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001583 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Adam Cohenfbba09b2011-07-18 21:43:05 -07001587 /**
1588 * Process a shortcut drop.
1589 *
1590 * @param componentName The name of the component
1591 * @param screen The screen where it should be added
1592 * @param cell The cell it should be added to, optional
1593 * @param position The location on the screen where it was dropped, optional
1594 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001595 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1596 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001597 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001598 mPendingAddInfo.container = container;
1599 mPendingAddInfo.screen = screen;
1600 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001601
1602 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001603 mPendingAddInfo.cellX = cell[0];
1604 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001605 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606
1607 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1608 createShortcutIntent.setComponent(componentName);
1609 processShortcut(createShortcutIntent);
1610 }
1611
Adam Cohenfbba09b2011-07-18 21:43:05 -07001612 /**
1613 * Process a widget drop.
1614 *
1615 * @param info The PendingAppWidgetInfo of the widget being added.
1616 * @param screen The screen where it should be added
1617 * @param cell The cell it should be added to, optional
1618 * @param position The location on the screen where it was dropped, optional
1619 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001620 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001622 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001623 mPendingAddInfo.container = info.container = container;
1624 mPendingAddInfo.screen = info.screen = screen;
1625 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001626 mPendingAddInfo.minSpanX = info.minSpanX;
1627 mPendingAddInfo.minSpanY = info.minSpanY;
1628
Adam Cohenfbba09b2011-07-18 21:43:05 -07001629 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001630 mPendingAddInfo.cellX = cell[0];
1631 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001632 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001633 if (span != null) {
1634 mPendingAddInfo.spanX = span[0];
1635 mPendingAddInfo.spanY = span[1];
1636 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001637
Adam Cohened66b2b2012-01-23 17:28:51 -08001638 AppWidgetHostView hostView = info.boundWidget;
1639 int appWidgetId;
1640 if (hostView != null) {
1641 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001642 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001643 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001644 // In this case, we either need to start an activity to get permission to bind
1645 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001646 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001647 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001648 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001649 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001650 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001651 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1652 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1653 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1654 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1655 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001656 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001657 }
1658
Joe Onoratodeb98af2010-02-19 14:59:39 -08001659 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001660 // Handle case where user selected "Applications"
1661 String applicationName = getResources().getString(R.string.group_applications);
1662 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001663
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001664 if (applicationName != null && applicationName.equals(shortcutName)) {
1665 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1666 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001667
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001668 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1669 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001670 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001671 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001672 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001673 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
1676
Winson Chung24ab2f12010-09-16 14:10:47 -07001677 void processWallpaper(Intent intent) {
1678 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1679 }
1680
Winson Chung3d503fb2011-07-13 17:25:49 -07001681 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1682 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001683 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 folderInfo.title = getText(R.string.folder_name);
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001687 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1688 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001689 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690
1691 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 FolderIcon newFolder =
1693 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1694 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1695 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001696 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
Romain Guycbb89e42009-06-08 15:52:54 -07001698
Joe Onorato9c1289c2009-08-17 11:03:03 -04001699 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001700 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001701 }
1702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001704 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001706 Intent chooser = Intent.createChooser(pickWallpaper,
1707 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001708 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1709 // Removed in Eclair MR1
1710// WallpaperManager wm = (WallpaperManager)
1711// getSystemService(Context.WALLPAPER_SERVICE);
1712// WallpaperInfo wi = wm.getWallpaperInfo();
1713// if (wi != null && wi.getSettingsActivity() != null) {
1714// LabeledIntent li = new LabeledIntent(getPackageName(),
1715// R.string.configure_wallpaper, 0);
1716// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1717// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1718// }
Mike Clerona0618e42009-10-22 13:55:21 -07001719 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
1721
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001722 /**
1723 * Registers various content observers. The current implementation registers
1724 * only a favorites observer to keep track of the favorites applications.
1725 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001726 private void registerContentObservers() {
1727 ContentResolver resolver = getContentResolver();
1728 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1729 true, mWidgetObserver);
1730 }
1731
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 @Override
1733 public boolean dispatchKeyEvent(KeyEvent event) {
1734 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1735 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001737 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001738 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001739 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001740 dumpState();
1741 return true;
1742 }
1743 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001744 }
1745 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1746 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001747 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 return true;
1749 }
1750 }
1751
1752 return super.dispatchKeyEvent(event);
1753 }
1754
Joe Onorato88ec0992009-11-19 13:16:06 -08001755 @Override
1756 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001757 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001758 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001759 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001760 Folder openFolder = mWorkspace.getOpenFolder();
1761 if (openFolder.isEditingName()) {
1762 openFolder.dismissEditingName();
1763 } else {
1764 closeFolder();
1765 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001766 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001767 mWorkspace.exitWidgetResizeMode();
1768
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001769 // Back button is a no-op here, but give at least some feedback for the button press
1770 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001771 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001772 }
1773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001775 * Re-listen when widgets are reset.
1776 */
1777 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001778 if (mAppWidgetHost != null) {
1779 mAppWidgetHost.startListening();
1780 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001781 }
1782
1783 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1785 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001787 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001788 if (mModel != null) {
1789 mModel.unbindWorkspaceItems();
1790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 /**
1794 * Launches the intent referred by the clicked shortcut.
1795 *
1796 * @param v The view representing the clicked shortcut.
1797 */
1798 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001799 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1800 // view has detached (it's possible for this to happen if the view is removed mid touch).
1801 if (v.getWindowToken() == null) {
1802 return;
1803 }
1804
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001805 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001806 return;
1807 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001810 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001812 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001813 int[] pos = new int[2];
1814 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001815 intent.setSourceBounds(new Rect(pos[0], pos[1],
1816 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001817
1818 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001819
1820 if (success && v instanceof BubbleTextView) {
1821 mWaitingForResume = (BubbleTextView) v;
1822 mWaitingForResume.setStayPressed(true);
1823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001825 if (v instanceof FolderIcon) {
1826 FolderIcon fi = (FolderIcon) v;
1827 handleFolderClick(fi);
1828 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001829 } else if (v == mAllAppsButton) {
1830 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001831 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001832 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001833 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836 }
1837
Michael Jurka0e260592010-06-30 17:07:39 -07001838 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001839 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001840 // clicking anywhere on the workspace causes the customization drawer to slide down
1841 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001842 return false;
1843 }
1844
Michael Jurkaaf442092010-06-10 17:01:57 -07001845 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001846 * Event handler for the search button
1847 *
1848 * @param v The view that was clicked.
1849 */
1850 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001851 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1852
Amith Yamasania135ba82011-08-09 17:42:01 -07001853 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001854 }
1855
1856 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001857 * Event handler for the voice button
1858 *
1859 * @param v The view that was clicked.
1860 */
1861 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001862 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001863
Michael Jurkaae65ee32012-04-30 11:45:54 -07001864 try {
1865 final SearchManager searchManager =
1866 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1867 ComponentName activityName = searchManager.getGlobalSearchActivity();
1868 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1869 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1870 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1871 if (activityName != null) {
1872 intent.setPackage(activityName.getPackageName());
1873 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001874 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001875 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001876 } catch (ActivityNotFoundException e) {
1877 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1878 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1879 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1880 startActivitySafely(null, intent, "onClickVoiceButton");
1881 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001882 }
1883
1884 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001885 * Event handler for the "grid" button that appears on the home screen, which
1886 * enters all apps mode.
1887 *
1888 * @param v The view that was clicked.
1889 */
1890 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001891 showAllApps(true);
1892 }
1893
1894 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001895 // Provide the same haptic feedback that the system offers for virtual keys.
1896 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001897 }
1898
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001899 public void onClickAppMarketButton(View v) {
1900 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001901 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001902 } else {
1903 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001904 }
1905 }
1906
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001907 void startApplicationDetailsActivity(ComponentName componentName) {
1908 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001909 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1910 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001911 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001912 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001913 }
1914
Patrick Dubroy5539af72010-09-07 15:22:01 -07001915 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1916 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1917 // System applications cannot be installed. For now, show a toast explaining that.
1918 // We may give them the option of disabling apps this way.
1919 int messageId = R.string.uninstall_system_app_text;
1920 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1921 } else {
1922 String packageName = appInfo.componentName.getPackageName();
1923 String className = appInfo.componentName.getClassName();
1924 Intent intent = new Intent(
1925 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001926 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1927 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001928 startActivity(intent);
1929 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001930 }
1931
Michael Jurka86a720e2012-05-09 11:23:23 -07001932 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001936 // Only launch using the new animation if the shortcut has not opted out (this is a
1937 // private contract between launcher and may be ignored in the future).
1938 boolean useLaunchAnimation = (v != null) &&
1939 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1940 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001941 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1942 v.getMeasuredWidth(), v.getMeasuredHeight());
1943
1944 startActivity(intent, opts.toBundle());
1945 } else {
1946 startActivity(intent);
1947 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001948 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 } catch (SecurityException e) {
1950 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001951 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001953 "or use the exported attribute for this activity. "
1954 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001956 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001958
Michael Jurka86a720e2012-05-09 11:23:23 -07001959 boolean startActivitySafely(View v, Intent intent, Object tag) {
1960 boolean success = false;
1961 try {
1962 success = startActivity(v, intent, tag);
1963 } catch (ActivityNotFoundException e) {
1964 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1965 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1966 }
1967 return success;
1968 }
1969
Romain Guy8e633c52010-03-04 12:51:36 -08001970 void startActivityForResultSafely(Intent intent, int requestCode) {
1971 try {
1972 startActivityForResult(intent, requestCode);
1973 } catch (ActivityNotFoundException e) {
1974 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1975 } catch (SecurityException e) {
1976 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1977 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1978 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1979 "or use the exported attribute for this activity.", e);
1980 }
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982
Adam Cohena9cf38f2011-05-02 15:36:58 -07001983 private void handleFolderClick(FolderIcon folderIcon) {
1984 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001985 Folder openFolder = mWorkspace.getFolderForTag(info);
1986
1987 // If the folder info reports that the associated folder is open, then verify that
1988 // it is actually opened. There have been a few instances where this gets out of sync.
1989 if (info.opened && openFolder == null) {
1990 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1991 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1992 info.opened = false;
1993 }
1994
Adam Cohena9cf38f2011-05-02 15:36:58 -07001995 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 // Close any open folder
1997 closeFolder();
1998 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001999 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 } else {
2001 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 int folderScreen;
2003 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002004 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 // .. and close it
2006 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002007 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 // Close any folder open on the current screen
2009 closeFolder();
2010 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002011 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
2013 }
2014 }
2015 }
2016
Adam Cohen2801caf2011-05-13 20:57:39 -07002017 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002018 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002019 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2020 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2021 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2022
Adam Cohenc51934b2011-07-26 21:07:43 -07002023 FolderInfo info = (FolderInfo) fi.getTag();
2024 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2025 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002026 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2027 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002028 }
2029
Adam Cohen2801caf2011-05-13 20:57:39 -07002030 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2031 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2032 oa.start();
2033 }
2034
2035 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002036 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002037 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2038 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2039 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2040
Adam Cohenc51934b2011-07-26 21:07:43 -07002041 FolderInfo info = (FolderInfo) fi.getTag();
2042 CellLayout cl = null;
2043 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2044 cl = (CellLayout) fi.getParent().getParent();
2045 }
2046
2047 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002048 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2049 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002050 oa.addListener(new AnimatorListenerAdapter() {
2051 @Override
2052 public void onAnimationEnd(Animator animation) {
2053 if (layout != null) {
2054 layout.clearFolderLeaveBehind();
2055 }
2056 }
2057 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002058 oa.start();
2059 }
2060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002062 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 * is animated relative to the specified View. If the View is null, no animation
2064 * is played.
2065 *
2066 * @param folderInfo The FolderInfo describing the folder to open.
2067 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002068 public void openFolder(FolderIcon folderIcon) {
2069 Folder folder = folderIcon.mFolder;
2070 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071
Adam Cohen2801caf2011-05-13 20:57:39 -07002072 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002073 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074
Adam Cohen4554ee12011-08-03 16:13:21 -07002075 // Just verify that the folder hasn't already been added to the DragLayer.
2076 // There was a one-off crash where the folder had a parent already.
2077 if (folder.getParent() == null) {
2078 mDragLayer.addView(folder);
2079 mDragController.addDropTarget((DropTarget) folder);
2080 } else {
2081 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2082 folder.getParent() + ").");
2083 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002084 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002085 }
2086
2087 public void closeFolder() {
2088 Folder folder = mWorkspace.getOpenFolder();
2089 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002090 if (folder.isEditingName()) {
2091 folder.dismissEditingName();
2092 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002093 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002094
2095 // Dismiss the folder cling
2096 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002097 }
2098 }
2099
2100 void closeFolder(Folder folder) {
2101 folder.getInfo().opened = false;
2102
2103 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2104 if (parent != null) {
2105 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2106 shrinkAndFadeInFolderIcon(fi);
2107 }
2108 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 }
2110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002112 if (!isDraggingEnabled()) return false;
2113 if (isWorkspaceLocked()) return false;
2114 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115
2116 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002117 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
2119
Michael Jurka0280c3b2010-09-17 15:00:07 -07002120 resetAddInfo();
2121 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002123 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 return true;
2125 }
2126
Winson Chung3d503fb2011-07-13 17:25:49 -07002127 // The hotseat touch handling does not go through Workspace, and we always allow long press
2128 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002129 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002130 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2131 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002132 if (itemUnderLongClick == null) {
2133 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002136 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002138 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002140 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 }
2142 }
2143 }
2144 return true;
2145 }
2146
Winson Chung3d503fb2011-07-13 17:25:49 -07002147 boolean isHotseatLayout(View layout) {
2148 return mHotseat != null && layout != null &&
2149 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2150 }
2151 Hotseat getHotseat() {
2152 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002153 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002154 SearchDropTargetBar getSearchBar() {
2155 return mSearchDropTargetBar;
2156 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002157
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 /**
2159 * Returns the CellLayout of the specified container at the specified screen.
2160 */
2161 CellLayout getCellLayout(long container, int screen) {
2162 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2163 if (mHotseat != null) {
2164 return mHotseat.getLayout();
2165 } else {
2166 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002167 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 } else {
2169 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002170 }
2171 }
2172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 Workspace getWorkspace() {
2174 return mWorkspace;
2175 }
2176
Daniel Sandler843e8602010-06-07 14:59:01 -04002177 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2178 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002179 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002180 }
2181
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002182 public boolean isAllAppsButtonRank(int rank) {
2183 return mHotseat.isAllAppsButtonRank(rank);
2184 }
2185
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002186 // AllAppsView.Watcher
2187 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002188 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002189 mWorkspace.setVisibility(View.GONE);
2190 }
2191 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002192
2193 /**
2194 * Helper method for the cameraZoomIn/cameraZoomOut animations
2195 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002196 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002197 * @param scaleFactor The scale factor used for the zoom
2198 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002199 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002200 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002201 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002202 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002203
Winson Chung18f41f82012-05-09 13:28:10 -07002204 void disableWallpaperIfInAllApps() {
2205 // Only disable it if we are in all apps
2206 if (mState == State.APPS_CUSTOMIZE) {
2207 if (mAppsCustomizeTabHost != null &&
2208 !mAppsCustomizeTabHost.isTransitioning()) {
2209 updateWallpaperVisibility(false);
2210 }
2211 }
2212 }
2213
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002214 void updateWallpaperVisibility(boolean visible) {
2215 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2216 int curflags = getWindow().getAttributes().flags
2217 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2218 if (wpflags != curflags) {
2219 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2220 }
2221 }
2222
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002223 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2224 if (v instanceof LauncherTransitionable) {
2225 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2226 }
2227 }
2228
Michael Jurkabed61d22012-02-14 22:51:29 -08002229 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2230 if (v instanceof LauncherTransitionable) {
2231 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2232 }
Winson Chung70442722012-02-10 15:43:22 -08002233
2234 // Update the workspace transition step as well
2235 dispatchOnLauncherTransitionStep(v, 0f);
2236 }
2237
2238 private void dispatchOnLauncherTransitionStep(View v, float t) {
2239 if (v instanceof LauncherTransitionable) {
2240 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2241 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002242 }
2243
2244 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2245 if (v instanceof LauncherTransitionable) {
2246 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2247 }
Winson Chung70442722012-02-10 15:43:22 -08002248
2249 // Update the workspace transition step as well
2250 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002251 }
2252
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002253 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002254 * Things to test when changing the following seven functions.
2255 * - Home from workspace
2256 * - from center screen
2257 * - from other screens
2258 * - Home from all apps
2259 * - from center screen
2260 * - from other screens
2261 * - Back from all apps
2262 * - from center screen
2263 * - from other screens
2264 * - Launch app from workspace and quit
2265 * - with back
2266 * - with home
2267 * - Launch app from all apps and quit
2268 * - with back
2269 * - with home
2270 * - Go to a screen that's not the default, then all
2271 * apps, and launch and app, and go back
2272 * - with back
2273 * -with home
2274 * - On workspace, long press power and go back
2275 * - with back
2276 * - with home
2277 * - On all apps, long press power and go back
2278 * - with back
2279 * - with home
2280 * - On workspace, power off
2281 * - On all apps, power off
2282 * - Launch an app and turn off the screen while in that app
2283 * - Go back with home key
2284 * - Go back with back key TODO: make this not go to workspace
2285 * - From all apps
2286 * - From workspace
2287 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2288 * - From all apps
2289 * - From the center workspace
2290 * - From another workspace
2291 */
2292
2293 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002294 * Zoom the camera out from the workspace to reveal 'toView'.
2295 * Assumes that the view to show is anchored at either the very top or very bottom
2296 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002297 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002298 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002299 if (mStateAnimation != null) {
2300 mStateAnimation.cancel();
2301 mStateAnimation = null;
2302 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002303 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002304
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2306 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2307 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002308 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002309 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002310 final int startDelay =
2311 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002312
Michael Jurkab3e22d92011-10-31 15:58:33 -07002313 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002314
Michael Jurkad74c9842011-07-10 12:44:21 -07002315 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002316 Animator workspaceAnim =
2317 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002318
2319 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002320 toView.setScaleX(scale);
2321 toView.setScaleY(scale);
2322 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2323 scaleAnim.
2324 scaleX(1f).scaleY(1f).
2325 setDuration(duration).
2326 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002327
Winson Chungf0ea4d32011-06-06 14:27:16 -07002328 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002329 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002330 final ObjectAnimator alphaAnim = ObjectAnimator
2331 .ofFloat(toView, "alpha", 0f, 1f)
2332 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002333 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002334 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2335 @Override
2336 public void onAnimationUpdate(ValueAnimator animation) {
2337 float t = (Float) animation.getAnimatedValue();
2338 dispatchOnLauncherTransitionStep(fromView, t);
2339 dispatchOnLauncherTransitionStep(toView, t);
2340 }
2341 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002342
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002343 // toView should appear right at the end of the workspace shrink
2344 // animation
2345 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002346 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002347 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002348
2349 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002350 boolean animationCancelled = false;
2351
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002352 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002353 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002354 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355 // Prepare the position
2356 toView.setTranslationX(0.0f);
2357 toView.setTranslationY(0.0f);
2358 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002359 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002360 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002361 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002362 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002363 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2364 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002365
2366 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2367 // Hide the workspace scrollbar
2368 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002369 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002370 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002371 if (!animationCancelled) {
2372 updateWallpaperVisibility(false);
2373 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002374
2375 // Hide the search bar
2376 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002377 }
2378
Adam Cohencff6af82011-09-13 14:51:53 -07002379 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002380 public void onAnimationCancel(Animator animation) {
2381 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002382 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002383 });
2384
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002385 if (workspaceAnim != null) {
2386 mStateAnimation.play(workspaceAnim);
2387 }
Michael Jurka1899a362011-11-03 13:50:45 -07002388
2389 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002390 final ViewTreeObserver observer;
2391
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002392 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2393 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002394
2395 // If any of the objects being animated haven't been measured/laid out
2396 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002397 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002398 (mWorkspace.getMeasuredWidth() == 0) ||
2399 (toView.getMeasuredWidth() == 0)) {
2400 observer = mWorkspace.getViewTreeObserver();
2401 delayAnim = true;
2402 } else {
2403 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002404 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002405
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002406 final AnimatorSet stateAnimation = mStateAnimation;
2407 final Runnable startAnimRunnable = new Runnable() {
2408 public void run() {
2409 // Check that mStateAnimation hasn't changed while
2410 // we waited for a layout/draw pass
2411 if (mStateAnimation != stateAnimation)
2412 return;
2413 setPivotsForZoom(toView, scale);
2414 dispatchOnLauncherTransitionStart(fromView, animated, false);
2415 dispatchOnLauncherTransitionStart(toView, animated, false);
2416 mWorkspace.post(new Runnable() {
2417 public void run() {
2418 // Check that mStateAnimation hasn't changed while
2419 // we waited for a layout/draw pass
2420 if (mStateAnimation != stateAnimation)
2421 return;
2422 mStateAnimation.start();
2423 }
2424 });
2425 }
2426 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002427 if (delayAnim) {
2428 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2429 public void onGlobalLayout() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002430 mWorkspace.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002431 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002432 }
2433 };
2434 observer.addOnGlobalLayoutListener(delayedStart);
2435 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002436 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002437 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002438 } else {
2439 toView.setTranslationX(0.0f);
2440 toView.setTranslationY(0.0f);
2441 toView.setScaleX(1.0f);
2442 toView.setScaleY(1.0f);
2443 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002444 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002445
Michael Jurkabed61d22012-02-14 22:51:29 -08002446 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2447 // Hide the workspace scrollbar
2448 mWorkspace.hideScrollingIndicator(true);
2449 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002450
2451 // Hide the search bar
2452 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002453 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002454 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002455 dispatchOnLauncherTransitionStart(fromView, animated, false);
2456 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002457 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002458 dispatchOnLauncherTransitionStart(toView, animated, false);
2459 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002460 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002461 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002462 }
2463
2464 /**
2465 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002466 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002467 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002468 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002469 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2470 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002471
Michael Jurkab3e22d92011-10-31 15:58:33 -07002472 if (mStateAnimation != null) {
2473 mStateAnimation.cancel();
2474 mStateAnimation = null;
2475 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002476 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002477
Winson Chungf0ea4d32011-06-06 14:27:16 -07002478 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002479 final int fadeOutDuration =
2480 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002481 final float scaleFactor = (float)
2482 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2483 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002484 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002485 Animator workspaceAnim = null;
2486
2487 if (toState == State.WORKSPACE) {
2488 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2489 workspaceAnim = mWorkspace.getChangeStateAnimation(
2490 Workspace.State.NORMAL, animated, stagger);
2491 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2492 workspaceAnim = mWorkspace.getChangeStateAnimation(
2493 Workspace.State.SPRING_LOADED, animated);
2494 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002495
Michael Jurkab3e22d92011-10-31 15:58:33 -07002496 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002497 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002498 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002499 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002500 final LauncherViewPropertyAnimator scaleAnim =
2501 new LauncherViewPropertyAnimator(fromView);
2502 scaleAnim.
2503 scaleX(scaleFactor).scaleY(scaleFactor).
2504 setDuration(duration).
2505 setInterpolator(new Workspace.ZoomInInterpolator());
2506
2507 final ObjectAnimator alphaAnim = ObjectAnimator
2508 .ofFloat(fromView, "alpha", 1f, 0f)
2509 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002510 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002511 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2512 @Override
2513 public void onAnimationUpdate(ValueAnimator animation) {
2514 float t = 1f - (Float) animation.getAnimatedValue();
2515 dispatchOnLauncherTransitionStep(fromView, t);
2516 dispatchOnLauncherTransitionStep(toView, t);
2517 }
2518 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002519
Michael Jurkabed61d22012-02-14 22:51:29 -08002520 mStateAnimation = new AnimatorSet();
2521
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002522 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2523 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002524
2525 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002526 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002527 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002528 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002530 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2531 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002532 if (mWorkspace != null) {
2533 mWorkspace.hideScrollingIndicator(false);
2534 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002535 if (onCompleteRunnable != null) {
2536 onCompleteRunnable.run();
2537 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002538 }
2539 });
2540
Winson Chungf0ea4d32011-06-06 14:27:16 -07002541 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002542 if (workspaceAnim != null) {
2543 mStateAnimation.play(workspaceAnim);
2544 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002545 dispatchOnLauncherTransitionStart(fromView, animated, true);
2546 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002547 final Animator stateAnimation = mStateAnimation;
2548 mWorkspace.post(new Runnable() {
2549 public void run() {
2550 if (stateAnimation != mStateAnimation)
2551 return;
2552 mStateAnimation.start();
2553 }
2554 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002555 } else {
2556 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002557 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002558 dispatchOnLauncherTransitionStart(fromView, animated, true);
2559 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002560 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002561 dispatchOnLauncherTransitionStart(toView, animated, true);
2562 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002563 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002564 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002565 }
2566
Michael Jurkae326f182011-11-21 14:05:46 -08002567 @Override
2568 public void onTrimMemory(int level) {
2569 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002570 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002571 mAppsCustomizeTabHost.onTrimMemory();
2572 }
2573 }
2574
Winson Chung18f41f82012-05-09 13:28:10 -07002575 @Override
2576 public void onWindowFocusChanged(boolean hasFocus) {
2577 if (!hasFocus) {
2578 // When another window occludes launcher (like the notification shade, or recents),
2579 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2580 updateWallpaperVisibility(true);
2581 } else {
2582 // When launcher has focus again, disable the wallpaper if we are in AllApps
2583 disableWallpaperIfInAllApps();
2584 }
2585 }
2586
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002587 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002588 showWorkspace(animated, null);
2589 }
2590
2591 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002592 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002593 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002594 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002595 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002596
Winson Chungc7d2b602012-05-16 17:02:20 -07002597 // Show the search bar (only animate if we were showing the drop target bar in spring
2598 // loaded mode)
2599 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2600
Michael Jurkab737ee62011-11-15 15:57:22 -08002601 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002602 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002603
2604 // Set focus to the AppsCustomize button
2605 if (mAllAppsButton != null) {
2606 mAllAppsButton.requestFocus();
2607 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002608 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002609
Michael Jurkab737ee62011-11-15 15:57:22 -08002610 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002611
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002612 // Change the state *after* we've called all the transition code
2613 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002614
Winson Chung5fb63472011-02-02 17:03:37 -08002615 // Resume the auto-advance of widgets
2616 mUserPresent = true;
2617 updateRunning();
2618
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002619 // send an accessibility event to announce the context change
2620 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002621 }
2622
Michael Jurkab3e22d92011-10-31 15:58:33 -07002623 void showAllApps(boolean animated) {
2624 if (mState != State.WORKSPACE) return;
2625
2626 showAppsCustomizeHelper(animated, false);
2627 mAppsCustomizeTabHost.requestFocus();
2628
Michael Jurkab3e22d92011-10-31 15:58:33 -07002629 // Change the state *after* we've called all the transition code
2630 mState = State.APPS_CUSTOMIZE;
2631
2632 // Pause the auto-advance of widgets until we are out of AllApps
2633 mUserPresent = false;
2634 updateRunning();
2635 closeFolder();
2636
2637 // Send an accessibility event to announce the context change
2638 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2639 }
2640
Adam Cohen7777d962011-08-18 18:58:38 -07002641 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002642 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002643 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002644 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002645 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002646 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002647 }
Adam Cohen7777d962011-08-18 18:58:38 -07002648
Adam Cohened66b2b2012-01-23 17:28:51 -08002649 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2650 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002651 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2652
Winson Chunge7a03942011-08-05 15:05:12 -07002653 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002654 @Override
2655 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002656 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002657 // Before we show workspace, hide all apps again because
2658 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2659 // clean up our state transition functions
2660 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002661 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002662 } else {
2663 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002664 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002665 }
2666 }, (extendedDelay ?
2667 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2668 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2669 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002670
Michael Jurkad3ef3062010-11-23 16:23:58 -08002671 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002672 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002673 final boolean animated = true;
2674 final boolean springLoaded = true;
2675 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002676 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002677 }
2678 // Otherwise, we are not in spring loaded mode, so don't do anything.
2679 }
2680
Michael Jurkab737ee62011-11-15 15:57:22 -08002681 void hideDockDivider() {
2682 if (mQsbDivider != null && mDockDivider != null) {
2683 mQsbDivider.setVisibility(View.INVISIBLE);
2684 mDockDivider.setVisibility(View.INVISIBLE);
2685 }
2686 }
2687
2688 void showDockDivider(boolean animated) {
2689 if (mQsbDivider != null && mDockDivider != null) {
2690 mQsbDivider.setVisibility(View.VISIBLE);
2691 mDockDivider.setVisibility(View.VISIBLE);
2692 if (mDividerAnimator != null) {
2693 mDividerAnimator.cancel();
2694 mQsbDivider.setAlpha(1f);
2695 mDockDivider.setAlpha(1f);
2696 mDividerAnimator = null;
2697 }
2698 if (animated) {
2699 mDividerAnimator = new AnimatorSet();
2700 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2701 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2702 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2703 mDividerAnimator.start();
2704 }
2705 }
2706 }
2707
Michael Jurkab3e22d92011-10-31 15:58:33 -07002708 void lockAllApps() {
2709 // TODO
2710 }
2711
2712 void unlockAllApps() {
2713 // TODO
2714 }
2715
Adam Cohenfc53cd22011-07-20 15:45:11 -07002716 public boolean isAllAppsCustomizeOpen() {
2717 return mState == State.APPS_CUSTOMIZE;
2718 }
2719
Winson Chungf0ea4d32011-06-06 14:27:16 -07002720 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002721 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002722 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002723 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002724 if (!LauncherApplication.isScreenLarge()) {
2725 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002726 if (mHotseat.getAlpha() != 1f) {
2727 int duration = mSearchDropTargetBar.getTransitionInDuration();
2728 mHotseat.animate().alpha(1f).setDuration(duration);
2729 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002730 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002731 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002732 }
2733 }
2734 }
2735
2736 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002737 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002738 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002739 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002740 if (!LauncherApplication.isScreenLarge()) {
2741 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002742 if (mHotseat.getAlpha() != 0f) {
2743 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2744 mHotseat.animate().alpha(0f).setDuration(duration);
2745 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002746 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002747 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002748 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002749 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002750 }
2751
Patrick Dubroy5f445422011-02-18 14:35:21 -08002752 /**
2753 * Add an item from all apps or customize onto the given workspace screen.
2754 * If layout is null, add to the current screen.
2755 */
2756 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002757 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002758 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002759 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002760 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002761
Winson Chungdff8ebb2011-09-08 17:25:31 -07002762 /** Maps the current orientation to an index for referencing orientation correct global icons */
2763 private int getCurrentOrientationIndexForGlobalIcons() {
2764 // default - 0, landscape - 1
2765 switch (getResources().getConfiguration().orientation) {
2766 case Configuration.ORIENTATION_LANDSCAPE:
2767 return 1;
2768 default:
2769 return 0;
2770 }
2771 }
2772
Michael Jurkaae65ee32012-04-30 11:45:54 -07002773 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002774 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002775 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002776 // Look for the toolbar icon specified in the activity meta-data
2777 Bundle metaData = packageManager.getActivityInfo(
2778 activityName, PackageManager.GET_META_DATA).metaData;
2779 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002780 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002781 if (iconResId != 0) {
2782 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002783 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002784 }
2785 }
2786 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002787 // This can happen if the activity defines an invalid drawable
2788 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2789 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002790 } catch (Resources.NotFoundException nfe) {
2791 // This can happen if the activity defines an invalid drawable
2792 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2793 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002794 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002795 return null;
2796 }
2797
2798 // if successful in getting icon, return it; otherwise, set button to use default drawable
2799 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002800 int buttonId, ComponentName activityName, int fallbackDrawableId,
2801 String toolbarResourceName) {
2802 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002803 Resources r = getResources();
2804 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2805 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002807 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002808 // If we were unable to find the icon via the meta-data, use a generic one
2809 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002810 toolbarIcon = r.getDrawable(fallbackDrawableId);
2811 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002812 if (button != null) {
2813 button.setCompoundDrawables(toolbarIcon, null, null, null);
2814 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002815 return null;
2816 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002817 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002818 if (button != null) {
2819 button.setCompoundDrawables(toolbarIcon, null, null, null);
2820 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002821 return toolbarIcon.getConstantState();
2822 }
2823 }
2824
2825 // if successful in getting icon, return it; otherwise, set button to use default drawable
2826 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002827 int buttonId, ComponentName activityName, int fallbackDrawableId,
2828 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002829 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002830 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002831
Michael Jurka19e0fc52011-07-22 18:00:21 -07002832 if (button != null) {
2833 // If we were unable to find the icon via the meta-data, use a
2834 // generic one
2835 if (toolbarIcon == null) {
2836 button.setImageResource(fallbackDrawableId);
2837 } else {
2838 button.setImageDrawable(toolbarIcon);
2839 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002840 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002841
2842 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2843
Michael Jurka0423dcf2010-10-05 14:56:18 -07002844 }
2845
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002846 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2847 TextView button = (TextView) findViewById(buttonId);
2848 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2849 }
2850
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002851 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002852 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002853 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002854 }
2855
Winson Chungbb185bd2011-11-21 12:31:42 -08002856 private void invalidatePressedFocusedStates(View container, View button) {
2857 if (container instanceof HolographicLinearLayout) {
2858 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2859 layout.invalidatePressedFocusedStates();
2860 } else if (button instanceof HolographicImageView) {
2861 HolographicImageView view = (HolographicImageView) button;
2862 view.invalidatePressedFocusedStates();
2863 }
2864 }
2865
Winson Chungc51db6a2011-10-05 11:44:49 -07002866 private boolean updateGlobalSearchIcon() {
2867 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002868 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2869 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002870 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002871 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002872 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002873
Winson Chung1cad91e2011-05-25 17:41:01 -07002874 final SearchManager searchManager =
2875 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2876 ComponentName activityName = searchManager.getGlobalSearchActivity();
2877 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002878 int coi = getCurrentOrientationIndexForGlobalIcons();
2879 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002880 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2881 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2882 if (sGlobalSearchIcon[coi] == null) {
2883 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2884 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2885 TOOLBAR_ICON_METADATA_NAME);
2886 }
2887
Winson Chung649723c2011-07-06 20:41:23 -07002888 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002889 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2890 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002891 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002892 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002893 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002894 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002895 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002896 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2897 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2898 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002899 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002900 if (voiceButtonProxy != null) {
2901 voiceButtonProxy.setVisibility(View.GONE);
2902 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002903 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002904 }
2905 }
2906
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002907 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002908 final View searchButtonContainer = findViewById(R.id.search_button_container);
2909 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002910 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002911 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002912 }
2913
Winson Chungc51db6a2011-10-05 11:44:49 -07002914 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002915 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002916 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002917 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002918 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002919
Winson Chungc51db6a2011-10-05 11:44:49 -07002920 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002921 final SearchManager searchManager =
2922 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2923 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2924
2925 ComponentName activityName = null;
2926 if (globalSearchActivity != null) {
2927 // Check if the global search activity handles voice search
2928 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2929 intent.setPackage(globalSearchActivity.getPackageName());
2930 activityName = intent.resolveActivity(getPackageManager());
2931 }
2932
2933 if (activityName == null) {
2934 // Fallback: check if an activity other than the global search activity
2935 // resolves this
2936 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2937 activityName = intent.resolveActivity(getPackageManager());
2938 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002939 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002940 int coi = getCurrentOrientationIndexForGlobalIcons();
2941 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002942 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2943 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2944 if (sVoiceSearchIcon[coi] == null) {
2945 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2946 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2947 TOOLBAR_ICON_METADATA_NAME);
2948 }
Winson Chung649723c2011-07-06 20:41:23 -07002949 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002950 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002951 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002952 if (voiceButtonProxy != null) {
2953 voiceButtonProxy.setVisibility(View.VISIBLE);
2954 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002955 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002956 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002957 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002958 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002959 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002960 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002961 if (voiceButtonProxy != null) {
2962 voiceButtonProxy.setVisibility(View.GONE);
2963 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002964 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002965 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002966 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002967
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002968 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002969 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2970 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002971 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002972 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002973 }
2974
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002975 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002976 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002977 */
2978 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002979 final View marketButton = findViewById(R.id.market_button);
2980 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2981 // Find the app market activity by resolving an intent.
2982 // (If multiple app markets are installed, it will return the ResolverActivity.)
2983 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002984 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002985 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002986 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002987 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002988 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2989 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002990 marketButton.setVisibility(View.VISIBLE);
2991 } else {
2992 // We should hide and disable the view so that we don't try and restore the visibility
2993 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2994 marketButton.setVisibility(View.GONE);
2995 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002996 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002997 }
2998
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002999 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003000 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003001 }
3002
Michael Jurkad7c28052012-04-27 15:43:36 -07003003 @Override
3004 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3005 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3006 final List<CharSequence> text = event.getText();
3007 text.clear();
3008 text.add(getString(R.string.home));
3009 return result;
3010 }
3011
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003012 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003013 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003014 */
3015 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3016 @Override
3017 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003018 closeSystemDialogs();
3019 }
3020 }
3021
3022 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003023 * Receives notifications whenever the appwidgets are reset.
3024 */
3025 private class AppWidgetResetObserver extends ContentObserver {
3026 public AppWidgetResetObserver() {
3027 super(new Handler());
3028 }
3029
3030 @Override
3031 public void onChange(boolean selfChange) {
3032 onAppWidgetReset();
3033 }
3034 }
3035
3036 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003037 * If the activity is currently paused, signal that we need to re-run the loader
3038 * in onResume.
3039 *
3040 * This needs to be called from incoming places where resources might have been loaded
3041 * while we are paused. That is becaues the Configuration might be wrong
3042 * when we're not running, and if it comes back to what it was when we
3043 * were paused, we are not restarted.
3044 *
3045 * Implementation of the method from LauncherModel.Callbacks.
3046 *
3047 * @return true if we are currently paused. The caller might be able to
3048 * skip some work in that case since we will come back again.
3049 */
3050 public boolean setLoadOnResume() {
3051 if (mPaused) {
3052 Log.i(TAG, "setLoadOnResume");
3053 mOnResumeNeedsLoad = true;
3054 return true;
3055 } else {
3056 return false;
3057 }
3058 }
3059
3060 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003064 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003065 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003066 } else {
3067 return SCREEN_COUNT / 2;
3068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003070
Joe Onorato9c1289c2009-08-17 11:03:03 -04003071 /**
3072 * Refreshes the shortcuts shown on the workspace.
3073 *
3074 * Implementation of the method from LauncherModel.Callbacks.
3075 */
3076 public void startBinding() {
3077 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003078
Winson Chungf0c6ae02012-03-21 16:10:31 -07003079 mNewShortcutAnimatePage = -1;
3080 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003081 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 int count = workspace.getChildCount();
3083 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003084 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003085 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3086 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003088 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003089 if (mHotseat != null) {
3090 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 }
3092 }
3093
3094 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 * Bind the items start-end from the list.
3096 *
3097 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003100 setLoadOnResume();
3101
Winson Chungf0c6ae02012-03-21 16:10:31 -07003102 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3103 Set<String> newApps = new HashSet<String>();
3104 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3105
3106 Workspace workspace = mWorkspace;
3107 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003109
3110 // Short circuit if we are loading dock items for a configuration which has no dock
3111 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3112 mHotseat == null) {
3113 continue;
3114 }
3115
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 switch (item.itemType) {
3117 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3118 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003119 ShortcutInfo info = (ShortcutInfo) item;
3120 String uri = info.intent.toUri(0).toString();
3121 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003122 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3123 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003124 if (newApps.contains(uri)) {
3125 newApps.remove(uri);
3126
3127 // Prepare the view to be animated up
3128 shortcut.setAlpha(0f);
3129 shortcut.setScaleX(0f);
3130 shortcut.setScaleY(0f);
3131 mNewShortcutAnimatePage = item.screen;
3132 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3133 mNewShortcutAnimateViews.add(shortcut);
3134 }
3135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003137 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003138 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003139 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003140 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003141 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3142 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003146
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003148 }
3149
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 /**
3151 * Implementation of the method from LauncherModel.Callbacks.
3152 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003153 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003154 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003155 sFolders.clear();
3156 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158
3159 /**
3160 * Add the views for a widget to the workspace.
3161 *
3162 * Implementation of the method from LauncherModel.Callbacks.
3163 */
3164 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003165 setLoadOnResume();
3166
Daniel Sandler843e8602010-06-07 14:59:01 -04003167 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3168 if (DEBUG_WIDGETS) {
3169 Log.d(TAG, "bindAppWidget: " + item);
3170 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 final Workspace workspace = mWorkspace;
3172
3173 final int appWidgetId = item.appWidgetId;
3174 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003175 if (DEBUG_WIDGETS) {
3176 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3177 }
3178
Joe Onorato9c1289c2009-08-17 11:03:03 -04003179 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3180
Joe Onorato9c1289c2009-08-17 11:03:03 -04003181 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3182 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003183 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003187 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3188
Joe Onorato9c1289c2009-08-17 11:03:03 -04003189 workspace.requestLayout();
3190
Daniel Sandler843e8602010-06-07 14:59:01 -04003191 if (DEBUG_WIDGETS) {
3192 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3193 + (SystemClock.uptimeMillis()-start) + "ms");
3194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003195 }
3196
3197 /**
3198 * Callback saying that there aren't any more items to bind.
3199 *
3200 * Implementation of the method from LauncherModel.Callbacks.
3201 */
3202 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003203 setLoadOnResume();
3204
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205 if (mSavedState != null) {
3206 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003207 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003208 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 mSavedState = null;
3210 }
3211
3212 if (mSavedInstanceState != null) {
3213 super.onRestoreInstanceState(mSavedInstanceState);
3214 mSavedInstanceState = null;
3215 }
3216
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003217 // If we received the result of any pending adds while the loader was running (e.g. the
3218 // widget configuration forced an orientation change), process them now.
3219 for (int i = 0; i < sPendingAddList.size(); i++) {
3220 completeAdd(sPendingAddList.get(i));
3221 }
3222 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223
Winson Chungc51db6a2011-10-05 11:44:49 -07003224 // Update the market app icon as necessary (the other icons will be managed in response to
3225 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003226 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003227
Winson Chungf0c6ae02012-03-21 16:10:31 -07003228 // Animate up any icons as necessary
3229 if (mVisible || mWorkspaceLoading) {
3230 Runnable newAppsRunnable = new Runnable() {
3231 @Override
3232 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003233 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003234 }
3235 };
Winson Chunga2413752012-04-03 14:22:34 -07003236
3237 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3238 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3239 if (canRunNewAppsAnimation()) {
3240 // If the user has not interacted recently, then either snap to the new page to show
3241 // the new-apps animation or just run them if they are to appear on the current page
3242 if (willSnapPage) {
3243 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3244 } else {
3245 runNewAppsAnimation(false);
3246 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003247 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003248 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003249 // are on another page (or just normally if they are added to the current page)
3250 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003251 }
3252 }
3253
3254 mWorkspaceLoading = false;
3255 }
3256
Winson Chunga2413752012-04-03 14:22:34 -07003257 private boolean canRunNewAppsAnimation() {
3258 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3259 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3260 }
3261
Winson Chungf0c6ae02012-03-21 16:10:31 -07003262 /**
3263 * Runs a new animation that scales up icons that were added while Launcher was in the
3264 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003265 *
3266 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003267 */
Winson Chunga2413752012-04-03 14:22:34 -07003268 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003269 AnimatorSet anim = new AnimatorSet();
3270 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003271
3272 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003273 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3274 @Override
3275 public int compare(View a, View b) {
3276 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3277 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3278 int cellCountX = LauncherModel.getCellCountX();
3279 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3280 }
3281 });
Winson Chunga2413752012-04-03 14:22:34 -07003282
3283 // Animate each of the views in place (or show them immediately if requested)
3284 if (immediate) {
3285 for (View v : mNewShortcutAnimateViews) {
3286 v.setAlpha(1f);
3287 v.setScaleX(1f);
3288 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003289 }
Winson Chunga2413752012-04-03 14:22:34 -07003290 } else {
3291 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3292 View v = mNewShortcutAnimateViews.get(i);
3293 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3294 PropertyValuesHolder.ofFloat("alpha", 1f),
3295 PropertyValuesHolder.ofFloat("scaleX", 1f),
3296 PropertyValuesHolder.ofFloat("scaleY", 1f));
3297 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3298 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3299 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3300 bounceAnims.add(bounceAnim);
3301 }
3302 anim.playTogether(bounceAnims);
3303 anim.addListener(new AnimatorListenerAdapter() {
3304 @Override
3305 public void onAnimationEnd(Animator animation) {
3306 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3307 }
3308 });
3309 anim.start();
3310 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003311
3312 // Clean up
3313 mNewShortcutAnimatePage = -1;
3314 mNewShortcutAnimateViews.clear();
3315 new Thread("clearNewAppsThread") {
3316 public void run() {
3317 mSharedPrefs.edit()
3318 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3319 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3320 .commit();
3321 }
3322 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003323 }
3324
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003325 @Override
3326 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 boolean searchVisible = updateGlobalSearchIcon();
3328 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3329 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003330 }
3331
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003332 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 * Add the icons for all apps.
3334 *
3335 * Implementation of the method from LauncherModel.Callbacks.
3336 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003337 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3338 // Remove the progress bar entirely; we could also make it GONE
3339 // but better to remove it since we know it's not going to be used
3340 View progressBar = mAppsCustomizeTabHost.
3341 findViewById(R.id.apps_customize_progress_bar);
3342 if (progressBar != null) {
3343 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003344 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003345 // We just post the call to setApps so the user sees the progress bar
3346 // disappear-- otherwise, it just looks like the progress bar froze
3347 // which doesn't look great
3348 mAppsCustomizeTabHost.post(new Runnable() {
3349 public void run() {
3350 if (mAppsCustomizeContent != null) {
3351 mAppsCustomizeContent.setApps(apps);
3352 }
3353 }
3354 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 }
3356
3357 /**
3358 * A package was installed.
3359 *
3360 * Implementation of the method from LauncherModel.Callbacks.
3361 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003362 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003363 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003364
Winson Chung785d2eb2011-04-14 16:08:02 -07003365 if (mAppsCustomizeContent != null) {
3366 mAppsCustomizeContent.addApps(apps);
3367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 }
3369
3370 /**
3371 * A package was updated.
3372 *
3373 * Implementation of the method from LauncherModel.Callbacks.
3374 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003375 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003376 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003377 if (mWorkspace != null) {
3378 mWorkspace.updateShortcuts(apps);
3379 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003380
Winson Chung785d2eb2011-04-14 16:08:02 -07003381 if (mAppsCustomizeContent != null) {
3382 mAppsCustomizeContent.updateApps(apps);
3383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003384 }
3385
3386 /**
3387 * A package was uninstalled.
3388 *
3389 * Implementation of the method from LauncherModel.Callbacks.
3390 */
Joe Onorato36115782010-06-17 13:28:48 -04003391 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003392 if (permanent) {
3393 mWorkspace.removeItems(apps);
3394 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003395
Winson Chung785d2eb2011-04-14 16:08:02 -07003396 if (mAppsCustomizeContent != null) {
3397 mAppsCustomizeContent.removeApps(apps);
3398 }
Winson Chunga1820962011-10-03 16:31:06 -07003399
3400 // Notify the drag controller
3401 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 }
Joe Onoratobe386092009-11-17 17:32:16 -08003403
3404 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003405 * A number of packages were updated.
3406 */
3407 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003408 if (mAppsCustomizeContent != null) {
3409 mAppsCustomizeContent.onPackagesUpdated();
3410 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003411 }
3412
Winson Chung400438b2011-01-16 17:53:48 -08003413 private int mapConfigurationOriActivityInfoOri(int configOri) {
3414 final Display d = getWindowManager().getDefaultDisplay();
3415 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3416 switch (d.getRotation()) {
3417 case Surface.ROTATION_0:
3418 case Surface.ROTATION_180:
3419 // We are currently in the same basic orientation as the natural orientation
3420 naturalOri = configOri;
3421 break;
3422 case Surface.ROTATION_90:
3423 case Surface.ROTATION_270:
3424 // We are currently in the other basic orientation to the natural orientation
3425 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3426 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3427 break;
3428 }
3429
3430 int[] oriMap = {
3431 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3432 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3433 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3434 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3435 };
3436 // Since the map starts at portrait, we need to offset if this device's natural orientation
3437 // is landscape.
3438 int indexOffset = 0;
3439 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3440 indexOffset = 1;
3441 }
3442 return oriMap[(d.getRotation() + indexOffset) % 4];
3443 }
Adam Cohen446e9402011-09-15 18:21:21 -07003444
Winson Chung4b919f82012-05-01 10:44:08 -07003445 public boolean isRotationEnabled() {
3446 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3447 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3448 boolean enableRotation = forceEnableRotation ||
3449 getResources().getBoolean(R.bool.allow_rotation);
3450 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003451 }
Winson Chung4b919f82012-05-01 10:44:08 -07003452 public void lockScreenOrientation() {
3453 if (isRotationEnabled()) {
3454 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3455 .getConfiguration().orientation));
3456 }
3457 }
3458 public void unlockScreenOrientation(boolean immediate) {
3459 if (isRotationEnabled()) {
3460 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003461 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003462 } else {
3463 mHandler.postDelayed(new Runnable() {
3464 public void run() {
3465 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3466 }
3467 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003468 }
Winson Chung4b919f82012-05-01 10:44:08 -07003469 }
Winson Chung400438b2011-01-16 17:53:48 -08003470 }
3471
Winson Chung82f55532011-08-09 14:14:23 -07003472 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003473 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003474 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003475 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003476
Winson Chung7d7541e2011-09-16 20:14:36 -07003477 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003478 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003479 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3480 Cling cling = (Cling) findViewById(clingId);
3481 if (cling != null) {
3482 cling.init(this, positionData);
3483 cling.setVisibility(View.VISIBLE);
3484 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -07003485 cling.requestAccessibilityFocus();
Winson Chung7d7541e2011-09-16 20:14:36 -07003486 if (animate) {
3487 cling.buildLayer();
3488 cling.setAlpha(0f);
3489 cling.animate()
3490 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003491 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003492 .setDuration(SHOW_CLING_DURATION)
3493 .setStartDelay(delay)
3494 .start();
3495 } else {
3496 cling.setAlpha(1f);
3497 }
3498 }
3499 return cling;
3500 }
3501 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003502 if (cling != null) {
3503 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003504 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003505 anim.addListener(new AnimatorListenerAdapter() {
3506 public void onAnimationEnd(Animator animation) {
3507 cling.setVisibility(View.GONE);
3508 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003509 // We should update the shared preferences on a background thread
3510 new Thread("dismissClingThread") {
3511 public void run() {
3512 SharedPreferences.Editor editor = mSharedPrefs.edit();
3513 editor.putBoolean(flag, true);
3514 editor.commit();
3515 }
3516 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003517 };
3518 });
3519 anim.start();
3520 }
3521 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003522 private void removeCling(int id) {
3523 final View cling = findViewById(id);
3524 if (cling != null) {
3525 final ViewGroup parent = (ViewGroup) cling.getParent();
3526 parent.post(new Runnable() {
3527 @Override
3528 public void run() {
3529 parent.removeView(cling);
3530 }
3531 });
3532 }
3533 }
Michael Jurka974c3862012-05-22 22:00:31 -07003534
3535 private boolean skipCustomClingIfNoAccounts() {
3536 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3537 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3538 if (customCling) {
3539 AccountManager am = AccountManager.get(this);
3540 Account[] accounts = am.getAccountsByType("com.google");
3541 return accounts.length == 0;
3542 }
3543 return false;
3544 }
3545
Winson Chung7d7541e2011-09-16 20:14:36 -07003546 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003547 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003548 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003549 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3550 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003551 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003552 } else {
3553 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003554 }
3555 }
3556 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003557 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003558 if (isClingsEnabled() &&
3559 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003560 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003561 } else {
3562 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003563 }
3564 }
3565 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003566 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003567 if (isClingsEnabled() &&
3568 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3569 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003570 } else {
3571 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003572 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003573 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003574 }
3575 public boolean isFolderClingVisible() {
3576 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003577 if (cling != null) {
3578 return cling.getVisibility() == View.VISIBLE;
3579 }
3580 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003581 }
Winson Chung82f55532011-08-09 14:14:23 -07003582 public void dismissWorkspaceCling(View v) {
3583 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003584 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003585 }
3586 public void dismissAllAppsCling(View v) {
3587 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003588 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3589 }
3590 public void dismissFolderCling(View v) {
3591 Cling cling = (Cling) findViewById(R.id.folder_cling);
3592 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003593 }
3594
Winson Chung80baf5a2010-08-09 16:03:15 -07003595 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003596 * Prints out out state for debugging.
3597 */
3598 public void dumpState() {
3599 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003600 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003601 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3602 Log.d(TAG, "mRestoring=" + mRestoring);
3603 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3604 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003605 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003606 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003607
Winson Chung785d2eb2011-04-14 16:08:02 -07003608 if (mAppsCustomizeContent != null) {
3609 mAppsCustomizeContent.dumpState();
3610 }
Joe Onoratobe386092009-11-17 17:32:16 -08003611 Log.d(TAG, "END launcher2 dump state");
3612 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003613
3614 @Override
3615 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3616 super.dump(prefix, fd, writer, args);
3617 writer.println(" ");
3618 writer.println("Debug logs: ");
3619 for (int i = 0; i < sDumpLogs.size(); i++) {
3620 writer.println(" " + sDumpLogs.get(i));
3621 }
3622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003623}
Michael Jurka2763be32011-02-24 11:19:57 -08003624
Michael Jurkaabded662011-03-04 12:06:57 -08003625interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003626 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003627 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003628 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003629 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003630 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003631}