blob: 028a4515a8d1bcd37e711699342e87e21551ca35 [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";
165
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700166 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700168 private State mState = State.WORKSPACE;
169 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800170 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700171
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700173 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
174 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700175 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800179 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chunga2413752012-04-03 14:22:34 -0700181 // How long to wait before the new-shortcut animation automatically pans the workspace
182 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
183
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800184 private final BroadcastReceiver mCloseSystemDialogsReceiver
185 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800186 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private LayoutInflater mInflater;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800191 private View mQsbDivider;
192 private View mDockDivider;
193 private DragLayer mDragLayer;
194 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 private AppWidgetManager mAppWidgetManager;
197 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Michael Jurkac9d95c52011-08-29 14:03:34 -0700199 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 private int[] mTmpAddItemCellCoordinates = new int[2];
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private FolderInfo mFolderInfo;
203
Winson Chung3d503fb2011-07-13 17:25:49 -0700204 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800205 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700206
Winson Chungc51db6a2011-10-05 11:44:49 -0700207 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700208 private AppsCustomizeTabHost mAppsCustomizeTabHost;
209 private AppsCustomizePagedView mAppsCustomizeContent;
210 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private Bundle mSavedState;
213
214 private SpannableStringBuilder mDefaultKeySsb = null;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private boolean mWorkspaceLoading = true;
217
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800218 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private boolean mRestoring;
220 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700221 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222
223 private Bundle mSavedInstanceState;
224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800226 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800227 private boolean mUserPresent = true;
228 private boolean mVisible = false;
229 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700231 private static LocaleConfiguration sLocaleConfiguration = null;
232
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700233 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700235 private Intent mAppMarketIntent = null;
236
Adam Cohended9f8d2010-11-03 13:25:16 -0700237 // Related to the auto-advancing of widgets
238 private final int ADVANCE_MSG = 1;
239 private final int mAdvanceInterval = 20000;
240 private final int mAdvanceStagger = 250;
241 private long mAutoAdvanceSentTime;
242 private long mAutoAdvanceTimeLeft = -1;
243 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
244 new HashMap<View, AppWidgetProviderInfo>();
245
Winson Chung400438b2011-01-16 17:53:48 -0800246 // Determines how long to wait after a rotation before restoring the screen orientation to
247 // match the sensor state.
248 private final int mRestoreScreenOrientationDelay = 500;
249
Michael Jurka4ef207b2010-11-29 17:05:45 -0800250 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700251 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
252 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
253 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800254
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700255 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700256 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700257
Winson Chung46353de2012-02-16 14:05:10 -0800258 // We only want to get the SharedPreferences once since it does an FS stat each time we get
259 // it from the context.
260 private SharedPreferences mSharedPrefs;
261
Winson Chungf0c6ae02012-03-21 16:10:31 -0700262 // Holds the page that we need to animate to, and the icon views that we need to animate up
263 // when we scroll to that page on resume.
264 private int mNewShortcutAnimatePage = -1;
265 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700266
Michael Jurkaddd62e92011-02-16 17:49:14 -0800267 private BubbleTextView mWaitingForResume;
268
Michael Jurkac1f5d262011-09-30 19:32:27 -0700269 private Runnable mBuildLayersRunnable = new Runnable() {
270 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700271 if (mWorkspace != null) {
272 mWorkspace.buildPageHardwareLayers();
273 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700274 }
275 };
276
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800277 private static ArrayList<PendingAddArguments> sPendingAddList
278 = new ArrayList<PendingAddArguments>();
279
280 private static class PendingAddArguments {
281 int requestCode;
282 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700283 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800284 int screen;
285 int cellX;
286 int cellY;
287 }
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 @Override
290 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700291 if (DEBUG_STRICT_MODE) {
292 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
293 .detectDiskReads()
294 .detectDiskWrites()
295 .detectNetwork() // or .detectAll() for all detectable problems
296 .penaltyLog()
297 .build());
298 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
299 .detectLeakedSqlLiteObjects()
300 .detectLeakedClosableObjects()
301 .penaltyLog()
302 .penaltyDeath()
303 .build());
304 }
305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
309 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800310 mModel = app.setLauncher(this);
311 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400312 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700314
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700316 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
317 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200320 android.os.Debug.startMethodTracing(
321 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
324 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 setContentView(R.layout.launcher);
326 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700327 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800329 registerContentObservers();
330
Joe Onorato7c312c12009-08-13 21:36:53 -0700331 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 mSavedState = savedInstanceState;
334 restoreState(mSavedState);
335
Winson Chunga12a2502010-12-20 14:41:35 -0800336 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700337 if (mAppsCustomizeContent != null) {
338 mAppsCustomizeContent.onPackagesUpdated();
339 }
Winson Chunga12a2502010-12-20 14:41:35 -0800340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 if (PROFILE_STARTUP) {
342 android.os.Debug.stopMethodTracing();
343 }
344
345 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
348
Michael Jurkac57b7a82011-08-09 22:02:20 -0700349 if (!mModel.isAllAppsLoaded()) {
350 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
351 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 // For handling default keys
355 mDefaultKeySsb = new SpannableStringBuilder();
356 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800357
358 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
359 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800360
Winson Chungc51db6a2011-10-05 11:44:49 -0700361 boolean searchVisible = false;
362 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800363 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700364 int coi = getCurrentOrientationIndexForGlobalIcons();
365 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
366 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700367 updateAppMarketIcon();
368 searchVisible = updateGlobalSearchIcon();
369 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700370 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700371 if (sGlobalSearchIcon[coi] != null) {
372 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700373 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700375 if (sVoiceSearchIcon[coi] != null) {
376 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700377 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 if (sAppMarketIcon[coi] != null) {
380 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800381 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700382 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700383
Jim Miller14091b12012-04-24 19:27:54 -0700384 final String forceEnableRotation =
Adam Cohen23a4d302011-12-01 17:26:44 -0800385 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
386
Adam Cohencb3f98e2012-04-24 14:24:35 -0700387 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
388
Adam Cohen446e9402011-09-15 18:21:21 -0700389 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700390 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700391 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
Romain Guycbb89e42009-06-08 15:52:54 -0700394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700396 if (sLocaleConfiguration == null) {
397 new AsyncTask<Void, Void, LocaleConfiguration>() {
398 @Override
399 protected LocaleConfiguration doInBackground(Void... unused) {
400 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
401 readConfiguration(Launcher.this, localeConfiguration);
402 return localeConfiguration;
403 }
404
405 @Override
406 protected void onPostExecute(LocaleConfiguration result) {
407 sLocaleConfiguration = result;
408 checkForLocaleChange(); // recursive, but now with a locale configuration
409 }
410 }.execute();
411 return;
412 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 final Configuration configuration = getResources().getConfiguration();
415
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 final String locale = configuration.locale.toString();
418
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700419 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 final int mcc = configuration.mcc;
421
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 final int mnc = configuration.mnc;
424
Romain Guy84f296c2009-11-04 15:00:44 -0800425 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426
Romain Guy84f296c2009-11-04 15:00:44 -0800427 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700428 sLocaleConfiguration.locale = locale;
429 sLocaleConfiguration.mcc = mcc;
430 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700431
Joe Onorato0589f0f2010-02-08 13:44:00 -0800432 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700433
434 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
435 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700436 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700437 public void run() {
438 writeConfiguration(Launcher.this, localeConfiguration);
439 }
440 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700441 }
442 }
443
444 private static class LocaleConfiguration {
445 public String locale;
446 public int mcc = -1;
447 public int mnc = -1;
448 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700449
Romain Guy98d01652009-06-30 16:21:04 -0700450 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
451 DataInputStream in = null;
452 try {
453 in = new DataInputStream(context.openFileInput(PREFERENCES));
454 configuration.locale = in.readUTF();
455 configuration.mcc = in.readInt();
456 configuration.mnc = in.readInt();
457 } catch (FileNotFoundException e) {
458 // Ignore
459 } catch (IOException e) {
460 // Ignore
461 } finally {
462 if (in != null) {
463 try {
464 in.close();
465 } catch (IOException e) {
466 // Ignore
467 }
468 }
469 }
470 }
471
472 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
473 DataOutputStream out = null;
474 try {
475 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
476 out.writeUTF(configuration.locale);
477 out.writeInt(configuration.mcc);
478 out.writeInt(configuration.mnc);
479 out.flush();
480 } catch (FileNotFoundException e) {
481 // Ignore
482 } catch (IOException e) {
483 //noinspection ResultOfMethodCallIgnored
484 context.getFileStreamPath(PREFERENCES).delete();
485 } finally {
486 if (out != null) {
487 try {
488 out.close();
489 } catch (IOException e) {
490 // Ignore
491 }
492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 }
494 }
495
Adam Cohen716b51e2011-06-30 12:09:54 -0700496 public DragLayer getDragLayer() {
497 return mDragLayer;
498 }
499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 static int getScreen() {
501 synchronized (sLock) {
502 return sScreen;
503 }
504 }
505
506 static void setScreen(int screen) {
507 synchronized (sLock) {
508 sScreen = screen;
509 }
510 }
511
Winson Chung557d6ed2011-07-08 15:34:52 -0700512 /**
513 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
514 * a configuration step, this allows the proper animations to run after other transitions.
515 */
516 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 switch (args.requestCode) {
519 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700520 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
521 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 break;
523 case REQUEST_PICK_SHORTCUT:
524 processShortcut(args.intent);
525 break;
526 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700527 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
528 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700529 result = true;
530 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 case REQUEST_PICK_APPWIDGET:
532 addAppWidgetFromPick(args.intent);
533 break;
534 case REQUEST_CREATE_APPWIDGET:
535 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800536 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700537 result = true;
538 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 case REQUEST_PICK_WALLPAPER:
540 // We just wanted the activity result here so we can clear mWaitingForResult
541 break;
542 }
Michael Jurka27614d22012-04-02 06:40:22 -0700543 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
544 // if you turned the screen off and then back while in All Apps, Launcher would not
545 // return to the workspace. Clearing mAddInfo.container here fixes this issue
546 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700547 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800548 }
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700551 protected void onActivityResult(
552 final int requestCode, final int resultCode, final Intent data) {
553 if (requestCode == REQUEST_BIND_APPWIDGET) {
554 int appWidgetId = data != null ?
555 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
556 if (resultCode == RESULT_CANCELED) {
557 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
558 } else if (resultCode == RESULT_OK) {
559 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
560 }
561 return;
562 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700563 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800564 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
565 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700566 mWaitingForResult = false;
567
Adam Cohened66b2b2012-01-23 17:28:51 -0800568 // We have special handling for widgets
569 if (isWidgetDrop) {
570 int appWidgetId = data != null ?
571 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700572 if (appWidgetId < 0) {
573 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
574 "widget configuration activity.");
575 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
576 } else {
577 completeTwoStageWidgetDrop(resultCode, appWidgetId);
578 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 return;
580 }
581
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 // The pattern used here is that a user PICKs a specific application,
583 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
586 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700587 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 final PendingAddArguments args = new PendingAddArguments();
589 args.requestCode = requestCode;
590 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700591 args.container = mPendingAddInfo.container;
592 args.screen = mPendingAddInfo.screen;
593 args.cellX = mPendingAddInfo.cellX;
594 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 if (isWorkspaceLocked()) {
596 sPendingAddList.add(args);
597 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700598 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700602 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
604 null);
605 }
606
607 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 CellLayout cellLayout =
609 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 Runnable onCompleteRunnable = null;
611 int animationType = 0;
612
613 if (resultCode == RESULT_OK) {
614 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
615 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700616 mWidgetBeingBoundOrConfigured.info);
617 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800618 onCompleteRunnable = new Runnable() {
619 @Override
620 public void run() {
621 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
622 mPendingAddInfo.screen, layout, null);
623 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
624 null);
625 }
626 };
627 } else if (resultCode == RESULT_CANCELED) {
628 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
629 onCompleteRunnable = new Runnable() {
630 @Override
631 public void run() {
632 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
633 null);
634 }
635 };
636 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700639 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
640 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
642
643 @Override
644 protected void onResume() {
645 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700646
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800647 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700648 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700650 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700652 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Winson Chunge4e50662012-01-23 14:45:13 -0800654
655 // Reset the pressed state of icons that were locked in the press state while activities
656 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800657 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800658 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800659 mWaitingForResume.setStayPressed(false);
660 }
Winson Chunge4e50662012-01-23 14:45:13 -0800661 if (mAppsCustomizeContent != null) {
662 // Resets the previous all apps icon press state
663 mAppsCustomizeContent.resetDrawableState();
664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666
667 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 protected void onPause() {
669 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700670 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800671 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700672 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 }
Romain Guycbb89e42009-06-08 15:52:54 -0700674
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700675 @Override
676 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 // Flag the loader to stop early before switching
678 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 if (mAppsCustomizeContent != null) {
680 mAppsCustomizeContent.surrender();
681 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800682 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700683 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800685 // We can't hide the IME if it was forced open. So don't bother
686 /*
687 @Override
688 public void onWindowFocusChanged(boolean hasFocus) {
689 super.onWindowFocusChanged(hasFocus);
690
691 if (hasFocus) {
692 final InputMethodManager inputManager = (InputMethodManager)
693 getSystemService(Context.INPUT_METHOD_SERVICE);
694 WindowManager.LayoutParams lp = getWindow().getAttributes();
695 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
696 android.os.Handler()) {
697 protected void onReceiveResult(int resultCode, Bundle resultData) {
698 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
699 }
700 });
701 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
702 }
703 }
704 */
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 private boolean acceptFilter() {
707 final InputMethodManager inputManager = (InputMethodManager)
708 getSystemService(Context.INPUT_METHOD_SERVICE);
709 return !inputManager.isFullscreenMode();
710 }
711
712 @Override
713 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700714 final int uniChar = event.getUnicodeChar();
715 final boolean handled = super.onKeyDown(keyCode, event);
716 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
717 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
719 keyCode, event);
720 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700721 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700722 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700723 // showSearchDialog()
724 // If there are multiple keystrokes before the search dialog takes focus,
725 // onSearchRequested() will be called for every keystroke,
726 // but it is idempotent, so it's fine.
727 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729 }
730
Joe Onorato8a9625e2010-01-28 15:55:35 -0800731 // Eat the long press event so the keyboard doesn't come up.
732 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
733 return true;
734 }
735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 return handled;
737 }
738
Karl Rosaen138a0412009-04-23 19:00:21 -0700739 private String getTypedText() {
740 return mDefaultKeySsb.toString();
741 }
742
743 private void clearTypedText() {
744 mDefaultKeySsb.clear();
745 mDefaultKeySsb.clearSpans();
746 Selection.setSelection(mDefaultKeySsb, 0);
747 }
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700750 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
751 * State
752 */
753 private static State intToState(int stateOrdinal) {
754 State state = State.WORKSPACE;
755 final State[] stateValues = State.values();
756 for (int i = 0; i < stateValues.length; i++) {
757 if (stateValues[i].ordinal() == stateOrdinal) {
758 state = stateValues[i];
759 break;
760 }
761 }
762 return state;
763 }
764
765 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 * Restores the previous state, if it exists.
767 *
768 * @param savedState The previous state.
769 */
770 private void restoreState(Bundle savedState) {
771 if (savedState == null) {
772 return;
773 }
774
Michael Jurka883f55b2010-10-21 15:47:14 -0700775 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700776 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800777 showAllApps(false);
778 }
779
Winson Chungf0c6ae02012-03-21 16:10:31 -0700780 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700782 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
786 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787
Winson Chung3d503fb2011-07-13 17:25:49 -0700788 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
789 mPendingAddInfo.container = pendingAddContainer;
790 mPendingAddInfo.screen = pendingAddScreen;
791 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
792 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 mRestoring = true;
794 }
795
796 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
797 if (renameFolder) {
798 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700799 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 mRestoring = true;
801 }
Winson Chunga12a2502010-12-20 14:41:35 -0800802
Winson Chung785d2eb2011-04-14 16:08:02 -0700803
804 // Restore the AppsCustomize tab
805 if (mAppsCustomizeTabHost != null) {
806 String curTab = savedState.getString("apps_customize_currentTab");
807 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700808 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700809 mAppsCustomizeContent.setContentType(
810 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
811 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700812 mAppsCustomizeContent.loadAssociatedPages(
813 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700814 }
815
Winson Chung5afbf7b2011-07-25 11:53:08 -0700816 int currentIndex = savedState.getInt("apps_customize_currentIndex");
817 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
820
821 /**
822 * Finds all the views we need and configure them properly.
823 */
824 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700825 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400826
Winson Chung4c98d922011-05-31 16:50:48 -0700827 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
828 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800829 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
830 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831
Winson Chung4c98d922011-05-31 16:50:48 -0700832 // Setup the drag layer
833 mDragLayer.setup(this, dragController);
834
Winson Chung3d503fb2011-07-13 17:25:49 -0700835 // Setup the hotseat
836 mHotseat = (Hotseat) findViewById(R.id.hotseat);
837 if (mHotseat != null) {
838 mHotseat.setup(this);
839 }
840
Winson Chung4c98d922011-05-31 16:50:48 -0700841 // Setup the workspace
842 mWorkspace.setHapticFeedbackEnabled(false);
843 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700844 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700845 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700846
Winson Chungf0ea4d32011-06-06 14:27:16 -0700847 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700848 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700849
Winson Chungf0ea4d32011-06-06 14:27:16 -0700850 // Setup AppsCustomize
851 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
852 findViewById(R.id.apps_customize_pane);
853 mAppsCustomizeContent = (AppsCustomizePagedView)
854 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700855 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700856 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400857
Michael Jurka5130e402011-10-13 04:55:35 -0700858 // Get the all apps button
859 mAllAppsButton = findViewById(R.id.all_apps_button);
860 if (mAllAppsButton != null) {
861 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
862 @Override
863 public boolean onTouch(View v, MotionEvent event) {
864 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
865 onTouchDownAllAppsButton(v);
866 }
867 return false;
868 }
869 });
870 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700872 dragController.setDragScoller(mWorkspace);
873 dragController.setScrollView(mDragLayer);
874 dragController.setMoveTarget(mWorkspace);
875 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700876 if (mSearchDropTargetBar != null) {
877 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880
881 /**
882 * Creates a view representing a shortcut.
883 *
884 * @param info The data structure describing the shortcut.
885 *
886 * @return A View inflated from R.layout.application.
887 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800888 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700890 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 /**
894 * Creates a view representing a shortcut inflated from the specified resource.
895 *
896 * @param layoutResId The id of the XML layout used to create the shortcut.
897 * @param parent The group the shortcut belongs to.
898 * @param info The data structure describing the shortcut.
899 *
900 * @return A View inflated from layoutResId.
901 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800902 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800903 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
904 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 return favorite;
907 }
908
909 /**
910 * Add an application shortcut to the workspace.
911 *
912 * @param data The intent describing the application.
913 * @param cellInfo The position on screen where to create the shortcut.
914 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700915 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700916 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700917 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700918
Adam Cohenfbba09b2011-07-18 21:43:05 -0700919 // First we check if we already know the exact location where we want to add this item.
920 if (cellX >= 0 && cellY >= 0) {
921 cellXY[0] = cellX;
922 cellXY[1] = cellY;
923 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700924 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700925 return;
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800928 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800929
Romain Guy73b979d2009-06-09 12:57:21 -0700930 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800931 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800933 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700935 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800936 } else {
937 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939 }
Romain Guycbb89e42009-06-08 15:52:54 -0700940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 /**
942 * Add a shortcut to the workspace.
943 *
944 * @param data The intent describing the shortcut.
945 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
948 int cellY) {
949 int[] cellXY = mTmpAddItemCellCoordinates;
950 int[] touchXY = mPendingAddInfo.dropPos;
951 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700952
Michael Jurkad3ef3062010-11-23 16:23:58 -0800953 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700954
Adam Cohen558baaf2011-08-15 15:22:57 -0700955 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800956 if (info == null) {
957 return;
958 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700959 final View view = createShortcut(info);
960
Adam Cohenfbba09b2011-07-18 21:43:05 -0700961 // First we check if we already know the exact location where we want to add this item.
962 if (cellX >= 0 && cellY >= 0) {
963 cellXY[0] = cellX;
964 cellXY[1] = cellY;
965 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700966
967 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800968 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700969 true, null,null)) {
970 return;
971 }
972 DragObject dragObject = new DragObject();
973 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800974 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
975 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700976 return;
977 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700978 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800979 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700980 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800981 foundCellSpan = (result != null);
982 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700983 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800984 }
985
986 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700987 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700988 return;
989 }
990
Adam Cohen558baaf2011-08-15 15:22:57 -0700991 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
993 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700994 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
995 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997 }
998
Winson Chunga5c96362012-04-12 14:04:41 -0700999 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -08001000 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001001 // We want to account for the extra amount of padding that we are adding to the widget
1002 // to ensure that it gets the full amount of space that it has requested
1003 int requiredWidth = minWidth + padding.left + padding.right;
1004 int requiredHeight = minHeight + padding.top + padding.bottom;
1005 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
1006 }
1007
Winson Chunga5c96362012-04-12 14:04:41 -07001008 int[] getSpanForWidget(AppWidgetProviderInfo info) {
1009 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001010 }
1011
Winson Chunga5c96362012-04-12 14:04:41 -07001012 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1013 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001014 }
1015
Winson Chunga5c96362012-04-12 14:04:41 -07001016 int[] getSpanForWidget(PendingAddWidgetInfo info) {
1017 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001018 }
1019
Winson Chunga5c96362012-04-12 14:04:41 -07001020 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001021 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001022 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001023 }
1024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001028 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001031 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1032 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1033 if (appWidgetInfo == null) {
1034 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001039
Winson Chunga5c96362012-04-12 14:04:41 -07001040 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1041 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001044 // We have saved the position to which the widget was dragged-- this really only matters
1045 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001046 int[] cellXY = mTmpAddItemCellCoordinates;
1047 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001048 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1051 cellXY[0] = mPendingAddInfo.cellX;
1052 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 spanXY[0] = mPendingAddInfo.spanX;
1054 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001055 foundCellSpan = true;
1056 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001059 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1060 spanXY[1], cellXY, finalSpan);
1061 spanXY[0] = finalSpan[0];
1062 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001063 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001065 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001066 }
1067
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001069 if (appWidgetId != -1) {
1070 // Deleting an app widget ID is a void call but writes to disk before returning
1071 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001072 new Thread("deleteAppWidgetId") {
1073 public void run() {
1074 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1075 }
1076 }.start();
1077 }
Winson Chung93eef082012-03-23 15:59:27 -07001078 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001079 return;
1080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001083 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1084 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 launcherInfo.spanX = spanXY[0];
1086 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001087 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1088 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092
1093 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001094 if (hostView == null) {
1095 // Perform actual inflation because we're live
1096 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1097 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1098 } else {
1099 // The AppWidgetHostView has already been inflated and instantiated
1100 launcherInfo.hostView = hostView;
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001104 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001105 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001107
1108 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001110 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Adam Cohended9f8d2010-11-03 13:25:16 -07001113 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1114 @Override
1115 public void onReceive(Context context, Intent intent) {
1116 final String action = intent.getAction();
1117 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1118 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001119 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001120 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001121
Winson Chungc100e8e2011-08-09 16:02:43 -07001122 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001123 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001124 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1125 mAppsCustomizeTabHost.reset();
1126 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001127 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1129 mUserPresent = true;
1130 updateRunning();
1131 }
1132 }
1133 };
1134
1135 @Override
1136 public void onAttachedToWindow() {
1137 super.onAttachedToWindow();
1138
1139 // Listen for broadcasts related to user-presence
1140 final IntentFilter filter = new IntentFilter();
1141 filter.addAction(Intent.ACTION_SCREEN_OFF);
1142 filter.addAction(Intent.ACTION_USER_PRESENT);
1143 registerReceiver(mReceiver, filter);
1144
Adam Cohend113e0c2010-11-11 10:48:05 -08001145 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001146 mVisible = true;
1147 }
1148
1149 @Override
1150 public void onDetachedFromWindow() {
1151 super.onDetachedFromWindow();
1152 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001153 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001154
Adam Cohend113e0c2010-11-11 10:48:05 -08001155 if (mAttached) {
1156 unregisterReceiver(mReceiver);
1157 mAttached = false;
1158 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001159 updateRunning();
1160 }
1161
1162 public void onWindowVisibilityChanged(int visibility) {
1163 mVisible = visibility == View.VISIBLE;
1164 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001165 // The following code used to be in onResume, but it turns out onResume is called when
1166 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1167 // is a more appropriate event to handle
1168 if (mVisible) {
1169 mAppsCustomizeTabHost.onWindowVisible();
1170 if (!mWorkspaceLoading) {
1171 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001172 // We want to let Launcher draw itself at least once before we force it to build
1173 // layers on all the workspace pages, so that transitioning to Launcher from other
1174 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1175 // a true draw so we wait until the second preDraw call to be safe
1176 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001177 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001178 // We delay the layer building a bit in order to give
1179 // other message processing a time to run. In particular
1180 // this avoids a delay in hiding the IME if it was
1181 // currently shown, because doing that may involve
1182 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001183 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001184 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001185 return true;
1186 }
1187 });
1188 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001189 // When Launcher comes back to foreground, a different Activity might be responsible for
1190 // the app market intent, so refresh the icon
1191 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001192 clearTypedText();
1193 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001194 }
1195
1196 private void sendAdvanceMessage(long delay) {
1197 mHandler.removeMessages(ADVANCE_MSG);
1198 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1199 mHandler.sendMessageDelayed(msg, delay);
1200 mAutoAdvanceSentTime = System.currentTimeMillis();
1201 }
1202
1203 private void updateRunning() {
1204 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1205 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1206 mAutoAdvanceRunning = autoAdvanceRunning;
1207 if (autoAdvanceRunning) {
1208 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1209 sendAdvanceMessage(delay);
1210 } else {
1211 if (!mWidgetsToAdvance.isEmpty()) {
1212 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1213 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1214 }
1215 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001216 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001217 }
1218 }
1219 }
1220
1221 private final Handler mHandler = new Handler() {
1222 @Override
1223 public void handleMessage(Message msg) {
1224 if (msg.what == ADVANCE_MSG) {
1225 int i = 0;
1226 for (View key: mWidgetsToAdvance.keySet()) {
1227 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1228 final int delay = mAdvanceStagger * i;
1229 if (v instanceof Advanceable) {
1230 postDelayed(new Runnable() {
1231 public void run() {
1232 ((Advanceable) v).advance();
1233 }
1234 }, delay);
1235 }
1236 i++;
1237 }
1238 sendAdvanceMessage(mAdvanceInterval);
1239 }
1240 }
1241 };
1242
1243 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001244 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1246 if (v instanceof Advanceable) {
1247 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001248 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001249 updateRunning();
1250 }
1251 }
1252
1253 void removeWidgetToAutoAdvance(View hostView) {
1254 if (mWidgetsToAdvance.containsKey(hostView)) {
1255 mWidgetsToAdvance.remove(hostView);
1256 updateRunning();
1257 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 }
1259
Joe Onorato9c1289c2009-08-17 11:03:03 -04001260 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001261 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 launcherInfo.hostView = null;
1263 }
1264
Winson Chung93eef082012-03-23 15:59:27 -07001265 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1266 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1267 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001268 }
1269
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001270 public LauncherAppWidgetHost getAppWidgetHost() {
1271 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
Romain Guycbb89e42009-06-08 15:52:54 -07001273
Winson Chunga9abd0e2010-10-27 17:18:37 -07001274 public LauncherModel getModel() {
1275 return mModel;
1276 }
1277
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001278 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001279 getWindow().closeAllPanels();
1280
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001281 // Whatever we were doing is hereby canceled.
1282 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001283 }
1284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 @Override
1286 protected void onNewIntent(Intent intent) {
1287 super.onNewIntent(intent);
1288
1289 // Close the menu
1290 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001291 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001292 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001293
Joe Onorato14f122b2009-11-19 14:06:36 -08001294 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1295 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001296
Adam Cohenac56cff2011-09-28 20:45:37 -07001297 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001298 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001299 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001300 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1301 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001302 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001303 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001304
1305 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001306 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001307 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001308
Joe Onorato3a8820b2009-11-10 15:06:42 -08001309 final View v = getWindow().peekDecorView();
1310 if (v != null && v.getWindowToken() != null) {
1311 InputMethodManager imm = (InputMethodManager)getSystemService(
1312 INPUT_METHOD_SERVICE);
1313 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001315
Michael Jurka35aa14d2011-07-07 17:01:08 -07001316 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001317 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001318 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
1321 }
1322
1323 @Override
1324 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1325 // Do not call super here
1326 mSavedInstanceState = savedInstanceState;
1327 }
1328
1329 @Override
1330 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001331 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001332 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333
Michael Jurka883f55b2010-10-21 15:47:14 -07001334 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001335 // We close any open folder since it will not be re-opened, and we need to make sure
1336 // this state is reflected.
1337 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1340 mWaitingForResult) {
1341 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1342 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1343 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1344 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 if (mFolderInfo != null && mWaitingForResult) {
1348 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1349 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1350 }
Michael Jurka946ad472010-07-09 18:05:18 -07001351
Winson Chung785d2eb2011-04-14 16:08:02 -07001352 // Save the current AppsCustomize tab
1353 if (mAppsCustomizeTabHost != null) {
1354 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1355 if (currentTabTag != null) {
1356 outState.putString("apps_customize_currentTab", currentTabTag);
1357 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001358 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1359 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362
1363 @Override
1364 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001366
Winson Chunge7a03942011-08-05 15:05:12 -07001367 // Remove all pending runnables
1368 mHandler.removeMessages(ADVANCE_MSG);
1369 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001370 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001371
Winson Chungcd2b0142011-06-08 16:02:26 -07001372 // Stop callbacks from LauncherModel
1373 LauncherApplication app = ((LauncherApplication) getApplication());
1374 mModel.stopLoader();
1375 app.setLauncher(null);
1376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001378 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001380 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001382 mAppWidgetHost = null;
1383
1384 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385
1386 TextKeyListener.getInstance().release();
1387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388
Winson Chung3d503fb2011-07-13 17:25:49 -07001389 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001390
1391 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001392 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001393
1394 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1395 mWorkspace.removeAllViews();
1396 mWorkspace = null;
1397 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001398
1399 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 }
1401
Adam Cohena9cf38f2011-05-02 15:36:58 -07001402 public DragController getDragController() {
1403 return mDragController;
1404 }
1405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 @Override
1407 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001408 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 super.startActivityForResult(intent, requestCode);
1410 }
1411
Winson Chung70d72102011-08-12 11:24:35 -07001412 /**
1413 * Indicates that we want global search for this activity by setting the globalSearch
1414 * argument for {@link #startSearch} to true.
1415 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001417 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001419
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001420 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Karl Rosaen138a0412009-04-23 19:00:21 -07001422 if (initialQuery == null) {
1423 // Use any text typed in the launcher as the initial query
1424 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 if (appSearchData == null) {
1427 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001428 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001430 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001431
Karl Rosaen138a0412009-04-23 19:00:21 -07001432 final SearchManager searchManager =
1433 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001434 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001435 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
1437
Winson Chung70d72102011-08-12 11:24:35 -07001438 @Override
1439 public boolean onCreateOptionsMenu(Menu menu) {
1440 if (isWorkspaceLocked()) {
1441 return false;
1442 }
1443
1444 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001445
1446 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1447 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1448 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001449 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1450 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1451 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001452 String helpUrl = getString(R.string.help_url);
1453 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001454 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1455 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1456
Winson Chung70d72102011-08-12 11:24:35 -07001457 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1458 .setIcon(android.R.drawable.ic_menu_gallery)
1459 .setAlphabeticShortcut('W');
1460 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1461 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001462 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001463 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001464 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1465 .setIcon(android.R.drawable.ic_menu_preferences)
1466 .setIntent(settings)
1467 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001468 if (!helpUrl.isEmpty()) {
1469 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1470 .setIcon(android.R.drawable.ic_menu_help)
1471 .setIntent(help)
1472 .setAlphabeticShortcut('H');
1473 }
Winson Chung70d72102011-08-12 11:24:35 -07001474 return true;
1475 }
1476
1477 @Override
1478 public boolean onPrepareOptionsMenu(Menu menu) {
1479 super.onPrepareOptionsMenu(menu);
1480
1481 if (mAppsCustomizeTabHost.isTransitioning()) {
1482 return false;
1483 }
1484 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1485 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1486
1487 return true;
1488 }
1489
1490 @Override
1491 public boolean onOptionsItemSelected(MenuItem item) {
1492 switch (item.getItemId()) {
1493 case MENU_WALLPAPER_SETTINGS:
1494 startWallpaper();
1495 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001496 }
1497
1498 return super.onOptionsItemSelected(item);
1499 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001501 @Override
1502 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001503 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001504 // Use a custom animation for launching search
1505 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001506 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001507 }
1508
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 public boolean isWorkspaceLocked() {
1510 return mWorkspaceLoading || mWaitingForResult;
1511 }
1512
Michael Jurka0280c3b2010-09-17 15:00:07 -07001513 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 mPendingAddInfo.container = ItemInfo.NO_ID;
1515 mPendingAddInfo.screen = -1;
1516 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1517 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001518 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001521
Michael Jurkaaf442092010-06-10 17:01:57 -07001522 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 // TODO: catch bad widget exception when sent
1524 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001525 // TODO: Is this log message meaningful?
1526 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001527 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001528 }
1529
Adam Cohened66b2b2012-01-23 17:28:51 -08001530 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1531 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001532 if (appWidget.configure != null) {
1533 // Launch over to configure widget, if needed
1534 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1535 intent.setComponent(appWidget.configure);
1536 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001537 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001538 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001539 intent.putExtra(InstallWidgetReceiver.
1540 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001541
1542 final String mimeType = info.mimeType;
1543 final ClipData clipData = (ClipData) info.configurationData;
1544 final ClipDescription clipDesc = clipData.getDescription();
1545 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1546 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001547 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001548 final CharSequence stringData = item.getText();
1549 final Uri uriData = item.getUri();
1550 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 final String key = InstallWidgetReceiver.
1552 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001553 if (uriData != null) {
1554 intent.putExtra(key, uriData);
1555 } else if (intentData != null) {
1556 intent.putExtra(key, intentData);
1557 } else if (stringData != null) {
1558 intent.putExtra(key, stringData);
1559 }
1560 break;
1561 }
1562 }
1563 }
Winson Chung55cef262010-10-28 14:14:18 -07001564 }
Romain Guy8e633c52010-03-04 12:51:36 -08001565 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001566 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001568 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001570 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573 }
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 /**
1576 * Process a shortcut drop.
1577 *
1578 * @param componentName The name of the component
1579 * @param screen The screen where it should be added
1580 * @param cell The cell it should be added to, optional
1581 * @param position The location on the screen where it was dropped, optional
1582 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1584 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001585 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001586 mPendingAddInfo.container = container;
1587 mPendingAddInfo.screen = screen;
1588 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001589
1590 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001591 mPendingAddInfo.cellX = cell[0];
1592 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001593 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594
1595 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1596 createShortcutIntent.setComponent(componentName);
1597 processShortcut(createShortcutIntent);
1598 }
1599
Adam Cohenfbba09b2011-07-18 21:43:05 -07001600 /**
1601 * Process a widget drop.
1602 *
1603 * @param info The PendingAppWidgetInfo of the widget being added.
1604 * @param screen The screen where it should be added
1605 * @param cell The cell it should be added to, optional
1606 * @param position The location on the screen where it was dropped, optional
1607 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001608 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001609 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001611 mPendingAddInfo.container = info.container = container;
1612 mPendingAddInfo.screen = info.screen = screen;
1613 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001614 mPendingAddInfo.minSpanX = info.minSpanX;
1615 mPendingAddInfo.minSpanY = info.minSpanY;
1616
Adam Cohenfbba09b2011-07-18 21:43:05 -07001617 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001618 mPendingAddInfo.cellX = cell[0];
1619 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001620 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 if (span != null) {
1622 mPendingAddInfo.spanX = span[0];
1623 mPendingAddInfo.spanY = span[1];
1624 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001625
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 AppWidgetHostView hostView = info.boundWidget;
1627 int appWidgetId;
1628 if (hostView != null) {
1629 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001630 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001631 } else {
1632 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001633 mWidgetBeingBoundOrConfigured = info;
1634 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1635 addAppWidgetImpl(appWidgetId, info);
1636 } else {
1637 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1638 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1639 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1640 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1641 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001642 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001643 }
1644
Joe Onoratodeb98af2010-02-19 14:59:39 -08001645 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 // Handle case where user selected "Applications"
1647 String applicationName = getResources().getString(R.string.group_applications);
1648 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 if (applicationName != null && applicationName.equals(shortcutName)) {
1651 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1652 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001653
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1655 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001656 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001657 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001658 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001659 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
1662
Winson Chung24ab2f12010-09-16 14:10:47 -07001663 void processWallpaper(Intent intent) {
1664 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1665 }
1666
Winson Chung3d503fb2011-07-13 17:25:49 -07001667 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1668 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001669 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 folderInfo.title = getText(R.string.folder_name);
1671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001673 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1674 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001675 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
1677 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001678 FolderIcon newFolder =
1679 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1680 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1681 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001682 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001686 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 }
1688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001690 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001692 Intent chooser = Intent.createChooser(pickWallpaper,
1693 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001694 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1695 // Removed in Eclair MR1
1696// WallpaperManager wm = (WallpaperManager)
1697// getSystemService(Context.WALLPAPER_SERVICE);
1698// WallpaperInfo wi = wm.getWallpaperInfo();
1699// if (wi != null && wi.getSettingsActivity() != null) {
1700// LabeledIntent li = new LabeledIntent(getPackageName(),
1701// R.string.configure_wallpaper, 0);
1702// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1703// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1704// }
Mike Clerona0618e42009-10-22 13:55:21 -07001705 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
1707
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001708 /**
1709 * Registers various content observers. The current implementation registers
1710 * only a favorites observer to keep track of the favorites applications.
1711 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001712 private void registerContentObservers() {
1713 ContentResolver resolver = getContentResolver();
1714 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1715 true, mWidgetObserver);
1716 }
1717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 @Override
1719 public boolean dispatchKeyEvent(KeyEvent event) {
1720 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1721 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001723 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001724 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001725 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001726 dumpState();
1727 return true;
1728 }
1729 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001730 }
1731 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1732 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001733 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 return true;
1735 }
1736 }
1737
1738 return super.dispatchKeyEvent(event);
1739 }
1740
Joe Onorato88ec0992009-11-19 13:16:06 -08001741 @Override
1742 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001743 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001744 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001745 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001746 Folder openFolder = mWorkspace.getOpenFolder();
1747 if (openFolder.isEditingName()) {
1748 openFolder.dismissEditingName();
1749 } else {
1750 closeFolder();
1751 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001752 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001753 mWorkspace.exitWidgetResizeMode();
1754
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001755 // Back button is a no-op here, but give at least some feedback for the button press
1756 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001757 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001758 }
1759
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001761 * Re-listen when widgets are reset.
1762 */
1763 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001764 if (mAppWidgetHost != null) {
1765 mAppWidgetHost.startListening();
1766 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001767 }
1768
1769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001770 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1771 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001774 if (mModel != null) {
1775 mModel.unbindWorkspaceItems();
1776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 /**
1780 * Launches the intent referred by the clicked shortcut.
1781 *
1782 * @param v The view representing the clicked shortcut.
1783 */
1784 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001785 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1786 // view has detached (it's possible for this to happen if the view is removed mid touch).
1787 if (v.getWindowToken() == null) {
1788 return;
1789 }
1790
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001791 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001792 return;
1793 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001796 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001798 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001799 int[] pos = new int[2];
1800 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001801 intent.setSourceBounds(new Rect(pos[0], pos[1],
1802 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001803
1804 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001805
1806 if (success && v instanceof BubbleTextView) {
1807 mWaitingForResume = (BubbleTextView) v;
1808 mWaitingForResume.setStayPressed(true);
1809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001811 if (v instanceof FolderIcon) {
1812 FolderIcon fi = (FolderIcon) v;
1813 handleFolderClick(fi);
1814 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001815 } else if (v == mAllAppsButton) {
1816 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001817 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001818 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001819 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822 }
1823
Michael Jurka0e260592010-06-30 17:07:39 -07001824 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001825 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001826 // clicking anywhere on the workspace causes the customization drawer to slide down
1827 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001828 return false;
1829 }
1830
Michael Jurkaaf442092010-06-10 17:01:57 -07001831 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001832 * Event handler for the search button
1833 *
1834 * @param v The view that was clicked.
1835 */
1836 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001837 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1838
Amith Yamasania135ba82011-08-09 17:42:01 -07001839 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001840 }
1841
1842 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001843 * Event handler for the voice button
1844 *
1845 * @param v The view that was clicked.
1846 */
1847 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001848 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001849
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001850 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001851 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001852 startActivitySafely(null, intent, "onClickVoiceButton");
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001853 }
1854
1855 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001856 * Event handler for the "grid" button that appears on the home screen, which
1857 * enters all apps mode.
1858 *
1859 * @param v The view that was clicked.
1860 */
1861 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001862 showAllApps(true);
1863 }
1864
1865 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001866 // Provide the same haptic feedback that the system offers for virtual keys.
1867 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001868 }
1869
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001870 public void onClickAppMarketButton(View v) {
1871 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001872 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001873 } else {
1874 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001875 }
1876 }
1877
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001878 void startApplicationDetailsActivity(ComponentName componentName) {
1879 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001880 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1881 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001882 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001883 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001884 }
1885
Patrick Dubroy5539af72010-09-07 15:22:01 -07001886 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1887 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1888 // System applications cannot be installed. For now, show a toast explaining that.
1889 // We may give them the option of disabling apps this way.
1890 int messageId = R.string.uninstall_system_app_text;
1891 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1892 } else {
1893 String packageName = appInfo.componentName.getPackageName();
1894 String className = appInfo.componentName.getClassName();
1895 Intent intent = new Intent(
1896 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001897 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1898 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001899 startActivity(intent);
1900 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001901 }
1902
Winson Chungc7450e32012-04-17 17:34:08 -07001903 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001905
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001907 if (v != null) {
1908 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1909 v.getMeasuredWidth(), v.getMeasuredHeight());
1910
1911 startActivity(intent, opts.toBundle());
1912 } else {
1913 startActivity(intent);
1914 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001915 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 } catch (ActivityNotFoundException e) {
1917 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001918 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 } catch (SecurityException e) {
1920 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001921 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001923 "or use the exported attribute for this activity. "
1924 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001926 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001928
Romain Guy8e633c52010-03-04 12:51:36 -08001929 void startActivityForResultSafely(Intent intent, int requestCode) {
1930 try {
1931 startActivityForResult(intent, requestCode);
1932 } catch (ActivityNotFoundException e) {
1933 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1934 } catch (SecurityException e) {
1935 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1936 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1937 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1938 "or use the exported attribute for this activity.", e);
1939 }
1940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941
Adam Cohena9cf38f2011-05-02 15:36:58 -07001942 private void handleFolderClick(FolderIcon folderIcon) {
1943 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001944 Folder openFolder = mWorkspace.getFolderForTag(info);
1945
1946 // If the folder info reports that the associated folder is open, then verify that
1947 // it is actually opened. There have been a few instances where this gets out of sync.
1948 if (info.opened && openFolder == null) {
1949 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1950 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1951 info.opened = false;
1952 }
1953
Adam Cohena9cf38f2011-05-02 15:36:58 -07001954 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 // Close any open folder
1956 closeFolder();
1957 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001958 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 } else {
1960 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 int folderScreen;
1962 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001963 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 // .. and close it
1965 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001966 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 // Close any folder open on the current screen
1968 closeFolder();
1969 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001970 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972 }
1973 }
1974 }
1975
Adam Cohen2801caf2011-05-13 20:57:39 -07001976 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001977 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001978 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1979 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1980 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1981
Adam Cohenc51934b2011-07-26 21:07:43 -07001982 FolderInfo info = (FolderInfo) fi.getTag();
1983 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1984 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001985 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1986 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001987 }
1988
Adam Cohen2801caf2011-05-13 20:57:39 -07001989 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1990 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1991 oa.start();
1992 }
1993
1994 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001995 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001996 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1997 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1998 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1999
Adam Cohenc51934b2011-07-26 21:07:43 -07002000 FolderInfo info = (FolderInfo) fi.getTag();
2001 CellLayout cl = null;
2002 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2003 cl = (CellLayout) fi.getParent().getParent();
2004 }
2005
2006 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002007 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2008 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002009 oa.addListener(new AnimatorListenerAdapter() {
2010 @Override
2011 public void onAnimationEnd(Animator animation) {
2012 if (layout != null) {
2013 layout.clearFolderLeaveBehind();
2014 }
2015 }
2016 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002017 oa.start();
2018 }
2019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002021 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 * is animated relative to the specified View. If the View is null, no animation
2023 * is played.
2024 *
2025 * @param folderInfo The FolderInfo describing the folder to open.
2026 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002027 public void openFolder(FolderIcon folderIcon) {
2028 Folder folder = folderIcon.mFolder;
2029 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030
Adam Cohen2801caf2011-05-13 20:57:39 -07002031 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002032 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033
Adam Cohen4554ee12011-08-03 16:13:21 -07002034 // Just verify that the folder hasn't already been added to the DragLayer.
2035 // There was a one-off crash where the folder had a parent already.
2036 if (folder.getParent() == null) {
2037 mDragLayer.addView(folder);
2038 mDragController.addDropTarget((DropTarget) folder);
2039 } else {
2040 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2041 folder.getParent() + ").");
2042 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002043 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002044 }
2045
2046 public void closeFolder() {
2047 Folder folder = mWorkspace.getOpenFolder();
2048 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002049 if (folder.isEditingName()) {
2050 folder.dismissEditingName();
2051 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002052 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002053
2054 // Dismiss the folder cling
2055 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002056 }
2057 }
2058
2059 void closeFolder(Folder folder) {
2060 folder.getInfo().opened = false;
2061
2062 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2063 if (parent != null) {
2064 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2065 shrinkAndFadeInFolderIcon(fi);
2066 }
2067 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
2069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002071
Patrick Dubroye708c522011-03-01 16:03:43 -08002072 if (mState != State.WORKSPACE) {
2073 return false;
2074 }
2075
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 return false;
2078 }
2079
2080 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002081 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
2083
Michael Jurka0280c3b2010-09-17 15:00:07 -07002084 resetAddInfo();
2085 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002087 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 return true;
2089 }
2090
Winson Chung3d503fb2011-07-13 17:25:49 -07002091 // The hotseat touch handling does not go through Workspace, and we always allow long press
2092 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002093 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2095 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 if (itemUnderLongClick == null) {
2097 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002098 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2099 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002100 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002102 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002104 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
2106 }
2107 }
2108 return true;
2109 }
2110
Winson Chung3d503fb2011-07-13 17:25:49 -07002111 boolean isHotseatLayout(View layout) {
2112 return mHotseat != null && layout != null &&
2113 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2114 }
2115 Hotseat getHotseat() {
2116 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002117 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002118 SearchDropTargetBar getSearchBar() {
2119 return mSearchDropTargetBar;
2120 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002121
Winson Chung3d503fb2011-07-13 17:25:49 -07002122 /**
2123 * Returns the CellLayout of the specified container at the specified screen.
2124 */
2125 CellLayout getCellLayout(long container, int screen) {
2126 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2127 if (mHotseat != null) {
2128 return mHotseat.getLayout();
2129 } else {
2130 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002131 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002132 } else {
2133 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002134 }
2135 }
2136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 Workspace getWorkspace() {
2138 return mWorkspace;
2139 }
2140
Daniel Sandler843e8602010-06-07 14:59:01 -04002141 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2142 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002143 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002144 }
2145
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002146 public boolean isAllAppsButtonRank(int rank) {
2147 return mHotseat.isAllAppsButtonRank(rank);
2148 }
2149
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002150 // AllAppsView.Watcher
2151 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002152 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002153 mWorkspace.setVisibility(View.GONE);
2154 }
2155 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002156
2157 /**
2158 * Helper method for the cameraZoomIn/cameraZoomOut animations
2159 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002160 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002161 * @param scaleFactor The scale factor used for the zoom
2162 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002163 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002164 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002165 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002166 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002167
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002168 void updateWallpaperVisibility(boolean visible) {
2169 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2170 int curflags = getWindow().getAttributes().flags
2171 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2172 if (wpflags != curflags) {
2173 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2174 }
2175 }
2176
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002177 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2178 if (v instanceof LauncherTransitionable) {
2179 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2180 }
2181 }
2182
Michael Jurkabed61d22012-02-14 22:51:29 -08002183 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2184 if (v instanceof LauncherTransitionable) {
2185 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2186 }
Winson Chung70442722012-02-10 15:43:22 -08002187
2188 // Update the workspace transition step as well
2189 dispatchOnLauncherTransitionStep(v, 0f);
2190 }
2191
2192 private void dispatchOnLauncherTransitionStep(View v, float t) {
2193 if (v instanceof LauncherTransitionable) {
2194 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2195 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002196 }
2197
2198 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2199 if (v instanceof LauncherTransitionable) {
2200 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2201 }
Winson Chung70442722012-02-10 15:43:22 -08002202
2203 // Update the workspace transition step as well
2204 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002205 }
2206
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002207 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002208 * Things to test when changing the following seven functions.
2209 * - Home from workspace
2210 * - from center screen
2211 * - from other screens
2212 * - Home from all apps
2213 * - from center screen
2214 * - from other screens
2215 * - Back from all apps
2216 * - from center screen
2217 * - from other screens
2218 * - Launch app from workspace and quit
2219 * - with back
2220 * - with home
2221 * - Launch app from all apps and quit
2222 * - with back
2223 * - with home
2224 * - Go to a screen that's not the default, then all
2225 * apps, and launch and app, and go back
2226 * - with back
2227 * -with home
2228 * - On workspace, long press power and go back
2229 * - with back
2230 * - with home
2231 * - On all apps, long press power and go back
2232 * - with back
2233 * - with home
2234 * - On workspace, power off
2235 * - On all apps, power off
2236 * - Launch an app and turn off the screen while in that app
2237 * - Go back with home key
2238 * - Go back with back key TODO: make this not go to workspace
2239 * - From all apps
2240 * - From workspace
2241 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2242 * - From all apps
2243 * - From the center workspace
2244 * - From another workspace
2245 */
2246
2247 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002248 * Zoom the camera out from the workspace to reveal 'toView'.
2249 * Assumes that the view to show is anchored at either the very top or very bottom
2250 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002251 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002252 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002253 if (mStateAnimation != null) {
2254 mStateAnimation.cancel();
2255 mStateAnimation = null;
2256 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002257 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002258
Winson Chungf0ea4d32011-06-06 14:27:16 -07002259 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2260 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2261 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002262 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002263 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002264 final int startDelay =
2265 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002266
Michael Jurkab3e22d92011-10-31 15:58:33 -07002267 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002268
Michael Jurkad74c9842011-07-10 12:44:21 -07002269 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002270 Animator workspaceAnim =
2271 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002272
2273 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002274 toView.setScaleX(scale);
2275 toView.setScaleY(scale);
2276 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2277 scaleAnim.
2278 scaleX(1f).scaleY(1f).
2279 setDuration(duration).
2280 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002281
Winson Chungf0ea4d32011-06-06 14:27:16 -07002282 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002283 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002284 final ObjectAnimator alphaAnim = ObjectAnimator
2285 .ofFloat(toView, "alpha", 0f, 1f)
2286 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002287 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002288 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2289 @Override
2290 public void onAnimationUpdate(ValueAnimator animation) {
2291 float t = (Float) animation.getAnimatedValue();
2292 dispatchOnLauncherTransitionStep(fromView, t);
2293 dispatchOnLauncherTransitionStep(toView, t);
2294 }
2295 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002296
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002297 // toView should appear right at the end of the workspace shrink
2298 // animation
2299 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002300 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002301 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002302
2303 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002304 boolean animationCancelled = false;
2305
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002306 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002307 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002308 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002309 // Prepare the position
2310 toView.setTranslationX(0.0f);
2311 toView.setTranslationY(0.0f);
2312 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002313 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002314 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002315 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002316 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002317 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2318 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002319
2320 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2321 // Hide the workspace scrollbar
2322 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002323 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002324 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002325 if (!animationCancelled) {
2326 updateWallpaperVisibility(false);
2327 }
2328 }
2329
Adam Cohencff6af82011-09-13 14:51:53 -07002330 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002331 public void onAnimationCancel(Animator animation) {
2332 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002333 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002334 });
2335
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002336 if (workspaceAnim != null) {
2337 mStateAnimation.play(workspaceAnim);
2338 }
Michael Jurka1899a362011-11-03 13:50:45 -07002339
2340 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002341 final ViewTreeObserver observer;
2342
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002343 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2344 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002345
2346 // If any of the objects being animated haven't been measured/laid out
2347 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002348 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002349 (mWorkspace.getMeasuredWidth() == 0) ||
2350 (toView.getMeasuredWidth() == 0)) {
2351 observer = mWorkspace.getViewTreeObserver();
2352 delayAnim = true;
2353 } else {
2354 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002355 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002356
2357 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002358 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002359 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2360 public void onGlobalLayout() {
2361 mWorkspace.post(new Runnable() {
2362 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002363 // Check that mStateAnimation hasn't changed while
2364 // we waited for a layout pass
2365 if (mStateAnimation == stateAnimation) {
2366 // Need to update pivots for zoom if layout changed
2367 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002368 dispatchOnLauncherTransitionStart(fromView, animated, false);
2369 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002370 mStateAnimation.start();
2371 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002372 }
2373 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002374 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002375 }
2376 };
2377 observer.addOnGlobalLayoutListener(delayedStart);
2378 } else {
2379 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002380 dispatchOnLauncherTransitionStart(fromView, animated, false);
2381 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002382 mStateAnimation.start();
2383 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002384 } else {
2385 toView.setTranslationX(0.0f);
2386 toView.setTranslationY(0.0f);
2387 toView.setScaleX(1.0f);
2388 toView.setScaleY(1.0f);
2389 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002390 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002391
Michael Jurkabed61d22012-02-14 22:51:29 -08002392 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2393 // Hide the workspace scrollbar
2394 mWorkspace.hideScrollingIndicator(true);
2395 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002396 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002397 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002398 dispatchOnLauncherTransitionStart(fromView, animated, false);
2399 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002400 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002401 dispatchOnLauncherTransitionStart(toView, animated, false);
2402 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002403 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002404 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002405 }
2406
2407 /**
2408 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002409 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002410 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002411 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002412 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2413 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002414
Michael Jurkab3e22d92011-10-31 15:58:33 -07002415 if (mStateAnimation != null) {
2416 mStateAnimation.cancel();
2417 mStateAnimation = null;
2418 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002419 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002420
Winson Chungf0ea4d32011-06-06 14:27:16 -07002421 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002422 final int fadeOutDuration =
2423 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002424 final float scaleFactor = (float)
2425 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2426 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002427 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002428 Animator workspaceAnim = null;
2429
2430 if (toState == State.WORKSPACE) {
2431 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2432 workspaceAnim = mWorkspace.getChangeStateAnimation(
2433 Workspace.State.NORMAL, animated, stagger);
2434 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2435 workspaceAnim = mWorkspace.getChangeStateAnimation(
2436 Workspace.State.SPRING_LOADED, animated);
2437 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002438
Michael Jurkab3e22d92011-10-31 15:58:33 -07002439 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002440 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002441 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002442 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002443 final LauncherViewPropertyAnimator scaleAnim =
2444 new LauncherViewPropertyAnimator(fromView);
2445 scaleAnim.
2446 scaleX(scaleFactor).scaleY(scaleFactor).
2447 setDuration(duration).
2448 setInterpolator(new Workspace.ZoomInInterpolator());
2449
2450 final ObjectAnimator alphaAnim = ObjectAnimator
2451 .ofFloat(fromView, "alpha", 1f, 0f)
2452 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002453 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002454 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2455 @Override
2456 public void onAnimationUpdate(ValueAnimator animation) {
2457 float t = 1f - (Float) animation.getAnimatedValue();
2458 dispatchOnLauncherTransitionStep(fromView, t);
2459 dispatchOnLauncherTransitionStep(toView, t);
2460 }
2461 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002462
Michael Jurkabed61d22012-02-14 22:51:29 -08002463 mStateAnimation = new AnimatorSet();
2464
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002465 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2466 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002467
2468 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002469 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002470 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002471 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002473 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2474 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002475 if (mWorkspace != null) {
2476 mWorkspace.hideScrollingIndicator(false);
2477 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002478 if (onCompleteRunnable != null) {
2479 onCompleteRunnable.run();
2480 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002481 }
2482 });
2483
Winson Chungf0ea4d32011-06-06 14:27:16 -07002484 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002485 if (workspaceAnim != null) {
2486 mStateAnimation.play(workspaceAnim);
2487 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002488 dispatchOnLauncherTransitionStart(fromView, animated, true);
2489 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002490 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002491 } else {
2492 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002493 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002494 dispatchOnLauncherTransitionStart(fromView, animated, true);
2495 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002496 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002497 dispatchOnLauncherTransitionStart(toView, animated, true);
2498 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002499 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002500 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002501 }
2502
Michael Jurkae326f182011-11-21 14:05:46 -08002503 @Override
2504 public void onTrimMemory(int level) {
2505 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002506 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002507 mAppsCustomizeTabHost.onTrimMemory();
2508 }
2509 }
2510
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002511 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002512 showWorkspace(animated, null);
2513 }
2514
2515 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002516 if (mState != State.WORKSPACE) {
2517 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002518 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002519
2520 // Show the search bar and hotseat
2521 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002522 // We only need to animate in the dock divider if we're going from spring loaded mode
2523 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002524
2525 // Set focus to the AppsCustomize button
2526 if (mAllAppsButton != null) {
2527 mAllAppsButton.requestFocus();
2528 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002529 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002530
Michael Jurkab737ee62011-11-15 15:57:22 -08002531 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002532
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002533 // Change the state *after* we've called all the transition code
2534 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002535
Winson Chung5fb63472011-02-02 17:03:37 -08002536 // Resume the auto-advance of widgets
2537 mUserPresent = true;
2538 updateRunning();
2539
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002540 // send an accessibility event to announce the context change
2541 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002542 }
2543
Michael Jurkab3e22d92011-10-31 15:58:33 -07002544 void showAllApps(boolean animated) {
2545 if (mState != State.WORKSPACE) return;
2546
2547 showAppsCustomizeHelper(animated, false);
2548 mAppsCustomizeTabHost.requestFocus();
2549
2550 // Hide the search bar and hotseat
2551 mSearchDropTargetBar.hideSearchBar(animated);
2552
2553 // Change the state *after* we've called all the transition code
2554 mState = State.APPS_CUSTOMIZE;
2555
2556 // Pause the auto-advance of widgets until we are out of AllApps
2557 mUserPresent = false;
2558 updateRunning();
2559 closeFolder();
2560
2561 // Send an accessibility event to announce the context change
2562 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2563 }
2564
Adam Cohen7777d962011-08-18 18:58:38 -07002565 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002566 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002567 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002568 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002569 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002570 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002571 }
Adam Cohen7777d962011-08-18 18:58:38 -07002572
Adam Cohened66b2b2012-01-23 17:28:51 -08002573 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2574 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002575 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2576
Winson Chunge7a03942011-08-05 15:05:12 -07002577 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002578 @Override
2579 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002580 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002581 // Before we show workspace, hide all apps again because
2582 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2583 // clean up our state transition functions
2584 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002585 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002586 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002587 } else {
2588 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002589 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002590 }
2591 }, (extendedDelay ?
2592 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2593 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2594 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002595
Michael Jurkad3ef3062010-11-23 16:23:58 -08002596 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002597 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002598 final boolean animated = true;
2599 final boolean springLoaded = true;
2600 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002601 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002602 }
2603 // Otherwise, we are not in spring loaded mode, so don't do anything.
2604 }
2605
Michael Jurkab737ee62011-11-15 15:57:22 -08002606 void hideDockDivider() {
2607 if (mQsbDivider != null && mDockDivider != null) {
2608 mQsbDivider.setVisibility(View.INVISIBLE);
2609 mDockDivider.setVisibility(View.INVISIBLE);
2610 }
2611 }
2612
2613 void showDockDivider(boolean animated) {
2614 if (mQsbDivider != null && mDockDivider != null) {
2615 mQsbDivider.setVisibility(View.VISIBLE);
2616 mDockDivider.setVisibility(View.VISIBLE);
2617 if (mDividerAnimator != null) {
2618 mDividerAnimator.cancel();
2619 mQsbDivider.setAlpha(1f);
2620 mDockDivider.setAlpha(1f);
2621 mDividerAnimator = null;
2622 }
2623 if (animated) {
2624 mDividerAnimator = new AnimatorSet();
2625 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2626 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2627 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2628 mDividerAnimator.start();
2629 }
2630 }
2631 }
2632
Michael Jurkab3e22d92011-10-31 15:58:33 -07002633 void lockAllApps() {
2634 // TODO
2635 }
2636
2637 void unlockAllApps() {
2638 // TODO
2639 }
2640
Adam Cohenfc53cd22011-07-20 15:45:11 -07002641 public boolean isAllAppsCustomizeOpen() {
2642 return mState == State.APPS_CUSTOMIZE;
2643 }
2644
Winson Chungf0ea4d32011-06-06 14:27:16 -07002645 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002646 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002647 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002648 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002649 if (!LauncherApplication.isScreenLarge()) {
2650 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002651 if (mHotseat.getAlpha() != 1f) {
2652 int duration = mSearchDropTargetBar.getTransitionInDuration();
2653 mHotseat.animate().alpha(1f).setDuration(duration);
2654 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002655 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002656 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002657 }
2658 }
2659 }
2660
2661 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002662 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002663 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002664 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002665 if (!LauncherApplication.isScreenLarge()) {
2666 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002667 if (mHotseat.getAlpha() != 0f) {
2668 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2669 mHotseat.animate().alpha(0f).setDuration(duration);
2670 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002671 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002672 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002673 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002674 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002675 }
2676
Patrick Dubroy5f445422011-02-18 14:35:21 -08002677 /**
2678 * Add an item from all apps or customize onto the given workspace screen.
2679 * If layout is null, add to the current screen.
2680 */
2681 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002682 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002683 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002684 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002685 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002686
Winson Chungdff8ebb2011-09-08 17:25:31 -07002687 /** Maps the current orientation to an index for referencing orientation correct global icons */
2688 private int getCurrentOrientationIndexForGlobalIcons() {
2689 // default - 0, landscape - 1
2690 switch (getResources().getConfiguration().orientation) {
2691 case Configuration.ORIENTATION_LANDSCAPE:
2692 return 1;
2693 default:
2694 return 0;
2695 }
2696 }
2697
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002698 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002699 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002700 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002701 // Look for the toolbar icon specified in the activity meta-data
2702 Bundle metaData = packageManager.getActivityInfo(
2703 activityName, PackageManager.GET_META_DATA).metaData;
2704 if (metaData != null) {
2705 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2706 if (iconResId != 0) {
2707 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002708 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002709 }
2710 }
2711 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002712 // This can happen if the activity defines an invalid drawable
2713 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2714 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002715 } catch (Resources.NotFoundException nfe) {
2716 // This can happen if the activity defines an invalid drawable
2717 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2718 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002719 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002720 return null;
2721 }
2722
2723 // if successful in getting icon, return it; otherwise, set button to use default drawable
2724 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2725 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002726 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002727 Resources r = getResources();
2728 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2729 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002730
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002731 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002732 // If we were unable to find the icon via the meta-data, use a generic one
2733 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002734 toolbarIcon = r.getDrawable(fallbackDrawableId);
2735 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002736 if (button != null) {
2737 button.setCompoundDrawables(toolbarIcon, null, null, null);
2738 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002739 return null;
2740 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002741 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002742 if (button != null) {
2743 button.setCompoundDrawables(toolbarIcon, null, null, null);
2744 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002745 return toolbarIcon.getConstantState();
2746 }
2747 }
2748
2749 // if successful in getting icon, return it; otherwise, set button to use default drawable
2750 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2751 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2752 ImageView button = (ImageView) findViewById(buttonId);
2753 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2754
Michael Jurka19e0fc52011-07-22 18:00:21 -07002755 if (button != null) {
2756 // If we were unable to find the icon via the meta-data, use a
2757 // generic one
2758 if (toolbarIcon == null) {
2759 button.setImageResource(fallbackDrawableId);
2760 } else {
2761 button.setImageDrawable(toolbarIcon);
2762 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002763 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002764
2765 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2766
Michael Jurka0423dcf2010-10-05 14:56:18 -07002767 }
2768
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002769 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2770 TextView button = (TextView) findViewById(buttonId);
2771 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2772 }
2773
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002774 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002775 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002776 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002777 }
2778
Winson Chungbb185bd2011-11-21 12:31:42 -08002779 private void invalidatePressedFocusedStates(View container, View button) {
2780 if (container instanceof HolographicLinearLayout) {
2781 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2782 layout.invalidatePressedFocusedStates();
2783 } else if (button instanceof HolographicImageView) {
2784 HolographicImageView view = (HolographicImageView) button;
2785 view.invalidatePressedFocusedStates();
2786 }
2787 }
2788
Winson Chungc51db6a2011-10-05 11:44:49 -07002789 private boolean updateGlobalSearchIcon() {
2790 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002791 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2792 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002793 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002794 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002795 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002796
Winson Chung1cad91e2011-05-25 17:41:01 -07002797 final SearchManager searchManager =
2798 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2799 ComponentName activityName = searchManager.getGlobalSearchActivity();
2800 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002801 int coi = getCurrentOrientationIndexForGlobalIcons();
2802 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002803 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002804 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002805 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2806 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002807 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002808 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002809 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002810 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002811 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002812 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2813 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2814 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002815 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002816 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002817 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002818 }
2819 }
2820
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002821 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002822 final View searchButtonContainer = findViewById(R.id.search_button_container);
2823 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002824 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002825 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002826 }
2827
Winson Chungc51db6a2011-10-05 11:44:49 -07002828 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002829 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002830 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002831 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002832 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002833
Winson Chungc51db6a2011-10-05 11:44:49 -07002834 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002835 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2836 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002837 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002838 int coi = getCurrentOrientationIndexForGlobalIcons();
2839 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002840 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002841 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002842 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002843 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002844 if (voiceButtonProxy != null) {
2845 voiceButtonProxy.setVisibility(View.VISIBLE);
2846 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002847 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002848 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002849 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002850 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002851 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002852 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002853 if (voiceButtonProxy != null) {
2854 voiceButtonProxy.setVisibility(View.GONE);
2855 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002856 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002857 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002858 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002859
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002860 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002861 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2862 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002863 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002864 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002865 }
2866
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002867 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002868 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002869 */
2870 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002871 final View marketButton = findViewById(R.id.market_button);
2872 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2873 // Find the app market activity by resolving an intent.
2874 // (If multiple app markets are installed, it will return the ResolverActivity.)
2875 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002876 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002877 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002878 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002879 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002880 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002881 marketButton.setVisibility(View.VISIBLE);
2882 } else {
2883 // We should hide and disable the view so that we don't try and restore the visibility
2884 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2885 marketButton.setVisibility(View.GONE);
2886 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002887 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002888 }
2889
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002890 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002891 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002892 }
2893
Michael Jurkad7c28052012-04-27 15:43:36 -07002894 @Override
2895 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2896 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2897 final List<CharSequence> text = event.getText();
2898 text.clear();
2899 text.add(getString(R.string.home));
2900 return result;
2901 }
2902
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002903 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002904 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002905 */
2906 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2907 @Override
2908 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002909 closeSystemDialogs();
2910 }
2911 }
2912
2913 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002914 * Receives notifications whenever the appwidgets are reset.
2915 */
2916 private class AppWidgetResetObserver extends ContentObserver {
2917 public AppWidgetResetObserver() {
2918 super(new Handler());
2919 }
2920
2921 @Override
2922 public void onChange(boolean selfChange) {
2923 onAppWidgetReset();
2924 }
2925 }
2926
2927 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002928 * If the activity is currently paused, signal that we need to re-run the loader
2929 * in onResume.
2930 *
2931 * This needs to be called from incoming places where resources might have been loaded
2932 * while we are paused. That is becaues the Configuration might be wrong
2933 * when we're not running, and if it comes back to what it was when we
2934 * were paused, we are not restarted.
2935 *
2936 * Implementation of the method from LauncherModel.Callbacks.
2937 *
2938 * @return true if we are currently paused. The caller might be able to
2939 * skip some work in that case since we will come back again.
2940 */
2941 public boolean setLoadOnResume() {
2942 if (mPaused) {
2943 Log.i(TAG, "setLoadOnResume");
2944 mOnResumeNeedsLoad = true;
2945 return true;
2946 } else {
2947 return false;
2948 }
2949 }
2950
2951 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002952 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002955 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002956 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002957 } else {
2958 return SCREEN_COUNT / 2;
2959 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002961
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 /**
2963 * Refreshes the shortcuts shown on the workspace.
2964 *
2965 * Implementation of the method from LauncherModel.Callbacks.
2966 */
2967 public void startBinding() {
2968 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002969
Winson Chungf0c6ae02012-03-21 16:10:31 -07002970 mNewShortcutAnimatePage = -1;
2971 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002972 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 int count = workspace.getChildCount();
2974 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002975 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002976 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2977 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 }
Michael Jurka05bf6442011-11-30 20:28:53 -08002979 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002980 if (mHotseat != null) {
2981 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 }
2983 }
2984
2985 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 * Bind the items start-end from the list.
2987 *
2988 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002990 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002991 setLoadOnResume();
2992
Winson Chungf0c6ae02012-03-21 16:10:31 -07002993 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2994 Set<String> newApps = new HashSet<String>();
2995 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2996
2997 Workspace workspace = mWorkspace;
2998 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003000
3001 // Short circuit if we are loading dock items for a configuration which has no dock
3002 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3003 mHotseat == null) {
3004 continue;
3005 }
3006
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 switch (item.itemType) {
3008 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3009 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003010 ShortcutInfo info = (ShortcutInfo) item;
3011 String uri = info.intent.toUri(0).toString();
3012 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003013 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3014 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003015 if (newApps.contains(uri)) {
3016 newApps.remove(uri);
3017
3018 // Prepare the view to be animated up
3019 shortcut.setAlpha(0f);
3020 shortcut.setScaleX(0f);
3021 shortcut.setScaleY(0f);
3022 mNewShortcutAnimatePage = item.screen;
3023 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3024 mNewShortcutAnimateViews.add(shortcut);
3025 }
3026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003028 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003029 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003030 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003031 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003032 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3033 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003034 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003037
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 }
3040
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 /**
3042 * Implementation of the method from LauncherModel.Callbacks.
3043 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003044 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003045 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003046 sFolders.clear();
3047 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049
3050 /**
3051 * Add the views for a widget to the workspace.
3052 *
3053 * Implementation of the method from LauncherModel.Callbacks.
3054 */
3055 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003056 setLoadOnResume();
3057
Daniel Sandler843e8602010-06-07 14:59:01 -04003058 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3059 if (DEBUG_WIDGETS) {
3060 Log.d(TAG, "bindAppWidget: " + item);
3061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 final Workspace workspace = mWorkspace;
3063
3064 final int appWidgetId = item.appWidgetId;
3065 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003066 if (DEBUG_WIDGETS) {
3067 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3068 }
3069
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3071
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3073 item.hostView.setTag(item);
3074
Winson Chung3d503fb2011-07-13 17:25:49 -07003075 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 item.cellY, item.spanX, item.spanY, false);
3077
Adam Cohended9f8d2010-11-03 13:25:16 -07003078 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3079
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080 workspace.requestLayout();
3081
Daniel Sandler843e8602010-06-07 14:59:01 -04003082 if (DEBUG_WIDGETS) {
3083 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3084 + (SystemClock.uptimeMillis()-start) + "ms");
3085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 }
3087
3088 /**
3089 * Callback saying that there aren't any more items to bind.
3090 *
3091 * Implementation of the method from LauncherModel.Callbacks.
3092 */
3093 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003094 setLoadOnResume();
3095
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 if (mSavedState != null) {
3097 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003098 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 mSavedState = null;
3101 }
3102
3103 if (mSavedInstanceState != null) {
3104 super.onRestoreInstanceState(mSavedInstanceState);
3105 mSavedInstanceState = null;
3106 }
3107
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003108 // If we received the result of any pending adds while the loader was running (e.g. the
3109 // widget configuration forced an orientation change), process them now.
3110 for (int i = 0; i < sPendingAddList.size(); i++) {
3111 completeAdd(sPendingAddList.get(i));
3112 }
3113 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114
Winson Chungc51db6a2011-10-05 11:44:49 -07003115 // Update the market app icon as necessary (the other icons will be managed in response to
3116 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003117 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003118
Winson Chungf0c6ae02012-03-21 16:10:31 -07003119 // Animate up any icons as necessary
3120 if (mVisible || mWorkspaceLoading) {
3121 Runnable newAppsRunnable = new Runnable() {
3122 @Override
3123 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003124 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003125 }
3126 };
Winson Chunga2413752012-04-03 14:22:34 -07003127
3128 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3129 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3130 if (canRunNewAppsAnimation()) {
3131 // If the user has not interacted recently, then either snap to the new page to show
3132 // the new-apps animation or just run them if they are to appear on the current page
3133 if (willSnapPage) {
3134 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3135 } else {
3136 runNewAppsAnimation(false);
3137 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003138 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003139 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003140 // are on another page (or just normally if they are added to the current page)
3141 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003142 }
3143 }
3144
3145 mWorkspaceLoading = false;
3146 }
3147
Winson Chunga2413752012-04-03 14:22:34 -07003148 private boolean canRunNewAppsAnimation() {
3149 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3150 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3151 }
3152
Winson Chungf0c6ae02012-03-21 16:10:31 -07003153 /**
3154 * Runs a new animation that scales up icons that were added while Launcher was in the
3155 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003156 *
3157 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003158 */
Winson Chunga2413752012-04-03 14:22:34 -07003159 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003160 AnimatorSet anim = new AnimatorSet();
3161 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003162
3163 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003164 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3165 @Override
3166 public int compare(View a, View b) {
3167 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3168 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3169 int cellCountX = LauncherModel.getCellCountX();
3170 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3171 }
3172 });
Winson Chunga2413752012-04-03 14:22:34 -07003173
3174 // Animate each of the views in place (or show them immediately if requested)
3175 if (immediate) {
3176 for (View v : mNewShortcutAnimateViews) {
3177 v.setAlpha(1f);
3178 v.setScaleX(1f);
3179 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003180 }
Winson Chunga2413752012-04-03 14:22:34 -07003181 } else {
3182 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3183 View v = mNewShortcutAnimateViews.get(i);
3184 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3185 PropertyValuesHolder.ofFloat("alpha", 1f),
3186 PropertyValuesHolder.ofFloat("scaleX", 1f),
3187 PropertyValuesHolder.ofFloat("scaleY", 1f));
3188 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3189 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3190 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3191 bounceAnims.add(bounceAnim);
3192 }
3193 anim.playTogether(bounceAnims);
3194 anim.addListener(new AnimatorListenerAdapter() {
3195 @Override
3196 public void onAnimationEnd(Animator animation) {
3197 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3198 }
3199 });
3200 anim.start();
3201 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003202
3203 // Clean up
3204 mNewShortcutAnimatePage = -1;
3205 mNewShortcutAnimateViews.clear();
3206 new Thread("clearNewAppsThread") {
3207 public void run() {
3208 mSharedPrefs.edit()
3209 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3210 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3211 .commit();
3212 }
3213 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003214 }
3215
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003216 @Override
3217 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003218 boolean searchVisible = updateGlobalSearchIcon();
3219 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3220 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003221 }
3222
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003223 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 * Add the icons for all apps.
3225 *
3226 * Implementation of the method from LauncherModel.Callbacks.
3227 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003228 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3229 // Remove the progress bar entirely; we could also make it GONE
3230 // but better to remove it since we know it's not going to be used
3231 View progressBar = mAppsCustomizeTabHost.
3232 findViewById(R.id.apps_customize_progress_bar);
3233 if (progressBar != null) {
3234 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003235 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003236 // We just post the call to setApps so the user sees the progress bar
3237 // disappear-- otherwise, it just looks like the progress bar froze
3238 // which doesn't look great
3239 mAppsCustomizeTabHost.post(new Runnable() {
3240 public void run() {
3241 if (mAppsCustomizeContent != null) {
3242 mAppsCustomizeContent.setApps(apps);
3243 }
3244 }
3245 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003246 }
3247
3248 /**
3249 * A package was installed.
3250 *
3251 * Implementation of the method from LauncherModel.Callbacks.
3252 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003253 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003254 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003255
Winson Chung785d2eb2011-04-14 16:08:02 -07003256 if (mAppsCustomizeContent != null) {
3257 mAppsCustomizeContent.addApps(apps);
3258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003259 }
3260
3261 /**
3262 * A package was updated.
3263 *
3264 * Implementation of the method from LauncherModel.Callbacks.
3265 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003266 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003267 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003268 if (mWorkspace != null) {
3269 mWorkspace.updateShortcuts(apps);
3270 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003271
Winson Chung785d2eb2011-04-14 16:08:02 -07003272 if (mAppsCustomizeContent != null) {
3273 mAppsCustomizeContent.updateApps(apps);
3274 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 }
3276
3277 /**
3278 * A package was uninstalled.
3279 *
3280 * Implementation of the method from LauncherModel.Callbacks.
3281 */
Joe Onorato36115782010-06-17 13:28:48 -04003282 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003283 if (permanent) {
3284 mWorkspace.removeItems(apps);
3285 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003286
Winson Chung785d2eb2011-04-14 16:08:02 -07003287 if (mAppsCustomizeContent != null) {
3288 mAppsCustomizeContent.removeApps(apps);
3289 }
Winson Chunga1820962011-10-03 16:31:06 -07003290
3291 // Notify the drag controller
3292 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 }
Joe Onoratobe386092009-11-17 17:32:16 -08003294
3295 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003296 * A number of packages were updated.
3297 */
3298 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003299 if (mAppsCustomizeContent != null) {
3300 mAppsCustomizeContent.onPackagesUpdated();
3301 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003302 }
3303
Winson Chung400438b2011-01-16 17:53:48 -08003304 private int mapConfigurationOriActivityInfoOri(int configOri) {
3305 final Display d = getWindowManager().getDefaultDisplay();
3306 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3307 switch (d.getRotation()) {
3308 case Surface.ROTATION_0:
3309 case Surface.ROTATION_180:
3310 // We are currently in the same basic orientation as the natural orientation
3311 naturalOri = configOri;
3312 break;
3313 case Surface.ROTATION_90:
3314 case Surface.ROTATION_270:
3315 // We are currently in the other basic orientation to the natural orientation
3316 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3317 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3318 break;
3319 }
3320
3321 int[] oriMap = {
3322 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3323 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3324 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3325 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3326 };
3327 // Since the map starts at portrait, we need to offset if this device's natural orientation
3328 // is landscape.
3329 int indexOffset = 0;
3330 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3331 indexOffset = 1;
3332 }
3333 return oriMap[(d.getRotation() + indexOffset) % 4];
3334 }
Adam Cohen446e9402011-09-15 18:21:21 -07003335
Winson Chung641d71d2012-04-26 15:58:01 -07003336 public void lockScreenOrientation() {
3337 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3338 .getConfiguration().orientation));
Winson Chung400438b2011-01-16 17:53:48 -08003339 }
Winson Chung641d71d2012-04-26 15:58:01 -07003340 public void unlockScreenOrientation() {
3341 mHandler.postDelayed(new Runnable() {
3342 public void run() {
3343 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3344 }
3345 }, mRestoreScreenOrientationDelay);
Winson Chung400438b2011-01-16 17:53:48 -08003346 }
3347
Winson Chung82f55532011-08-09 14:14:23 -07003348 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003349 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003350 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003351 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003352
Winson Chung7d7541e2011-09-16 20:14:36 -07003353 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003354 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003355 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3356 Cling cling = (Cling) findViewById(clingId);
3357 if (cling != null) {
3358 cling.init(this, positionData);
3359 cling.setVisibility(View.VISIBLE);
3360 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3361 if (animate) {
3362 cling.buildLayer();
3363 cling.setAlpha(0f);
3364 cling.animate()
3365 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003366 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003367 .setDuration(SHOW_CLING_DURATION)
3368 .setStartDelay(delay)
3369 .start();
3370 } else {
3371 cling.setAlpha(1f);
3372 }
3373 }
3374 return cling;
3375 }
3376 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003377 if (cling != null) {
3378 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003379 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003380 anim.addListener(new AnimatorListenerAdapter() {
3381 public void onAnimationEnd(Animator animation) {
3382 cling.setVisibility(View.GONE);
3383 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003384 // We should update the shared preferences on a background thread
3385 new Thread("dismissClingThread") {
3386 public void run() {
3387 SharedPreferences.Editor editor = mSharedPrefs.edit();
3388 editor.putBoolean(flag, true);
3389 editor.commit();
3390 }
3391 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003392 };
3393 });
3394 anim.start();
3395 }
3396 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003397 private void removeCling(int id) {
3398 final View cling = findViewById(id);
3399 if (cling != null) {
3400 final ViewGroup parent = (ViewGroup) cling.getParent();
3401 parent.post(new Runnable() {
3402 @Override
3403 public void run() {
3404 parent.removeView(cling);
3405 }
3406 });
3407 }
3408 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003409 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003410 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003411 if (isClingsEnabled() &&
3412 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003413 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003414 } else {
3415 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003416 }
3417 }
3418 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003419 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003420 if (isClingsEnabled() &&
3421 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003422 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003423 } else {
3424 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003425 }
3426 }
3427 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003428 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003429 if (isClingsEnabled() &&
3430 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3431 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003432 } else {
3433 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003434 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003435 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003436 }
3437 public boolean isFolderClingVisible() {
3438 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003439 if (cling != null) {
3440 return cling.getVisibility() == View.VISIBLE;
3441 }
3442 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003443 }
Winson Chung82f55532011-08-09 14:14:23 -07003444 public void dismissWorkspaceCling(View v) {
3445 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003446 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003447 }
3448 public void dismissAllAppsCling(View v) {
3449 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003450 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3451 }
3452 public void dismissFolderCling(View v) {
3453 Cling cling = (Cling) findViewById(R.id.folder_cling);
3454 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003455 }
3456
Winson Chung80baf5a2010-08-09 16:03:15 -07003457 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003458 * Prints out out state for debugging.
3459 */
3460 public void dumpState() {
3461 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003462 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003463 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3464 Log.d(TAG, "mRestoring=" + mRestoring);
3465 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3466 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003467 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003468 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003469
Winson Chung785d2eb2011-04-14 16:08:02 -07003470 if (mAppsCustomizeContent != null) {
3471 mAppsCustomizeContent.dumpState();
3472 }
Joe Onoratobe386092009-11-17 17:32:16 -08003473 Log.d(TAG, "END launcher2 dump state");
3474 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003475
3476 @Override
3477 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3478 super.dump(prefix, fd, writer, args);
3479 writer.println(" ");
3480 writer.println("Debug logs: ");
3481 for (int i = 0; i < sDumpLogs.size(); i++) {
3482 writer.println(" " + sDumpLogs.get(i));
3483 }
3484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003485}
Michael Jurka2763be32011-02-24 11:19:57 -08003486
Michael Jurkaabded662011-03-04 12:06:57 -08003487interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003488 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003489 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003490 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003491 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003492 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003493}