blob: a592f208e49407ec51a94a91976d9330ae55dc31 [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
Winson Chung400438b2011-01-16 17:53:48 -080020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080029import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070031import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070032import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070033import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070034import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.app.Dialog;
37import android.app.SearchManager;
38import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070043import android.content.ClipData;
44import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040045import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080050import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080051import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080054import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080055import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040058import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080061import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080063import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040065import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020068import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080069import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070070import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070074import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070075import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080076import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.text.Selection;
78import android.text.SpannableStringBuilder;
79import android.text.TextUtils;
80import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070081import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080082import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080083import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.KeyEvent;
85import android.view.LayoutInflater;
86import android.view.Menu;
87import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070088import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080089import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.View;
91import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070092import android.view.WindowManager;
Adam Cohen860f4c52011-01-27 20:16:13 -080093import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070097import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070099import android.widget.ImageView;
100import android.widget.LinearLayout;
101import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700102import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800103import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700104import android.widget.TextView;
105import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800106import android.widget.TabHost.OnTabChangeListener;
107import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700108
Winson Chung400438b2011-01-16 17:53:48 -0800109import com.android.common.Search;
110import com.android.launcher.R;
111import com.android.launcher2.CustomizePagedView.CustomizationType;
112import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -0800113
Adam Cohended9f8d2010-11-03 13:25:16 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800129 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700132 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
133 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
135 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700136 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 private static final int REQUEST_CREATE_SHORTCUT = 1;
139 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 private static final int REQUEST_PICK_APPLICATION = 6;
142 private static final int REQUEST_PICK_SHORTCUT = 7;
143 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700145 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Joe Onorato7c312c12009-08-13 21:36:53 -0700152 static final int DIALOG_CREATE_SHORTCUT = 1;
153 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
157 // Type: int
158 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700159 // Type: int
160 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: long
162 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: boolean
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
171 // Type: long
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
173
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 // tags for the customization tabs
175 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700176 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700177 private static final String SHORTCUTS_TAG = "shortcuts";
178 private static final String WALLPAPERS_TAG = "wallpapers";
179
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
181
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800183 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
184 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700185 private State mState = State.WORKSPACE;
186 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800191 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800193 private final BroadcastReceiver mCloseSystemDialogsReceiver
194 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800195 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private LayoutInflater mInflater;
198
Joe Onorato00acb122009-08-04 16:04:30 -0400199 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Michael Jurka0280c3b2010-09-17 15:00:07 -0700205 private int mAddScreen = -1;
206 private int mAddIntersectCellX = -1;
207 private int mAddIntersectCellY = -1;
208 private int[] mAddDropPosition;
209 private int[] mTmpAddItemCellCoordinates = new int[2];
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private FolderInfo mFolderInfo;
212
Joe Onorato7c312c12009-08-13 21:36:53 -0700213 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700216 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700217 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700218
Winson Chunga12a2502010-12-20 14:41:35 -0800219 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private Bundle mSavedState;
223
224 private SpannableStringBuilder mDefaultKeySsb = null;
225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 private boolean mWorkspaceLoading = true;
227
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800228 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private boolean mRestoring;
230 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700231 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
233 private Bundle mSavedInstanceState;
234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800236 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800237 private boolean mUserPresent = true;
238 private boolean mVisible = false;
239 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700241 private static LocaleConfiguration sLocaleConfiguration = null;
242
Romain Guy84f296c2009-11-04 15:00:44 -0800243 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700244
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700245 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700246
Romain Guy1fbc1c82009-11-09 20:43:08 -0800247 private ImageView mPreviousView;
248 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500249
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400251 private String[] mHotseatConfig = null;
252 private Intent[] mHotseats = null;
253 private Drawable[] mHotseatIcons = null;
254 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400255
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700256 private Intent mAppMarketIntent = null;
257
Adam Cohended9f8d2010-11-03 13:25:16 -0700258 // Related to the auto-advancing of widgets
259 private final int ADVANCE_MSG = 1;
260 private final int mAdvanceInterval = 20000;
261 private final int mAdvanceStagger = 250;
262 private long mAutoAdvanceSentTime;
263 private long mAutoAdvanceTimeLeft = -1;
264 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
265 new HashMap<View, AppWidgetProviderInfo>();
266
Winson Chung400438b2011-01-16 17:53:48 -0800267 // Determines how long to wait after a rotation before restoring the screen orientation to
268 // match the sensor state.
269 private final int mRestoreScreenOrientationDelay = 500;
270
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800272 private static Drawable.ConstantState sGlobalSearchIcon;
273 private static Drawable.ConstantState sVoiceSearchIcon;
274 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800275
Winson Chunga12a2502010-12-20 14:41:35 -0800276 private CustomizationType getCustomizeFilterForTabTag(String tag) {
277 if (tag.equals(WIDGETS_TAG)) {
278 return CustomizationType.WidgetCustomization;
279 } else if (tag.equals(APPLICATIONS_TAG)) {
280 return CustomizationType.ApplicationCustomization;
281 } else if (tag.equals(WALLPAPERS_TAG)) {
282 return CustomizePagedView.CustomizationType.WallpaperCustomization;
283 } else if (tag.equals(SHORTCUTS_TAG)) {
284 return CustomizePagedView.CustomizationType.ShortcutCustomization;
285 }
286 return CustomizationType.WidgetCustomization;
287 }
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 @Override
290 protected void onCreate(Bundle savedInstanceState) {
291 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700292
Winson Chungaafa03c2010-06-11 17:34:16 -0700293 if (LauncherApplication.isInPlaceRotationEnabled()) {
294 // hide the status bar (temporary until we get the status bar design figured out)
295 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
296 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
297 }
298
Joe Onorato0589f0f2010-02-08 13:44:00 -0800299 LauncherApplication app = ((LauncherApplication)getApplication());
300 mModel = app.setLauncher(this);
301 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400302 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700305 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700306 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
307 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200310 android.os.Debug.startMethodTracing(
311 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 }
313
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400314 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800317 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700318 if (mHomeCustomizationDrawer != null) {
319 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700320
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700322 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
323 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 TabContentFactory contentFactory = new TabContentFactory() {
325 public View createTabContent(String tag) {
326 return mCustomizePagedView;
327 }
328 };
329
Winson Chung49767ae2010-11-29 14:48:30 -0800330
331 TextView tabView;
332 TabWidget tabWidget = (TabWidget)
333 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
334
335 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
336 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800338 .setIndicator(tabView).setContent(contentFactory));
339 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
340 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700341 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800342 .setIndicator(tabView).setContent(contentFactory));
343 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
344 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700345 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800346 .setIndicator(tabView).setContent(contentFactory));
347 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
348 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700349 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800350 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
352 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800353 final CustomizePagedView.CustomizationType newType =
354 getCustomizeFilterForTabTag(tabId);
355 if (newType != mCustomizePagedView.getCustomizationFilter()) {
356 // animate the changing of the tab content by fading pages in and out
357 final Resources res = getResources();
358 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
359 final float alpha = mCustomizePagedView.getAlpha();
360 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
361 "alpha", alpha, 0.0f);
362 alphaAnim.setDuration(duration);
363 alphaAnim.addListener(new AnimatorListenerAdapter() {
364 @Override
365 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800366 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700367
Winson Chunga12a2502010-12-20 14:41:35 -0800368 final float alpha = mCustomizePagedView.getAlpha();
369 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
370 mCustomizePagedView, "alpha", alpha, 1.0f);
371 alphaAnim.setDuration(duration);
372 alphaAnim.start();
373 }
374 });
375 alphaAnim.start();
376 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700377 }
378 });
Michael Jurka946ad472010-07-09 18:05:18 -0700379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 setupViews();
381
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800382 registerContentObservers();
383
Joe Onorato7c312c12009-08-13 21:36:53 -0700384 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 mSavedState = savedInstanceState;
387 restoreState(mSavedState);
388
Winson Chunga12a2502010-12-20 14:41:35 -0800389 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800390 if (mCustomizePagedView != null) {
391 mCustomizePagedView.update();
392 }
Winson Chunga12a2502010-12-20 14:41:35 -0800393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 if (PROFILE_STARTUP) {
395 android.os.Debug.stopMethodTracing();
396 }
397
398 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 }
401
402 // For handling default keys
403 mDefaultKeySsb = new SpannableStringBuilder();
404 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800405
406 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
407 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800408
409 // If we have a saved version of these external icons, we load them up immediately
410 if (LauncherApplication.isScreenXLarge()) {
411 if (sGlobalSearchIcon != null) {
412 updateGlobalSearchIcon(sGlobalSearchIcon);
413 }
414 if (sVoiceSearchIcon != null) {
415 updateVoiceSearchIcon(sVoiceSearchIcon);
416 }
417 if (sAppMarketIcon != null) {
418 updateAppMarketIcon(sAppMarketIcon);
419 }
420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 }
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800423 @Override
424 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
425 super.dispatchPopulateAccessibilityEvent(event);
426
427 // we want to take over text population so it is context dependent
428 event.getText().clear();
429 if (mState == State.ALL_APPS) {
430 event.getText().add(getString(R.string.all_apps_button_label));
431 } else if (mState == State.WORKSPACE) {
432 event.getText().add(getString(R.string.all_apps_home_button_label));
433 }
434
435 return true;
436 }
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700439 if (sLocaleConfiguration == null) {
440 new AsyncTask<Void, Void, LocaleConfiguration>() {
441 @Override
442 protected LocaleConfiguration doInBackground(Void... unused) {
443 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
444 readConfiguration(Launcher.this, localeConfiguration);
445 return localeConfiguration;
446 }
447
448 @Override
449 protected void onPostExecute(LocaleConfiguration result) {
450 sLocaleConfiguration = result;
451 checkForLocaleChange(); // recursive, but now with a locale configuration
452 }
453 }.execute();
454 return;
455 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 final Configuration configuration = getResources().getConfiguration();
458
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700459 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 final String locale = configuration.locale.toString();
461
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 final int mcc = configuration.mcc;
464
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700465 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 final int mnc = configuration.mnc;
467
Romain Guy84f296c2009-11-04 15:00:44 -0800468 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469
Romain Guy84f296c2009-11-04 15:00:44 -0800470 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 sLocaleConfiguration.locale = locale;
472 sLocaleConfiguration.mcc = mcc;
473 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700474
Joe Onorato0589f0f2010-02-08 13:44:00 -0800475 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400476 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700477
478 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
479 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700480 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 public void run() {
482 writeConfiguration(Launcher.this, localeConfiguration);
483 }
484 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700485 }
486 }
487
488 private static class LocaleConfiguration {
489 public String locale;
490 public int mcc = -1;
491 public int mnc = -1;
492 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700493
Romain Guy98d01652009-06-30 16:21:04 -0700494 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
495 DataInputStream in = null;
496 try {
497 in = new DataInputStream(context.openFileInput(PREFERENCES));
498 configuration.locale = in.readUTF();
499 configuration.mcc = in.readInt();
500 configuration.mnc = in.readInt();
501 } catch (FileNotFoundException e) {
502 // Ignore
503 } catch (IOException e) {
504 // Ignore
505 } finally {
506 if (in != null) {
507 try {
508 in.close();
509 } catch (IOException e) {
510 // Ignore
511 }
512 }
513 }
514 }
515
516 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
517 DataOutputStream out = null;
518 try {
519 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
520 out.writeUTF(configuration.locale);
521 out.writeInt(configuration.mcc);
522 out.writeInt(configuration.mnc);
523 out.flush();
524 } catch (FileNotFoundException e) {
525 // Ignore
526 } catch (IOException e) {
527 //noinspection ResultOfMethodCallIgnored
528 context.getFileStreamPath(PREFERENCES).delete();
529 } finally {
530 if (out != null) {
531 try {
532 out.close();
533 } catch (IOException e) {
534 // Ignore
535 }
536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 }
538 }
539
540 static int getScreen() {
541 synchronized (sLock) {
542 return sScreen;
543 }
544 }
545
546 static void setScreen(int screen) {
547 synchronized (sLock) {
548 sScreen = screen;
549 }
550 }
551
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400552 // Note: This doesn't do all the client-id magic that BrowserProvider does
553 // in Browser. (http://b/2425179)
554 private Uri getDefaultBrowserUri() {
555 String url = getString(R.string.default_browser_url);
556 if (url.indexOf("{CID}") != -1) {
557 url = url.replace("{CID}", "android-google");
558 }
559 return Uri.parse(url);
560 }
561
562 // Load the Intent templates from arrays.xml to populate the hotseats. For
563 // each Intent, if it resolves to a single app, use that as the launch
564 // intent & use that app's label as the contentDescription. Otherwise,
565 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400566 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 if (mHotseatConfig == null) {
568 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
569 if (mHotseatConfig.length > 0) {
570 mHotseats = new Intent[mHotseatConfig.length];
571 mHotseatLabels = new CharSequence[mHotseatConfig.length];
572 mHotseatIcons = new Drawable[mHotseatConfig.length];
573 } else {
574 mHotseats = null;
575 mHotseatIcons = null;
576 mHotseatLabels = null;
577 }
578
579 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
580 for (int i=0; i<mHotseatConfig.length; i++) {
581 // load icon for this slot; currently unrelated to the actual activity
582 try {
583 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
584 } catch (ArrayIndexOutOfBoundsException ex) {
585 Log.w(TAG, "Missing hotseat_icons array item #" + i);
586 mHotseatIcons[i] = null;
587 }
588 }
589 hotseatIconDrawables.recycle();
590 }
591
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400592 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400593 for (int i=0; i<mHotseatConfig.length; i++) {
594 Intent intent = null;
595 if (mHotseatConfig[i].equals("*BROWSER*")) {
596 // magic value meaning "launch user's default web browser"
597 // replace it with a generic web request so we can see if there is indeed a default
598 String defaultUri = getString(R.string.default_browser_url);
599 intent = new Intent(
600 Intent.ACTION_VIEW,
601 ((defaultUri != null)
602 ? Uri.parse(defaultUri)
603 : getDefaultBrowserUri())
604 ).addCategory(Intent.CATEGORY_BROWSABLE);
605 // note: if the user launches this without a default set, she
606 // will always be taken to the default URL above; this is
607 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700608 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400609 // URL is unavoidably sent to the chosen app.
610 } else {
611 try {
612 intent = Intent.parseUri(mHotseatConfig[i], 0);
613 } catch (java.net.URISyntaxException ex) {
614 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
615 // bogus; leave intent=null
616 }
617 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700618
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 if (intent == null) {
620 mHotseats[i] = null;
621 mHotseatLabels[i] = getText(R.string.activity_not_found);
622 continue;
623 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400624
625 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 Log.d(TAG, "loadHotseats: hotseat " + i
627 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400629 + "]");
630 }
631
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400632 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
633 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400635 Log.d(TAG, "Best match for intent: " + bestMatch);
636 Log.d(TAG, "All matches: ");
637 for (ResolveInfo ri : allMatches) {
638 Log.d(TAG, " --> " + ri);
639 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400640 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 // did this resolve to a single app, or the resolver?
642 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 // can't find any activity to handle this. let's leave the
644 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400645 // to launch.
646 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400647
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400648 // set accessibility text to "Not installed"
649 mHotseatLabels[i] = getText(R.string.activity_not_found);
650 } else {
651 boolean found = false;
652 for (ResolveInfo ri : allMatches) {
653 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
654 && bestMatch.activityInfo.applicationInfo.packageName
655 .equals(ri.activityInfo.applicationInfo.packageName)) {
656 found = true;
657 break;
658 }
659 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700660
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400661 if (!found) {
662 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
663 // the bestMatch is probably the ResolveActivity, meaning the
664 // user has not yet selected a default
665 // so: we'll keep the original intent for now
666 mHotseats[i] = intent;
667
668 // set the accessibility text to "Select shortcut"
669 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
670 } else {
671 // we have an app!
672 // now reconstruct the intent to launch it through the front
673 // door
674 ComponentName com = new ComponentName(
675 bestMatch.activityInfo.applicationInfo.packageName,
676 bestMatch.activityInfo.name);
677 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
678
679 // load the app label for accessibility
680 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
681 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400682 }
683
684 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700685 Log.d(TAG, "loadHotseats: hotseat " + i
686 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400687 + ((mHotseats[i] == null)
688 ? "null"
689 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400690 + "] label=[" + mHotseatLabels[i]
691 + "]"
692 );
693 }
694 }
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 @Override
698 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700699 mWaitingForResult = false;
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 // The pattern used here is that a user PICKs a specific application,
702 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
705 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700706
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 switch (requestCode) {
709 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700710 completeAddApplication(
711 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 break;
713 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800714 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 break;
716 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700717 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 break;
719 case REQUEST_PICK_LIVE_FOLDER:
720 addLiveFolder(data);
721 break;
722 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700723 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700730 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700732 case REQUEST_PICK_WALLPAPER:
733 // We just wanted the activity result here so we can clear mWaitingForResult
734 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
Romain Guy18042c82009-11-06 11:44:55 -0800736 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
737 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
738 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700739 // Clean up the appWidgetId if we canceled
740 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
741 if (appWidgetId != -1) {
742 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
746
747 @Override
748 protected void onResume() {
749 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800750 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700751 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 mWorkspaceLoading = true;
753 mModel.startLoader(this, true);
754 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700755 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700757 // When we resume Launcher, a different Activity might be responsible for the app
758 // market intent, so refresh the icon
759 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700763 protected void onPause() {
764 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700765 // Some launcher layouts don't have a previous and next view
766 if (mPreviousView != null) {
767 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700768 }
769 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 dismissPreview(mNextView);
771 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700772 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800773 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700774 }
Romain Guycbb89e42009-06-08 15:52:54 -0700775
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700776 @Override
777 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 // Flag the loader to stop early before switching
779 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800780 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800781 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700782 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700783
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800784 // We can't hide the IME if it was forced open. So don't bother
785 /*
786 @Override
787 public void onWindowFocusChanged(boolean hasFocus) {
788 super.onWindowFocusChanged(hasFocus);
789
790 if (hasFocus) {
791 final InputMethodManager inputManager = (InputMethodManager)
792 getSystemService(Context.INPUT_METHOD_SERVICE);
793 WindowManager.LayoutParams lp = getWindow().getAttributes();
794 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
795 android.os.Handler()) {
796 protected void onReceiveResult(int resultCode, Bundle resultData) {
797 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
798 }
799 });
800 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
801 }
802 }
803 */
804
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 private boolean acceptFilter() {
806 final InputMethodManager inputManager = (InputMethodManager)
807 getSystemService(Context.INPUT_METHOD_SERVICE);
808 return !inputManager.isFullscreenMode();
809 }
810
811 @Override
812 public boolean onKeyDown(int keyCode, KeyEvent event) {
813 boolean handled = super.onKeyDown(keyCode, event);
814 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
815 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
816 keyCode, event);
817 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700818 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700819 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700820 // showSearchDialog()
821 // If there are multiple keystrokes before the search dialog takes focus,
822 // onSearchRequested() will be called for every keystroke,
823 // but it is idempotent, so it's fine.
824 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 }
826 }
827
Joe Onorato8a9625e2010-01-28 15:55:35 -0800828 // Eat the long press event so the keyboard doesn't come up.
829 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
830 return true;
831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 return handled;
834 }
835
Karl Rosaen138a0412009-04-23 19:00:21 -0700836 private String getTypedText() {
837 return mDefaultKeySsb.toString();
838 }
839
840 private void clearTypedText() {
841 mDefaultKeySsb.clear();
842 mDefaultKeySsb.clearSpans();
843 Selection.setSelection(mDefaultKeySsb, 0);
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700847 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
848 * State
849 */
850 private static State intToState(int stateOrdinal) {
851 State state = State.WORKSPACE;
852 final State[] stateValues = State.values();
853 for (int i = 0; i < stateValues.length; i++) {
854 if (stateValues[i].ordinal() == stateOrdinal) {
855 state = stateValues[i];
856 break;
857 }
858 }
859 return state;
860 }
861
862 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 * Restores the previous state, if it exists.
864 *
865 * @param savedState The previous state.
866 */
867 private void restoreState(Bundle savedState) {
868 if (savedState == null) {
869 return;
870 }
871
Michael Jurka883f55b2010-10-21 15:47:14 -0700872 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
873
874 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800875 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700876 } else if (state == State.CUSTOMIZE) {
877 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800878 }
879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
881 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700882 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
884
885 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700888 mAddScreen = addScreen;
889 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
890 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 mRestoring = true;
892 }
893
894 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
895 if (renameFolder) {
896 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700897 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 mRestoring = true;
899 }
Winson Chunga12a2502010-12-20 14:41:35 -0800900
901 // Restore the current AllApps drawer tab
902 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
903 String curTab = savedState.getString("allapps_currentTab");
904 if (curTab != null) {
905 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
906 tabhost.setCurrentTabByTag(curTab);
907 }
908 int curPage = savedState.getInt("allapps_currentPage", -1);
909 if (curPage > -1) {
910 mAllAppsPagedView.setRestorePage(curPage);
911 }
912 }
913
914 // Restore the current customization drawer tab
915 if (mHomeCustomizationDrawer != null) {
916 String curTab = savedState.getString("customize_currentTab");
917 if (curTab != null) {
918 // We set this directly so that there is no delay before the tab is set
919 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
920 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
921 }
922
923 // Note: currently we do not restore the page for the customization tray because unlike
924 // AllApps, the page content can change drastically
925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927
928 /**
929 * Finds all the views we need and configure them properly.
930 */
931 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700932 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400933
Romain Guyb1b69f52009-08-10 15:10:15 -0700934 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400935 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
Joe Onorato7c312c12009-08-13 21:36:53 -0700937 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700938 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700939 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800940 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700941 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700942 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700943
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700944 if (LauncherApplication.isScreenXLarge()) {
945 // They need to be INVISIBLE initially so that they will be measured in the layout.
946 // Otherwise the animations are messed up when we show them for the first time.
947 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
948 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
949 }
950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500954 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700955
Joe Onorato7c312c12009-08-13 21:36:53 -0700956 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
957 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700959 View handleView = findViewById(R.id.all_apps_button);
960 if (handleView != null && handleView instanceof HandleView) {
961 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700962 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700963 mHandleView.setLauncher(this);
964 mHandleView.setOnClickListener(this);
965 mHandleView.setOnLongClickListener(this);
966 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800967
Winson Chung80baf5a2010-08-09 16:03:15 -0700968 if (mCustomizePagedView != null) {
969 mCustomizePagedView.setLauncher(this);
970 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700971 } else {
972 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
973 hotseatLeft.setContentDescription(mHotseatLabels[0]);
974 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
975 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
976 hotseatRight.setContentDescription(mHotseatLabels[1]);
977 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400978
Michael Jurkaaf442092010-06-10 17:01:57 -0700979 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
980 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800981
Michael Jurkaaf442092010-06-10 17:01:57 -0700982 Drawable previous = mPreviousView.getDrawable();
983 Drawable next = mNextView.getDrawable();
984 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985
Michael Jurkaaf442092010-06-10 17:01:57 -0700986 mPreviousView.setHapticFeedbackEnabled(false);
987 mPreviousView.setOnLongClickListener(this);
988 mNextView.setHapticFeedbackEnabled(false);
989 mNextView.setOnLongClickListener(this);
990 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400993 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800995 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
997 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400998 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800999
1000 final View allAppsButton = findViewById(R.id.all_apps_button);
1001 final View divider = findViewById(R.id.divider);
1002 final View configureButton = findViewById(R.id.configure_button);
1003
Michael Jurka54f7ac32010-08-02 13:56:46 -07001004 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001005 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001006 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001007 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001008 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001009 dragController.addDragListener(deleteZone);
1010
Adam Cohencdc30d52010-12-01 15:09:47 -08001011 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1012 if (allAppsDeleteZone != null) {
1013 allAppsDeleteZone.setLauncher(this);
1014 allAppsDeleteZone.setDragController(dragController);
1015 allAppsDeleteZone.setDragAndDropEnabled(false);
1016 dragController.addDragListener(allAppsDeleteZone);
1017 dragController.addDropTarget(allAppsDeleteZone);
1018 }
1019
1020 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1021 findViewById(R.id.all_apps_info_target);
1022 if (allAppsInfoTarget != null) {
1023 allAppsInfoTarget.setLauncher(this);
1024 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001025 allAppsInfoTarget.setDragAndDropEnabled(false);
1026 View marketButton = findViewById(R.id.market_button);
1027 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001028 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001029 }
1030 }
1031
Joe Onorato00acb122009-08-04 16:04:30 -04001032 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001033 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001034 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001035
Joe Onorato00acb122009-08-04 16:04:30 -04001036 // The order here is bottom to top.
1037 dragController.addDropTarget(workspace);
1038 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001039 if (allAppsInfoTarget != null) {
1040 dragController.addDropTarget(allAppsInfoTarget);
1041 }
1042 if (allAppsDeleteZone != null) {
1043 dragController.addDropTarget(allAppsDeleteZone);
1044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 }
1046
Romain Guy8a73c512009-11-09 19:19:59 -08001047 @SuppressWarnings({"UnusedDeclaration"})
1048 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001049 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001050 mWorkspace.scrollLeft();
1051 }
Romain Guy8a73c512009-11-09 19:19:59 -08001052 }
1053
1054 @SuppressWarnings({"UnusedDeclaration"})
1055 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001056 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001057 mWorkspace.scrollRight();
1058 }
Romain Guy8a73c512009-11-09 19:19:59 -08001059 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001060
1061 @SuppressWarnings({"UnusedDeclaration"})
1062 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001063 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001064
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001065 int index = -1;
1066 if (v.getId() == R.id.hotseat_left) {
1067 index = 0;
1068 } else if (v.getId() == R.id.hotseat_right) {
1069 index = 1;
1070 }
1071
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001072 // reload these every tap; you never know when they might change
1073 loadHotseats();
1074 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001075 startActivitySafely(
1076 mHotseats[index],
1077 "hotseat"
1078 );
1079 }
1080 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 /**
1083 * Creates a view representing a shortcut.
1084 *
1085 * @param info The data structure describing the shortcut.
1086 *
1087 * @return A View inflated from R.layout.application.
1088 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001089 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001091 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093
1094 /**
1095 * Creates a view representing a shortcut inflated from the specified resource.
1096 *
1097 * @param layoutResId The id of the XML layout used to create the shortcut.
1098 * @param parent The group the shortcut belongs to.
1099 * @param info The data structure describing the shortcut.
1100 *
1101 * @return A View inflated from layoutResId.
1102 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001103 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001104 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1105 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 return favorite;
1108 }
1109
1110 /**
1111 * Add an application shortcut to the workspace.
1112 *
1113 * @param data The intent describing the application.
1114 * @param cellInfo The position on screen where to create the shortcut.
1115 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 void completeAddApplication(Context context, Intent data, int screen,
1117 int intersectCellX, int intersectCellY) {
1118 final int[] cellXY = mTmpAddItemCellCoordinates;
1119 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1120
1121 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1122 showOutOfSpaceMessage();
1123 return;
1124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1127 data, context);
1128
Romain Guy73b979d2009-06-09 12:57:21 -07001129 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001130 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001132 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1134 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001135 } else {
1136 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 /**
1141 * Add a shortcut to the workspace.
1142 *
1143 * @param data The intent describing the shortcut.
1144 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001146 private void completeAddShortcut(Intent data, int screen,
1147 int intersectCellX, int intersectCellY) {
1148 final int[] cellXY = mTmpAddItemCellCoordinates;
1149 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001151 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 boolean foundCellSpan = false;
1153 if (touchXY != null) {
1154 // when dragging and dropping, just find the closest free spot
1155 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1156 int[] result = screenLayout.findNearestVacantArea(
1157 touchXY[0], touchXY[1], 1, 1, cellXY);
1158 foundCellSpan = (result != null);
1159 } else {
1160 foundCellSpan = layout.findCellForSpanThatIntersects(
1161 cellXY, 1, 1, intersectCellX, intersectCellY);
1162 }
1163
1164 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 showOutOfSpaceMessage();
1166 return;
1167 }
1168
1169 final ShortcutInfo info = mModel.addShortcut(
1170 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171
1172 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001174 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176 }
1177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001181 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001186
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001187 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1189 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001192 // We have saved the position to which the widget was dragged-- this really only matters
1193 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001195
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001196 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 boolean foundCellSpan = false;
1198 if (touchXY != null) {
1199 // when dragging and dropping, just find the closest free spot
1200 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1201 int[] result = screenLayout.findNearestVacantArea(
1202 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001203 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001205 // if we long pressed on an empty cell to bring up a menu,
1206 // make sure we intersect the empty cell
1207 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1208 // findCellForSpanThatIntersects will just ignore them
1209 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1210 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001211 }
1212
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001214 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001216 return;
1217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001219 // Build Launcher-specific widget info and save to database
1220 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 launcherInfo.spanX = spanXY[0];
1222 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 LauncherModel.addItemToDatabase(this, launcherInfo,
1225 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227
1228 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001229 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001232 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001233
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001234 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001239
1240 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242 }
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Adam Cohended9f8d2010-11-03 13:25:16 -07001244 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1245 @Override
1246 public void onReceive(Context context, Intent intent) {
1247 final String action = intent.getAction();
1248 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1249 mUserPresent = false;
1250 updateRunning();
1251 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1252 mUserPresent = true;
1253 updateRunning();
1254 }
1255 }
1256 };
1257
1258 @Override
1259 public void onAttachedToWindow() {
1260 super.onAttachedToWindow();
1261
1262 // Listen for broadcasts related to user-presence
1263 final IntentFilter filter = new IntentFilter();
1264 filter.addAction(Intent.ACTION_SCREEN_OFF);
1265 filter.addAction(Intent.ACTION_USER_PRESENT);
1266 registerReceiver(mReceiver, filter);
1267
Adam Cohend113e0c2010-11-11 10:48:05 -08001268 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001269 mVisible = true;
1270 }
1271
1272 @Override
1273 public void onDetachedFromWindow() {
1274 super.onDetachedFromWindow();
1275 mVisible = false;
1276
Adam Cohend113e0c2010-11-11 10:48:05 -08001277 if (mAttached) {
1278 unregisterReceiver(mReceiver);
1279 mAttached = false;
1280 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 updateRunning();
1282 }
1283
1284 public void onWindowVisibilityChanged(int visibility) {
1285 mVisible = visibility == View.VISIBLE;
1286 updateRunning();
1287 }
1288
1289 private void sendAdvanceMessage(long delay) {
1290 mHandler.removeMessages(ADVANCE_MSG);
1291 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1292 mHandler.sendMessageDelayed(msg, delay);
1293 mAutoAdvanceSentTime = System.currentTimeMillis();
1294 }
1295
1296 private void updateRunning() {
1297 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1298 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1299 mAutoAdvanceRunning = autoAdvanceRunning;
1300 if (autoAdvanceRunning) {
1301 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1302 sendAdvanceMessage(delay);
1303 } else {
1304 if (!mWidgetsToAdvance.isEmpty()) {
1305 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1306 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1307 }
1308 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001309 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 }
1311 }
1312 }
1313
1314 private final Handler mHandler = new Handler() {
1315 @Override
1316 public void handleMessage(Message msg) {
1317 if (msg.what == ADVANCE_MSG) {
1318 int i = 0;
1319 for (View key: mWidgetsToAdvance.keySet()) {
1320 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1321 final int delay = mAdvanceStagger * i;
1322 if (v instanceof Advanceable) {
1323 postDelayed(new Runnable() {
1324 public void run() {
1325 ((Advanceable) v).advance();
1326 }
1327 }, delay);
1328 }
1329 i++;
1330 }
1331 sendAdvanceMessage(mAdvanceInterval);
1332 }
1333 }
1334 };
1335
1336 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1337 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1338 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1339 if (v instanceof Advanceable) {
1340 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001341 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001342 updateRunning();
1343 }
1344 }
1345
1346 void removeWidgetToAutoAdvance(View hostView) {
1347 if (mWidgetsToAdvance.containsKey(hostView)) {
1348 mWidgetsToAdvance.remove(hostView);
1349 updateRunning();
1350 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 }
1352
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1354 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 launcherInfo.hostView = null;
1357 }
1358
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 void showOutOfSpaceMessage() {
1360 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1361 }
1362
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001363 public LauncherAppWidgetHost getAppWidgetHost() {
1364 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Romain Guycbb89e42009-06-08 15:52:54 -07001366
Winson Chunga9abd0e2010-10-27 17:18:37 -07001367 public LauncherModel getModel() {
1368 return mModel;
1369 }
1370
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001371 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 getWindow().closeAllPanels();
1373
1374 try {
1375 dismissDialog(DIALOG_CREATE_SHORTCUT);
1376 // Unlock the workspace if the dialog was showing
1377 } catch (Exception e) {
1378 // An exception is thrown if the dialog is not visible, which is fine
1379 }
1380
1381 try {
1382 dismissDialog(DIALOG_RENAME_FOLDER);
1383 // Unlock the workspace if the dialog was showing
1384 } catch (Exception e) {
1385 // An exception is thrown if the dialog is not visible, which is fine
1386 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001387
1388 // Whatever we were doing is hereby canceled.
1389 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001390 }
1391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 @Override
1393 protected void onNewIntent(Intent intent) {
1394 super.onNewIntent(intent);
1395
1396 // Close the menu
1397 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001398 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001399 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001400
Joe Onorato14f122b2009-11-19 14:06:36 -08001401 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1402 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001403
Michael Jurka4cb37242010-08-09 21:05:32 -07001404 // in all these cases, only animate if we're already on home
1405 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001406 mWorkspace.unshrink(alreadyOnHome);
1407 }
1408 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001409 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001410 boolean animate = alreadyOnHome &&
1411 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1412 mWorkspace.moveToDefaultScreen(animate);
1413 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001414 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001415 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001416
Joe Onorato3a8820b2009-11-10 15:06:42 -08001417 final View v = getWindow().peekDecorView();
1418 if (v != null && v.getWindowToken() != null) {
1419 InputMethodManager imm = (InputMethodManager)getSystemService(
1420 INPUT_METHOD_SERVICE);
1421 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
1423 }
1424 }
1425
1426 @Override
1427 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1428 // Do not call super here
1429 mSavedInstanceState = savedInstanceState;
1430 }
1431
1432 @Override
1433 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001434 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435
1436 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1437 if (folders.size() > 0) {
1438 final int count = folders.size();
1439 long[] ids = new long[count];
1440 for (int i = 0; i < count; i++) {
1441 final FolderInfo info = folders.get(i).getInfo();
1442 ids[i] = info.id;
1443 }
1444 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1445 } else {
1446 super.onSaveInstanceState(outState);
1447 }
1448
Michael Jurka883f55b2010-10-21 15:47:14 -07001449 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450
Michael Jurka0280c3b2010-09-17 15:00:07 -07001451 if (mAddScreen > -1 && mWaitingForResult) {
1452 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1453 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1454 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456
1457 if (mFolderInfo != null && mWaitingForResult) {
1458 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1459 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1460 }
Michael Jurka946ad472010-07-09 18:05:18 -07001461
Winson Chung43b119d2010-12-06 16:45:05 -08001462 // Save the current AllApps drawer tab
1463 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1464 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1465 String currentTabTag = tabhost.getCurrentTabTag();
1466 if (currentTabTag != null) {
1467 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001468 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001469 }
1470 }
1471
1472 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001473 if (mHomeCustomizationDrawer != null) {
1474 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1475 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001476 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001477 }
1478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
1481 @Override
1482 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001486 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001488 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001490 mAppWidgetHost = null;
1491
1492 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
1494 TextKeyListener.getInstance().release();
1495
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001499
1500 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001501
Patrick Dubroyab962b72010-07-26 12:10:10 -07001502 // Some launcher layouts don't have a previous and next view
1503 if (mPreviousView != null) {
1504 dismissPreview(mPreviousView);
1505 }
1506 if (mNextView != null) {
1507 dismissPreview(mNextView);
1508 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001509
1510 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001511
1512 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1513 mWorkspace.removeAllViews();
1514 mWorkspace = null;
1515 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001516
1517 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
1519
1520 @Override
1521 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001522 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 super.startActivityForResult(intent, requestCode);
1524 }
1525
1526 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001527 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001529
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001530 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 if (initialQuery == null) {
1533 // Use any text typed in the launcher as the initial query
1534 initialQuery = getTypedText();
1535 clearTypedText();
1536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 if (appSearchData == null) {
1538 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001539 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Karl Rosaen138a0412009-04-23 19:00:21 -07001542 final SearchManager searchManager =
1543 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001544 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001545 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547
1548 @Override
1549 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001550 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001551 return false;
1552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1557 .setIcon(android.R.drawable.ic_menu_add)
1558 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001559 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1560 .setIcon(android.R.drawable.ic_menu_manage)
1561 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001562 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 .setIcon(android.R.drawable.ic_menu_gallery)
1564 .setAlphabeticShortcut('W');
1565 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1566 .setIcon(android.R.drawable.ic_search_category_default)
1567 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1568 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1569 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1570 .setAlphabeticShortcut('N');
1571
1572 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001573 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1574 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575
1576 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1577 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1578 .setIntent(settings);
1579
1580 return true;
1581 }
1582
1583 @Override
1584 public boolean onPrepareOptionsMenu(Menu menu) {
1585 super.onPrepareOptionsMenu(menu);
1586
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001587 // If all apps is animating, don't show the menu, because we don't know
1588 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001589 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001590 return false;
1591 }
1592
1593 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001594 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001595 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1596 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1597
1598 // Disable add if the workspace is full.
1599 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001600 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1601 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603
1604 return true;
1605 }
1606
1607 @Override
1608 public boolean onOptionsItemSelected(MenuItem item) {
1609 switch (item.getItemId()) {
1610 case MENU_ADD:
1611 addItems();
1612 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001613 case MENU_MANAGE_APPS:
1614 manageApps();
1615 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 case MENU_WALLPAPER_SETTINGS:
1617 startWallpaper();
1618 return true;
1619 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001620 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 return true;
1622 case MENU_NOTIFICATIONS:
1623 showNotifications();
1624 return true;
1625 }
1626
1627 return super.onOptionsItemSelected(item);
1628 }
Romain Guycbb89e42009-06-08 15:52:54 -07001629
Karl Rosaen138a0412009-04-23 19:00:21 -07001630 /**
1631 * Indicates that we want global search for this activity by setting the globalSearch
1632 * argument for {@link #startSearch} to true.
1633 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001635 @Override
1636 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001637 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001638 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001639 }
1640
Joe Onorato9c1289c2009-08-17 11:03:03 -04001641 public boolean isWorkspaceLocked() {
1642 return mWorkspaceLoading || mWaitingForResult;
1643 }
1644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001646 if (LauncherApplication.isScreenXLarge()) {
1647 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001648 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001649 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001650 }
1651 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001652 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001653 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 }
1656
Michael Jurka0280c3b2010-09-17 15:00:07 -07001657 private void resetAddInfo() {
1658 mAddScreen = -1;
1659 mAddIntersectCellX = -1;
1660 mAddIntersectCellY = -1;
1661 mAddDropPosition = null;
1662 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001663
Winson Chung55cef262010-10-28 14:14:18 -07001664 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001665 resetAddInfo();
1666 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001667 mAddDropPosition = position;
1668
Michael Jurkaaf442092010-06-10 17:01:57 -07001669 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001670 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1671 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001672 }
1673
Winson Chung7ad01412010-09-27 11:33:03 -07001674 private void manageApps() {
1675 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1676 }
1677
Michael Jurkaaf442092010-06-10 17:01:57 -07001678 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001679 // TODO: catch bad widget exception when sent
1680 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001681 // TODO: Is this log message meaningful?
1682 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001683 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001684 }
1685
Winson Chung55cef262010-10-28 14:14:18 -07001686 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001687 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
Bjorn Bringert7984c942009-12-09 15:38:25 +00001689 if (appWidget.configure != null) {
1690 // Launch over to configure widget, if needed
1691 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1692 intent.setComponent(appWidget.configure);
1693 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001694 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001695 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1696 intent.putExtra(
1697 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1698 info.mimeType);
1699
1700 final String mimeType = info.mimeType;
1701 final ClipData clipData = (ClipData) info.configurationData;
1702 final ClipDescription clipDesc = clipData.getDescription();
1703 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1704 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001705 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001706 final CharSequence stringData = item.getText();
1707 final Uri uriData = item.getUri();
1708 final Intent intentData = item.getIntent();
1709 final String key =
1710 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1711 if (uriData != null) {
1712 intent.putExtra(key, uriData);
1713 } else if (intentData != null) {
1714 intent.putExtra(key, intentData);
1715 } else if (stringData != null) {
1716 intent.putExtra(key, stringData);
1717 }
1718 break;
1719 }
1720 }
1721 }
Winson Chung55cef262010-10-28 14:14:18 -07001722 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001723
Romain Guy8e633c52010-03-04 12:51:36 -08001724 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001726 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001727 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 }
1729 }
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Michael Jurka0280c3b2010-09-17 15:00:07 -07001731 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1732 resetAddInfo();
1733 mAddScreen = screen;
1734 mAddDropPosition = position;
1735
1736 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1737 createShortcutIntent.setComponent(componentName);
1738 processShortcut(createShortcutIntent);
1739 }
1740
Joe Onoratodeb98af2010-02-19 14:59:39 -08001741 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001742 // Handle case where user selected "Applications"
1743 String applicationName = getResources().getString(R.string.group_applications);
1744 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001745
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001746 if (applicationName != null && applicationName.equals(shortcutName)) {
1747 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1748 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001749
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001750 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1751 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001752 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001753 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001754 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001755 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
1758
Winson Chung24ab2f12010-09-16 14:10:47 -07001759 void processWallpaper(Intent intent) {
1760 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1761 }
1762
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1764 resetAddInfo();
1765 mAddScreen = screen;
1766 mAddDropPosition = position;
1767
1768 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1769 createFolderIntent.setComponent(componentName);
1770
1771 addLiveFolder(createFolderIntent);
1772 }
1773
1774 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001776 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001781 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001782 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 }
1785
Michael Jurka0280c3b2010-09-17 15:00:07 -07001786 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 UserFolderInfo folderInfo = new UserFolderInfo();
1788 folderInfo.title = getText(R.string.folder_name);
1789
Michael Jurka0280c3b2010-09-17 15:00:07 -07001790 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1791 final int[] cellXY = mTmpAddItemCellCoordinates;
1792 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1793 showOutOfSpaceMessage();
1794 return;
1795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796
1797 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 LauncherModel.addItemToDatabase(this, folderInfo,
1799 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001801 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802
1803 // Create the view
1804 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001805 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1806 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001807 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
Romain Guycbb89e42009-06-08 15:52:54 -07001809
Joe Onorato9c1289c2009-08-17 11:03:03 -04001810 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001811 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812 }
1813
Michael Jurka28750fb2010-09-24 17:43:49 -07001814 private void completeAddLiveFolder(
1815 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001816 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1817 final int[] cellXY = mTmpAddItemCellCoordinates;
1818 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1819 showOutOfSpaceMessage();
1820 return;
1821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822
Michael Jurka0280c3b2010-09-17 15:00:07 -07001823 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824
1825 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001827 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001828 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
1830 }
1831
1832 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834
1835 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1836 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1837
1838 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 Intent.ShortcutIconResource iconResource = null;
1840
1841 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1842 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1843 try {
1844 iconResource = (Intent.ShortcutIconResource) extra;
1845 final PackageManager packageManager = context.getPackageManager();
1846 Resources resources = packageManager.getResourcesForApplication(
1847 iconResource.packageName);
1848 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1849 icon = resources.getDrawable(id);
1850 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001851 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
1853 }
1854
1855 if (icon == null) {
1856 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1857 }
1858
1859 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001860 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 info.title = name;
1862 info.iconResource = iconResource;
1863 info.uri = data.getData();
1864 info.baseIntent = baseIntent;
1865 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1866 LiveFolders.DISPLAY_MODE_GRID);
1867
1868 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001869 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001870 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
1872 return info;
1873 }
1874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 private void showNotifications() {
1876 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1877 if (statusBar != null) {
1878 statusBar.expand();
1879 }
1880 }
1881
1882 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001883 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001885 Intent chooser = Intent.createChooser(pickWallpaper,
1886 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001887 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1888 // Removed in Eclair MR1
1889// WallpaperManager wm = (WallpaperManager)
1890// getSystemService(Context.WALLPAPER_SERVICE);
1891// WallpaperInfo wi = wm.getWallpaperInfo();
1892// if (wi != null && wi.getSettingsActivity() != null) {
1893// LabeledIntent li = new LabeledIntent(getPackageName(),
1894// R.string.configure_wallpaper, 0);
1895// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1896// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1897// }
Mike Clerona0618e42009-10-22 13:55:21 -07001898 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 /**
1902 * Registers various content observers. The current implementation registers
1903 * only a favorites observer to keep track of the favorites applications.
1904 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001905 private void registerContentObservers() {
1906 ContentResolver resolver = getContentResolver();
1907 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1908 true, mWidgetObserver);
1909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 @Override
1912 public boolean dispatchKeyEvent(KeyEvent event) {
1913 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1914 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001916 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001917 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001918 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001919 dumpState();
1920 return true;
1921 }
1922 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001923 }
1924 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1925 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001926 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 return true;
1928 }
1929 }
1930
1931 return super.dispatchKeyEvent(event);
1932 }
1933
Joe Onorato88ec0992009-11-19 13:16:06 -08001934 @Override
1935 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001936 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1937 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001938 } else {
1939 closeFolder();
1940 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001941 // Some launcher layouts don't have a previous and next view
1942 if (mPreviousView != null) {
1943 dismissPreview(mPreviousView);
1944 dismissPreview(mNextView);
1945 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 private void closeFolder() {
1949 Folder folder = mWorkspace.getOpenFolder();
1950 if (folder != null) {
1951 closeFolder(folder);
1952 }
1953 }
1954
1955 void closeFolder(Folder folder) {
1956 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001957 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001959 CellLayout cl = (CellLayout) parent;
1960 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001961 if (folder instanceof DropTarget) {
1962 // Live folders aren't DropTargets.
1963 mDragController.removeDropTarget((DropTarget)folder);
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966 folder.onClose();
1967 }
1968
1969 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001970 * Re-listen when widgets are reset.
1971 */
1972 private void onAppWidgetReset() {
1973 mAppWidgetHost.startListening();
1974 }
1975
1976 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1978 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001980 private void unbindDesktopItems() {
1981 for (ItemInfo item: mDesktopItems) {
1982 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001984 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 /**
1988 * Launches the intent referred by the clicked shortcut.
1989 *
1990 * @param v The view representing the clicked shortcut.
1991 */
1992 public void onClick(View v) {
1993 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001994 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001996 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001997 int[] pos = new int[2];
1998 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001999 intent.setSourceBounds(new Rect(pos[0], pos[1],
2000 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002001 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 } else if (tag instanceof FolderInfo) {
2003 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002004 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002005 if (mState == State.ALL_APPS) {
2006 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002007 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002008 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
2011 }
2012
Michael Jurka0e260592010-06-30 17:07:39 -07002013 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002014 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002015 // clicking anywhere on the workspace causes the customization drawer to slide down
2016 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002017 return false;
2018 }
2019
Michael Jurkaaf442092010-06-10 17:01:57 -07002020 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002021 * Event handler for the search button
2022 *
2023 * @param v The view that was clicked.
2024 */
2025 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002026 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002027 // Use a custom animation for launching search
2028 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002029 }
2030
2031 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002032 * Event handler for the voice button
2033 *
2034 * @param v The view that was clicked.
2035 */
2036 public void onClickVoiceButton(View v) {
2037 startVoiceSearch();
2038 }
2039
2040 private void startVoiceSearch() {
2041 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2042 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2043 startActivity(intent);
2044 }
2045
2046 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002047 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002048 * enters home screen customization mode.
2049 *
2050 * @param v The view that was clicked.
2051 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002052 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002053 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002054 }
2055
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002056 /**
2057 * Event handler for the "grid" button that appears on the home screen, which
2058 * enters all apps mode.
2059 *
2060 * @param v The view that was clicked.
2061 */
2062 public void onClickAllAppsButton(View v) {
2063 showAllApps(true);
2064 }
2065
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002066 public void onClickAppMarketButton(View v) {
2067 if (mAppMarketIntent != null) {
2068 startActivitySafely(mAppMarketIntent, "app market");
2069 }
2070 }
2071
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002072 void startApplicationDetailsActivity(ComponentName componentName) {
2073 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002074 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2075 Uri.fromParts("package", packageName, null));
2076 startActivity(intent);
2077 }
2078
Patrick Dubroy5539af72010-09-07 15:22:01 -07002079 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2080 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2081 // System applications cannot be installed. For now, show a toast explaining that.
2082 // We may give them the option of disabling apps this way.
2083 int messageId = R.string.uninstall_system_app_text;
2084 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2085 } else {
2086 String packageName = appInfo.componentName.getPackageName();
2087 String className = appInfo.componentName.getClassName();
2088 Intent intent = new Intent(
2089 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2090 startActivity(intent);
2091 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002092 }
2093
Joe Onoratof984e852010-03-25 09:47:45 -07002094 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2096 try {
2097 startActivity(intent);
2098 } catch (ActivityNotFoundException e) {
2099 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002100 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 } catch (SecurityException e) {
2102 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002103 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002105 "or use the exported attribute for this activity. "
2106 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002109
Romain Guy8e633c52010-03-04 12:51:36 -08002110 void startActivityForResultSafely(Intent intent, int requestCode) {
2111 try {
2112 startActivityForResult(intent, requestCode);
2113 } catch (ActivityNotFoundException e) {
2114 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2115 } catch (SecurityException e) {
2116 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2117 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2118 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2119 "or use the exported attribute for this activity.", e);
2120 }
2121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122
2123 private void handleFolderClick(FolderInfo folderInfo) {
2124 if (!folderInfo.opened) {
2125 // Close any open folder
2126 closeFolder();
2127 // Open the requested folder
2128 openFolder(folderInfo);
2129 } else {
2130 // Find the open folder...
2131 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2132 int folderScreen;
2133 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002134 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 // .. and close it
2136 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002137 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 // Close any folder open on the current screen
2139 closeFolder();
2140 // Pull the folder onto this screen
2141 openFolder(folderInfo);
2142 }
2143 }
2144 }
2145 }
2146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002148 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 * is animated relative to the specified View. If the View is null, no animation
2150 * is played.
2151 *
2152 * @param folderInfo The FolderInfo describing the folder to open.
2153 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002154 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 Folder openFolder;
2156
2157 if (folderInfo instanceof UserFolderInfo) {
2158 openFolder = UserFolder.fromXml(this);
2159 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002160 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 } else {
2162 return;
2163 }
2164
Joe Onorato00acb122009-08-04 16:04:30 -04002165 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 openFolder.setLauncher(this);
2167
2168 openFolder.bind(folderInfo);
2169 folderInfo.opened = true;
2170
Winson Chungaafa03c2010-06-11 17:34:16 -07002171 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 openFolder.onOpen();
2174 }
2175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002177 switch (v.getId()) {
2178 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002179 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002180 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2181 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002182 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002183 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002184 return true;
2185 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002186 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002187 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2188 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002189 showPreviews(v);
2190 }
2191 return true;
2192 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002193 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002194 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2195 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2196 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002197 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002198 return true;
2199 }
2200
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 return false;
2203 }
2204
2205 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002206 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
2208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209
Michael Jurka0280c3b2010-09-17 15:00:07 -07002210 resetAddInfo();
2211 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002213 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 return true;
2215 }
2216
Michael Jurka0280c3b2010-09-17 15:00:07 -07002217 final View itemUnderLongClick = longClickCellInfo.cell;
2218
Winson Chung304dcde2011-01-07 11:17:23 -08002219 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 if (itemUnderLongClick == null) {
2221 // User long pressed on empty space
2222 mWorkspace.setAllowLongPress(false);
2223 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2224 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002225 if (LauncherApplication.isScreenXLarge()) {
2226 addItems();
2227 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002228 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 }
2230 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002231 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002233 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2234 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 mAddIntersectCellX = longClickCellInfo.cellX;
2236 mAddIntersectCellY = longClickCellInfo.cellY;
2237 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 }
2239 }
2240 }
2241 return true;
2242 }
2243
Romain Guye6b8e2f2009-11-10 11:56:55 -08002244 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002245 private void dismissPreview(final View v) {
2246 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002247 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002248 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2249 public void onDismiss() {
2250 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2251 int count = group.getChildCount();
2252 for (int i = 0; i < count; i++) {
2253 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2254 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002255 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2256 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2257
2258 v.setTag(R.id.workspace, null);
2259 v.setTag(R.id.icon, null);
2260 window.setOnDismissListener(null);
2261 }
2262 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002263 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002264 }
2265 v.setTag(null);
2266 }
2267
Romain Guyf8e6a802009-12-07 17:48:02 -08002268 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002269 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002270 }
2271
Romain Guya6abce82009-11-10 02:54:41 -08002272 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002273 final Resources resources = getResources();
2274 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002275
Romain Guya6abce82009-11-10 02:54:41 -08002276 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002277
Romain Guy9d31e9f2009-11-11 18:54:28 -08002278 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002279
Romain Guyf8e6a802009-12-07 17:48:02 -08002280 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002281 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002282 int extraW = (int) ((r.left + r.right) * max);
2283 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002284
2285 int aW = cell.getWidth() - extraW;
2286 float w = aW / max;
2287
2288 int width = cell.getWidth();
2289 int height = cell.getHeight();
2290 int x = cell.getLeftPadding();
2291 int y = cell.getTopPadding();
2292 width -= (x + cell.getRightPadding());
2293 height -= (y + cell.getBottomPadding());
2294
2295 float scale = w / width;
2296
2297 int count = end - start;
2298
2299 final float sWidth = width * scale;
2300 float sHeight = height * scale;
2301
Romain Guye6b8e2f2009-11-10 11:56:55 -08002302 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002303
Romain Guye6b8e2f2009-11-10 11:56:55 -08002304 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2305 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002306
2307 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002308 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002309 cell = (CellLayout) workspace.getChildAt(i);
2310
Romain Guyf8e6a802009-12-07 17:48:02 -08002311 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002312 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002313
2314 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002315 c.scale(scale, scale);
2316 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002317 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002318
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002320 image.setImageBitmap(bitmap);
2321 image.setTag(i);
2322 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002323 image.setOnFocusChangeListener(handler);
2324 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002325 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002326
2327 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002328 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2329
Winson Chungaafa03c2010-06-11 17:34:16 -07002330 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002331 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002332
2333 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002334 p.setContentView(preview);
2335 p.setWidth((int) (sWidth * count + extraW));
2336 p.setHeight((int) (sHeight + extraH));
2337 p.setAnimationStyle(R.style.AnimationPreview);
2338 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002339 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002340 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002341 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002342
Romain Guye6b8e2f2009-11-10 11:56:55 -08002343 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2344 public void onDismiss() {
2345 dismissPreview(anchor);
2346 }
2347 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002348
2349 anchor.setTag(p);
2350 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002351 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002352 }
2353
Romain Guy9d31e9f2009-11-11 18:54:28 -08002354 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002355 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002356
Romain Guye6b8e2f2009-11-10 11:56:55 -08002357 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002358 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002359 }
2360
2361 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002362 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002363 v.post(this);
2364 }
2365
2366 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002367 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002368 }
2369
2370 public void onFocusChange(View v, boolean hasFocus) {
2371 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002372 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 }
Romain Guya6abce82009-11-10 02:54:41 -08002374 }
2375 }
2376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 Workspace getWorkspace() {
2378 return mWorkspace;
2379 }
2380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 @Override
2382 protected Dialog onCreateDialog(int id) {
2383 switch (id) {
2384 case DIALOG_CREATE_SHORTCUT:
2385 return new CreateShortcut().createDialog();
2386 case DIALOG_RENAME_FOLDER:
2387 return new RenameFolder().createDialog();
2388 }
2389
2390 return super.onCreateDialog(id);
2391 }
2392
2393 @Override
2394 protected void onPrepareDialog(int id, Dialog dialog) {
2395 switch (id) {
2396 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 break;
2398 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002399 if (mFolderInfo != null) {
2400 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2401 final CharSequence text = mFolderInfo.title;
2402 input.setText(text);
2403 input.setSelection(0, text.length());
2404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 break;
2406 }
2407 }
2408
2409 void showRenameDialog(FolderInfo info) {
2410 mFolderInfo = info;
2411 mWaitingForResult = true;
2412 showDialog(DIALOG_RENAME_FOLDER);
2413 }
2414
Michael Jurka0280c3b2010-09-17 15:00:07 -07002415 private void showAddDialog(int intersectX, int intersectY) {
2416 resetAddInfo();
2417 mAddIntersectCellX = intersectX;
2418 mAddIntersectCellY = intersectY;
2419 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 mWaitingForResult = true;
2421 showDialog(DIALOG_CREATE_SHORTCUT);
2422 }
2423
Joe Onoratodeb98af2010-02-19 14:59:39 -08002424 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002425 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002426 Bundle bundle = new Bundle();
2427
2428 ArrayList<String> shortcutNames = new ArrayList<String>();
2429 shortcutNames.add(getString(R.string.group_applications));
2430 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2431
2432 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2433 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2434 R.drawable.ic_launcher_application));
2435 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2436
2437 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2438 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002439 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002440 pickIntent.putExtras(bundle);
2441
Joe Onoratodeb98af2010-02-19 14:59:39 -08002442 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002443 }
2444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 private class RenameFolder {
2446 private EditText mInput;
2447
2448 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2450 mInput = (EditText) layout.findViewById(R.id.folder_name);
2451
2452 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2453 builder.setIcon(0);
2454 builder.setTitle(getString(R.string.rename_folder_title));
2455 builder.setCancelable(true);
2456 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2457 public void onCancel(DialogInterface dialog) {
2458 cleanup();
2459 }
2460 });
2461 builder.setNegativeButton(getString(R.string.cancel_action),
2462 new Dialog.OnClickListener() {
2463 public void onClick(DialogInterface dialog, int which) {
2464 cleanup();
2465 }
2466 }
2467 );
2468 builder.setPositiveButton(getString(R.string.rename_action),
2469 new Dialog.OnClickListener() {
2470 public void onClick(DialogInterface dialog, int which) {
2471 changeFolderName();
2472 }
2473 }
2474 );
2475 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002476
2477 final AlertDialog dialog = builder.create();
2478 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2479 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002480 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002481 mInput.requestFocus();
2482 InputMethodManager inputManager = (InputMethodManager)
2483 getSystemService(Context.INPUT_METHOD_SERVICE);
2484 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002485 }
2486 });
2487
2488 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 }
2490
2491 private void changeFolderName() {
2492 final String name = mInput.getText().toString();
2493 if (!TextUtils.isEmpty(name)) {
2494 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002495 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 mFolderInfo.title = name;
2497 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2498
Joe Onorato9c1289c2009-08-17 11:03:03 -04002499 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002500 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002501 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 } else {
2503 final FolderIcon folderIcon = (FolderIcon)
2504 mWorkspace.getViewForTag(mFolderInfo);
2505 if (folderIcon != null) {
2506 folderIcon.setText(name);
2507 getWorkspace().requestLayout();
2508 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002509 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002510 mWorkspaceLoading = true;
2511 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 }
2513 }
2514 }
2515 cleanup();
2516 }
2517
2518 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 dismissDialog(DIALOG_RENAME_FOLDER);
2520 mWaitingForResult = false;
2521 mFolderInfo = null;
2522 }
2523 }
2524
Daniel Sandler843e8602010-06-07 14:59:01 -04002525 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2526 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002527 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002528 }
2529
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002530 // AllAppsView.Watcher
2531 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002532 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2533 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002534 mWorkspace.setVisibility(View.GONE);
2535 }
2536 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002537
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002538 private void showToolbarButton(View button) {
2539 button.setAlpha(1.0f);
2540 button.setVisibility(View.VISIBLE);
2541 button.setFocusable(true);
2542 button.setClickable(true);
2543 }
2544
2545 private void hideToolbarButton(View button) {
2546 button.setAlpha(0.0f);
2547 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2548 button.setVisibility(View.INVISIBLE);
2549 button.setFocusable(false);
2550 button.setClickable(false);
2551 }
2552
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002553 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002555 *
2556 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2557 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002558 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002559 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002560 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002561 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 final boolean showing = show;
2563 final boolean hiding = !show;
2564
2565 final int duration = show ?
2566 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2567 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2568
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002569 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002570 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2571 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002572 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002573 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002574 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002577 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002578 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002579 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002580 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002581 });
2582 seq.play(anim);
2583 } else {
2584 if (showing) {
2585 showToolbarButton(view);
2586 } else {
2587 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002588 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 }
2591
2592 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002593 * Show/hide the appropriate toolbar buttons for newState.
2594 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 *
2596 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002597 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2598 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002600 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Michael Jurkae90cd232011-01-26 16:13:55 -08002601 final View buttonCluster = findViewById(R.id.all_apps_button_cluster);
2602
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002604 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 final View configureButton = findViewById(R.id.configure_button);
2606
2607 switch (newState) {
2608 case WORKSPACE:
Michael Jurkae90cd232011-01-26 16:13:55 -08002609 hideOrShowToolbarButton(true, buttonCluster, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002610 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002611 mDeleteZone.setDragAndDropEnabled(true);
2612 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 break;
2614 case ALL_APPS:
Michael Jurkae90cd232011-01-26 16:13:55 -08002615 hideOrShowToolbarButton(false, buttonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002616 mDeleteZone.setDragAndDropEnabled(false);
2617 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 break;
2619 case CUSTOMIZE:
Michael Jurkae90cd232011-01-26 16:13:55 -08002620 hideOrShowToolbarButton(false, buttonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002621 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002622 break;
2623 }
2624 }
2625
2626 /**
2627 * Helper method for the cameraZoomIn/cameraZoomOut animations
2628 * @param view The view being animated
2629 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2630 * @param scaleFactor The scale factor used for the zoom
2631 */
2632 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2633 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002634
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002636 // Set pivotY so that at the starting zoom factor, the view is partially
2637 // visible. Modifying initialHeightFactor changes how much of the view is
2638 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002640 final float initialHeightFactor = 0.165f;
2641 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002643 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002644 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002645 }
2646 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002647
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002648 /**
2649 * Zoom the camera out from the workspace to reveal 'toView'.
2650 * Assumes that the view to show is anchored at either the very top or very bottom
2651 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002652 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002653 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002654 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002655 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002657
2658 final int duration = toAllApps ?
2659 res.getInteger(R.integer.config_allAppsZoomInTime) :
2660 res.getInteger(R.integer.config_customizeZoomInTime);
2661
2662 final float scale = toAllApps ?
2663 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2664 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2665
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002667
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 setPivotsForZoom(toView, toState, scale);
2669
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002670 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002671 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002673 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002675
2676 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002677 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2678 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2679 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
Adam Cohenf16e5712011-01-13 13:31:45 -08002680
Chet Haase472b2812010-10-14 07:02:04 -07002681 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002682
Adam Cohenf16e5712011-01-13 13:31:45 -08002683 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002684 toView.setAlpha(0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002685 ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2686 PropertyValuesHolder.ofFloat("alpha", 1.0f));
Adam Cohen860f4c52011-01-27 20:16:13 -08002687 alphaAnim.setInterpolator(new DecelerateInterpolator(1.0f));
Adam Cohenf16e5712011-01-13 13:31:45 -08002688 alphaAnim.setDuration(duration);
2689 alphaAnim.start();
2690 }
2691
Adam Cohen61033d32010-11-15 18:29:44 -08002692 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002693 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002694 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002695 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002696 // Prepare the position
2697 toView.setTranslationX(0.0f);
2698 toView.setTranslationY(0.0f);
2699 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002700 if (!toAllApps) {
2701 toView.setAlpha(1.0f);
2702 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002704 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002705 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002706 // If we don't set the final scale values here, if this animation is cancelled
2707 // it will have the wrong scale value and subsequent cameraPan animations will
2708 // not fix that
2709 toView.setScaleX(1.0f);
2710 toView.setScaleY(1.0f);
2711 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 });
2713
Chet Haaseb1254a62010-09-07 13:35:00 -07002714 AnimatorSet toolbarHideAnim = new AnimatorSet();
2715 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2717
2718 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002719 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002720
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002721 if (mStateAnimation != null) mStateAnimation.cancel();
2722 mStateAnimation = new AnimatorSet();
2723 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2724 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002725
2726 // Show the new toolbar buttons just as the main animation is ending
2727 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002728 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2729 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002730 } else {
2731 toView.setTranslationX(0.0f);
2732 toView.setTranslationY(0.0f);
2733 toView.setScaleX(1.0f);
2734 toView.setScaleY(1.0f);
2735 toView.setVisibility(View.VISIBLE);
2736 hideAndShowToolbarButtons(toState, null, null);
2737 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002738 }
2739
2740 /**
2741 * Zoom the camera back into the workspace, hiding 'fromView'.
2742 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002743 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2744 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002745 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002747 cameraZoomIn(fromState, animated, false);
2748 }
2749
2750 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002751 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002752 final boolean fromAllApps = (fromState == State.ALL_APPS);
2753
2754 int duration = fromAllApps ?
2755 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2756 res.getInteger(R.integer.config_customizeZoomOutTime);
2757
2758 float scaleFactor = fromAllApps ?
2759 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2760 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2761
2762 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002763
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002764 mCustomizePagedView.endChoiceMode();
2765 mAllAppsPagedView.endChoiceMode();
2766
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002767 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768
Michael Jurkad3ef3062010-11-23 16:23:58 -08002769 if (!springLoaded) {
2770 mWorkspace.unshrink(animated);
2771 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772
2773 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002774 if (mStateAnimation != null) mStateAnimation.cancel();
2775 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002776 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2777 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2778 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2779 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002780 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2781
2782 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2783 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2784 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohen860f4c52011-01-27 20:16:13 -08002785 alphaAnim.setInterpolator(new DecelerateInterpolator(2.0f));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002786 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002787 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002788 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002789 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 }
2791 });
2792
Chet Haaseb1254a62010-09-07 13:35:00 -07002793 AnimatorSet toolbarHideAnim = new AnimatorSet();
2794 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002795 if (!springLoaded) {
2796 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2797 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002798
Adam Cohen61033d32010-11-15 18:29:44 -08002799 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800
2801 // Show the new toolbar buttons at the very end of the whole animation
2802 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2803 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002804 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2805 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 } else {
2807 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002808 if (!springLoaded) {
2809 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2810 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002811 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002812 }
2813
2814 /**
2815 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2816 * This is the transition used on xlarge screens to go between all apps and
2817 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002818 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2819 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2820 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002821 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002822 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002823 final Resources res = getResources();
2824 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 final int workspaceHeight = mWorkspace.getHeight();
2826
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827 final boolean fromAllApps = (fromState == State.ALL_APPS);
2828 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2829 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002830
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002831 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2832 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2833 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2834 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002836 mCustomizePagedView.endChoiceMode();
2837 mAllAppsPagedView.endChoiceMode();
2838
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002839 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002840 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002841 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002842 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844
2845 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002846 if (mStateAnimation != null) mStateAnimation.cancel();
2847 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002848 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002849 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002850 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002851 toView.setVisibility(View.VISIBLE);
2852 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002853 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002854 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002855 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002856 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002857 fromView.setVisibility(View.GONE);
2858 }
2859 });
2860
Chet Haaseb1254a62010-09-07 13:35:00 -07002861 AnimatorSet toolbarHideAnim = new AnimatorSet();
2862 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2864
Chet Haase472b2812010-10-14 07:02:04 -07002865 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2866 fromViewStartY, fromViewEndY);
2867 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002868 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2869 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2870 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2871 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2872 );
Chet Haase472b2812010-10-14 07:02:04 -07002873 fromAnim.setDuration(duration);
2874 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875
2876 // Show the new toolbar buttons just as the main animation is ending
2877 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002878 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2879 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002880 } else {
2881 fromView.setY(fromViewEndY);
2882 fromView.setVisibility(View.GONE);
2883 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002884 toView.setScaleX(1.0f);
2885 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002886 toView.setVisibility(View.VISIBLE);
2887 hideAndShowToolbarButtons(toState, null, null);
2888 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002889 }
2890
2891 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002892 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002893 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002894 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002895
Michael Jurka79212d82010-07-30 16:36:20 -07002896 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002897 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002898 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002899 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002900 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002901 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002902 } else {
2903 mAllAppsGrid.zoom(1.0f, animated);
2904 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002905
Romain Guyc16fea72010-03-12 17:17:56 -08002906 ((View) mAllAppsGrid).setFocusable(true);
2907 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002908
Joe Onorato7c312c12009-08-13 21:36:53 -07002909 // TODO: fade these two too
2910 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002911
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002912 // Change the state *after* we've called all the transition code
2913 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002914
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002915 // send an accessibility event to announce the context change
2916 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2917 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002918
2919 void showWorkspace(boolean animated) {
2920 showWorkspace(animated, null);
2921 }
2922
2923 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002924 if (layout != null) {
2925 // always animated, but that's ok since we never specify a layout and
2926 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002927 mWorkspace.unshrink(layout);
2928 } else {
2929 mWorkspace.unshrink(animated);
2930 }
2931 if (mState == State.ALL_APPS) {
2932 closeAllApps(animated);
2933 } else if (mState == State.CUSTOMIZE) {
2934 hideCustomizationDrawer(animated);
2935 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002936
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002937 // Change the state *after* we've called all the transition code
2938 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002939
2940 // send an accessibility event to announce the context change
2941 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002942 }
2943
Michael Jurkad3ef3062010-11-23 16:23:58 -08002944 void enterSpringLoadedDragMode(CellLayout layout) {
2945 mWorkspace.enterSpringLoadedDragMode(layout);
2946 if (mState == State.ALL_APPS) {
2947 cameraZoomIn(State.ALL_APPS, true, true);
2948 mState = State.ALL_APPS_SPRING_LOADED;
2949 } else if (mState == State.CUSTOMIZE) {
2950 cameraZoomIn(State.CUSTOMIZE, true, true);
2951 mState = State.CUSTOMIZE_SPRING_LOADED;
2952 }/* else {
2953 // we're already in spring loaded mode; don't do anything
2954 }*/
2955 }
2956
2957 void exitSpringLoadedDragMode() {
2958 if (mState == State.ALL_APPS_SPRING_LOADED) {
2959 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2960 cameraZoomOut(State.ALL_APPS, true);
2961 mState = State.ALL_APPS;
2962 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2963 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2964 cameraZoomOut(State.CUSTOMIZE, true);
2965 mState = State.CUSTOMIZE;
2966 }/* else {
2967 // we're not in spring loaded mode; don't do anything
2968 }*/
2969 }
2970
Joe Onoratob2061212009-11-24 16:13:54 -05002971 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002972 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002973 * - Home from workspace
2974 * - from center screen
2975 * - from other screens
2976 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002977 * - from center screen
2978 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002979 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002980 * - from center screen
2981 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002982 * - Launch app from workspace and quit
2983 * - with back
2984 * - with home
2985 * - Launch app from all apps and quit
2986 * - with back
2987 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002988 * - Go to a screen that's not the default, then all
2989 * apps, and launch and app, and go back
2990 * - with back
2991 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002992 * - On workspace, long press power and go back
2993 * - with back
2994 * - with home
2995 * - On all apps, long press power and go back
2996 * - with back
2997 * - with home
2998 * - On workspace, power off
2999 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003000 * - Launch an app and turn off the screen while in that app
3001 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003002 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003003 * - From all apps
3004 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003005 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3006 * - From all apps
3007 * - From the center workspace
3008 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003009 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003010 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003011 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003012 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003013 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003014 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003015 } else {
3016 mAllAppsGrid.zoom(0.0f, animated);
3017 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003018 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003019 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003020 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003021 }
3022
Joe Onorato7c312c12009-08-13 21:36:53 -07003023 void lockAllApps() {
3024 // TODO
3025 }
3026
3027 void unlockAllApps() {
3028 // TODO
3029 }
3030
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003031 // Show the customization drawer (only exists in x-large configuration)
3032 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003033 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003034 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003035 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003036 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003037 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003038 // Change the state *after* we've called all the transition code
3039 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003040 }
3041
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003042 // Hide the customization drawer (only exists in x-large configuration)
3043 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003044 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003045 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003046 }
3047 }
3048
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003049 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3050 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3051 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003052 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003053 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003054
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003055 void onWorkspaceClick(CellLayout layout) {
3056 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003057 }
3058
Michael Jurka4ef207b2010-11-29 17:05:45 -08003059 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003060 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003061 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3062 ImageView button = (ImageView) findViewById(buttonId);
3063 Drawable toolbarIcon = null;
3064 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003065 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003066 // Look for the toolbar icon specified in the activity meta-data
3067 Bundle metaData = packageManager.getActivityInfo(
3068 activityName, PackageManager.GET_META_DATA).metaData;
3069 if (metaData != null) {
3070 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3071 if (iconResId != 0) {
3072 Resources res = packageManager.getResourcesForActivity(activityName);
3073 toolbarIcon = res.getDrawable(iconResId);
3074 }
3075 }
3076 } catch (NameNotFoundException e) {
3077 // Do nothing
3078 }
3079 // If we were unable to find the icon via the meta-data, use a generic one
3080 if (toolbarIcon == null) {
3081 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003082 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003083 } else {
3084 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003085 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003086 }
3087 }
3088
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003089 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003090 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003091 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003092 }
3093
Michael Jurka0423dcf2010-10-05 14:56:18 -07003094 private void updateGlobalSearchIcon() {
3095 if (LauncherApplication.isScreenXLarge()) {
3096 final SearchManager searchManager =
3097 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3098 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003099 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003100 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003101 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003102 } else {
3103 findViewById(R.id.search_button).setVisibility(View.GONE);
3104 }
3105 }
3106 }
3107
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003108 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003109 updateButtonWithDrawable(R.id.search_button, d);
3110 }
3111
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003112 private void updateVoiceSearchIcon() {
3113 if (LauncherApplication.isScreenXLarge()) {
3114 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3115 ComponentName activityName = intent.resolveActivity(getPackageManager());
3116 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003117 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003118 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3119 } else {
3120 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003121 }
3122 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003123 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003124
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003125 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003126 updateButtonWithDrawable(R.id.voice_button, d);
3127 }
3128
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003129 /**
3130 * Sets the app market icon (shown when all apps is visible on x-large screens)
3131 */
3132 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003133 if (LauncherApplication.isScreenXLarge()) {
3134 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3135 // Find the app market activity by resolving an intent.
3136 // (If multiple app markets are installed, it will return the ResolverActivity.)
3137 ComponentName activityName = intent.resolveActivity(getPackageManager());
3138 if (activityName != null) {
3139 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003141 R.id.market_button, activityName, R.drawable.app_market_generic);
3142 }
3143 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003144 }
3145
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003146 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003147 updateButtonWithDrawable(R.id.market_button, d);
3148 }
3149
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003150 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 * Displays the shortcut creation dialog and launches, if necessary, the
3152 * appropriate activity.
3153 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003154 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003155 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3156 DialogInterface.OnShowListener {
3157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003163 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3164 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003165 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 builder.setInverseBackgroundForced(true);
3168
3169 AlertDialog dialog = builder.create();
3170 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003171 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003172 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 return dialog;
3175 }
3176
3177 public void onCancel(DialogInterface dialog) {
3178 mWaitingForResult = false;
3179 cleanup();
3180 }
3181
Romain Guycbb89e42009-06-08 15:52:54 -07003182 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003183 }
3184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003186 try {
3187 dismissDialog(DIALOG_CREATE_SHORTCUT);
3188 } catch (Exception e) {
3189 // An exception is thrown if the dialog is not visible, which is fine
3190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 }
3192
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003193 /**
3194 * Handle the action clicked in the "Add to home" dialog.
3195 */
3196 public void onClick(DialogInterface dialog, int which) {
3197 Resources res = getResources();
3198 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003199
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003200 switch (which) {
3201 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003202 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003203 break;
3204 }
Romain Guycbb89e42009-06-08 15:52:54 -07003205
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003206 case AddAdapter.ITEM_APPWIDGET: {
3207 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003208
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003209 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3210 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003211 // start the pick activity
3212 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3213 break;
3214 }
Romain Guycbb89e42009-06-08 15:52:54 -07003215
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003216 case AddAdapter.ITEM_LIVE_FOLDER: {
3217 // Insert extra item to handle inserting folder
3218 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003219
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003220 ArrayList<String> shortcutNames = new ArrayList<String>();
3221 shortcutNames.add(res.getString(R.string.group_folder));
3222 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003223
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003224 ArrayList<ShortcutIconResource> shortcutIcons =
3225 new ArrayList<ShortcutIconResource>();
3226 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3227 R.drawable.ic_launcher_folder));
3228 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3229
3230 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3231 pickIntent.putExtra(Intent.EXTRA_INTENT,
3232 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3233 pickIntent.putExtra(Intent.EXTRA_TITLE,
3234 getText(R.string.title_select_live_folder));
3235 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003236
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003237 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3238 break;
3239 }
3240
3241 case AddAdapter.ITEM_WALLPAPER: {
3242 startWallpaper();
3243 break;
3244 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 }
3246 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003247
3248 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003249 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 }
3252
3253 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003254 * Receives notifications when applications are added/removed.
3255 */
3256 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3257 @Override
3258 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003259 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003260 String reason = intent.getStringExtra("reason");
3261 if (!"homekey".equals(reason)) {
3262 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003263 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003264 animate = false;
3265 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003266 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003267 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003268 }
3269 }
3270
3271 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003272 * Receives notifications whenever the appwidgets are reset.
3273 */
3274 private class AppWidgetResetObserver extends ContentObserver {
3275 public AppWidgetResetObserver() {
3276 super(new Handler());
3277 }
3278
3279 @Override
3280 public void onChange(boolean selfChange) {
3281 onAppWidgetReset();
3282 }
3283 }
3284
3285 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003286 * If the activity is currently paused, signal that we need to re-run the loader
3287 * in onResume.
3288 *
3289 * This needs to be called from incoming places where resources might have been loaded
3290 * while we are paused. That is becaues the Configuration might be wrong
3291 * when we're not running, and if it comes back to what it was when we
3292 * were paused, we are not restarted.
3293 *
3294 * Implementation of the method from LauncherModel.Callbacks.
3295 *
3296 * @return true if we are currently paused. The caller might be able to
3297 * skip some work in that case since we will come back again.
3298 */
3299 public boolean setLoadOnResume() {
3300 if (mPaused) {
3301 Log.i(TAG, "setLoadOnResume");
3302 mOnResumeNeedsLoad = true;
3303 return true;
3304 } else {
3305 return false;
3306 }
3307 }
3308
3309 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003310 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003311 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003313 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003314 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003315 } else {
3316 return SCREEN_COUNT / 2;
3317 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003318 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003319
Winson Chunga12a2502010-12-20 14:41:35 -08003320 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003321 mAllAppsPagedView = view;
3322 }
3323
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 /**
3325 * Refreshes the shortcuts shown on the workspace.
3326 *
3327 * Implementation of the method from LauncherModel.Callbacks.
3328 */
3329 public void startBinding() {
3330 final Workspace workspace = mWorkspace;
3331 int count = workspace.getChildCount();
3332 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003333 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung6e314082011-01-27 16:46:51 -08003334 final ViewGroup layout = ((CellLayout) workspace.getChildAt(i)).getChildrenLayout();
3335 layout.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003337
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 if (DEBUG_USER_INTERFACE) {
3339 android.widget.Button finishButton = new android.widget.Button(this);
3340 finishButton.setText("Finish");
3341 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3342
3343 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3344 public void onClick(View v) {
3345 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003348 }
3349 }
3350
3351 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 * Bind the items start-end from the list.
3353 *
3354 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003355 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3357
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003358 setLoadOnResume();
3359
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 final Workspace workspace = mWorkspace;
3361
3362 for (int i=start; i<end; i++) {
3363 final ItemInfo item = shortcuts.get(i);
3364 mDesktopItems.add(item);
3365 switch (item.itemType) {
3366 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3367 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003368 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3370 false);
3371 break;
3372 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3373 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003374 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003375 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3377 false);
3378 break;
3379 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3380 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3381 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003382 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 (LiveFolderInfo) item);
3384 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3385 false);
3386 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003388 }
3389
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003391 }
3392
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 /**
3394 * Implementation of the method from LauncherModel.Callbacks.
3395 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003396 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003397 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003398 sFolders.clear();
3399 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401
3402 /**
3403 * Add the views for a widget to the workspace.
3404 *
3405 * Implementation of the method from LauncherModel.Callbacks.
3406 */
3407 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003408 setLoadOnResume();
3409
Daniel Sandler843e8602010-06-07 14:59:01 -04003410 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3411 if (DEBUG_WIDGETS) {
3412 Log.d(TAG, "bindAppWidget: " + item);
3413 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 final Workspace workspace = mWorkspace;
3415
3416 final int appWidgetId = item.appWidgetId;
3417 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003418 if (DEBUG_WIDGETS) {
3419 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3420 }
3421
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3423
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3425 item.hostView.setTag(item);
3426
3427 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3428 item.cellY, item.spanX, item.spanY, false);
3429
Adam Cohended9f8d2010-11-03 13:25:16 -07003430 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3431
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 workspace.requestLayout();
3433
3434 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003435
3436 if (DEBUG_WIDGETS) {
3437 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3438 + (SystemClock.uptimeMillis()-start) + "ms");
3439 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 }
3441
3442 /**
3443 * Callback saying that there aren't any more items to bind.
3444 *
3445 * Implementation of the method from LauncherModel.Callbacks.
3446 */
3447 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003448 setLoadOnResume();
3449
Joe Onorato9c1289c2009-08-17 11:03:03 -04003450 if (mSavedState != null) {
3451 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003452 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453 }
3454
3455 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3456 if (userFolders != null) {
3457 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003458 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 if (info != null) {
3460 openFolder(info);
3461 }
3462 }
3463 final Folder openFolder = mWorkspace.getOpenFolder();
3464 if (openFolder != null) {
3465 openFolder.requestFocus();
3466 }
3467 }
3468
Joe Onorato9c1289c2009-08-17 11:03:03 -04003469 mSavedState = null;
3470 }
3471
3472 if (mSavedInstanceState != null) {
3473 super.onRestoreInstanceState(mSavedInstanceState);
3474 mSavedInstanceState = null;
3475 }
3476
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 mWorkspaceLoading = false;
3478 }
3479
3480 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003481 * Updates the icons on the launcher that are affected by changes to the package list
3482 * on the device.
3483 */
3484 private void updateIconsAffectedByPackageManagerChanges() {
3485 updateAppMarketIcon();
3486 updateGlobalSearchIcon();
3487 updateVoiceSearchIcon();
3488 }
3489
3490 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 * Add the icons for all apps.
3492 *
3493 * Implementation of the method from LauncherModel.Callbacks.
3494 */
3495 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003496 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003497 if (mCustomizePagedView != null) {
3498 mCustomizePagedView.setApps(apps);
3499 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003500 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 }
3502
3503 /**
3504 * A package was installed.
3505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
3507 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003508 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003509 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003511 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003512 if (mCustomizePagedView != null) {
3513 mCustomizePagedView.addApps(apps);
3514 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003515 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 }
3517
3518 /**
3519 * A package was updated.
3520 *
3521 * Implementation of the method from LauncherModel.Callbacks.
3522 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003523 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003524 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003526 mWorkspace.updateShortcuts(apps);
3527 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003528 if (mCustomizePagedView != null) {
3529 mCustomizePagedView.updateApps(apps);
3530 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003531 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 }
3533
3534 /**
3535 * A package was uninstalled.
3536 *
3537 * Implementation of the method from LauncherModel.Callbacks.
3538 */
Joe Onorato36115782010-06-17 13:28:48 -04003539 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003541 if (permanent) {
3542 mWorkspace.removeItems(apps);
3543 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003544 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003545 if (mCustomizePagedView != null) {
3546 mCustomizePagedView.removeApps(apps);
3547 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003548 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003549 }
Joe Onoratobe386092009-11-17 17:32:16 -08003550
3551 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003552 * A number of packages were updated.
3553 */
3554 public void bindPackagesUpdated() {
3555 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003556 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003557 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003558 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003559 }
3560
Winson Chung400438b2011-01-16 17:53:48 -08003561 private int mapConfigurationOriActivityInfoOri(int configOri) {
3562 final Display d = getWindowManager().getDefaultDisplay();
3563 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3564 switch (d.getRotation()) {
3565 case Surface.ROTATION_0:
3566 case Surface.ROTATION_180:
3567 // We are currently in the same basic orientation as the natural orientation
3568 naturalOri = configOri;
3569 break;
3570 case Surface.ROTATION_90:
3571 case Surface.ROTATION_270:
3572 // We are currently in the other basic orientation to the natural orientation
3573 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3574 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3575 break;
3576 }
3577
3578 int[] oriMap = {
3579 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3580 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3581 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3582 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3583 };
3584 // Since the map starts at portrait, we need to offset if this device's natural orientation
3585 // is landscape.
3586 int indexOffset = 0;
3587 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3588 indexOffset = 1;
3589 }
3590 return oriMap[(d.getRotation() + indexOffset) % 4];
3591 }
3592 public void lockScreenOrientation() {
3593 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3594 .getConfiguration().orientation));
3595 }
3596 public void unlockScreenOrientation() {
3597 mHandler.postDelayed(new Runnable() {
3598 public void run() {
3599 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3600 }
3601 }, mRestoreScreenOrientationDelay);
3602 }
3603
Winson Chung80baf5a2010-08-09 16:03:15 -07003604 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003605 * Prints out out state for debugging.
3606 */
3607 public void dumpState() {
3608 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003609 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003610 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3611 Log.d(TAG, "mRestoring=" + mRestoring);
3612 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3613 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3614 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003615 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003616 mModel.dumpState();
3617 mAllAppsGrid.dumpState();
3618 Log.d(TAG, "END launcher2 dump state");
3619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003620}