blob: 3c20a5850999925c4c880120374cbfd1faa1e420 [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
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
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700152 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
153 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700164 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700165 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
166 "com.android.launcher.toolbar_search_icon";
167 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
168 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700169
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800174 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700177 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
178 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700179 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700180 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800183 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Winson Chunga2413752012-04-03 14:22:34 -0700185 // How long to wait before the new-shortcut animation automatically pans the workspace
186 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
187
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800188 private final BroadcastReceiver mCloseSystemDialogsReceiver
189 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800190 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private LayoutInflater mInflater;
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800195 private View mQsbDivider;
196 private View mDockDivider;
197 private DragLayer mDragLayer;
198 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 private AppWidgetManager mAppWidgetManager;
201 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Michael Jurkac9d95c52011-08-29 14:03:34 -0700203 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700204 private int[] mTmpAddItemCellCoordinates = new int[2];
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private FolderInfo mFolderInfo;
207
Winson Chung3d503fb2011-07-13 17:25:49 -0700208 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700210
Winson Chungc51db6a2011-10-05 11:44:49 -0700211 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700212 private AppsCustomizeTabHost mAppsCustomizeTabHost;
213 private AppsCustomizePagedView mAppsCustomizeContent;
214 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700239 private Intent mAppMarketIntent = null;
240
Adam Cohended9f8d2010-11-03 13:25:16 -0700241 // Related to the auto-advancing of widgets
242 private final int ADVANCE_MSG = 1;
243 private final int mAdvanceInterval = 20000;
244 private final int mAdvanceStagger = 250;
245 private long mAutoAdvanceSentTime;
246 private long mAutoAdvanceTimeLeft = -1;
247 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
248 new HashMap<View, AppWidgetProviderInfo>();
249
Winson Chung400438b2011-01-16 17:53:48 -0800250 // Determines how long to wait after a rotation before restoring the screen orientation to
251 // match the sensor state.
252 private final int mRestoreScreenOrientationDelay = 500;
253
Michael Jurka4ef207b2010-11-29 17:05:45 -0800254 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700255 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
256 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
257 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800258
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700259 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700260 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700261
Winson Chung46353de2012-02-16 14:05:10 -0800262 // We only want to get the SharedPreferences once since it does an FS stat each time we get
263 // it from the context.
264 private SharedPreferences mSharedPrefs;
265
Winson Chungf0c6ae02012-03-21 16:10:31 -0700266 // Holds the page that we need to animate to, and the icon views that we need to animate up
267 // when we scroll to that page on resume.
268 private int mNewShortcutAnimatePage = -1;
269 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700270
Michael Jurkaddd62e92011-02-16 17:49:14 -0800271 private BubbleTextView mWaitingForResume;
272
Michael Jurkac1f5d262011-09-30 19:32:27 -0700273 private Runnable mBuildLayersRunnable = new Runnable() {
274 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700275 if (mWorkspace != null) {
276 mWorkspace.buildPageHardwareLayers();
277 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700278 }
279 };
280
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281 private static ArrayList<PendingAddArguments> sPendingAddList
282 = new ArrayList<PendingAddArguments>();
283
284 private static class PendingAddArguments {
285 int requestCode;
286 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700287 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800288 int screen;
289 int cellX;
290 int cellY;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 @Override
294 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700295 if (DEBUG_STRICT_MODE) {
296 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
297 .detectDiskReads()
298 .detectDiskWrites()
299 .detectNetwork() // or .detectAll() for all detectable problems
300 .penaltyLog()
301 .build());
302 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
303 .detectLeakedSqlLiteObjects()
304 .detectLeakedClosableObjects()
305 .penaltyLog()
306 .penaltyDeath()
307 .build());
308 }
309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800311 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
313 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800314 mModel = app.setLauncher(this);
315 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400316 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700318
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700319 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700320 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
321 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700322
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200324 android.os.Debug.startMethodTracing(
325 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 }
327
328 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 setContentView(R.layout.launcher);
330 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700331 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800333 registerContentObservers();
334
Joe Onorato7c312c12009-08-13 21:36:53 -0700335 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 mSavedState = savedInstanceState;
338 restoreState(mSavedState);
339
Winson Chunga12a2502010-12-20 14:41:35 -0800340 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700341 if (mAppsCustomizeContent != null) {
342 mAppsCustomizeContent.onPackagesUpdated();
343 }
Winson Chunga12a2502010-12-20 14:41:35 -0800344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 if (PROFILE_STARTUP) {
346 android.os.Debug.stopMethodTracing();
347 }
348
349 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700350 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
Michael Jurkac57b7a82011-08-09 22:02:20 -0700353 if (!mModel.isAllAppsLoaded()) {
354 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
355 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // For handling default keys
359 mDefaultKeySsb = new SpannableStringBuilder();
360 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800361
362 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
363 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800364
Winson Chungc51db6a2011-10-05 11:44:49 -0700365 boolean searchVisible = false;
366 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700368 int coi = getCurrentOrientationIndexForGlobalIcons();
369 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
370 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700371 updateAppMarketIcon();
372 searchVisible = updateGlobalSearchIcon();
373 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700375 if (sGlobalSearchIcon[coi] != null) {
376 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700377 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 if (sVoiceSearchIcon[coi] != null) {
380 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700381 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700382 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700383 if (sAppMarketIcon[coi] != null) {
384 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800385 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700386 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700387
Jim Miller14091b12012-04-24 19:27:54 -0700388 final String forceEnableRotation =
Adam Cohen23a4d302011-12-01 17:26:44 -0800389 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
390
Adam Cohencb3f98e2012-04-24 14:24:35 -0700391 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
392
Adam Cohen446e9402011-09-15 18:21:21 -0700393 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700394 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700395 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
396 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
Romain Guycbb89e42009-06-08 15:52:54 -0700398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 if (sLocaleConfiguration == null) {
401 new AsyncTask<Void, Void, LocaleConfiguration>() {
402 @Override
403 protected LocaleConfiguration doInBackground(Void... unused) {
404 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
405 readConfiguration(Launcher.this, localeConfiguration);
406 return localeConfiguration;
407 }
408
409 @Override
410 protected void onPostExecute(LocaleConfiguration result) {
411 sLocaleConfiguration = result;
412 checkForLocaleChange(); // recursive, but now with a locale configuration
413 }
414 }.execute();
415 return;
416 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700417
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 final Configuration configuration = getResources().getConfiguration();
419
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700420 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 final String locale = configuration.locale.toString();
422
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700423 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 final int mcc = configuration.mcc;
425
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700426 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 final int mnc = configuration.mnc;
428
Romain Guy84f296c2009-11-04 15:00:44 -0800429 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430
Romain Guy84f296c2009-11-04 15:00:44 -0800431 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432 sLocaleConfiguration.locale = locale;
433 sLocaleConfiguration.mcc = mcc;
434 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700435
Joe Onorato0589f0f2010-02-08 13:44:00 -0800436 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700437
438 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
439 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700440 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700441 public void run() {
442 writeConfiguration(Launcher.this, localeConfiguration);
443 }
444 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700445 }
446 }
447
448 private static class LocaleConfiguration {
449 public String locale;
450 public int mcc = -1;
451 public int mnc = -1;
452 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700453
Romain Guy98d01652009-06-30 16:21:04 -0700454 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
455 DataInputStream in = null;
456 try {
457 in = new DataInputStream(context.openFileInput(PREFERENCES));
458 configuration.locale = in.readUTF();
459 configuration.mcc = in.readInt();
460 configuration.mnc = in.readInt();
461 } catch (FileNotFoundException e) {
462 // Ignore
463 } catch (IOException e) {
464 // Ignore
465 } finally {
466 if (in != null) {
467 try {
468 in.close();
469 } catch (IOException e) {
470 // Ignore
471 }
472 }
473 }
474 }
475
476 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
477 DataOutputStream out = null;
478 try {
479 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
480 out.writeUTF(configuration.locale);
481 out.writeInt(configuration.mcc);
482 out.writeInt(configuration.mnc);
483 out.flush();
484 } catch (FileNotFoundException e) {
485 // Ignore
486 } catch (IOException e) {
487 //noinspection ResultOfMethodCallIgnored
488 context.getFileStreamPath(PREFERENCES).delete();
489 } finally {
490 if (out != null) {
491 try {
492 out.close();
493 } catch (IOException e) {
494 // Ignore
495 }
496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 }
498 }
499
Adam Cohen716b51e2011-06-30 12:09:54 -0700500 public DragLayer getDragLayer() {
501 return mDragLayer;
502 }
503
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 static int getScreen() {
505 synchronized (sLock) {
506 return sScreen;
507 }
508 }
509
510 static void setScreen(int screen) {
511 synchronized (sLock) {
512 sScreen = screen;
513 }
514 }
515
Winson Chung557d6ed2011-07-08 15:34:52 -0700516 /**
517 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
518 * a configuration step, this allows the proper animations to run after other transitions.
519 */
520 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700521 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 switch (args.requestCode) {
523 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700524 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
525 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 break;
527 case REQUEST_PICK_SHORTCUT:
528 processShortcut(args.intent);
529 break;
530 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700531 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
532 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700533 result = true;
534 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800535 case REQUEST_PICK_APPWIDGET:
536 addAppWidgetFromPick(args.intent);
537 break;
538 case REQUEST_CREATE_APPWIDGET:
539 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800540 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700541 result = true;
542 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800543 case REQUEST_PICK_WALLPAPER:
544 // We just wanted the activity result here so we can clear mWaitingForResult
545 break;
546 }
Michael Jurka27614d22012-04-02 06:40:22 -0700547 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
548 // if you turned the screen off and then back while in All Apps, Launcher would not
549 // return to the workspace. Clearing mAddInfo.container here fixes this issue
550 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700551 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800552 }
553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700555 protected void onActivityResult(
556 final int requestCode, final int resultCode, final Intent data) {
557 if (requestCode == REQUEST_BIND_APPWIDGET) {
558 int appWidgetId = data != null ?
559 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
560 if (resultCode == RESULT_CANCELED) {
561 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
562 } else if (resultCode == RESULT_OK) {
563 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
564 }
565 return;
566 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700567 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800568 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
569 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700570 mWaitingForResult = false;
571
Adam Cohened66b2b2012-01-23 17:28:51 -0800572 // We have special handling for widgets
573 if (isWidgetDrop) {
574 int appWidgetId = data != null ?
575 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700576 if (appWidgetId < 0) {
577 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
578 "widget configuration activity.");
579 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
580 } else {
581 completeTwoStageWidgetDrop(resultCode, appWidgetId);
582 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800583 return;
584 }
585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 // The pattern used here is that a user PICKs a specific application,
587 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
590 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700591 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 final PendingAddArguments args = new PendingAddArguments();
593 args.requestCode = requestCode;
594 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700595 args.container = mPendingAddInfo.container;
596 args.screen = mPendingAddInfo.screen;
597 args.cellX = mPendingAddInfo.cellX;
598 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 if (isWorkspaceLocked()) {
600 sPendingAddList.add(args);
601 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700602 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700606 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
608 null);
609 }
610
611 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700612 CellLayout cellLayout =
613 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800614 Runnable onCompleteRunnable = null;
615 int animationType = 0;
616
617 if (resultCode == RESULT_OK) {
618 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
619 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700620 mWidgetBeingBoundOrConfigured.info);
621 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 onCompleteRunnable = new Runnable() {
623 @Override
624 public void run() {
625 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
626 mPendingAddInfo.screen, layout, null);
627 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
628 null);
629 }
630 };
631 } else if (resultCode == RESULT_CANCELED) {
632 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
633 onCompleteRunnable = new Runnable() {
634 @Override
635 public void run() {
636 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
637 null);
638 }
639 };
640 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700641 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800642 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700643 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
644 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
646
647 @Override
648 protected void onResume() {
649 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700650
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800651 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700652 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700654 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700656 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
Winson Chunge4e50662012-01-23 14:45:13 -0800658
659 // Reset the pressed state of icons that were locked in the press state while activities
660 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800661 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800662 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800663 mWaitingForResume.setStayPressed(false);
664 }
Winson Chunge4e50662012-01-23 14:45:13 -0800665 if (mAppsCustomizeContent != null) {
666 // Resets the previous all apps icon press state
667 mAppsCustomizeContent.resetDrawableState();
668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
670
671 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 protected void onPause() {
673 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700674 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800675 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700676 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 }
Romain Guycbb89e42009-06-08 15:52:54 -0700678
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700679 @Override
680 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 // Flag the loader to stop early before switching
682 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 if (mAppsCustomizeContent != null) {
684 mAppsCustomizeContent.surrender();
685 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800686 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700687 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800689 // We can't hide the IME if it was forced open. So don't bother
690 /*
691 @Override
692 public void onWindowFocusChanged(boolean hasFocus) {
693 super.onWindowFocusChanged(hasFocus);
694
695 if (hasFocus) {
696 final InputMethodManager inputManager = (InputMethodManager)
697 getSystemService(Context.INPUT_METHOD_SERVICE);
698 WindowManager.LayoutParams lp = getWindow().getAttributes();
699 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
700 android.os.Handler()) {
701 protected void onReceiveResult(int resultCode, Bundle resultData) {
702 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
703 }
704 });
705 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
706 }
707 }
708 */
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 private boolean acceptFilter() {
711 final InputMethodManager inputManager = (InputMethodManager)
712 getSystemService(Context.INPUT_METHOD_SERVICE);
713 return !inputManager.isFullscreenMode();
714 }
715
716 @Override
717 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700718 final int uniChar = event.getUnicodeChar();
719 final boolean handled = super.onKeyDown(keyCode, event);
720 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
721 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
723 keyCode, event);
724 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700725 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700726 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700727 // showSearchDialog()
728 // If there are multiple keystrokes before the search dialog takes focus,
729 // onSearchRequested() will be called for every keystroke,
730 // but it is idempotent, so it's fine.
731 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733 }
734
Joe Onorato8a9625e2010-01-28 15:55:35 -0800735 // Eat the long press event so the keyboard doesn't come up.
736 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
737 return true;
738 }
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 return handled;
741 }
742
Karl Rosaen138a0412009-04-23 19:00:21 -0700743 private String getTypedText() {
744 return mDefaultKeySsb.toString();
745 }
746
747 private void clearTypedText() {
748 mDefaultKeySsb.clear();
749 mDefaultKeySsb.clearSpans();
750 Selection.setSelection(mDefaultKeySsb, 0);
751 }
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700754 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
755 * State
756 */
757 private static State intToState(int stateOrdinal) {
758 State state = State.WORKSPACE;
759 final State[] stateValues = State.values();
760 for (int i = 0; i < stateValues.length; i++) {
761 if (stateValues[i].ordinal() == stateOrdinal) {
762 state = stateValues[i];
763 break;
764 }
765 }
766 return state;
767 }
768
769 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 * Restores the previous state, if it exists.
771 *
772 * @param savedState The previous state.
773 */
774 private void restoreState(Bundle savedState) {
775 if (savedState == null) {
776 return;
777 }
778
Michael Jurka883f55b2010-10-21 15:47:14 -0700779 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700780 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800781 showAllApps(false);
782 }
783
Winson Chungf0c6ae02012-03-21 16:10:31 -0700784 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700786 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
790 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700791
Winson Chung3d503fb2011-07-13 17:25:49 -0700792 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
793 mPendingAddInfo.container = pendingAddContainer;
794 mPendingAddInfo.screen = pendingAddScreen;
795 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
796 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 mRestoring = true;
798 }
799
800 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
801 if (renameFolder) {
802 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700803 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 mRestoring = true;
805 }
Winson Chunga12a2502010-12-20 14:41:35 -0800806
Winson Chung785d2eb2011-04-14 16:08:02 -0700807
808 // Restore the AppsCustomize tab
809 if (mAppsCustomizeTabHost != null) {
810 String curTab = savedState.getString("apps_customize_currentTab");
811 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700812 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700813 mAppsCustomizeContent.setContentType(
814 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
815 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700816 mAppsCustomizeContent.loadAssociatedPages(
817 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700818 }
819
Winson Chung5afbf7b2011-07-25 11:53:08 -0700820 int currentIndex = savedState.getInt("apps_customize_currentIndex");
821 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 }
824
825 /**
826 * Finds all the views we need and configure them properly.
827 */
828 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700829 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400830
Winson Chung4c98d922011-05-31 16:50:48 -0700831 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
832 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800833 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
834 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835
Winson Chung4c98d922011-05-31 16:50:48 -0700836 // Setup the drag layer
837 mDragLayer.setup(this, dragController);
838
Winson Chung3d503fb2011-07-13 17:25:49 -0700839 // Setup the hotseat
840 mHotseat = (Hotseat) findViewById(R.id.hotseat);
841 if (mHotseat != null) {
842 mHotseat.setup(this);
843 }
844
Winson Chung4c98d922011-05-31 16:50:48 -0700845 // Setup the workspace
846 mWorkspace.setHapticFeedbackEnabled(false);
847 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700848 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700849 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700850
Winson Chungf0ea4d32011-06-06 14:27:16 -0700851 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700852 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700853
Winson Chungf0ea4d32011-06-06 14:27:16 -0700854 // Setup AppsCustomize
855 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
856 findViewById(R.id.apps_customize_pane);
857 mAppsCustomizeContent = (AppsCustomizePagedView)
858 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700859 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400861
Michael Jurka5130e402011-10-13 04:55:35 -0700862 // Get the all apps button
863 mAllAppsButton = findViewById(R.id.all_apps_button);
864 if (mAllAppsButton != null) {
865 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
866 @Override
867 public boolean onTouch(View v, MotionEvent event) {
868 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
869 onTouchDownAllAppsButton(v);
870 }
871 return false;
872 }
873 });
874 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700875 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700876 dragController.setDragScoller(mWorkspace);
877 dragController.setScrollView(mDragLayer);
878 dragController.setMoveTarget(mWorkspace);
879 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700880 if (mSearchDropTargetBar != null) {
881 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
884
885 /**
886 * Creates a view representing a shortcut.
887 *
888 * @param info The data structure describing the shortcut.
889 *
890 * @return A View inflated from R.layout.application.
891 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800892 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700894 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 }
896
897 /**
898 * Creates a view representing a shortcut inflated from the specified resource.
899 *
900 * @param layoutResId The id of the XML layout used to create the shortcut.
901 * @param parent The group the shortcut belongs to.
902 * @param info The data structure describing the shortcut.
903 *
904 * @return A View inflated from layoutResId.
905 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800906 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800907 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
908 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 return favorite;
911 }
912
913 /**
914 * Add an application shortcut to the workspace.
915 *
916 * @param data The intent describing the application.
917 * @param cellInfo The position on screen where to create the shortcut.
918 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700919 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700920 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700922
Adam Cohenfbba09b2011-07-18 21:43:05 -0700923 // First we check if we already know the exact location where we want to add this item.
924 if (cellX >= 0 && cellY >= 0) {
925 cellXY[0] = cellX;
926 cellXY[1] = cellY;
927 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700928 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700929 return;
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800932 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800933
Romain Guy73b979d2009-06-09 12:57:21 -0700934 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800937 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700939 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800940 } else {
941 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943 }
Romain Guycbb89e42009-06-08 15:52:54 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 /**
946 * Add a shortcut to the workspace.
947 *
948 * @param data The intent describing the shortcut.
949 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700951 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
952 int cellY) {
953 int[] cellXY = mTmpAddItemCellCoordinates;
954 int[] touchXY = mPendingAddInfo.dropPos;
955 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700956
Michael Jurkad3ef3062010-11-23 16:23:58 -0800957 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700958
Adam Cohen558baaf2011-08-15 15:22:57 -0700959 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800960 if (info == null) {
961 return;
962 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700963 final View view = createShortcut(info);
964
Adam Cohenfbba09b2011-07-18 21:43:05 -0700965 // First we check if we already know the exact location where we want to add this item.
966 if (cellX >= 0 && cellY >= 0) {
967 cellXY[0] = cellX;
968 cellXY[1] = cellY;
969 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700970
971 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800972 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700973 true, null,null)) {
974 return;
975 }
976 DragObject dragObject = new DragObject();
977 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800978 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
979 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700980 return;
981 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800983 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800985 foundCellSpan = (result != null);
986 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700987 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800988 }
989
990 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700991 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700992 return;
993 }
994
Adam Cohen558baaf2011-08-15 15:22:57 -0700995 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
997 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
999 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001 }
1002
Adam Cohen2f093b62012-04-30 18:59:53 -07001003 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1004 int minHeight) {
1005 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001006 // We want to account for the extra amount of padding that we are adding to the widget
1007 // to ensure that it gets the full amount of space that it has requested
1008 int requiredWidth = minWidth + padding.left + padding.right;
1009 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001010 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001011 }
1012
Adam Cohen2f093b62012-04-30 18:59:53 -07001013 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1014 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001015 }
1016
Adam Cohen2f093b62012-04-30 18:59:53 -07001017 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1018 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001019 }
1020
Adam Cohen2f093b62012-04-30 18:59:53 -07001021 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1022 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001023 }
1024
Adam Cohen2f093b62012-04-30 18:59:53 -07001025 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1026 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001027 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001028 }
1029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001033 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001036 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1037 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1038 if (appWidgetInfo == null) {
1039 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001044
Adam Cohen2f093b62012-04-30 18:59:53 -07001045 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1046 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001047
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001049 // We have saved the position to which the widget was dragged-- this really only matters
1050 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001051 int[] cellXY = mTmpAddItemCellCoordinates;
1052 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001054 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001055 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1056 cellXY[0] = mPendingAddInfo.cellX;
1057 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001058 spanXY[0] = mPendingAddInfo.spanX;
1059 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001060 foundCellSpan = true;
1061 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001064 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1065 spanXY[1], cellXY, finalSpan);
1066 spanXY[0] = finalSpan[0];
1067 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001068 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001069 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001070 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 }
1072
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001074 if (appWidgetId != -1) {
1075 // Deleting an app widget ID is a void call but writes to disk before returning
1076 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001077 new Thread("deleteAppWidgetId") {
1078 public void run() {
1079 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1080 }
1081 }.start();
1082 }
Winson Chung93eef082012-03-23 15:59:27 -07001083 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001084 return;
1085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001088 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1089 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 launcherInfo.spanX = spanXY[0];
1091 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001092 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1093 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001096 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097
1098 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001099 if (hostView == null) {
1100 // Perform actual inflation because we're live
1101 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1102 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1103 } else {
1104 // The AppWidgetHostView has already been inflated and instantiated
1105 launcherInfo.hostView = hostView;
1106 }
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001109 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001110 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1111 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001112 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001114
1115 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001117 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Adam Cohended9f8d2010-11-03 13:25:16 -07001120 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1121 @Override
1122 public void onReceive(Context context, Intent intent) {
1123 final String action = intent.getAction();
1124 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1125 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001126 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001128
Winson Chungc100e8e2011-08-09 16:02:43 -07001129 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001130 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001131 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1132 mAppsCustomizeTabHost.reset();
1133 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1136 mUserPresent = true;
1137 updateRunning();
1138 }
1139 }
1140 };
1141
1142 @Override
1143 public void onAttachedToWindow() {
1144 super.onAttachedToWindow();
1145
1146 // Listen for broadcasts related to user-presence
1147 final IntentFilter filter = new IntentFilter();
1148 filter.addAction(Intent.ACTION_SCREEN_OFF);
1149 filter.addAction(Intent.ACTION_USER_PRESENT);
1150 registerReceiver(mReceiver, filter);
1151
Adam Cohend113e0c2010-11-11 10:48:05 -08001152 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001153 mVisible = true;
1154 }
1155
1156 @Override
1157 public void onDetachedFromWindow() {
1158 super.onDetachedFromWindow();
1159 mVisible = false;
1160
Adam Cohend113e0c2010-11-11 10:48:05 -08001161 if (mAttached) {
1162 unregisterReceiver(mReceiver);
1163 mAttached = false;
1164 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001165 updateRunning();
1166 }
1167
1168 public void onWindowVisibilityChanged(int visibility) {
1169 mVisible = visibility == View.VISIBLE;
1170 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001171 // The following code used to be in onResume, but it turns out onResume is called when
1172 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1173 // is a more appropriate event to handle
1174 if (mVisible) {
1175 mAppsCustomizeTabHost.onWindowVisible();
1176 if (!mWorkspaceLoading) {
1177 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001178 // We want to let Launcher draw itself at least once before we force it to build
1179 // layers on all the workspace pages, so that transitioning to Launcher from other
1180 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1181 // a true draw so we wait until the second preDraw call to be safe
1182 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001183 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001184 // We delay the layer building a bit in order to give
1185 // other message processing a time to run. In particular
1186 // this avoids a delay in hiding the IME if it was
1187 // currently shown, because doing that may involve
1188 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001189 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001190 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001191 return true;
1192 }
1193 });
1194 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001195 // When Launcher comes back to foreground, a different Activity might be responsible for
1196 // the app market intent, so refresh the icon
1197 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001198 clearTypedText();
1199 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001200 }
1201
1202 private void sendAdvanceMessage(long delay) {
1203 mHandler.removeMessages(ADVANCE_MSG);
1204 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1205 mHandler.sendMessageDelayed(msg, delay);
1206 mAutoAdvanceSentTime = System.currentTimeMillis();
1207 }
1208
1209 private void updateRunning() {
1210 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1211 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1212 mAutoAdvanceRunning = autoAdvanceRunning;
1213 if (autoAdvanceRunning) {
1214 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1215 sendAdvanceMessage(delay);
1216 } else {
1217 if (!mWidgetsToAdvance.isEmpty()) {
1218 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1219 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1220 }
1221 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001222 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001223 }
1224 }
1225 }
1226
1227 private final Handler mHandler = new Handler() {
1228 @Override
1229 public void handleMessage(Message msg) {
1230 if (msg.what == ADVANCE_MSG) {
1231 int i = 0;
1232 for (View key: mWidgetsToAdvance.keySet()) {
1233 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1234 final int delay = mAdvanceStagger * i;
1235 if (v instanceof Advanceable) {
1236 postDelayed(new Runnable() {
1237 public void run() {
1238 ((Advanceable) v).advance();
1239 }
1240 }, delay);
1241 }
1242 i++;
1243 }
1244 sendAdvanceMessage(mAdvanceInterval);
1245 }
1246 }
1247 };
1248
1249 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001250 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1252 if (v instanceof Advanceable) {
1253 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001254 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001255 updateRunning();
1256 }
1257 }
1258
1259 void removeWidgetToAutoAdvance(View hostView) {
1260 if (mWidgetsToAdvance.containsKey(hostView)) {
1261 mWidgetsToAdvance.remove(hostView);
1262 updateRunning();
1263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001264 }
1265
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001267 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 launcherInfo.hostView = null;
1269 }
1270
Winson Chung93eef082012-03-23 15:59:27 -07001271 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1272 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1273 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001274 }
1275
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 public LauncherAppWidgetHost getAppWidgetHost() {
1277 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
Romain Guycbb89e42009-06-08 15:52:54 -07001279
Winson Chunga9abd0e2010-10-27 17:18:37 -07001280 public LauncherModel getModel() {
1281 return mModel;
1282 }
1283
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001284 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001285 getWindow().closeAllPanels();
1286
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001287 // Whatever we were doing is hereby canceled.
1288 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001289 }
1290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 @Override
1292 protected void onNewIntent(Intent intent) {
1293 super.onNewIntent(intent);
1294
1295 // Close the menu
1296 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001297 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001298 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001299
Joe Onorato14f122b2009-11-19 14:06:36 -08001300 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1301 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001302
Adam Cohenac56cff2011-09-28 20:45:37 -07001303 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001304 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001305 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001306 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1307 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001308 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001309 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001310
1311 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001312 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001313 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001314
Joe Onorato3a8820b2009-11-10 15:06:42 -08001315 final View v = getWindow().peekDecorView();
1316 if (v != null && v.getWindowToken() != null) {
1317 InputMethodManager imm = (InputMethodManager)getSystemService(
1318 INPUT_METHOD_SERVICE);
1319 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001321
Michael Jurka35aa14d2011-07-07 17:01:08 -07001322 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001323 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001324 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327 }
1328
1329 @Override
1330 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1331 // Do not call super here
1332 mSavedInstanceState = savedInstanceState;
1333 }
1334
1335 @Override
1336 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001337 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001338 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339
Michael Jurka883f55b2010-10-21 15:47:14 -07001340 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001341 // We close any open folder since it will not be re-opened, and we need to make sure
1342 // this state is reflected.
1343 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344
Winson Chung3d503fb2011-07-13 17:25:49 -07001345 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1346 mWaitingForResult) {
1347 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1348 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1349 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1350 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
1353 if (mFolderInfo != null && mWaitingForResult) {
1354 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1355 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1356 }
Michael Jurka946ad472010-07-09 18:05:18 -07001357
Winson Chung785d2eb2011-04-14 16:08:02 -07001358 // Save the current AppsCustomize tab
1359 if (mAppsCustomizeTabHost != null) {
1360 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1361 if (currentTabTag != null) {
1362 outState.putString("apps_customize_currentTab", currentTabTag);
1363 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001364 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1365 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368
1369 @Override
1370 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001372
Winson Chunge7a03942011-08-05 15:05:12 -07001373 // Remove all pending runnables
1374 mHandler.removeMessages(ADVANCE_MSG);
1375 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001376 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001377
Winson Chungcd2b0142011-06-08 16:02:26 -07001378 // Stop callbacks from LauncherModel
1379 LauncherApplication app = ((LauncherApplication) getApplication());
1380 mModel.stopLoader();
1381 app.setLauncher(null);
1382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001384 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001386 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001388 mAppWidgetHost = null;
1389
1390 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391
1392 TextKeyListener.getInstance().release();
1393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001396
1397 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001398 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001399
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001400 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001401 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1402 mWorkspace.removeAllViews();
1403 mWorkspace = null;
1404 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001405
1406 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
Adam Cohena9cf38f2011-05-02 15:36:58 -07001409 public DragController getDragController() {
1410 return mDragController;
1411 }
1412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 @Override
1414 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001415 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 super.startActivityForResult(intent, requestCode);
1417 }
1418
Winson Chung70d72102011-08-12 11:24:35 -07001419 /**
1420 * Indicates that we want global search for this activity by setting the globalSearch
1421 * argument for {@link #startSearch} to true.
1422 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001424 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001426
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001427 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001428
Karl Rosaen138a0412009-04-23 19:00:21 -07001429 if (initialQuery == null) {
1430 // Use any text typed in the launcher as the initial query
1431 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001432 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 if (appSearchData == null) {
1434 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001435 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001437 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001438
Karl Rosaen138a0412009-04-23 19:00:21 -07001439 final SearchManager searchManager =
1440 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001441 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001442 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444
Winson Chung70d72102011-08-12 11:24:35 -07001445 @Override
1446 public boolean onCreateOptionsMenu(Menu menu) {
1447 if (isWorkspaceLocked()) {
1448 return false;
1449 }
1450
1451 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001452
1453 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1454 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1455 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001456 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1457 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1458 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001459 String helpUrl = getString(R.string.help_url);
1460 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001461 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1462 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1463
Winson Chung70d72102011-08-12 11:24:35 -07001464 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1465 .setIcon(android.R.drawable.ic_menu_gallery)
1466 .setAlphabeticShortcut('W');
1467 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1468 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001469 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001470 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001471 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1472 .setIcon(android.R.drawable.ic_menu_preferences)
1473 .setIntent(settings)
1474 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001475 if (!helpUrl.isEmpty()) {
1476 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1477 .setIcon(android.R.drawable.ic_menu_help)
1478 .setIntent(help)
1479 .setAlphabeticShortcut('H');
1480 }
Winson Chung70d72102011-08-12 11:24:35 -07001481 return true;
1482 }
1483
1484 @Override
1485 public boolean onPrepareOptionsMenu(Menu menu) {
1486 super.onPrepareOptionsMenu(menu);
1487
1488 if (mAppsCustomizeTabHost.isTransitioning()) {
1489 return false;
1490 }
1491 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1492 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1493
1494 return true;
1495 }
1496
1497 @Override
1498 public boolean onOptionsItemSelected(MenuItem item) {
1499 switch (item.getItemId()) {
1500 case MENU_WALLPAPER_SETTINGS:
1501 startWallpaper();
1502 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001503 }
1504
1505 return super.onOptionsItemSelected(item);
1506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001508 @Override
1509 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001510 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001511 // Use a custom animation for launching search
1512 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001513 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001514 }
1515
Joe Onorato9c1289c2009-08-17 11:03:03 -04001516 public boolean isWorkspaceLocked() {
1517 return mWorkspaceLoading || mWaitingForResult;
1518 }
1519
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 mPendingAddInfo.container = ItemInfo.NO_ID;
1522 mPendingAddInfo.screen = -1;
1523 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1524 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001525 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001526 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001528
Michael Jurkaaf442092010-06-10 17:01:57 -07001529 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001530 // TODO: catch bad widget exception when sent
1531 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001532 // TODO: Is this log message meaningful?
1533 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001534 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001535 }
1536
Adam Cohened66b2b2012-01-23 17:28:51 -08001537 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1538 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001539 if (appWidget.configure != null) {
1540 // Launch over to configure widget, if needed
1541 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1542 intent.setComponent(appWidget.configure);
1543 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001544 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001545 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001546 intent.putExtra(InstallWidgetReceiver.
1547 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001548
1549 final String mimeType = info.mimeType;
1550 final ClipData clipData = (ClipData) info.configurationData;
1551 final ClipDescription clipDesc = clipData.getDescription();
1552 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1553 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001554 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001555 final CharSequence stringData = item.getText();
1556 final Uri uriData = item.getUri();
1557 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001558 final String key = InstallWidgetReceiver.
1559 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001560 if (uriData != null) {
1561 intent.putExtra(key, uriData);
1562 } else if (intentData != null) {
1563 intent.putExtra(key, intentData);
1564 } else if (stringData != null) {
1565 intent.putExtra(key, stringData);
1566 }
1567 break;
1568 }
1569 }
1570 }
Winson Chung55cef262010-10-28 14:14:18 -07001571 }
Romain Guy8e633c52010-03-04 12:51:36 -08001572 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001573 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001575 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001576 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001577 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001578 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
1580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Adam Cohenfbba09b2011-07-18 21:43:05 -07001582 /**
1583 * Process a shortcut drop.
1584 *
1585 * @param componentName The name of the component
1586 * @param screen The screen where it should be added
1587 * @param cell The cell it should be added to, optional
1588 * @param position The location on the screen where it was dropped, optional
1589 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001590 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1591 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001592 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001593 mPendingAddInfo.container = container;
1594 mPendingAddInfo.screen = screen;
1595 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596
1597 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001598 mPendingAddInfo.cellX = cell[0];
1599 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001600 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001601
1602 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1603 createShortcutIntent.setComponent(componentName);
1604 processShortcut(createShortcutIntent);
1605 }
1606
Adam Cohenfbba09b2011-07-18 21:43:05 -07001607 /**
1608 * Process a widget drop.
1609 *
1610 * @param info The PendingAppWidgetInfo of the widget being added.
1611 * @param screen The screen where it should be added
1612 * @param cell The cell it should be added to, optional
1613 * @param position The location on the screen where it was dropped, optional
1614 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001615 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001616 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001617 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001618 mPendingAddInfo.container = info.container = container;
1619 mPendingAddInfo.screen = info.screen = screen;
1620 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 mPendingAddInfo.minSpanX = info.minSpanX;
1622 mPendingAddInfo.minSpanY = info.minSpanY;
1623
Adam Cohenfbba09b2011-07-18 21:43:05 -07001624 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001625 mPendingAddInfo.cellX = cell[0];
1626 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001627 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001628 if (span != null) {
1629 mPendingAddInfo.spanX = span[0];
1630 mPendingAddInfo.spanY = span[1];
1631 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001632
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 AppWidgetHostView hostView = info.boundWidget;
1634 int appWidgetId;
1635 if (hostView != null) {
1636 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001637 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001638 } else {
1639 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001640 mWidgetBeingBoundOrConfigured = info;
1641 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1642 addAppWidgetImpl(appWidgetId, info);
1643 } else {
1644 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1645 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1646 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1647 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1648 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001649 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001650 }
1651
Joe Onoratodeb98af2010-02-19 14:59:39 -08001652 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001653 // Handle case where user selected "Applications"
1654 String applicationName = getResources().getString(R.string.group_applications);
1655 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001656
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001657 if (applicationName != null && applicationName.equals(shortcutName)) {
1658 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1659 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001660
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001661 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1662 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001663 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001664 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001665 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001666 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 }
1669
Winson Chung24ab2f12010-09-16 14:10:47 -07001670 void processWallpaper(Intent intent) {
1671 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1672 }
1673
Winson Chung3d503fb2011-07-13 17:25:49 -07001674 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1675 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001676 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 folderInfo.title = getText(R.string.folder_name);
1678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1681 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001682 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683
1684 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001685 FolderIcon newFolder =
1686 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1687 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1688 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001689 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 }
Romain Guycbb89e42009-06-08 15:52:54 -07001691
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001693 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001694 }
1695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001697 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001699 Intent chooser = Intent.createChooser(pickWallpaper,
1700 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001701 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1702 // Removed in Eclair MR1
1703// WallpaperManager wm = (WallpaperManager)
1704// getSystemService(Context.WALLPAPER_SERVICE);
1705// WallpaperInfo wi = wm.getWallpaperInfo();
1706// if (wi != null && wi.getSettingsActivity() != null) {
1707// LabeledIntent li = new LabeledIntent(getPackageName(),
1708// R.string.configure_wallpaper, 0);
1709// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1710// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1711// }
Mike Clerona0618e42009-10-22 13:55:21 -07001712 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001715 /**
1716 * Registers various content observers. The current implementation registers
1717 * only a favorites observer to keep track of the favorites applications.
1718 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001719 private void registerContentObservers() {
1720 ContentResolver resolver = getContentResolver();
1721 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1722 true, mWidgetObserver);
1723 }
1724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 @Override
1726 public boolean dispatchKeyEvent(KeyEvent event) {
1727 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1728 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001730 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001731 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001732 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001733 dumpState();
1734 return true;
1735 }
1736 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001737 }
1738 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1739 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001740 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 return true;
1742 }
1743 }
1744
1745 return super.dispatchKeyEvent(event);
1746 }
1747
Joe Onorato88ec0992009-11-19 13:16:06 -08001748 @Override
1749 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001750 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001751 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001752 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001753 Folder openFolder = mWorkspace.getOpenFolder();
1754 if (openFolder.isEditingName()) {
1755 openFolder.dismissEditingName();
1756 } else {
1757 closeFolder();
1758 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001759 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001760 mWorkspace.exitWidgetResizeMode();
1761
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001762 // Back button is a no-op here, but give at least some feedback for the button press
1763 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001764 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001765 }
1766
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001768 * Re-listen when widgets are reset.
1769 */
1770 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001771 if (mAppWidgetHost != null) {
1772 mAppWidgetHost.startListening();
1773 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001774 }
1775
1776 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1778 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001780 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001781 if (mModel != null) {
1782 mModel.unbindWorkspaceItems();
1783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 /**
1787 * Launches the intent referred by the clicked shortcut.
1788 *
1789 * @param v The view representing the clicked shortcut.
1790 */
1791 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001792 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1793 // view has detached (it's possible for this to happen if the view is removed mid touch).
1794 if (v.getWindowToken() == null) {
1795 return;
1796 }
1797
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001798 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001799 return;
1800 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001803 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001805 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001806 int[] pos = new int[2];
1807 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001808 intent.setSourceBounds(new Rect(pos[0], pos[1],
1809 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001810
1811 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001812
1813 if (success && v instanceof BubbleTextView) {
1814 mWaitingForResume = (BubbleTextView) v;
1815 mWaitingForResume.setStayPressed(true);
1816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001818 if (v instanceof FolderIcon) {
1819 FolderIcon fi = (FolderIcon) v;
1820 handleFolderClick(fi);
1821 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001822 } else if (v == mAllAppsButton) {
1823 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001824 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001825 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001826 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
1829 }
1830
Michael Jurka0e260592010-06-30 17:07:39 -07001831 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001832 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001833 // clicking anywhere on the workspace causes the customization drawer to slide down
1834 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001835 return false;
1836 }
1837
Michael Jurkaaf442092010-06-10 17:01:57 -07001838 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001839 * Event handler for the search button
1840 *
1841 * @param v The view that was clicked.
1842 */
1843 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001844 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1845
Amith Yamasania135ba82011-08-09 17:42:01 -07001846 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001847 }
1848
1849 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001850 * Event handler for the voice button
1851 *
1852 * @param v The view that was clicked.
1853 */
1854 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001855 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001856
Michael Jurkaae65ee32012-04-30 11:45:54 -07001857 try {
1858 final SearchManager searchManager =
1859 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1860 ComponentName activityName = searchManager.getGlobalSearchActivity();
1861 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1862 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1863 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1864 if (activityName != null) {
1865 intent.setPackage(activityName.getPackageName());
1866 }
1867 startActivitySafely(null, intent, "onClickVoiceButton");
1868 } catch (ActivityNotFoundException e) {
1869 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1870 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1871 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1872 startActivitySafely(null, intent, "onClickVoiceButton");
1873 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001874 }
1875
1876 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001877 * Event handler for the "grid" button that appears on the home screen, which
1878 * enters all apps mode.
1879 *
1880 * @param v The view that was clicked.
1881 */
1882 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001883 showAllApps(true);
1884 }
1885
1886 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001887 // Provide the same haptic feedback that the system offers for virtual keys.
1888 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001889 }
1890
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001891 public void onClickAppMarketButton(View v) {
1892 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001893 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001894 } else {
1895 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001896 }
1897 }
1898
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001899 void startApplicationDetailsActivity(ComponentName componentName) {
1900 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001901 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1902 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001903 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001904 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001905 }
1906
Patrick Dubroy5539af72010-09-07 15:22:01 -07001907 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1908 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1909 // System applications cannot be installed. For now, show a toast explaining that.
1910 // We may give them the option of disabling apps this way.
1911 int messageId = R.string.uninstall_system_app_text;
1912 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1913 } else {
1914 String packageName = appInfo.componentName.getPackageName();
1915 String className = appInfo.componentName.getClassName();
1916 Intent intent = new Intent(
1917 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001918 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1919 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001920 startActivity(intent);
1921 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001922 }
1923
Winson Chungc7450e32012-04-17 17:34:08 -07001924 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001928 if (v != null) {
1929 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1930 v.getMeasuredWidth(), v.getMeasuredHeight());
1931
1932 startActivity(intent, opts.toBundle());
1933 } else {
1934 startActivity(intent);
1935 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001936 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 } catch (ActivityNotFoundException e) {
1938 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001939 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 } catch (SecurityException e) {
1941 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001942 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001944 "or use the exported attribute for this activity. "
1945 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001947 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001949
Romain Guy8e633c52010-03-04 12:51:36 -08001950 void startActivityForResultSafely(Intent intent, int requestCode) {
1951 try {
1952 startActivityForResult(intent, requestCode);
1953 } catch (ActivityNotFoundException e) {
1954 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1955 } catch (SecurityException e) {
1956 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1957 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1958 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1959 "or use the exported attribute for this activity.", e);
1960 }
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Adam Cohena9cf38f2011-05-02 15:36:58 -07001963 private void handleFolderClick(FolderIcon folderIcon) {
1964 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001965 Folder openFolder = mWorkspace.getFolderForTag(info);
1966
1967 // If the folder info reports that the associated folder is open, then verify that
1968 // it is actually opened. There have been a few instances where this gets out of sync.
1969 if (info.opened && openFolder == null) {
1970 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1971 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1972 info.opened = false;
1973 }
1974
Adam Cohena9cf38f2011-05-02 15:36:58 -07001975 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 // Close any open folder
1977 closeFolder();
1978 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001979 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 } else {
1981 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 int folderScreen;
1983 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001984 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 // .. and close it
1986 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001987 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 // Close any folder open on the current screen
1989 closeFolder();
1990 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
1993 }
1994 }
1995 }
1996
Adam Cohen2801caf2011-05-13 20:57:39 -07001997 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001998 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2000 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2001 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2002
Adam Cohenc51934b2011-07-26 21:07:43 -07002003 FolderInfo info = (FolderInfo) fi.getTag();
2004 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2005 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002006 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2007 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002008 }
2009
Adam Cohen2801caf2011-05-13 20:57:39 -07002010 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2011 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2012 oa.start();
2013 }
2014
2015 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002016 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002017 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2018 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2019 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2020
Adam Cohenc51934b2011-07-26 21:07:43 -07002021 FolderInfo info = (FolderInfo) fi.getTag();
2022 CellLayout cl = null;
2023 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2024 cl = (CellLayout) fi.getParent().getParent();
2025 }
2026
2027 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002028 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2029 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002030 oa.addListener(new AnimatorListenerAdapter() {
2031 @Override
2032 public void onAnimationEnd(Animator animation) {
2033 if (layout != null) {
2034 layout.clearFolderLeaveBehind();
2035 }
2036 }
2037 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002038 oa.start();
2039 }
2040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002042 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 * is animated relative to the specified View. If the View is null, no animation
2044 * is played.
2045 *
2046 * @param folderInfo The FolderInfo describing the folder to open.
2047 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002048 public void openFolder(FolderIcon folderIcon) {
2049 Folder folder = folderIcon.mFolder;
2050 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
Adam Cohen2801caf2011-05-13 20:57:39 -07002052 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002053 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054
Adam Cohen4554ee12011-08-03 16:13:21 -07002055 // Just verify that the folder hasn't already been added to the DragLayer.
2056 // There was a one-off crash where the folder had a parent already.
2057 if (folder.getParent() == null) {
2058 mDragLayer.addView(folder);
2059 mDragController.addDropTarget((DropTarget) folder);
2060 } else {
2061 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2062 folder.getParent() + ").");
2063 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002064 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002065 }
2066
2067 public void closeFolder() {
2068 Folder folder = mWorkspace.getOpenFolder();
2069 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002070 if (folder.isEditingName()) {
2071 folder.dismissEditingName();
2072 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002073 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002074
2075 // Dismiss the folder cling
2076 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002077 }
2078 }
2079
2080 void closeFolder(Folder folder) {
2081 folder.getInfo().opened = false;
2082
2083 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2084 if (parent != null) {
2085 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2086 shrinkAndFadeInFolderIcon(fi);
2087 }
2088 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002092
Patrick Dubroye708c522011-03-01 16:03:43 -08002093 if (mState != State.WORKSPACE) {
2094 return false;
2095 }
2096
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 return false;
2099 }
2100
2101 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002102 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104
Michael Jurka0280c3b2010-09-17 15:00:07 -07002105 resetAddInfo();
2106 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002108 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 return true;
2110 }
2111
Winson Chung3d503fb2011-07-13 17:25:49 -07002112 // The hotseat touch handling does not go through Workspace, and we always allow long press
2113 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002114 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2116 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002117 if (itemUnderLongClick == null) {
2118 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002119 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2120 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002121 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002123 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002125 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127 }
2128 }
2129 return true;
2130 }
2131
Winson Chung3d503fb2011-07-13 17:25:49 -07002132 boolean isHotseatLayout(View layout) {
2133 return mHotseat != null && layout != null &&
2134 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2135 }
2136 Hotseat getHotseat() {
2137 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002138 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002139 SearchDropTargetBar getSearchBar() {
2140 return mSearchDropTargetBar;
2141 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002142
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 /**
2144 * Returns the CellLayout of the specified container at the specified screen.
2145 */
2146 CellLayout getCellLayout(long container, int screen) {
2147 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2148 if (mHotseat != null) {
2149 return mHotseat.getLayout();
2150 } else {
2151 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002152 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002153 } else {
2154 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002155 }
2156 }
2157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 Workspace getWorkspace() {
2159 return mWorkspace;
2160 }
2161
Daniel Sandler843e8602010-06-07 14:59:01 -04002162 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2163 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002164 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002165 }
2166
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002167 public boolean isAllAppsButtonRank(int rank) {
2168 return mHotseat.isAllAppsButtonRank(rank);
2169 }
2170
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002171 // AllAppsView.Watcher
2172 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002173 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002174 mWorkspace.setVisibility(View.GONE);
2175 }
2176 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002177
2178 /**
2179 * Helper method for the cameraZoomIn/cameraZoomOut animations
2180 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002181 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002182 * @param scaleFactor The scale factor used for the zoom
2183 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002184 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002185 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002186 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002187 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002188
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002189 void updateWallpaperVisibility(boolean visible) {
2190 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2191 int curflags = getWindow().getAttributes().flags
2192 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2193 if (wpflags != curflags) {
2194 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2195 }
2196 }
2197
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002198 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2199 if (v instanceof LauncherTransitionable) {
2200 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2201 }
2202 }
2203
Michael Jurkabed61d22012-02-14 22:51:29 -08002204 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2205 if (v instanceof LauncherTransitionable) {
2206 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2207 }
Winson Chung70442722012-02-10 15:43:22 -08002208
2209 // Update the workspace transition step as well
2210 dispatchOnLauncherTransitionStep(v, 0f);
2211 }
2212
2213 private void dispatchOnLauncherTransitionStep(View v, float t) {
2214 if (v instanceof LauncherTransitionable) {
2215 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2216 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002217 }
2218
2219 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2220 if (v instanceof LauncherTransitionable) {
2221 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2222 }
Winson Chung70442722012-02-10 15:43:22 -08002223
2224 // Update the workspace transition step as well
2225 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002226 }
2227
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002228 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002229 * Things to test when changing the following seven functions.
2230 * - Home from workspace
2231 * - from center screen
2232 * - from other screens
2233 * - Home from all apps
2234 * - from center screen
2235 * - from other screens
2236 * - Back from all apps
2237 * - from center screen
2238 * - from other screens
2239 * - Launch app from workspace and quit
2240 * - with back
2241 * - with home
2242 * - Launch app from all apps and quit
2243 * - with back
2244 * - with home
2245 * - Go to a screen that's not the default, then all
2246 * apps, and launch and app, and go back
2247 * - with back
2248 * -with home
2249 * - On workspace, long press power and go back
2250 * - with back
2251 * - with home
2252 * - On all apps, long press power and go back
2253 * - with back
2254 * - with home
2255 * - On workspace, power off
2256 * - On all apps, power off
2257 * - Launch an app and turn off the screen while in that app
2258 * - Go back with home key
2259 * - Go back with back key TODO: make this not go to workspace
2260 * - From all apps
2261 * - From workspace
2262 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2263 * - From all apps
2264 * - From the center workspace
2265 * - From another workspace
2266 */
2267
2268 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002269 * Zoom the camera out from the workspace to reveal 'toView'.
2270 * Assumes that the view to show is anchored at either the very top or very bottom
2271 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002272 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002273 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002274 if (mStateAnimation != null) {
2275 mStateAnimation.cancel();
2276 mStateAnimation = null;
2277 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002278 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002279
Winson Chungf0ea4d32011-06-06 14:27:16 -07002280 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2281 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2282 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002283 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002284 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002285 final int startDelay =
2286 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002287
Michael Jurkab3e22d92011-10-31 15:58:33 -07002288 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002289
Michael Jurkad74c9842011-07-10 12:44:21 -07002290 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002291 Animator workspaceAnim =
2292 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002293
2294 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002295 toView.setScaleX(scale);
2296 toView.setScaleY(scale);
2297 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2298 scaleAnim.
2299 scaleX(1f).scaleY(1f).
2300 setDuration(duration).
2301 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002302
Winson Chungf0ea4d32011-06-06 14:27:16 -07002303 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002304 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002305 final ObjectAnimator alphaAnim = ObjectAnimator
2306 .ofFloat(toView, "alpha", 0f, 1f)
2307 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002308 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002309 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2310 @Override
2311 public void onAnimationUpdate(ValueAnimator animation) {
2312 float t = (Float) animation.getAnimatedValue();
2313 dispatchOnLauncherTransitionStep(fromView, t);
2314 dispatchOnLauncherTransitionStep(toView, t);
2315 }
2316 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002317
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002318 // toView should appear right at the end of the workspace shrink
2319 // animation
2320 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002321 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002322 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002323
2324 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002325 boolean animationCancelled = false;
2326
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002327 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002328 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002329 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002330 // Prepare the position
2331 toView.setTranslationX(0.0f);
2332 toView.setTranslationY(0.0f);
2333 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002334 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002335 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002336 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002337 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002338 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2339 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002340
2341 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2342 // Hide the workspace scrollbar
2343 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002344 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002345 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002346 if (!animationCancelled) {
2347 updateWallpaperVisibility(false);
2348 }
2349 }
2350
Adam Cohencff6af82011-09-13 14:51:53 -07002351 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002352 public void onAnimationCancel(Animator animation) {
2353 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002354 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355 });
2356
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002357 if (workspaceAnim != null) {
2358 mStateAnimation.play(workspaceAnim);
2359 }
Michael Jurka1899a362011-11-03 13:50:45 -07002360
2361 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002362 final ViewTreeObserver observer;
2363
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002364 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2365 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002366
2367 // If any of the objects being animated haven't been measured/laid out
2368 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002369 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002370 (mWorkspace.getMeasuredWidth() == 0) ||
2371 (toView.getMeasuredWidth() == 0)) {
2372 observer = mWorkspace.getViewTreeObserver();
2373 delayAnim = true;
2374 } else {
2375 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002376 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002377
2378 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002379 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002380 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2381 public void onGlobalLayout() {
2382 mWorkspace.post(new Runnable() {
2383 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002384 // Check that mStateAnimation hasn't changed while
2385 // we waited for a layout pass
2386 if (mStateAnimation == stateAnimation) {
2387 // Need to update pivots for zoom if layout changed
2388 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002389 dispatchOnLauncherTransitionStart(fromView, animated, false);
2390 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002391 mStateAnimation.start();
2392 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002393 }
2394 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002395 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002396 }
2397 };
2398 observer.addOnGlobalLayoutListener(delayedStart);
2399 } else {
2400 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002401 dispatchOnLauncherTransitionStart(fromView, animated, false);
2402 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002403 mStateAnimation.start();
2404 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002405 } else {
2406 toView.setTranslationX(0.0f);
2407 toView.setTranslationY(0.0f);
2408 toView.setScaleX(1.0f);
2409 toView.setScaleY(1.0f);
2410 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002411 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002412
Michael Jurkabed61d22012-02-14 22:51:29 -08002413 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2414 // Hide the workspace scrollbar
2415 mWorkspace.hideScrollingIndicator(true);
2416 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002417 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002418 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002419 dispatchOnLauncherTransitionStart(fromView, animated, false);
2420 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002421 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002422 dispatchOnLauncherTransitionStart(toView, animated, false);
2423 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002424 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002425 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002426 }
2427
2428 /**
2429 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002430 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002431 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002432 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002433 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2434 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002435
Michael Jurkab3e22d92011-10-31 15:58:33 -07002436 if (mStateAnimation != null) {
2437 mStateAnimation.cancel();
2438 mStateAnimation = null;
2439 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002440 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002441
Winson Chungf0ea4d32011-06-06 14:27:16 -07002442 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002443 final int fadeOutDuration =
2444 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002445 final float scaleFactor = (float)
2446 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2447 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002448 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002449 Animator workspaceAnim = null;
2450
2451 if (toState == State.WORKSPACE) {
2452 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2453 workspaceAnim = mWorkspace.getChangeStateAnimation(
2454 Workspace.State.NORMAL, animated, stagger);
2455 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2456 workspaceAnim = mWorkspace.getChangeStateAnimation(
2457 Workspace.State.SPRING_LOADED, animated);
2458 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002459
Michael Jurkab3e22d92011-10-31 15:58:33 -07002460 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002461 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002462 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002464 final LauncherViewPropertyAnimator scaleAnim =
2465 new LauncherViewPropertyAnimator(fromView);
2466 scaleAnim.
2467 scaleX(scaleFactor).scaleY(scaleFactor).
2468 setDuration(duration).
2469 setInterpolator(new Workspace.ZoomInInterpolator());
2470
2471 final ObjectAnimator alphaAnim = ObjectAnimator
2472 .ofFloat(fromView, "alpha", 1f, 0f)
2473 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002474 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002475 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2476 @Override
2477 public void onAnimationUpdate(ValueAnimator animation) {
2478 float t = 1f - (Float) animation.getAnimatedValue();
2479 dispatchOnLauncherTransitionStep(fromView, t);
2480 dispatchOnLauncherTransitionStep(toView, t);
2481 }
2482 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002483
Michael Jurkabed61d22012-02-14 22:51:29 -08002484 mStateAnimation = new AnimatorSet();
2485
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002486 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2487 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002488
2489 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002490 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002491 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002492 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002494 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2495 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002496 if (mWorkspace != null) {
2497 mWorkspace.hideScrollingIndicator(false);
2498 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002499 if (onCompleteRunnable != null) {
2500 onCompleteRunnable.run();
2501 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002502 }
2503 });
2504
Winson Chungf0ea4d32011-06-06 14:27:16 -07002505 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002506 if (workspaceAnim != null) {
2507 mStateAnimation.play(workspaceAnim);
2508 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002509 dispatchOnLauncherTransitionStart(fromView, animated, true);
2510 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002511 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512 } else {
2513 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002514 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002515 dispatchOnLauncherTransitionStart(fromView, animated, true);
2516 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002517 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002518 dispatchOnLauncherTransitionStart(toView, animated, true);
2519 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002520 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002521 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002522 }
2523
Michael Jurkae326f182011-11-21 14:05:46 -08002524 @Override
2525 public void onTrimMemory(int level) {
2526 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002527 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002528 mAppsCustomizeTabHost.onTrimMemory();
2529 }
2530 }
2531
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002533 showWorkspace(animated, null);
2534 }
2535
2536 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002537 if (mState != State.WORKSPACE) {
2538 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002539 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002540
2541 // Show the search bar and hotseat
2542 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002543 // We only need to animate in the dock divider if we're going from spring loaded mode
2544 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002545
2546 // Set focus to the AppsCustomize button
2547 if (mAllAppsButton != null) {
2548 mAllAppsButton.requestFocus();
2549 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002550 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002551
Michael Jurkab737ee62011-11-15 15:57:22 -08002552 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002553
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002554 // Change the state *after* we've called all the transition code
2555 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002556
Winson Chung5fb63472011-02-02 17:03:37 -08002557 // Resume the auto-advance of widgets
2558 mUserPresent = true;
2559 updateRunning();
2560
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002561 // send an accessibility event to announce the context change
2562 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002563 }
2564
Michael Jurkab3e22d92011-10-31 15:58:33 -07002565 void showAllApps(boolean animated) {
2566 if (mState != State.WORKSPACE) return;
2567
2568 showAppsCustomizeHelper(animated, false);
2569 mAppsCustomizeTabHost.requestFocus();
2570
2571 // Hide the search bar and hotseat
2572 mSearchDropTargetBar.hideSearchBar(animated);
2573
2574 // Change the state *after* we've called all the transition code
2575 mState = State.APPS_CUSTOMIZE;
2576
2577 // Pause the auto-advance of widgets until we are out of AllApps
2578 mUserPresent = false;
2579 updateRunning();
2580 closeFolder();
2581
2582 // Send an accessibility event to announce the context change
2583 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2584 }
2585
Adam Cohen7777d962011-08-18 18:58:38 -07002586 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002587 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002588 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002589 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002590 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002591 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002592 }
Adam Cohen7777d962011-08-18 18:58:38 -07002593
Adam Cohened66b2b2012-01-23 17:28:51 -08002594 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2595 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002596 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2597
Winson Chunge7a03942011-08-05 15:05:12 -07002598 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002599 @Override
2600 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002601 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002602 // Before we show workspace, hide all apps again because
2603 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2604 // clean up our state transition functions
2605 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002606 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002607 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002608 } else {
2609 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002610 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002611 }
2612 }, (extendedDelay ?
2613 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2614 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2615 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616
Michael Jurkad3ef3062010-11-23 16:23:58 -08002617 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002618 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002619 final boolean animated = true;
2620 final boolean springLoaded = true;
2621 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002622 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 }
2624 // Otherwise, we are not in spring loaded mode, so don't do anything.
2625 }
2626
Michael Jurkab737ee62011-11-15 15:57:22 -08002627 void hideDockDivider() {
2628 if (mQsbDivider != null && mDockDivider != null) {
2629 mQsbDivider.setVisibility(View.INVISIBLE);
2630 mDockDivider.setVisibility(View.INVISIBLE);
2631 }
2632 }
2633
2634 void showDockDivider(boolean animated) {
2635 if (mQsbDivider != null && mDockDivider != null) {
2636 mQsbDivider.setVisibility(View.VISIBLE);
2637 mDockDivider.setVisibility(View.VISIBLE);
2638 if (mDividerAnimator != null) {
2639 mDividerAnimator.cancel();
2640 mQsbDivider.setAlpha(1f);
2641 mDockDivider.setAlpha(1f);
2642 mDividerAnimator = null;
2643 }
2644 if (animated) {
2645 mDividerAnimator = new AnimatorSet();
2646 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2647 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2648 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2649 mDividerAnimator.start();
2650 }
2651 }
2652 }
2653
Michael Jurkab3e22d92011-10-31 15:58:33 -07002654 void lockAllApps() {
2655 // TODO
2656 }
2657
2658 void unlockAllApps() {
2659 // TODO
2660 }
2661
Adam Cohenfc53cd22011-07-20 15:45:11 -07002662 public boolean isAllAppsCustomizeOpen() {
2663 return mState == State.APPS_CUSTOMIZE;
2664 }
2665
Winson Chungf0ea4d32011-06-06 14:27:16 -07002666 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002668 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002669 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002670 if (!LauncherApplication.isScreenLarge()) {
2671 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002672 if (mHotseat.getAlpha() != 1f) {
2673 int duration = mSearchDropTargetBar.getTransitionInDuration();
2674 mHotseat.animate().alpha(1f).setDuration(duration);
2675 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002676 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002677 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002678 }
2679 }
2680 }
2681
2682 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002683 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002684 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002685 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002686 if (!LauncherApplication.isScreenLarge()) {
2687 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002688 if (mHotseat.getAlpha() != 0f) {
2689 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2690 mHotseat.animate().alpha(0f).setDuration(duration);
2691 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002692 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002693 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002694 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002695 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002696 }
2697
Patrick Dubroy5f445422011-02-18 14:35:21 -08002698 /**
2699 * Add an item from all apps or customize onto the given workspace screen.
2700 * If layout is null, add to the current screen.
2701 */
2702 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002703 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002704 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002705 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002706 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002707
Winson Chungdff8ebb2011-09-08 17:25:31 -07002708 /** Maps the current orientation to an index for referencing orientation correct global icons */
2709 private int getCurrentOrientationIndexForGlobalIcons() {
2710 // default - 0, landscape - 1
2711 switch (getResources().getConfiguration().orientation) {
2712 case Configuration.ORIENTATION_LANDSCAPE:
2713 return 1;
2714 default:
2715 return 0;
2716 }
2717 }
2718
Michael Jurkaae65ee32012-04-30 11:45:54 -07002719 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002720 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002721 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002722 // Look for the toolbar icon specified in the activity meta-data
2723 Bundle metaData = packageManager.getActivityInfo(
2724 activityName, PackageManager.GET_META_DATA).metaData;
2725 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002726 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002727 if (iconResId != 0) {
2728 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002729 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002730 }
2731 }
2732 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002733 // This can happen if the activity defines an invalid drawable
2734 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2735 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002736 } catch (Resources.NotFoundException nfe) {
2737 // This can happen if the activity defines an invalid drawable
2738 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2739 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002740 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002741 return null;
2742 }
2743
2744 // if successful in getting icon, return it; otherwise, set button to use default drawable
2745 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002746 int buttonId, ComponentName activityName, int fallbackDrawableId,
2747 String toolbarResourceName) {
2748 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002749 Resources r = getResources();
2750 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2751 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002752
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002753 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002754 // If we were unable to find the icon via the meta-data, use a generic one
2755 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002756 toolbarIcon = r.getDrawable(fallbackDrawableId);
2757 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002758 if (button != null) {
2759 button.setCompoundDrawables(toolbarIcon, null, null, null);
2760 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002761 return null;
2762 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002763 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002764 if (button != null) {
2765 button.setCompoundDrawables(toolbarIcon, null, null, null);
2766 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002767 return toolbarIcon.getConstantState();
2768 }
2769 }
2770
2771 // if successful in getting icon, return it; otherwise, set button to use default drawable
2772 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002773 int buttonId, ComponentName activityName, int fallbackDrawableId,
2774 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002775 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002776 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002777
Michael Jurka19e0fc52011-07-22 18:00:21 -07002778 if (button != null) {
2779 // If we were unable to find the icon via the meta-data, use a
2780 // generic one
2781 if (toolbarIcon == null) {
2782 button.setImageResource(fallbackDrawableId);
2783 } else {
2784 button.setImageDrawable(toolbarIcon);
2785 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002786 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002787
2788 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2789
Michael Jurka0423dcf2010-10-05 14:56:18 -07002790 }
2791
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002792 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2793 TextView button = (TextView) findViewById(buttonId);
2794 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2795 }
2796
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002797 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002798 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002799 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002800 }
2801
Winson Chungbb185bd2011-11-21 12:31:42 -08002802 private void invalidatePressedFocusedStates(View container, View button) {
2803 if (container instanceof HolographicLinearLayout) {
2804 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2805 layout.invalidatePressedFocusedStates();
2806 } else if (button instanceof HolographicImageView) {
2807 HolographicImageView view = (HolographicImageView) button;
2808 view.invalidatePressedFocusedStates();
2809 }
2810 }
2811
Winson Chungc51db6a2011-10-05 11:44:49 -07002812 private boolean updateGlobalSearchIcon() {
2813 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002814 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2815 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002816 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002817 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002818 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002819
Winson Chung1cad91e2011-05-25 17:41:01 -07002820 final SearchManager searchManager =
2821 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2822 ComponentName activityName = searchManager.getGlobalSearchActivity();
2823 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002824 int coi = getCurrentOrientationIndexForGlobalIcons();
2825 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002826 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2827 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2828 if (sGlobalSearchIcon[coi] == null) {
2829 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2830 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2831 TOOLBAR_ICON_METADATA_NAME);
2832 }
2833
Winson Chung649723c2011-07-06 20:41:23 -07002834 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002835 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2836 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002837 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002838 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002839 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002840 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002841 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002842 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2843 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2844 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002845 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002846 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002847 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002848 }
2849 }
2850
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002851 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002852 final View searchButtonContainer = findViewById(R.id.search_button_container);
2853 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002854 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002855 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002856 }
2857
Winson Chungc51db6a2011-10-05 11:44:49 -07002858 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002859 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002860 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002861 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002862 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002863
Winson Chungc51db6a2011-10-05 11:44:49 -07002864 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002865 final SearchManager searchManager =
2866 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2867 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2868
2869 ComponentName activityName = null;
2870 if (globalSearchActivity != null) {
2871 // Check if the global search activity handles voice search
2872 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2873 intent.setPackage(globalSearchActivity.getPackageName());
2874 activityName = intent.resolveActivity(getPackageManager());
2875 }
2876
2877 if (activityName == null) {
2878 // Fallback: check if an activity other than the global search activity
2879 // resolves this
2880 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2881 activityName = intent.resolveActivity(getPackageManager());
2882 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002883 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002884 int coi = getCurrentOrientationIndexForGlobalIcons();
2885 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002886 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2887 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
2888 if (sVoiceSearchIcon[coi] == null) {
2889 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2890 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
2891 TOOLBAR_ICON_METADATA_NAME);
2892 }
Winson Chung649723c2011-07-06 20:41:23 -07002893 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002894 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002895 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002896 if (voiceButtonProxy != null) {
2897 voiceButtonProxy.setVisibility(View.VISIBLE);
2898 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002899 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002900 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002901 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002902 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002903 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002904 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002905 if (voiceButtonProxy != null) {
2906 voiceButtonProxy.setVisibility(View.GONE);
2907 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002908 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002909 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002910 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002911
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002912 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002913 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2914 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002915 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002916 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002917 }
2918
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002919 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002920 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002921 */
2922 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002923 final View marketButton = findViewById(R.id.market_button);
2924 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2925 // Find the app market activity by resolving an intent.
2926 // (If multiple app markets are installed, it will return the ResolverActivity.)
2927 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002928 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002929 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002930 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002931 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002932 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
2933 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07002934 marketButton.setVisibility(View.VISIBLE);
2935 } else {
2936 // We should hide and disable the view so that we don't try and restore the visibility
2937 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2938 marketButton.setVisibility(View.GONE);
2939 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002940 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002941 }
2942
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002943 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002944 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002945 }
2946
Michael Jurkad7c28052012-04-27 15:43:36 -07002947 @Override
2948 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2949 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2950 final List<CharSequence> text = event.getText();
2951 text.clear();
2952 text.add(getString(R.string.home));
2953 return result;
2954 }
2955
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002956 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002957 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002958 */
2959 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2960 @Override
2961 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002962 closeSystemDialogs();
2963 }
2964 }
2965
2966 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002967 * Receives notifications whenever the appwidgets are reset.
2968 */
2969 private class AppWidgetResetObserver extends ContentObserver {
2970 public AppWidgetResetObserver() {
2971 super(new Handler());
2972 }
2973
2974 @Override
2975 public void onChange(boolean selfChange) {
2976 onAppWidgetReset();
2977 }
2978 }
2979
2980 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002981 * If the activity is currently paused, signal that we need to re-run the loader
2982 * in onResume.
2983 *
2984 * This needs to be called from incoming places where resources might have been loaded
2985 * while we are paused. That is becaues the Configuration might be wrong
2986 * when we're not running, and if it comes back to what it was when we
2987 * were paused, we are not restarted.
2988 *
2989 * Implementation of the method from LauncherModel.Callbacks.
2990 *
2991 * @return true if we are currently paused. The caller might be able to
2992 * skip some work in that case since we will come back again.
2993 */
2994 public boolean setLoadOnResume() {
2995 if (mPaused) {
2996 Log.i(TAG, "setLoadOnResume");
2997 mOnResumeNeedsLoad = true;
2998 return true;
2999 } else {
3000 return false;
3001 }
3002 }
3003
3004 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003008 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003009 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003010 } else {
3011 return SCREEN_COUNT / 2;
3012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003014
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 /**
3016 * Refreshes the shortcuts shown on the workspace.
3017 *
3018 * Implementation of the method from LauncherModel.Callbacks.
3019 */
3020 public void startBinding() {
3021 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003022
Winson Chungf0c6ae02012-03-21 16:10:31 -07003023 mNewShortcutAnimatePage = -1;
3024 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003025 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003026 int count = workspace.getChildCount();
3027 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003028 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003029 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3030 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003032 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003033 if (mHotseat != null) {
3034 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003035 }
3036 }
3037
3038 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 * Bind the items start-end from the list.
3040 *
3041 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003044 setLoadOnResume();
3045
Winson Chungf0c6ae02012-03-21 16:10:31 -07003046 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3047 Set<String> newApps = new HashSet<String>();
3048 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3049
3050 Workspace workspace = mWorkspace;
3051 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003053
3054 // Short circuit if we are loading dock items for a configuration which has no dock
3055 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3056 mHotseat == null) {
3057 continue;
3058 }
3059
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 switch (item.itemType) {
3061 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3062 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003063 ShortcutInfo info = (ShortcutInfo) item;
3064 String uri = info.intent.toUri(0).toString();
3065 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003066 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3067 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003068 if (newApps.contains(uri)) {
3069 newApps.remove(uri);
3070
3071 // Prepare the view to be animated up
3072 shortcut.setAlpha(0f);
3073 shortcut.setScaleX(0f);
3074 shortcut.setScaleY(0f);
3075 mNewShortcutAnimatePage = item.screen;
3076 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3077 mNewShortcutAnimateViews.add(shortcut);
3078 }
3079 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003081 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003082 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003083 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003084 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003085 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3086 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003090
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 }
3093
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 /**
3095 * Implementation of the method from LauncherModel.Callbacks.
3096 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003097 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003098 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003099 sFolders.clear();
3100 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102
3103 /**
3104 * Add the views for a widget to the workspace.
3105 *
3106 * Implementation of the method from LauncherModel.Callbacks.
3107 */
3108 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003109 setLoadOnResume();
3110
Daniel Sandler843e8602010-06-07 14:59:01 -04003111 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3112 if (DEBUG_WIDGETS) {
3113 Log.d(TAG, "bindAppWidget: " + item);
3114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 final Workspace workspace = mWorkspace;
3116
3117 final int appWidgetId = item.appWidgetId;
3118 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003119 if (DEBUG_WIDGETS) {
3120 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3121 }
3122
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3124
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3126 item.hostView.setTag(item);
3127
Winson Chung3d503fb2011-07-13 17:25:49 -07003128 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 item.cellY, item.spanX, item.spanY, false);
3130
Adam Cohended9f8d2010-11-03 13:25:16 -07003131 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3132
Joe Onorato9c1289c2009-08-17 11:03:03 -04003133 workspace.requestLayout();
3134
Daniel Sandler843e8602010-06-07 14:59:01 -04003135 if (DEBUG_WIDGETS) {
3136 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3137 + (SystemClock.uptimeMillis()-start) + "ms");
3138 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003139 }
3140
3141 /**
3142 * Callback saying that there aren't any more items to bind.
3143 *
3144 * Implementation of the method from LauncherModel.Callbacks.
3145 */
3146 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003147 setLoadOnResume();
3148
Joe Onorato9c1289c2009-08-17 11:03:03 -04003149 if (mSavedState != null) {
3150 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003151 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 mSavedState = null;
3154 }
3155
3156 if (mSavedInstanceState != null) {
3157 super.onRestoreInstanceState(mSavedInstanceState);
3158 mSavedInstanceState = null;
3159 }
3160
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003161 // If we received the result of any pending adds while the loader was running (e.g. the
3162 // widget configuration forced an orientation change), process them now.
3163 for (int i = 0; i < sPendingAddList.size(); i++) {
3164 completeAdd(sPendingAddList.get(i));
3165 }
3166 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167
Winson Chungc51db6a2011-10-05 11:44:49 -07003168 // Update the market app icon as necessary (the other icons will be managed in response to
3169 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003170 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003171
Winson Chungf0c6ae02012-03-21 16:10:31 -07003172 // Animate up any icons as necessary
3173 if (mVisible || mWorkspaceLoading) {
3174 Runnable newAppsRunnable = new Runnable() {
3175 @Override
3176 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003177 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003178 }
3179 };
Winson Chunga2413752012-04-03 14:22:34 -07003180
3181 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3182 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3183 if (canRunNewAppsAnimation()) {
3184 // If the user has not interacted recently, then either snap to the new page to show
3185 // the new-apps animation or just run them if they are to appear on the current page
3186 if (willSnapPage) {
3187 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3188 } else {
3189 runNewAppsAnimation(false);
3190 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003191 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003192 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003193 // are on another page (or just normally if they are added to the current page)
3194 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003195 }
3196 }
3197
3198 mWorkspaceLoading = false;
3199 }
3200
Winson Chunga2413752012-04-03 14:22:34 -07003201 private boolean canRunNewAppsAnimation() {
3202 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3203 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3204 }
3205
Winson Chungf0c6ae02012-03-21 16:10:31 -07003206 /**
3207 * Runs a new animation that scales up icons that were added while Launcher was in the
3208 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003209 *
3210 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003211 */
Winson Chunga2413752012-04-03 14:22:34 -07003212 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003213 AnimatorSet anim = new AnimatorSet();
3214 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003215
3216 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003217 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3218 @Override
3219 public int compare(View a, View b) {
3220 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3221 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3222 int cellCountX = LauncherModel.getCellCountX();
3223 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3224 }
3225 });
Winson Chunga2413752012-04-03 14:22:34 -07003226
3227 // Animate each of the views in place (or show them immediately if requested)
3228 if (immediate) {
3229 for (View v : mNewShortcutAnimateViews) {
3230 v.setAlpha(1f);
3231 v.setScaleX(1f);
3232 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003233 }
Winson Chunga2413752012-04-03 14:22:34 -07003234 } else {
3235 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3236 View v = mNewShortcutAnimateViews.get(i);
3237 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3238 PropertyValuesHolder.ofFloat("alpha", 1f),
3239 PropertyValuesHolder.ofFloat("scaleX", 1f),
3240 PropertyValuesHolder.ofFloat("scaleY", 1f));
3241 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3242 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3243 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3244 bounceAnims.add(bounceAnim);
3245 }
3246 anim.playTogether(bounceAnims);
3247 anim.addListener(new AnimatorListenerAdapter() {
3248 @Override
3249 public void onAnimationEnd(Animator animation) {
3250 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3251 }
3252 });
3253 anim.start();
3254 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003255
3256 // Clean up
3257 mNewShortcutAnimatePage = -1;
3258 mNewShortcutAnimateViews.clear();
3259 new Thread("clearNewAppsThread") {
3260 public void run() {
3261 mSharedPrefs.edit()
3262 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3263 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3264 .commit();
3265 }
3266 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003267 }
3268
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003269 @Override
3270 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 boolean searchVisible = updateGlobalSearchIcon();
3272 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3273 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003274 }
3275
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003276 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 * Add the icons for all apps.
3278 *
3279 * Implementation of the method from LauncherModel.Callbacks.
3280 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003281 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3282 // Remove the progress bar entirely; we could also make it GONE
3283 // but better to remove it since we know it's not going to be used
3284 View progressBar = mAppsCustomizeTabHost.
3285 findViewById(R.id.apps_customize_progress_bar);
3286 if (progressBar != null) {
3287 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003288 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003289 // We just post the call to setApps so the user sees the progress bar
3290 // disappear-- otherwise, it just looks like the progress bar froze
3291 // which doesn't look great
3292 mAppsCustomizeTabHost.post(new Runnable() {
3293 public void run() {
3294 if (mAppsCustomizeContent != null) {
3295 mAppsCustomizeContent.setApps(apps);
3296 }
3297 }
3298 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003299 }
3300
3301 /**
3302 * A package was installed.
3303 *
3304 * Implementation of the method from LauncherModel.Callbacks.
3305 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003306 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003307 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003308
Winson Chung785d2eb2011-04-14 16:08:02 -07003309 if (mAppsCustomizeContent != null) {
3310 mAppsCustomizeContent.addApps(apps);
3311 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 }
3313
3314 /**
3315 * A package was updated.
3316 *
3317 * Implementation of the method from LauncherModel.Callbacks.
3318 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003319 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003320 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003321 if (mWorkspace != null) {
3322 mWorkspace.updateShortcuts(apps);
3323 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003324
Winson Chung785d2eb2011-04-14 16:08:02 -07003325 if (mAppsCustomizeContent != null) {
3326 mAppsCustomizeContent.updateApps(apps);
3327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 }
3329
3330 /**
3331 * A package was uninstalled.
3332 *
3333 * Implementation of the method from LauncherModel.Callbacks.
3334 */
Joe Onorato36115782010-06-17 13:28:48 -04003335 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003336 if (permanent) {
3337 mWorkspace.removeItems(apps);
3338 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003339
Winson Chung785d2eb2011-04-14 16:08:02 -07003340 if (mAppsCustomizeContent != null) {
3341 mAppsCustomizeContent.removeApps(apps);
3342 }
Winson Chunga1820962011-10-03 16:31:06 -07003343
3344 // Notify the drag controller
3345 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003346 }
Joe Onoratobe386092009-11-17 17:32:16 -08003347
3348 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003349 * A number of packages were updated.
3350 */
3351 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003352 if (mAppsCustomizeContent != null) {
3353 mAppsCustomizeContent.onPackagesUpdated();
3354 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003355 }
3356
Winson Chung400438b2011-01-16 17:53:48 -08003357 private int mapConfigurationOriActivityInfoOri(int configOri) {
3358 final Display d = getWindowManager().getDefaultDisplay();
3359 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3360 switch (d.getRotation()) {
3361 case Surface.ROTATION_0:
3362 case Surface.ROTATION_180:
3363 // We are currently in the same basic orientation as the natural orientation
3364 naturalOri = configOri;
3365 break;
3366 case Surface.ROTATION_90:
3367 case Surface.ROTATION_270:
3368 // We are currently in the other basic orientation to the natural orientation
3369 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3370 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3371 break;
3372 }
3373
3374 int[] oriMap = {
3375 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3376 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3377 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3378 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3379 };
3380 // Since the map starts at portrait, we need to offset if this device's natural orientation
3381 // is landscape.
3382 int indexOffset = 0;
3383 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3384 indexOffset = 1;
3385 }
3386 return oriMap[(d.getRotation() + indexOffset) % 4];
3387 }
Adam Cohen446e9402011-09-15 18:21:21 -07003388
Winson Chung641d71d2012-04-26 15:58:01 -07003389 public void lockScreenOrientation() {
3390 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3391 .getConfiguration().orientation));
Winson Chung400438b2011-01-16 17:53:48 -08003392 }
Winson Chung641d71d2012-04-26 15:58:01 -07003393 public void unlockScreenOrientation() {
3394 mHandler.postDelayed(new Runnable() {
3395 public void run() {
3396 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3397 }
3398 }, mRestoreScreenOrientationDelay);
Winson Chung400438b2011-01-16 17:53:48 -08003399 }
3400
Winson Chung82f55532011-08-09 14:14:23 -07003401 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003402 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003403 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003404 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003405
Winson Chung7d7541e2011-09-16 20:14:36 -07003406 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003407 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003408 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3409 Cling cling = (Cling) findViewById(clingId);
3410 if (cling != null) {
3411 cling.init(this, positionData);
3412 cling.setVisibility(View.VISIBLE);
3413 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3414 if (animate) {
3415 cling.buildLayer();
3416 cling.setAlpha(0f);
3417 cling.animate()
3418 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003419 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003420 .setDuration(SHOW_CLING_DURATION)
3421 .setStartDelay(delay)
3422 .start();
3423 } else {
3424 cling.setAlpha(1f);
3425 }
3426 }
3427 return cling;
3428 }
3429 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003430 if (cling != null) {
3431 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003432 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003433 anim.addListener(new AnimatorListenerAdapter() {
3434 public void onAnimationEnd(Animator animation) {
3435 cling.setVisibility(View.GONE);
3436 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003437 // We should update the shared preferences on a background thread
3438 new Thread("dismissClingThread") {
3439 public void run() {
3440 SharedPreferences.Editor editor = mSharedPrefs.edit();
3441 editor.putBoolean(flag, true);
3442 editor.commit();
3443 }
3444 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003445 };
3446 });
3447 anim.start();
3448 }
3449 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003450 private void removeCling(int id) {
3451 final View cling = findViewById(id);
3452 if (cling != null) {
3453 final ViewGroup parent = (ViewGroup) cling.getParent();
3454 parent.post(new Runnable() {
3455 @Override
3456 public void run() {
3457 parent.removeView(cling);
3458 }
3459 });
3460 }
3461 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003463 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003464 if (isClingsEnabled() &&
3465 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003466 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003467 } else {
3468 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003469 }
3470 }
3471 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003472 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003473 if (isClingsEnabled() &&
3474 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003475 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003476 } else {
3477 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003478 }
3479 }
3480 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003481 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003482 if (isClingsEnabled() &&
3483 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3484 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003485 } else {
3486 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003487 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003488 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003489 }
3490 public boolean isFolderClingVisible() {
3491 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003492 if (cling != null) {
3493 return cling.getVisibility() == View.VISIBLE;
3494 }
3495 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003496 }
Winson Chung82f55532011-08-09 14:14:23 -07003497 public void dismissWorkspaceCling(View v) {
3498 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003499 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003500 }
3501 public void dismissAllAppsCling(View v) {
3502 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003503 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3504 }
3505 public void dismissFolderCling(View v) {
3506 Cling cling = (Cling) findViewById(R.id.folder_cling);
3507 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003508 }
3509
Winson Chung80baf5a2010-08-09 16:03:15 -07003510 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003511 * Prints out out state for debugging.
3512 */
3513 public void dumpState() {
3514 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003515 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003516 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3517 Log.d(TAG, "mRestoring=" + mRestoring);
3518 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3519 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003520 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003521 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522
Winson Chung785d2eb2011-04-14 16:08:02 -07003523 if (mAppsCustomizeContent != null) {
3524 mAppsCustomizeContent.dumpState();
3525 }
Joe Onoratobe386092009-11-17 17:32:16 -08003526 Log.d(TAG, "END launcher2 dump state");
3527 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003528
3529 @Override
3530 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3531 super.dump(prefix, fd, writer, args);
3532 writer.println(" ");
3533 writer.println("Debug logs: ");
3534 for (int i = 0; i < sDumpLogs.size(); i++) {
3535 writer.println(" " + sDumpLogs.get(i));
3536 }
3537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003538}
Michael Jurka2763be32011-02-24 11:19:57 -08003539
Michael Jurkaabded662011-03-04 12:06:57 -08003540interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003541 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003542 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003543 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003544 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003545 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003546}