blob: 59ca9b308d547abc53b0f6947daafea437637b2b [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.CustomizePagedView.CustomizationType;
23import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070029import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080031import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080048import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080049import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080052import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040056import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080059import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080061import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040063import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070064import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020066import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080067import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070068import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080074import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080087import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080091import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080092import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080093import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070096import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070098import android.widget.ImageView;
99import android.widget.LinearLayout;
100import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700101import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800102import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800105import android.widget.TabHost.OnTabChangeListener;
106import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700107
Michael Jurka742574b2011-02-02 23:51:01 -0800108import java.io.DataInputStream;
109import java.io.DataOutputStream;
110import java.io.FileNotFoundException;
111import java.io.IOException;
112import java.util.ArrayList;
113import java.util.HashMap;
114import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800115
Adam Cohended9f8d2010-11-03 13:25:16 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Michael Jurka946ad472010-07-09 18:05:18 -0700120public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
122 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800131 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700134 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
135 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
137 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700138 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 private static final int REQUEST_CREATE_SHORTCUT = 1;
141 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_PICK_APPLICATION = 6;
144 private static final int REQUEST_PICK_SHORTCUT = 7;
145 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 static final int SCREEN_COUNT = 5;
152 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Joe Onorato7c312c12009-08-13 21:36:53 -0700154 static final int DIALOG_CREATE_SHORTCUT = 1;
155 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Romain Guy98d01652009-06-30 16:21:04 -0700157 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
159 // Type: int
160 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700161 // Type: int
162 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: long
164 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
175
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 // tags for the customization tabs
177 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700178 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700179 private static final String SHORTCUTS_TAG = "shortcuts";
180 private static final String WALLPAPERS_TAG = "wallpapers";
181
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700182 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
183
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800185 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800186 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800195 private final BroadcastReceiver mCloseSystemDialogsReceiver
196 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800197 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private LayoutInflater mInflater;
200
Joe Onorato00acb122009-08-04 16:04:30 -0400201 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private AppWidgetManager mAppWidgetManager;
205 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
Michael Jurka0280c3b2010-09-17 15:00:07 -0700207 private int mAddScreen = -1;
208 private int mAddIntersectCellX = -1;
209 private int mAddIntersectCellY = -1;
210 private int[] mAddDropPosition;
211 private int[] mTmpAddItemCellCoordinates = new int[2];
212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private FolderInfo mFolderInfo;
214
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700217 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700218 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700219 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mButtonCluster;
222 private View mAllAppsButton;
223 private View mDivider;
224 private View mConfigureButton;
225
Winson Chunga12a2502010-12-20 14:41:35 -0800226 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700227 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private Bundle mSavedState;
230
231 private SpannableStringBuilder mDefaultKeySsb = null;
232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 private boolean mWorkspaceLoading = true;
234
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800235 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private boolean mRestoring;
237 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700238 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private Bundle mSavedInstanceState;
241
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800244 private boolean mUserPresent = true;
245 private boolean mVisible = false;
246 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700248 private static LocaleConfiguration sLocaleConfiguration = null;
249
Romain Guy84f296c2009-11-04 15:00:44 -0800250 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700251
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700252 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Romain Guy1fbc1c82009-11-09 20:43:08 -0800254 private ImageView mPreviousView;
255 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500256
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400257 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400258 private String[] mHotseatConfig = null;
259 private Intent[] mHotseats = null;
260 private Drawable[] mHotseatIcons = null;
261 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800279 private static Drawable.ConstantState sGlobalSearchIcon;
280 private static Drawable.ConstantState sVoiceSearchIcon;
281 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Winson Chunga12a2502010-12-20 14:41:35 -0800283 private CustomizationType getCustomizeFilterForTabTag(String tag) {
284 if (tag.equals(WIDGETS_TAG)) {
285 return CustomizationType.WidgetCustomization;
286 } else if (tag.equals(APPLICATIONS_TAG)) {
287 return CustomizationType.ApplicationCustomization;
288 } else if (tag.equals(WALLPAPERS_TAG)) {
289 return CustomizePagedView.CustomizationType.WallpaperCustomization;
290 } else if (tag.equals(SHORTCUTS_TAG)) {
291 return CustomizePagedView.CustomizationType.ShortcutCustomization;
292 }
293 return CustomizationType.WidgetCustomization;
294 }
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 @Override
297 protected void onCreate(Bundle savedInstanceState) {
298 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700299
Winson Chungaafa03c2010-06-11 17:34:16 -0700300 if (LauncherApplication.isInPlaceRotationEnabled()) {
301 // hide the status bar (temporary until we get the status bar design figured out)
302 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
303 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
304 }
305
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 LauncherApplication app = ((LauncherApplication)getApplication());
307 mModel = app.setLauncher(this);
308 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400309 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700313 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
314 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200317 android.os.Debug.startMethodTracing(
318 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400321 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800324 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700325 if (mHomeCustomizationDrawer != null) {
326 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700327
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700329 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
330 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 TabContentFactory contentFactory = new TabContentFactory() {
332 public View createTabContent(String tag) {
333 return mCustomizePagedView;
334 }
335 };
336
Winson Chung49767ae2010-11-29 14:48:30 -0800337
338 TextView tabView;
339 TabWidget tabWidget = (TabWidget)
340 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
341
342 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
343 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700344 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800345 .setIndicator(tabView).setContent(contentFactory));
346 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
347 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700348 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800349 .setIndicator(tabView).setContent(contentFactory));
350 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
351 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700352 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800353 .setIndicator(tabView).setContent(contentFactory));
354 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
355 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700356 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800357 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700358 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
359 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800360 final CustomizePagedView.CustomizationType newType =
361 getCustomizeFilterForTabTag(tabId);
362 if (newType != mCustomizePagedView.getCustomizationFilter()) {
363 // animate the changing of the tab content by fading pages in and out
364 final Resources res = getResources();
365 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
366 final float alpha = mCustomizePagedView.getAlpha();
367 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
368 "alpha", alpha, 0.0f);
369 alphaAnim.setDuration(duration);
370 alphaAnim.addListener(new AnimatorListenerAdapter() {
371 @Override
372 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800373 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700374
Winson Chunga12a2502010-12-20 14:41:35 -0800375 final float alpha = mCustomizePagedView.getAlpha();
376 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
377 mCustomizePagedView, "alpha", alpha, 1.0f);
378 alphaAnim.setDuration(duration);
379 alphaAnim.start();
380 }
381 });
382 alphaAnim.start();
383 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700384 }
385 });
Michael Jurka946ad472010-07-09 18:05:18 -0700386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 setupViews();
388
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800389 registerContentObservers();
390
Joe Onorato7c312c12009-08-13 21:36:53 -0700391 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 mSavedState = savedInstanceState;
394 restoreState(mSavedState);
395
Winson Chunga12a2502010-12-20 14:41:35 -0800396 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800397 if (mCustomizePagedView != null) {
398 mCustomizePagedView.update();
399 }
Winson Chunga12a2502010-12-20 14:41:35 -0800400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 if (PROFILE_STARTUP) {
402 android.os.Debug.stopMethodTracing();
403 }
404
405 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
409 // For handling default keys
410 mDefaultKeySsb = new SpannableStringBuilder();
411 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800412
413 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
414 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800415
416 // If we have a saved version of these external icons, we load them up immediately
417 if (LauncherApplication.isScreenXLarge()) {
418 if (sGlobalSearchIcon != null) {
419 updateGlobalSearchIcon(sGlobalSearchIcon);
420 }
421 if (sVoiceSearchIcon != null) {
422 updateVoiceSearchIcon(sVoiceSearchIcon);
423 }
424 if (sAppMarketIcon != null) {
425 updateAppMarketIcon(sAppMarketIcon);
426 }
427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800430 @Override
431 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
432 super.dispatchPopulateAccessibilityEvent(event);
433
434 // we want to take over text population so it is context dependent
435 event.getText().clear();
436 if (mState == State.ALL_APPS) {
437 event.getText().add(getString(R.string.all_apps_button_label));
438 } else if (mState == State.WORKSPACE) {
439 event.getText().add(getString(R.string.all_apps_home_button_label));
440 }
441
442 return true;
443 }
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700446 if (sLocaleConfiguration == null) {
447 new AsyncTask<Void, Void, LocaleConfiguration>() {
448 @Override
449 protected LocaleConfiguration doInBackground(Void... unused) {
450 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
451 readConfiguration(Launcher.this, localeConfiguration);
452 return localeConfiguration;
453 }
454
455 @Override
456 protected void onPostExecute(LocaleConfiguration result) {
457 sLocaleConfiguration = result;
458 checkForLocaleChange(); // recursive, but now with a locale configuration
459 }
460 }.execute();
461 return;
462 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 final Configuration configuration = getResources().getConfiguration();
465
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700466 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 final String locale = configuration.locale.toString();
468
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700469 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 final int mcc = configuration.mcc;
471
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 final int mnc = configuration.mnc;
474
Romain Guy84f296c2009-11-04 15:00:44 -0800475 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
Romain Guy84f296c2009-11-04 15:00:44 -0800477 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478 sLocaleConfiguration.locale = locale;
479 sLocaleConfiguration.mcc = mcc;
480 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700481
Joe Onorato0589f0f2010-02-08 13:44:00 -0800482 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400483 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484
485 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
486 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700487 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 public void run() {
489 writeConfiguration(Launcher.this, localeConfiguration);
490 }
491 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700492 }
493 }
494
495 private static class LocaleConfiguration {
496 public String locale;
497 public int mcc = -1;
498 public int mnc = -1;
499 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700500
Romain Guy98d01652009-06-30 16:21:04 -0700501 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
502 DataInputStream in = null;
503 try {
504 in = new DataInputStream(context.openFileInput(PREFERENCES));
505 configuration.locale = in.readUTF();
506 configuration.mcc = in.readInt();
507 configuration.mnc = in.readInt();
508 } catch (FileNotFoundException e) {
509 // Ignore
510 } catch (IOException e) {
511 // Ignore
512 } finally {
513 if (in != null) {
514 try {
515 in.close();
516 } catch (IOException e) {
517 // Ignore
518 }
519 }
520 }
521 }
522
523 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
524 DataOutputStream out = null;
525 try {
526 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
527 out.writeUTF(configuration.locale);
528 out.writeInt(configuration.mcc);
529 out.writeInt(configuration.mnc);
530 out.flush();
531 } catch (FileNotFoundException e) {
532 // Ignore
533 } catch (IOException e) {
534 //noinspection ResultOfMethodCallIgnored
535 context.getFileStreamPath(PREFERENCES).delete();
536 } finally {
537 if (out != null) {
538 try {
539 out.close();
540 } catch (IOException e) {
541 // Ignore
542 }
543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
547 static int getScreen() {
548 synchronized (sLock) {
549 return sScreen;
550 }
551 }
552
553 static void setScreen(int screen) {
554 synchronized (sLock) {
555 sScreen = screen;
556 }
557 }
558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 // Note: This doesn't do all the client-id magic that BrowserProvider does
560 // in Browser. (http://b/2425179)
561 private Uri getDefaultBrowserUri() {
562 String url = getString(R.string.default_browser_url);
563 if (url.indexOf("{CID}") != -1) {
564 url = url.replace("{CID}", "android-google");
565 }
566 return Uri.parse(url);
567 }
568
569 // Load the Intent templates from arrays.xml to populate the hotseats. For
570 // each Intent, if it resolves to a single app, use that as the launch
571 // intent & use that app's label as the contentDescription. Otherwise,
572 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 if (mHotseatConfig == null) {
575 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
576 if (mHotseatConfig.length > 0) {
577 mHotseats = new Intent[mHotseatConfig.length];
578 mHotseatLabels = new CharSequence[mHotseatConfig.length];
579 mHotseatIcons = new Drawable[mHotseatConfig.length];
580 } else {
581 mHotseats = null;
582 mHotseatIcons = null;
583 mHotseatLabels = null;
584 }
585
586 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
587 for (int i=0; i<mHotseatConfig.length; i++) {
588 // load icon for this slot; currently unrelated to the actual activity
589 try {
590 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
591 } catch (ArrayIndexOutOfBoundsException ex) {
592 Log.w(TAG, "Missing hotseat_icons array item #" + i);
593 mHotseatIcons[i] = null;
594 }
595 }
596 hotseatIconDrawables.recycle();
597 }
598
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400599 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 for (int i=0; i<mHotseatConfig.length; i++) {
601 Intent intent = null;
602 if (mHotseatConfig[i].equals("*BROWSER*")) {
603 // magic value meaning "launch user's default web browser"
604 // replace it with a generic web request so we can see if there is indeed a default
605 String defaultUri = getString(R.string.default_browser_url);
606 intent = new Intent(
607 Intent.ACTION_VIEW,
608 ((defaultUri != null)
609 ? Uri.parse(defaultUri)
610 : getDefaultBrowserUri())
611 ).addCategory(Intent.CATEGORY_BROWSABLE);
612 // note: if the user launches this without a default set, she
613 // will always be taken to the default URL above; this is
614 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400616 // URL is unavoidably sent to the chosen app.
617 } else {
618 try {
619 intent = Intent.parseUri(mHotseatConfig[i], 0);
620 } catch (java.net.URISyntaxException ex) {
621 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
622 // bogus; leave intent=null
623 }
624 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700625
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400626 if (intent == null) {
627 mHotseats[i] = null;
628 mHotseatLabels[i] = getText(R.string.activity_not_found);
629 continue;
630 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631
632 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700633 Log.d(TAG, "loadHotseats: hotseat " + i
634 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400635 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400636 + "]");
637 }
638
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400639 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
640 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 Log.d(TAG, "Best match for intent: " + bestMatch);
643 Log.d(TAG, "All matches: ");
644 for (ResolveInfo ri : allMatches) {
645 Log.d(TAG, " --> " + ri);
646 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400647 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400648 // did this resolve to a single app, or the resolver?
649 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 // can't find any activity to handle this. let's leave the
651 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400652 // to launch.
653 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400654
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 // set accessibility text to "Not installed"
656 mHotseatLabels[i] = getText(R.string.activity_not_found);
657 } else {
658 boolean found = false;
659 for (ResolveInfo ri : allMatches) {
660 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
661 && bestMatch.activityInfo.applicationInfo.packageName
662 .equals(ri.activityInfo.applicationInfo.packageName)) {
663 found = true;
664 break;
665 }
666 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700667
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400668 if (!found) {
669 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
670 // the bestMatch is probably the ResolveActivity, meaning the
671 // user has not yet selected a default
672 // so: we'll keep the original intent for now
673 mHotseats[i] = intent;
674
675 // set the accessibility text to "Select shortcut"
676 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
677 } else {
678 // we have an app!
679 // now reconstruct the intent to launch it through the front
680 // door
681 ComponentName com = new ComponentName(
682 bestMatch.activityInfo.applicationInfo.packageName,
683 bestMatch.activityInfo.name);
684 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
685
686 // load the app label for accessibility
687 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
688 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400689 }
690
691 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 Log.d(TAG, "loadHotseats: hotseat " + i
693 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400694 + ((mHotseats[i] == null)
695 ? "null"
696 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400697 + "] label=[" + mHotseatLabels[i]
698 + "]"
699 );
700 }
701 }
702 }
703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 @Override
705 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700706 mWaitingForResult = false;
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // The pattern used here is that a user PICKs a specific application,
709 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
712 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 switch (requestCode) {
716 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700717 completeAddApplication(
718 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 break;
720 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800721 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 break;
723 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700724 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
726 case REQUEST_PICK_LIVE_FOLDER:
727 addLiveFolder(data);
728 break;
729 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700730 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700737 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700739 case REQUEST_PICK_WALLPAPER:
740 // We just wanted the activity result here so we can clear mWaitingForResult
741 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
Romain Guy18042c82009-11-06 11:44:55 -0800743 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
744 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
745 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700746 // Clean up the appWidgetId if we canceled
747 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
748 if (appWidgetId != -1) {
749 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
755 protected void onResume() {
756 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800757 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700758 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 mWorkspaceLoading = true;
760 mModel.startLoader(this, true);
761 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700762 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700764 // When we resume Launcher, a different Activity might be responsible for the app
765 // market intent, so refresh the icon
766 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
769 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700770 protected void onPause() {
771 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 // Some launcher layouts don't have a previous and next view
773 if (mPreviousView != null) {
774 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700775 }
776 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 dismissPreview(mNextView);
778 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700779 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800780 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700781 }
Romain Guycbb89e42009-06-08 15:52:54 -0700782
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700783 @Override
784 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 // Flag the loader to stop early before switching
786 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800787 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800788 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700789 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700790
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800791 // We can't hide the IME if it was forced open. So don't bother
792 /*
793 @Override
794 public void onWindowFocusChanged(boolean hasFocus) {
795 super.onWindowFocusChanged(hasFocus);
796
797 if (hasFocus) {
798 final InputMethodManager inputManager = (InputMethodManager)
799 getSystemService(Context.INPUT_METHOD_SERVICE);
800 WindowManager.LayoutParams lp = getWindow().getAttributes();
801 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
802 android.os.Handler()) {
803 protected void onReceiveResult(int resultCode, Bundle resultData) {
804 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
805 }
806 });
807 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
808 }
809 }
810 */
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 private boolean acceptFilter() {
813 final InputMethodManager inputManager = (InputMethodManager)
814 getSystemService(Context.INPUT_METHOD_SERVICE);
815 return !inputManager.isFullscreenMode();
816 }
817
818 @Override
819 public boolean onKeyDown(int keyCode, KeyEvent event) {
820 boolean handled = super.onKeyDown(keyCode, event);
821 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
822 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
823 keyCode, event);
824 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700825 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700826 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700827 // showSearchDialog()
828 // If there are multiple keystrokes before the search dialog takes focus,
829 // onSearchRequested() will be called for every keystroke,
830 // but it is idempotent, so it's fine.
831 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833 }
834
Joe Onorato8a9625e2010-01-28 15:55:35 -0800835 // Eat the long press event so the keyboard doesn't come up.
836 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
837 return true;
838 }
839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 return handled;
841 }
842
Karl Rosaen138a0412009-04-23 19:00:21 -0700843 private String getTypedText() {
844 return mDefaultKeySsb.toString();
845 }
846
847 private void clearTypedText() {
848 mDefaultKeySsb.clear();
849 mDefaultKeySsb.clearSpans();
850 Selection.setSelection(mDefaultKeySsb, 0);
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700854 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
855 * State
856 */
857 private static State intToState(int stateOrdinal) {
858 State state = State.WORKSPACE;
859 final State[] stateValues = State.values();
860 for (int i = 0; i < stateValues.length; i++) {
861 if (stateValues[i].ordinal() == stateOrdinal) {
862 state = stateValues[i];
863 break;
864 }
865 }
866 return state;
867 }
868
869 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 * Restores the previous state, if it exists.
871 *
872 * @param savedState The previous state.
873 */
874 private void restoreState(Bundle savedState) {
875 if (savedState == null) {
876 return;
877 }
878
Michael Jurka883f55b2010-10-21 15:47:14 -0700879 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
880
881 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800882 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700883 } else if (state == State.CUSTOMIZE) {
884 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
888 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700889 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895 mAddScreen = addScreen;
896 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
897 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 mRestoring = true;
899 }
900
901 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
902 if (renameFolder) {
903 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700904 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 mRestoring = true;
906 }
Winson Chunga12a2502010-12-20 14:41:35 -0800907
908 // Restore the current AllApps drawer tab
909 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
910 String curTab = savedState.getString("allapps_currentTab");
911 if (curTab != null) {
912 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
913 tabhost.setCurrentTabByTag(curTab);
914 }
915 int curPage = savedState.getInt("allapps_currentPage", -1);
916 if (curPage > -1) {
917 mAllAppsPagedView.setRestorePage(curPage);
918 }
919 }
920
921 // Restore the current customization drawer tab
922 if (mHomeCustomizationDrawer != null) {
923 String curTab = savedState.getString("customize_currentTab");
924 if (curTab != null) {
925 // We set this directly so that there is no delay before the tab is set
926 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
927 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
928 }
929
930 // Note: currently we do not restore the page for the customization tray because unlike
931 // AllApps, the page content can change drastically
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
935 /**
936 * Finds all the views we need and configure them properly.
937 */
938 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700939 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400940
Romain Guyb1b69f52009-08-10 15:10:15 -0700941 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400942 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943
Joe Onorato7c312c12009-08-13 21:36:53 -0700944 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700945 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700946 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800947 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700948 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700949 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700950
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700951 if (LauncherApplication.isScreenXLarge()) {
952 // They need to be INVISIBLE initially so that they will be measured in the layout.
953 // Otherwise the animations are messed up when we show them for the first time.
954 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
955 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
956 }
957
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500961 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700962
Joe Onorato7c312c12009-08-13 21:36:53 -0700963 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
964 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700966 View handleView = findViewById(R.id.all_apps_button);
967 if (handleView != null && handleView instanceof HandleView) {
968 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700969 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700970 mHandleView.setLauncher(this);
971 mHandleView.setOnClickListener(this);
972 mHandleView.setOnLongClickListener(this);
973 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800974
Winson Chung80baf5a2010-08-09 16:03:15 -0700975 if (mCustomizePagedView != null) {
976 mCustomizePagedView.setLauncher(this);
977 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700978 } else {
979 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
980 hotseatLeft.setContentDescription(mHotseatLabels[0]);
981 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
982 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
983 hotseatRight.setContentDescription(mHotseatLabels[1]);
984 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400985
Michael Jurkaaf442092010-06-10 17:01:57 -0700986 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
987 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800988
Michael Jurkaaf442092010-06-10 17:01:57 -0700989 Drawable previous = mPreviousView.getDrawable();
990 Drawable next = mNextView.getDrawable();
991 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
Michael Jurkaaf442092010-06-10 17:01:57 -0700993 mPreviousView.setHapticFeedbackEnabled(false);
994 mPreviousView.setOnLongClickListener(this);
995 mNextView.setHapticFeedbackEnabled(false);
996 mNextView.setOnLongClickListener(this);
997 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800998
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001000 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001002 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003
1004 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001005 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001006
1007 final View allAppsButton = findViewById(R.id.all_apps_button);
1008 final View divider = findViewById(R.id.divider);
1009 final View configureButton = findViewById(R.id.configure_button);
1010
Michael Jurka54f7ac32010-08-02 13:56:46 -07001011 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001012 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001013 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001014 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001015 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001016 dragController.addDragListener(deleteZone);
1017
Adam Cohencdc30d52010-12-01 15:09:47 -08001018 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1019 if (allAppsDeleteZone != null) {
1020 allAppsDeleteZone.setLauncher(this);
1021 allAppsDeleteZone.setDragController(dragController);
1022 allAppsDeleteZone.setDragAndDropEnabled(false);
1023 dragController.addDragListener(allAppsDeleteZone);
1024 dragController.addDropTarget(allAppsDeleteZone);
1025 }
1026
1027 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1028 findViewById(R.id.all_apps_info_target);
1029 if (allAppsInfoTarget != null) {
1030 allAppsInfoTarget.setLauncher(this);
1031 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001032 allAppsInfoTarget.setDragAndDropEnabled(false);
1033 View marketButton = findViewById(R.id.market_button);
1034 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001035 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001036 }
1037 }
1038
Joe Onorato00acb122009-08-04 16:04:30 -04001039 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001040 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001041 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001042
Joe Onorato00acb122009-08-04 16:04:30 -04001043 // The order here is bottom to top.
1044 dragController.addDropTarget(workspace);
1045 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001046 if (allAppsInfoTarget != null) {
1047 dragController.addDropTarget(allAppsInfoTarget);
1048 }
1049 if (allAppsDeleteZone != null) {
1050 dragController.addDropTarget(allAppsDeleteZone);
1051 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001052 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1053
1054 mAllAppsButton = findViewById(R.id.all_apps_button);
1055 mDivider = findViewById(R.id.divider);
1056 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001057
1058 // We had previously set these click handlers in XML, but the first time we launched
1059 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1060 // found the right handler. Setting the handlers directly here eliminates that cost.
1061 if (mConfigureButton != null) {
1062 mConfigureButton.setOnClickListener(new OnClickListener() {
1063 public void onClick(View v) {
1064 onClickConfigureButton(v);
1065 }
1066 });
1067 }
1068 if (mDivider != null) {
1069 mDivider.setOnClickListener(new OnClickListener() {
1070 public void onClick(View v) {
1071 onClickAllAppsButton(v);
1072 }
1073 });
1074 }
1075 if (mAllAppsButton != null) {
1076 mAllAppsButton.setOnClickListener(new OnClickListener() {
1077 public void onClick(View v) {
1078 onClickAllAppsButton(v);
1079 }
1080 });
1081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083
Romain Guy8a73c512009-11-09 19:19:59 -08001084 @SuppressWarnings({"UnusedDeclaration"})
1085 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001086 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001087 mWorkspace.scrollLeft();
1088 }
Romain Guy8a73c512009-11-09 19:19:59 -08001089 }
1090
1091 @SuppressWarnings({"UnusedDeclaration"})
1092 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001093 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001094 mWorkspace.scrollRight();
1095 }
Romain Guy8a73c512009-11-09 19:19:59 -08001096 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001097
1098 @SuppressWarnings({"UnusedDeclaration"})
1099 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001100 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001101
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001102 int index = -1;
1103 if (v.getId() == R.id.hotseat_left) {
1104 index = 0;
1105 } else if (v.getId() == R.id.hotseat_right) {
1106 index = 1;
1107 }
1108
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001109 // reload these every tap; you never know when they might change
1110 loadHotseats();
1111 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001112 startActivitySafely(
1113 mHotseats[index],
1114 "hotseat"
1115 );
1116 }
1117 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 /**
1120 * Creates a view representing a shortcut.
1121 *
1122 * @param info The data structure describing the shortcut.
1123 *
1124 * @return A View inflated from R.layout.application.
1125 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001128 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
1131 /**
1132 * Creates a view representing a shortcut inflated from the specified resource.
1133 *
1134 * @param layoutResId The id of the XML layout used to create the shortcut.
1135 * @param parent The group the shortcut belongs to.
1136 * @param info The data structure describing the shortcut.
1137 *
1138 * @return A View inflated from layoutResId.
1139 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001140 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001141 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1142 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 return favorite;
1145 }
1146
1147 /**
1148 * Add an application shortcut to the workspace.
1149 *
1150 * @param data The intent describing the application.
1151 * @param cellInfo The position on screen where to create the shortcut.
1152 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001153 void completeAddApplication(Context context, Intent data, int screen,
1154 int intersectCellX, int intersectCellY) {
1155 final int[] cellXY = mTmpAddItemCellCoordinates;
1156 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1157
1158 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1159 showOutOfSpaceMessage();
1160 return;
1161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162
Joe Onorato0589f0f2010-02-08 13:44:00 -08001163 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1164 data, context);
1165
Romain Guy73b979d2009-06-09 12:57:21 -07001166 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001167 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001169 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001170 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1171 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001172 } else {
1173 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
Romain Guycbb89e42009-06-08 15:52:54 -07001176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 /**
1178 * Add a shortcut to the workspace.
1179 *
1180 * @param data The intent describing the shortcut.
1181 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183 private void completeAddShortcut(Intent data, int screen,
1184 int intersectCellX, int intersectCellY) {
1185 final int[] cellXY = mTmpAddItemCellCoordinates;
1186 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001188 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001189 boolean foundCellSpan = false;
1190 if (touchXY != null) {
1191 // when dragging and dropping, just find the closest free spot
1192 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1193 int[] result = screenLayout.findNearestVacantArea(
1194 touchXY[0], touchXY[1], 1, 1, cellXY);
1195 foundCellSpan = (result != null);
1196 } else {
1197 foundCellSpan = layout.findCellForSpanThatIntersects(
1198 cellXY, 1, 1, intersectCellX, intersectCellY);
1199 }
1200
1201 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 showOutOfSpaceMessage();
1203 return;
1204 }
1205
1206 final ShortcutInfo info = mModel.addShortcut(
1207 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208
1209 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
1214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001216 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001218 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001219 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001223
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1226 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001229 // We have saved the position to which the widget was dragged-- this really only matters
1230 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001232
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001233 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 boolean foundCellSpan = false;
1235 if (touchXY != null) {
1236 // when dragging and dropping, just find the closest free spot
1237 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1238 int[] result = screenLayout.findNearestVacantArea(
1239 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001240 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001242 // if we long pressed on an empty cell to bring up a menu,
1243 // make sure we intersect the empty cell
1244 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1245 // findCellForSpanThatIntersects will just ignore them
1246 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1247 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001248 }
1249
Michael Jurka0280c3b2010-09-17 15:00:07 -07001250 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001251 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001253 return;
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 // Build Launcher-specific widget info and save to database
1257 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 launcherInfo.spanX = spanXY[0];
1259 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 LauncherModel.addItemToDatabase(this, launcherInfo,
1262 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
1265 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001269 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001270
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001271 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001273
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001275 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001276
1277 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
Romain Guycbb89e42009-06-08 15:52:54 -07001280
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1282 @Override
1283 public void onReceive(Context context, Intent intent) {
1284 final String action = intent.getAction();
1285 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1286 mUserPresent = false;
1287 updateRunning();
1288 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1289 mUserPresent = true;
1290 updateRunning();
1291 }
1292 }
1293 };
1294
1295 @Override
1296 public void onAttachedToWindow() {
1297 super.onAttachedToWindow();
1298
1299 // Listen for broadcasts related to user-presence
1300 final IntentFilter filter = new IntentFilter();
1301 filter.addAction(Intent.ACTION_SCREEN_OFF);
1302 filter.addAction(Intent.ACTION_USER_PRESENT);
1303 registerReceiver(mReceiver, filter);
1304
Adam Cohend113e0c2010-11-11 10:48:05 -08001305 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001306 mVisible = true;
1307 }
1308
1309 @Override
1310 public void onDetachedFromWindow() {
1311 super.onDetachedFromWindow();
1312 mVisible = false;
1313
Adam Cohend113e0c2010-11-11 10:48:05 -08001314 if (mAttached) {
1315 unregisterReceiver(mReceiver);
1316 mAttached = false;
1317 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001318 updateRunning();
1319 }
1320
1321 public void onWindowVisibilityChanged(int visibility) {
1322 mVisible = visibility == View.VISIBLE;
1323 updateRunning();
1324 }
1325
1326 private void sendAdvanceMessage(long delay) {
1327 mHandler.removeMessages(ADVANCE_MSG);
1328 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1329 mHandler.sendMessageDelayed(msg, delay);
1330 mAutoAdvanceSentTime = System.currentTimeMillis();
1331 }
1332
1333 private void updateRunning() {
1334 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1335 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1336 mAutoAdvanceRunning = autoAdvanceRunning;
1337 if (autoAdvanceRunning) {
1338 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1339 sendAdvanceMessage(delay);
1340 } else {
1341 if (!mWidgetsToAdvance.isEmpty()) {
1342 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1343 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1344 }
1345 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001346 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 }
1348 }
1349 }
1350
1351 private final Handler mHandler = new Handler() {
1352 @Override
1353 public void handleMessage(Message msg) {
1354 if (msg.what == ADVANCE_MSG) {
1355 int i = 0;
1356 for (View key: mWidgetsToAdvance.keySet()) {
1357 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1358 final int delay = mAdvanceStagger * i;
1359 if (v instanceof Advanceable) {
1360 postDelayed(new Runnable() {
1361 public void run() {
1362 ((Advanceable) v).advance();
1363 }
1364 }, delay);
1365 }
1366 i++;
1367 }
1368 sendAdvanceMessage(mAdvanceInterval);
1369 }
1370 }
1371 };
1372
1373 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1374 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1375 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1376 if (v instanceof Advanceable) {
1377 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001378 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001379 updateRunning();
1380 }
1381 }
1382
1383 void removeWidgetToAutoAdvance(View hostView) {
1384 if (mWidgetsToAdvance.containsKey(hostView)) {
1385 mWidgetsToAdvance.remove(hostView);
1386 updateRunning();
1387 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001388 }
1389
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1391 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001392 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 launcherInfo.hostView = null;
1394 }
1395
Adam Cohended9f8d2010-11-03 13:25:16 -07001396 void showOutOfSpaceMessage() {
1397 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1398 }
1399
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001400 public LauncherAppWidgetHost getAppWidgetHost() {
1401 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Winson Chunga9abd0e2010-10-27 17:18:37 -07001404 public LauncherModel getModel() {
1405 return mModel;
1406 }
1407
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001408 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001409 getWindow().closeAllPanels();
1410
1411 try {
1412 dismissDialog(DIALOG_CREATE_SHORTCUT);
1413 // Unlock the workspace if the dialog was showing
1414 } catch (Exception e) {
1415 // An exception is thrown if the dialog is not visible, which is fine
1416 }
1417
1418 try {
1419 dismissDialog(DIALOG_RENAME_FOLDER);
1420 // Unlock the workspace if the dialog was showing
1421 } catch (Exception e) {
1422 // An exception is thrown if the dialog is not visible, which is fine
1423 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001424
1425 // Whatever we were doing is hereby canceled.
1426 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001427 }
1428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 @Override
1430 protected void onNewIntent(Intent intent) {
1431 super.onNewIntent(intent);
1432
1433 // Close the menu
1434 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001435 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001436 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001437
Joe Onorato14f122b2009-11-19 14:06:36 -08001438 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1439 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001440
Michael Jurka4cb37242010-08-09 21:05:32 -07001441 // in all these cases, only animate if we're already on home
1442 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001443 mWorkspace.unshrink(alreadyOnHome);
1444 }
1445 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001446 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001447 boolean animate = alreadyOnHome &&
1448 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1449 mWorkspace.moveToDefaultScreen(animate);
1450 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001451 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001452 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001453
Joe Onorato3a8820b2009-11-10 15:06:42 -08001454 final View v = getWindow().peekDecorView();
1455 if (v != null && v.getWindowToken() != null) {
1456 InputMethodManager imm = (InputMethodManager)getSystemService(
1457 INPUT_METHOD_SERVICE);
1458 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460 }
1461 }
1462
1463 @Override
1464 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1465 // Do not call super here
1466 mSavedInstanceState = savedInstanceState;
1467 }
1468
1469 @Override
1470 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001471 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
1473 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1474 if (folders.size() > 0) {
1475 final int count = folders.size();
1476 long[] ids = new long[count];
1477 for (int i = 0; i < count; i++) {
1478 final FolderInfo info = folders.get(i).getInfo();
1479 ids[i] = info.id;
1480 }
1481 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1482 } else {
1483 super.onSaveInstanceState(outState);
1484 }
1485
Michael Jurka883f55b2010-10-21 15:47:14 -07001486 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 if (mAddScreen > -1 && mWaitingForResult) {
1489 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1490 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1491 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 if (mFolderInfo != null && mWaitingForResult) {
1495 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1496 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1497 }
Michael Jurka946ad472010-07-09 18:05:18 -07001498
Winson Chung43b119d2010-12-06 16:45:05 -08001499 // Save the current AllApps drawer tab
1500 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1501 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1502 String currentTabTag = tabhost.getCurrentTabTag();
1503 if (currentTabTag != null) {
1504 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001505 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001506 }
1507 }
1508
1509 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001510 if (mHomeCustomizationDrawer != null) {
1511 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1512 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001513 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001514 }
1515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
1518 @Override
1519 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001525 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001527 mAppWidgetHost = null;
1528
1529 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
1531 TextKeyListener.getInstance().release();
1532
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
Joe Onorato9c1289c2009-08-17 11:03:03 -04001535 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001536
1537 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001538
Patrick Dubroyab962b72010-07-26 12:10:10 -07001539 // Some launcher layouts don't have a previous and next view
1540 if (mPreviousView != null) {
1541 dismissPreview(mPreviousView);
1542 }
1543 if (mNextView != null) {
1544 dismissPreview(mNextView);
1545 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001546
1547 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001548
1549 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1550 mWorkspace.removeAllViews();
1551 mWorkspace = null;
1552 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001553
1554 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
1557 @Override
1558 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001559 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 super.startActivityForResult(intent, requestCode);
1561 }
1562
1563 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001564 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001566
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001567 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001568
Karl Rosaen138a0412009-04-23 19:00:21 -07001569 if (initialQuery == null) {
1570 // Use any text typed in the launcher as the initial query
1571 initialQuery = getTypedText();
1572 clearTypedText();
1573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 if (appSearchData == null) {
1575 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001576 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Karl Rosaen138a0412009-04-23 19:00:21 -07001579 final SearchManager searchManager =
1580 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001581 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001582 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584
1585 @Override
1586 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001587 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 return false;
1589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
1591 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1594 .setIcon(android.R.drawable.ic_menu_add)
1595 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001596 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1597 .setIcon(android.R.drawable.ic_menu_manage)
1598 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001599 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 .setIcon(android.R.drawable.ic_menu_gallery)
1601 .setAlphabeticShortcut('W');
1602 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1603 .setIcon(android.R.drawable.ic_search_category_default)
1604 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1605 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1606 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1607 .setAlphabeticShortcut('N');
1608
1609 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001610 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1611 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
1613 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1614 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1615 .setIntent(settings);
1616
1617 return true;
1618 }
1619
1620 @Override
1621 public boolean onPrepareOptionsMenu(Menu menu) {
1622 super.onPrepareOptionsMenu(menu);
1623
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001624 // If all apps is animating, don't show the menu, because we don't know
1625 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001626 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001627 return false;
1628 }
1629
1630 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001631 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001632 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1633 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1634
1635 // Disable add if the workspace is full.
1636 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001637 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1638 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
1641 return true;
1642 }
1643
1644 @Override
1645 public boolean onOptionsItemSelected(MenuItem item) {
1646 switch (item.getItemId()) {
1647 case MENU_ADD:
1648 addItems();
1649 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001650 case MENU_MANAGE_APPS:
1651 manageApps();
1652 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 case MENU_WALLPAPER_SETTINGS:
1654 startWallpaper();
1655 return true;
1656 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001657 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 return true;
1659 case MENU_NOTIFICATIONS:
1660 showNotifications();
1661 return true;
1662 }
1663
1664 return super.onOptionsItemSelected(item);
1665 }
Romain Guycbb89e42009-06-08 15:52:54 -07001666
Karl Rosaen138a0412009-04-23 19:00:21 -07001667 /**
1668 * Indicates that we want global search for this activity by setting the globalSearch
1669 * argument for {@link #startSearch} to true.
1670 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001672 @Override
1673 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001674 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001675 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001676 }
1677
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678 public boolean isWorkspaceLocked() {
1679 return mWorkspaceLoading || mWaitingForResult;
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001683 if (LauncherApplication.isScreenXLarge()) {
1684 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001685 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001686 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001687 }
1688 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001689 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001690 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 }
1693
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 private void resetAddInfo() {
1695 mAddScreen = -1;
1696 mAddIntersectCellX = -1;
1697 mAddIntersectCellY = -1;
1698 mAddDropPosition = null;
1699 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001700
Winson Chung55cef262010-10-28 14:14:18 -07001701 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 resetAddInfo();
1703 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001704 mAddDropPosition = position;
1705
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001707 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1708 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001709 }
1710
Winson Chung7ad01412010-09-27 11:33:03 -07001711 private void manageApps() {
1712 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1713 }
1714
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001716 // TODO: catch bad widget exception when sent
1717 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001718 // TODO: Is this log message meaningful?
1719 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001720 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 }
1722
Winson Chung55cef262010-10-28 14:14:18 -07001723 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001724 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725
Bjorn Bringert7984c942009-12-09 15:38:25 +00001726 if (appWidget.configure != null) {
1727 // Launch over to configure widget, if needed
1728 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1729 intent.setComponent(appWidget.configure);
1730 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001731 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001732 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1733 intent.putExtra(
1734 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1735 info.mimeType);
1736
1737 final String mimeType = info.mimeType;
1738 final ClipData clipData = (ClipData) info.configurationData;
1739 final ClipDescription clipDesc = clipData.getDescription();
1740 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1741 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001742 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001743 final CharSequence stringData = item.getText();
1744 final Uri uriData = item.getUri();
1745 final Intent intentData = item.getIntent();
1746 final String key =
1747 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1748 if (uriData != null) {
1749 intent.putExtra(key, uriData);
1750 } else if (intentData != null) {
1751 intent.putExtra(key, intentData);
1752 } else if (stringData != null) {
1753 intent.putExtra(key, stringData);
1754 }
1755 break;
1756 }
1757 }
1758 }
Winson Chung55cef262010-10-28 14:14:18 -07001759 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001760
Romain Guy8e633c52010-03-04 12:51:36 -08001761 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001763 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
1766 }
Romain Guycbb89e42009-06-08 15:52:54 -07001767
Michael Jurka0280c3b2010-09-17 15:00:07 -07001768 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1769 resetAddInfo();
1770 mAddScreen = screen;
1771 mAddDropPosition = position;
1772
1773 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1774 createShortcutIntent.setComponent(componentName);
1775 processShortcut(createShortcutIntent);
1776 }
1777
Joe Onoratodeb98af2010-02-19 14:59:39 -08001778 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 // Handle case where user selected "Applications"
1780 String applicationName = getResources().getString(R.string.group_applications);
1781 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001782
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 if (applicationName != null && applicationName.equals(shortcutName)) {
1784 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1785 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001786
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1788 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001789 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001790 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001792 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795
Winson Chung24ab2f12010-09-16 14:10:47 -07001796 void processWallpaper(Intent intent) {
1797 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1798 }
1799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1801 resetAddInfo();
1802 mAddScreen = screen;
1803 mAddDropPosition = position;
1804
1805 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1806 createFolderIntent.setComponent(componentName);
1807
1808 addLiveFolder(createFolderIntent);
1809 }
1810
1811 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001812 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001813 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001814 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001816 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001818 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001819 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822
Michael Jurka0280c3b2010-09-17 15:00:07 -07001823 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 UserFolderInfo folderInfo = new UserFolderInfo();
1825 folderInfo.title = getText(R.string.folder_name);
1826
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1828 final int[] cellXY = mTmpAddItemCellCoordinates;
1829 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1830 showOutOfSpaceMessage();
1831 return;
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
1834 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 LauncherModel.addItemToDatabase(this, folderInfo,
1836 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001837 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001838 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839
1840 // Create the view
1841 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001842 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1843 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Romain Guycbb89e42009-06-08 15:52:54 -07001846
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001848 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 }
1850
Michael Jurka28750fb2010-09-24 17:43:49 -07001851 private void completeAddLiveFolder(
1852 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1854 final int[] cellXY = mTmpAddItemCellCoordinates;
1855 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1856 showOutOfSpaceMessage();
1857 return;
1858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
Michael Jurka0280c3b2010-09-17 15:00:07 -07001860 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861
1862 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001864 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
1867 }
1868
1869 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001870 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
1872 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1873 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1874
1875 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 Intent.ShortcutIconResource iconResource = null;
1877
1878 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1879 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1880 try {
1881 iconResource = (Intent.ShortcutIconResource) extra;
1882 final PackageManager packageManager = context.getPackageManager();
1883 Resources resources = packageManager.getResourcesForApplication(
1884 iconResource.packageName);
1885 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1886 icon = resources.getDrawable(id);
1887 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001888 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890 }
1891
1892 if (icon == null) {
1893 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1894 }
1895
1896 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001897 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 info.title = name;
1899 info.iconResource = iconResource;
1900 info.uri = data.getData();
1901 info.baseIntent = baseIntent;
1902 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1903 LiveFolders.DISPLAY_MODE_GRID);
1904
1905 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001906 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001907 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908
1909 return info;
1910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 private void showNotifications() {
1913 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1914 if (statusBar != null) {
1915 statusBar.expand();
1916 }
1917 }
1918
1919 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001920 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001922 Intent chooser = Intent.createChooser(pickWallpaper,
1923 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001924 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1925 // Removed in Eclair MR1
1926// WallpaperManager wm = (WallpaperManager)
1927// getSystemService(Context.WALLPAPER_SERVICE);
1928// WallpaperInfo wi = wm.getWallpaperInfo();
1929// if (wi != null && wi.getSettingsActivity() != null) {
1930// LabeledIntent li = new LabeledIntent(getPackageName(),
1931// R.string.configure_wallpaper, 0);
1932// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1933// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1934// }
Mike Clerona0618e42009-10-22 13:55:21 -07001935 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001938 /**
1939 * Registers various content observers. The current implementation registers
1940 * only a favorites observer to keep track of the favorites applications.
1941 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001942 private void registerContentObservers() {
1943 ContentResolver resolver = getContentResolver();
1944 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1945 true, mWidgetObserver);
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
1949 public boolean dispatchKeyEvent(KeyEvent event) {
1950 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1951 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001953 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001954 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001955 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001956 dumpState();
1957 return true;
1958 }
1959 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001960 }
1961 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1962 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001963 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 return true;
1965 }
1966 }
1967
1968 return super.dispatchKeyEvent(event);
1969 }
1970
Joe Onorato88ec0992009-11-19 13:16:06 -08001971 @Override
1972 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001973 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1974 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001975 } else {
1976 closeFolder();
1977 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001978 // Some launcher layouts don't have a previous and next view
1979 if (mPreviousView != null) {
1980 dismissPreview(mPreviousView);
1981 dismissPreview(mNextView);
1982 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 private void closeFolder() {
1986 Folder folder = mWorkspace.getOpenFolder();
1987 if (folder != null) {
1988 closeFolder(folder);
1989 }
1990 }
1991
1992 void closeFolder(Folder folder) {
1993 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001994 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001996 CellLayout cl = (CellLayout) parent;
1997 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001998 if (folder instanceof DropTarget) {
1999 // Live folders aren't DropTargets.
2000 mDragController.removeDropTarget((DropTarget)folder);
2001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003 folder.onClose();
2004 }
2005
2006 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002007 * Re-listen when widgets are reset.
2008 */
2009 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002010 if (mAppWidgetHost != null) {
2011 mAppWidgetHost.startListening();
2012 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002013 }
2014
2015 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2017 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 private void unbindDesktopItems() {
2020 for (ItemInfo item: mDesktopItems) {
2021 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002023 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 /**
2027 * Launches the intent referred by the clicked shortcut.
2028 *
2029 * @param v The view representing the clicked shortcut.
2030 */
2031 public void onClick(View v) {
2032 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002033 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002035 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002036 int[] pos = new int[2];
2037 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002038 intent.setSourceBounds(new Rect(pos[0], pos[1],
2039 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002040 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 } else if (tag instanceof FolderInfo) {
2042 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002043 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002044 if (mState == State.ALL_APPS) {
2045 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002046 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002047 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050 }
2051
Michael Jurka0e260592010-06-30 17:07:39 -07002052 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002053 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002054 // clicking anywhere on the workspace causes the customization drawer to slide down
2055 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002056 return false;
2057 }
2058
Michael Jurkaaf442092010-06-10 17:01:57 -07002059 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002060 * Event handler for the search button
2061 *
2062 * @param v The view that was clicked.
2063 */
2064 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002065 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002066 // Use a custom animation for launching search
2067 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002068 }
2069
2070 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002071 * Event handler for the voice button
2072 *
2073 * @param v The view that was clicked.
2074 */
2075 public void onClickVoiceButton(View v) {
2076 startVoiceSearch();
2077 }
2078
2079 private void startVoiceSearch() {
2080 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2081 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2082 startActivity(intent);
2083 }
2084
2085 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002086 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002087 * enters home screen customization mode.
2088 *
2089 * @param v The view that was clicked.
2090 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002091 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002092 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002093 }
2094
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002095 /**
2096 * Event handler for the "grid" button that appears on the home screen, which
2097 * enters all apps mode.
2098 *
2099 * @param v The view that was clicked.
2100 */
2101 public void onClickAllAppsButton(View v) {
2102 showAllApps(true);
2103 }
2104
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002105 public void onClickAppMarketButton(View v) {
2106 if (mAppMarketIntent != null) {
2107 startActivitySafely(mAppMarketIntent, "app market");
2108 }
2109 }
2110
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002111 void startApplicationDetailsActivity(ComponentName componentName) {
2112 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002113 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2114 Uri.fromParts("package", packageName, null));
2115 startActivity(intent);
2116 }
2117
Patrick Dubroy5539af72010-09-07 15:22:01 -07002118 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2119 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2120 // System applications cannot be installed. For now, show a toast explaining that.
2121 // We may give them the option of disabling apps this way.
2122 int messageId = R.string.uninstall_system_app_text;
2123 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2124 } else {
2125 String packageName = appInfo.componentName.getPackageName();
2126 String className = appInfo.componentName.getClassName();
2127 Intent intent = new Intent(
2128 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2129 startActivity(intent);
2130 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002131 }
2132
Joe Onoratof984e852010-03-25 09:47:45 -07002133 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2135 try {
2136 startActivity(intent);
2137 } catch (ActivityNotFoundException e) {
2138 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002139 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 } catch (SecurityException e) {
2141 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002142 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002144 "or use the exported attribute for this activity. "
2145 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 }
2147 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002148
Romain Guy8e633c52010-03-04 12:51:36 -08002149 void startActivityForResultSafely(Intent intent, int requestCode) {
2150 try {
2151 startActivityForResult(intent, requestCode);
2152 } catch (ActivityNotFoundException e) {
2153 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2154 } catch (SecurityException e) {
2155 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2156 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2157 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2158 "or use the exported attribute for this activity.", e);
2159 }
2160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161
2162 private void handleFolderClick(FolderInfo folderInfo) {
2163 if (!folderInfo.opened) {
2164 // Close any open folder
2165 closeFolder();
2166 // Open the requested folder
2167 openFolder(folderInfo);
2168 } else {
2169 // Find the open folder...
2170 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2171 int folderScreen;
2172 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002173 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 // .. and close it
2175 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002176 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 // Close any folder open on the current screen
2178 closeFolder();
2179 // Pull the folder onto this screen
2180 openFolder(folderInfo);
2181 }
2182 }
2183 }
2184 }
2185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002187 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 * is animated relative to the specified View. If the View is null, no animation
2189 * is played.
2190 *
2191 * @param folderInfo The FolderInfo describing the folder to open.
2192 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002193 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 Folder openFolder;
2195
2196 if (folderInfo instanceof UserFolderInfo) {
2197 openFolder = UserFolder.fromXml(this);
2198 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002199 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 } else {
2201 return;
2202 }
2203
Joe Onorato00acb122009-08-04 16:04:30 -04002204 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 openFolder.setLauncher(this);
2206
2207 openFolder.bind(folderInfo);
2208 folderInfo.opened = true;
2209
Winson Chungaafa03c2010-06-11 17:34:16 -07002210 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 openFolder.onOpen();
2213 }
2214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002216 switch (v.getId()) {
2217 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002218 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002219 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2220 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002221 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002222 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002223 return true;
2224 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002225 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002226 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2227 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002228 showPreviews(v);
2229 }
2230 return true;
2231 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002232 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002233 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2234 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2235 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002236 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002237 return true;
2238 }
2239
Joe Onorato9c1289c2009-08-17 11:03:03 -04002240 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 return false;
2242 }
2243
2244 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002245 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
2247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 resetAddInfo();
2250 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 return true;
2254 }
2255
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 final View itemUnderLongClick = longClickCellInfo.cell;
2257
Winson Chung304dcde2011-01-07 11:17:23 -08002258 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259 if (itemUnderLongClick == null) {
2260 // User long pressed on empty space
2261 mWorkspace.setAllowLongPress(false);
2262 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2263 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002264 if (LauncherApplication.isScreenXLarge()) {
2265 addItems();
2266 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002267 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 }
2269 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002272 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2273 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002274 mAddIntersectCellX = longClickCellInfo.cellX;
2275 mAddIntersectCellY = longClickCellInfo.cellY;
2276 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 }
2278 }
2279 }
2280 return true;
2281 }
2282
Romain Guye6b8e2f2009-11-10 11:56:55 -08002283 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002284 private void dismissPreview(final View v) {
2285 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002286 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002287 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2288 public void onDismiss() {
2289 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2290 int count = group.getChildCount();
2291 for (int i = 0; i < count; i++) {
2292 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2293 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002294 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2295 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2296
2297 v.setTag(R.id.workspace, null);
2298 v.setTag(R.id.icon, null);
2299 window.setOnDismissListener(null);
2300 }
2301 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002302 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002303 }
2304 v.setTag(null);
2305 }
2306
Romain Guyf8e6a802009-12-07 17:48:02 -08002307 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002308 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002309 }
2310
Romain Guya6abce82009-11-10 02:54:41 -08002311 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002312 final Resources resources = getResources();
2313 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002314
Romain Guya6abce82009-11-10 02:54:41 -08002315 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002316
Romain Guy9d31e9f2009-11-11 18:54:28 -08002317 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002318
Romain Guyf8e6a802009-12-07 17:48:02 -08002319 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002320 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002321 int extraW = (int) ((r.left + r.right) * max);
2322 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002323
2324 int aW = cell.getWidth() - extraW;
2325 float w = aW / max;
2326
2327 int width = cell.getWidth();
2328 int height = cell.getHeight();
2329 int x = cell.getLeftPadding();
2330 int y = cell.getTopPadding();
2331 width -= (x + cell.getRightPadding());
2332 height -= (y + cell.getBottomPadding());
2333
2334 float scale = w / width;
2335
2336 int count = end - start;
2337
2338 final float sWidth = width * scale;
2339 float sHeight = height * scale;
2340
Romain Guye6b8e2f2009-11-10 11:56:55 -08002341 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002342
Romain Guye6b8e2f2009-11-10 11:56:55 -08002343 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2344 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002345
2346 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002347 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002348 cell = (CellLayout) workspace.getChildAt(i);
2349
Romain Guyf8e6a802009-12-07 17:48:02 -08002350 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002351 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002352
2353 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002354 c.scale(scale, scale);
2355 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002356 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002357
Romain Guy9d31e9f2009-11-11 18:54:28 -08002358 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002359 image.setImageBitmap(bitmap);
2360 image.setTag(i);
2361 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002362 image.setOnFocusChangeListener(handler);
2363 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002364 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002365
2366 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002367 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2368
Winson Chungaafa03c2010-06-11 17:34:16 -07002369 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002370 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002371
2372 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002373 p.setContentView(preview);
2374 p.setWidth((int) (sWidth * count + extraW));
2375 p.setHeight((int) (sHeight + extraH));
2376 p.setAnimationStyle(R.style.AnimationPreview);
2377 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002378 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002379 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002380 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002381
Romain Guye6b8e2f2009-11-10 11:56:55 -08002382 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2383 public void onDismiss() {
2384 dismissPreview(anchor);
2385 }
2386 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002387
2388 anchor.setTag(p);
2389 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002390 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002391 }
2392
Romain Guy9d31e9f2009-11-11 18:54:28 -08002393 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002394 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002395
Romain Guye6b8e2f2009-11-10 11:56:55 -08002396 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002397 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002398 }
2399
2400 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002401 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002402 v.post(this);
2403 }
2404
2405 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002406 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002407 }
2408
2409 public void onFocusChange(View v, boolean hasFocus) {
2410 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002411 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002412 }
Romain Guya6abce82009-11-10 02:54:41 -08002413 }
2414 }
2415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 Workspace getWorkspace() {
2417 return mWorkspace;
2418 }
2419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 @Override
2421 protected Dialog onCreateDialog(int id) {
2422 switch (id) {
2423 case DIALOG_CREATE_SHORTCUT:
2424 return new CreateShortcut().createDialog();
2425 case DIALOG_RENAME_FOLDER:
2426 return new RenameFolder().createDialog();
2427 }
2428
2429 return super.onCreateDialog(id);
2430 }
2431
2432 @Override
2433 protected void onPrepareDialog(int id, Dialog dialog) {
2434 switch (id) {
2435 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 break;
2437 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002438 if (mFolderInfo != null) {
2439 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2440 final CharSequence text = mFolderInfo.title;
2441 input.setText(text);
2442 input.setSelection(0, text.length());
2443 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 break;
2445 }
2446 }
2447
2448 void showRenameDialog(FolderInfo info) {
2449 mFolderInfo = info;
2450 mWaitingForResult = true;
2451 showDialog(DIALOG_RENAME_FOLDER);
2452 }
2453
Michael Jurka0280c3b2010-09-17 15:00:07 -07002454 private void showAddDialog(int intersectX, int intersectY) {
2455 resetAddInfo();
2456 mAddIntersectCellX = intersectX;
2457 mAddIntersectCellY = intersectY;
2458 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 mWaitingForResult = true;
2460 showDialog(DIALOG_CREATE_SHORTCUT);
2461 }
2462
Joe Onoratodeb98af2010-02-19 14:59:39 -08002463 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002464 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002465 Bundle bundle = new Bundle();
2466
2467 ArrayList<String> shortcutNames = new ArrayList<String>();
2468 shortcutNames.add(getString(R.string.group_applications));
2469 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2470
2471 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2472 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2473 R.drawable.ic_launcher_application));
2474 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2475
2476 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2477 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002478 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002479 pickIntent.putExtras(bundle);
2480
Joe Onoratodeb98af2010-02-19 14:59:39 -08002481 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 private class RenameFolder {
2485 private EditText mInput;
2486
2487 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2489 mInput = (EditText) layout.findViewById(R.id.folder_name);
2490
2491 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2492 builder.setIcon(0);
2493 builder.setTitle(getString(R.string.rename_folder_title));
2494 builder.setCancelable(true);
2495 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2496 public void onCancel(DialogInterface dialog) {
2497 cleanup();
2498 }
2499 });
2500 builder.setNegativeButton(getString(R.string.cancel_action),
2501 new Dialog.OnClickListener() {
2502 public void onClick(DialogInterface dialog, int which) {
2503 cleanup();
2504 }
2505 }
2506 );
2507 builder.setPositiveButton(getString(R.string.rename_action),
2508 new Dialog.OnClickListener() {
2509 public void onClick(DialogInterface dialog, int which) {
2510 changeFolderName();
2511 }
2512 }
2513 );
2514 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002515
2516 final AlertDialog dialog = builder.create();
2517 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2518 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002519 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002520 mInput.requestFocus();
2521 InputMethodManager inputManager = (InputMethodManager)
2522 getSystemService(Context.INPUT_METHOD_SERVICE);
2523 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002524 }
2525 });
2526
2527 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
2529
2530 private void changeFolderName() {
2531 final String name = mInput.getText().toString();
2532 if (!TextUtils.isEmpty(name)) {
2533 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002534 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 mFolderInfo.title = name;
2536 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2537
Joe Onorato9c1289c2009-08-17 11:03:03 -04002538 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002539 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 } else {
2542 final FolderIcon folderIcon = (FolderIcon)
2543 mWorkspace.getViewForTag(mFolderInfo);
2544 if (folderIcon != null) {
2545 folderIcon.setText(name);
2546 getWorkspace().requestLayout();
2547 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002548 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 mWorkspaceLoading = true;
2550 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552 }
2553 }
2554 cleanup();
2555 }
2556
2557 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 dismissDialog(DIALOG_RENAME_FOLDER);
2559 mWaitingForResult = false;
2560 mFolderInfo = null;
2561 }
2562 }
2563
Daniel Sandler843e8602010-06-07 14:59:01 -04002564 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2565 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002567 }
2568
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002569 // AllAppsView.Watcher
2570 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002571 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2572 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002573 mWorkspace.setVisibility(View.GONE);
2574 }
2575 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576
Michael Jurkaafca9532011-02-16 14:50:35 -08002577 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 button.setVisibility(View.VISIBLE);
2579 button.setFocusable(true);
2580 button.setClickable(true);
2581 }
2582
2583 private void hideToolbarButton(View button) {
2584 button.setAlpha(0.0f);
2585 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2586 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002587 }
2588
2589 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 button.setFocusable(false);
2591 button.setClickable(false);
2592 }
2593
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 *
2597 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2598 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002599 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002600 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002602 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 final boolean showing = show;
2604 final boolean hiding = !show;
2605
2606 final int duration = show ?
2607 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2608 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2609
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002610 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002611 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002612 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002613 anim.addUpdateListener(new AnimatorUpdateListener() {
2614 public void onAnimationUpdate(ValueAnimator animation) {
2615 view.setAlpha((Float) animation.getAnimatedValue());
2616 }
2617 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002618 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002619 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002620 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002621 if (showing) showAndEnableToolbarButton(view);
2622 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002624 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002625 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 });
2629 seq.play(anim);
2630 } else {
2631 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002632 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002633 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002634 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002635 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002638 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 }
2640
2641 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 * Show/hide the appropriate toolbar buttons for newState.
2643 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 *
2645 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002646 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2647 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002649 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002650 switch (newState) {
2651 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002652 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2653 mDeleteZone.setOverlappingViews(
2654 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002655 mDeleteZone.setDragAndDropEnabled(true);
2656 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 break;
2658 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002659 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002660 mDeleteZone.setDragAndDropEnabled(false);
2661 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 break;
2663 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002664 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002665 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 break;
2667 }
2668 }
2669
2670 /**
2671 * Helper method for the cameraZoomIn/cameraZoomOut animations
2672 * @param view The view being animated
2673 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2674 * @param scaleFactor The scale factor used for the zoom
2675 */
2676 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2677 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002678
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002680 // Set pivotY so that at the starting zoom factor, the view is partially
2681 // visible. Modifying initialHeightFactor changes how much of the view is
2682 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002684 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002685 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002687 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002688 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 }
2690 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002691
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002692 /**
2693 * Zoom the camera out from the workspace to reveal 'toView'.
2694 * Assumes that the view to show is anchored at either the very top or very bottom
2695 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002697 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002698 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002699 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002701
2702 final int duration = toAllApps ?
2703 res.getInteger(R.integer.config_allAppsZoomInTime) :
2704 res.getInteger(R.integer.config_customizeZoomInTime);
2705
2706 final float scale = toAllApps ?
2707 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2708 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2709
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002710 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002711
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 setPivotsForZoom(toView, toState, scale);
2713
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002714 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002715 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002716 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002717 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002718 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002719
2720 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002721 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2722 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2723 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2724 public void onAnimationUpdate(ValueAnimator animation) {
2725 final float b = (Float) animation.getAnimatedValue();
2726 final float a = 1f - b;
2727 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002728 toView.setFastScaleX(a * scale + b * 1f);
2729 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002730 }
2731 });
Adam Cohen61033d32010-11-15 18:29:44 -08002732
Adam Cohenf16e5712011-01-13 13:31:45 -08002733 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002734 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002735 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002736 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002737 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2738 public void onAnimationUpdate(ValueAnimator animation) {
2739 final float b = (Float) animation.getAnimatedValue();
2740 final float a = 1f - b;
2741 // don't need to invalidate because we do so above
2742 toView.setFastAlpha(a * 0f + b * 1f);
2743 }
2744 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002745 alphaAnim.start();
2746 }
2747
Michael Jurkabfadaad2011-01-31 21:35:39 -08002748 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2749 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2750 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2751 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002752 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002753 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002754 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755 // Prepare the position
2756 toView.setTranslationX(0.0f);
2757 toView.setTranslationY(0.0f);
2758 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002759 if (!toAllApps) {
2760 toView.setAlpha(1.0f);
2761 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002762 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002763 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002764 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002765 // If we don't set the final scale values here, if this animation is cancelled
2766 // it will have the wrong scale value and subsequent cameraPan animations will
2767 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002768 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002769 toView.setScaleX(1.0f);
2770 toView.setScaleY(1.0f);
2771 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 });
2773
Chet Haaseb1254a62010-09-07 13:35:00 -07002774 AnimatorSet toolbarHideAnim = new AnimatorSet();
2775 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002776 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2777
2778 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002779 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002780
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002781 if (mStateAnimation != null) mStateAnimation.cancel();
2782 mStateAnimation = new AnimatorSet();
2783 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2784 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002785
2786 // Show the new toolbar buttons just as the main animation is ending
2787 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002788 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2789 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 } else {
2791 toView.setTranslationX(0.0f);
2792 toView.setTranslationY(0.0f);
2793 toView.setScaleX(1.0f);
2794 toView.setScaleY(1.0f);
2795 toView.setVisibility(View.VISIBLE);
2796 hideAndShowToolbarButtons(toState, null, null);
2797 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002798 }
2799
2800 /**
2801 * Zoom the camera back into the workspace, hiding 'fromView'.
2802 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002803 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2804 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002805 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002807 cameraZoomIn(fromState, animated, false);
2808 }
2809
2810 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002811 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002812 final boolean fromAllApps = (fromState == State.ALL_APPS);
2813
2814 int duration = fromAllApps ?
2815 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2816 res.getInteger(R.integer.config_customizeZoomOutTime);
2817
Michael Jurka742574b2011-02-02 23:51:01 -08002818 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002819 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2820 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2821
2822 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002823
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002824 mCustomizePagedView.endChoiceMode();
2825 mAllAppsPagedView.endChoiceMode();
2826
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002828
Michael Jurkad3ef3062010-11-23 16:23:58 -08002829 if (!springLoaded) {
2830 mWorkspace.unshrink(animated);
2831 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002832
2833 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002834 if (mStateAnimation != null) mStateAnimation.cancel();
2835 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002836
Michael Jurka742574b2011-02-02 23:51:01 -08002837 final float oldScaleX = fromView.getScaleX();
2838 final float oldScaleY = fromView.getScaleY();
2839
2840 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2841 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2842 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2843 public void onAnimationUpdate(ValueAnimator animation) {
2844 final float b = (Float) animation.getAnimatedValue();
2845 final float a = 1f - b;
2846 ((View)fromView.getParent()).fastInvalidate();
2847 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2848 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2849 }
2850 });
2851 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002852 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002853 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002854 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2855 public void onAnimationUpdate(ValueAnimator animation) {
2856 final float b = (Float) animation.getAnimatedValue();
2857 final float a = 1f - b;
2858 // don't need to invalidate because we do so above
2859 fromView.setFastAlpha(a * 1f + b * 0f);
2860 }
2861 });
2862
Michael Jurkacf6125c2011-01-28 15:20:01 -08002863 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002864 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002865 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002866 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002868 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869 }
2870 });
2871
Chet Haaseb1254a62010-09-07 13:35:00 -07002872 AnimatorSet toolbarHideAnim = new AnimatorSet();
2873 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002874 if (!springLoaded) {
2875 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2876 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002877
Adam Cohen61033d32010-11-15 18:29:44 -08002878 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002879
2880 // Show the new toolbar buttons at the very end of the whole animation
2881 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2882 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002883 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2884 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002885 } else {
2886 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002887 if (!springLoaded) {
2888 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2889 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002891 }
2892
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002893 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002894 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002895 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002896 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002897
Michael Jurka79212d82010-07-30 16:36:20 -07002898 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002899 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002900 } else {
2901 mAllAppsGrid.zoom(1.0f, animated);
2902 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002903
Romain Guyc16fea72010-03-12 17:17:56 -08002904 ((View) mAllAppsGrid).setFocusable(true);
2905 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002906
Joe Onorato7c312c12009-08-13 21:36:53 -07002907 // TODO: fade these two too
2908 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002909
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002910 // Change the state *after* we've called all the transition code
2911 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002912
Winson Chung5fb63472011-02-02 17:03:37 -08002913 // Pause the auto-advance of widgets until we are out of AllApps
2914 mUserPresent = false;
2915 updateRunning();
2916
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002917 // send an accessibility event to announce the context change
2918 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2919 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002920
2921 void showWorkspace(boolean animated) {
2922 showWorkspace(animated, null);
2923 }
2924
2925 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002926 if (layout != null) {
2927 // always animated, but that's ok since we never specify a layout and
2928 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002929 mWorkspace.unshrink(layout);
2930 } else {
2931 mWorkspace.unshrink(animated);
2932 }
2933 if (mState == State.ALL_APPS) {
2934 closeAllApps(animated);
2935 } else if (mState == State.CUSTOMIZE) {
2936 hideCustomizationDrawer(animated);
2937 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002938
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002939 // Change the state *after* we've called all the transition code
2940 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002941
Winson Chung5fb63472011-02-02 17:03:37 -08002942 // Resume the auto-advance of widgets
2943 mUserPresent = true;
2944 updateRunning();
2945
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002946 // send an accessibility event to announce the context change
2947 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002948 }
2949
Michael Jurkad3ef3062010-11-23 16:23:58 -08002950 void enterSpringLoadedDragMode(CellLayout layout) {
2951 mWorkspace.enterSpringLoadedDragMode(layout);
2952 if (mState == State.ALL_APPS) {
2953 cameraZoomIn(State.ALL_APPS, true, true);
2954 mState = State.ALL_APPS_SPRING_LOADED;
2955 } else if (mState == State.CUSTOMIZE) {
2956 cameraZoomIn(State.CUSTOMIZE, true, true);
2957 mState = State.CUSTOMIZE_SPRING_LOADED;
2958 }/* else {
2959 // we're already in spring loaded mode; don't do anything
2960 }*/
2961 }
2962
2963 void exitSpringLoadedDragMode() {
2964 if (mState == State.ALL_APPS_SPRING_LOADED) {
2965 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2966 cameraZoomOut(State.ALL_APPS, true);
2967 mState = State.ALL_APPS;
2968 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2969 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2970 cameraZoomOut(State.CUSTOMIZE, true);
2971 mState = State.CUSTOMIZE;
2972 }/* else {
2973 // we're not in spring loaded mode; don't do anything
2974 }*/
2975 }
2976
Joe Onoratob2061212009-11-24 16:13:54 -05002977 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002978 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002979 * - Home from workspace
2980 * - from center screen
2981 * - from other screens
2982 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002983 * - from center screen
2984 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002985 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002986 * - from center screen
2987 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002988 * - Launch app from workspace and quit
2989 * - with back
2990 * - with home
2991 * - Launch app from all apps and quit
2992 * - with back
2993 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002994 * - Go to a screen that's not the default, then all
2995 * apps, and launch and app, and go back
2996 * - with back
2997 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002998 * - On workspace, long press power and go back
2999 * - with back
3000 * - with home
3001 * - On all apps, long press power and go back
3002 * - with back
3003 * - with home
3004 * - On workspace, power off
3005 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003006 * - Launch an app and turn off the screen while in that app
3007 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003008 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003009 * - From all apps
3010 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003011 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3012 * - From all apps
3013 * - From the center workspace
3014 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003015 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003016 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003017 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003018 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003019 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003020 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003021 } else {
3022 mAllAppsGrid.zoom(0.0f, animated);
3023 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003024 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003025 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003026 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003027 }
3028
Joe Onorato7c312c12009-08-13 21:36:53 -07003029 void lockAllApps() {
3030 // TODO
3031 }
3032
3033 void unlockAllApps() {
3034 // TODO
3035 }
3036
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003037 // Show the customization drawer (only exists in x-large configuration)
3038 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003039 if (mState != State.WORKSPACE) {
3040 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003041 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003042
3043 cameraZoomOut(State.CUSTOMIZE, animated);
3044
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003045 // Change the state *after* we've called all the transition code
3046 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003047
3048 // Pause the auto-advance of widgets until we are out of Customization drawer
3049 mUserPresent = false;
3050 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003051 }
3052
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003053 // Hide the customization drawer (only exists in x-large configuration)
3054 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003055 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003056 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003057 }
3058 }
3059
Patrick Dubroy5f445422011-02-18 14:35:21 -08003060 /**
3061 * Add an item from all apps or customize onto the given workspace screen.
3062 * If layout is null, add to the current screen.
3063 */
3064 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003065 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3066 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003067 } else {
3068 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003069 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003070 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003071
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003072 void onWorkspaceClick(CellLayout layout) {
3073 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003074 }
3075
Michael Jurka4ef207b2010-11-29 17:05:45 -08003076 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003077 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003078 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3079 ImageView button = (ImageView) findViewById(buttonId);
3080 Drawable toolbarIcon = null;
3081 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003082 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003083 // Look for the toolbar icon specified in the activity meta-data
3084 Bundle metaData = packageManager.getActivityInfo(
3085 activityName, PackageManager.GET_META_DATA).metaData;
3086 if (metaData != null) {
3087 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3088 if (iconResId != 0) {
3089 Resources res = packageManager.getResourcesForActivity(activityName);
3090 toolbarIcon = res.getDrawable(iconResId);
3091 }
3092 }
3093 } catch (NameNotFoundException e) {
3094 // Do nothing
3095 }
3096 // If we were unable to find the icon via the meta-data, use a generic one
3097 if (toolbarIcon == null) {
3098 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003099 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003100 } else {
3101 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003102 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003103 }
3104 }
3105
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003106 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003107 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003108 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003109 }
3110
Michael Jurka0423dcf2010-10-05 14:56:18 -07003111 private void updateGlobalSearchIcon() {
3112 if (LauncherApplication.isScreenXLarge()) {
3113 final SearchManager searchManager =
3114 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3115 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003116 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003117 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003118 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003119 } else {
3120 findViewById(R.id.search_button).setVisibility(View.GONE);
3121 }
3122 }
3123 }
3124
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003125 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003126 updateButtonWithDrawable(R.id.search_button, d);
3127 }
3128
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003129 private void updateVoiceSearchIcon() {
3130 if (LauncherApplication.isScreenXLarge()) {
3131 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3132 ComponentName activityName = intent.resolveActivity(getPackageManager());
3133 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003134 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003135 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3136 } else {
3137 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003138 }
3139 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003140 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003141
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003142 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 updateButtonWithDrawable(R.id.voice_button, d);
3144 }
3145
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003146 /**
3147 * Sets the app market icon (shown when all apps is visible on x-large screens)
3148 */
3149 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003150 if (LauncherApplication.isScreenXLarge()) {
3151 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3152 // Find the app market activity by resolving an intent.
3153 // (If multiple app markets are installed, it will return the ResolverActivity.)
3154 ComponentName activityName = intent.resolveActivity(getPackageManager());
3155 if (activityName != null) {
3156 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003157 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 R.id.market_button, activityName, R.drawable.app_market_generic);
3159 }
3160 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003161 }
3162
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003163 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003164 updateButtonWithDrawable(R.id.market_button, d);
3165 }
3166
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003167 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 * Displays the shortcut creation dialog and launches, if necessary, the
3169 * appropriate activity.
3170 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003171 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003172 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3173 DialogInterface.OnShowListener {
3174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3181 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003182 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 builder.setInverseBackgroundForced(true);
3185
3186 AlertDialog dialog = builder.create();
3187 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003188 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003189 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 return dialog;
3192 }
3193
3194 public void onCancel(DialogInterface dialog) {
3195 mWaitingForResult = false;
3196 cleanup();
3197 }
3198
Romain Guycbb89e42009-06-08 15:52:54 -07003199 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003200 }
3201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003203 try {
3204 dismissDialog(DIALOG_CREATE_SHORTCUT);
3205 } catch (Exception e) {
3206 // An exception is thrown if the dialog is not visible, which is fine
3207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 }
3209
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003210 /**
3211 * Handle the action clicked in the "Add to home" dialog.
3212 */
3213 public void onClick(DialogInterface dialog, int which) {
3214 Resources res = getResources();
3215 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003216
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003217 switch (which) {
3218 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003219 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003220 break;
3221 }
Romain Guycbb89e42009-06-08 15:52:54 -07003222
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003223 case AddAdapter.ITEM_APPWIDGET: {
3224 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003225
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003226 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3227 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003228 // start the pick activity
3229 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3230 break;
3231 }
Romain Guycbb89e42009-06-08 15:52:54 -07003232
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003233 case AddAdapter.ITEM_LIVE_FOLDER: {
3234 // Insert extra item to handle inserting folder
3235 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003236
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003237 ArrayList<String> shortcutNames = new ArrayList<String>();
3238 shortcutNames.add(res.getString(R.string.group_folder));
3239 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003240
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003241 ArrayList<ShortcutIconResource> shortcutIcons =
3242 new ArrayList<ShortcutIconResource>();
3243 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3244 R.drawable.ic_launcher_folder));
3245 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3246
3247 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3248 pickIntent.putExtra(Intent.EXTRA_INTENT,
3249 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3250 pickIntent.putExtra(Intent.EXTRA_TITLE,
3251 getText(R.string.title_select_live_folder));
3252 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003253
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003254 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3255 break;
3256 }
3257
3258 case AddAdapter.ITEM_WALLPAPER: {
3259 startWallpaper();
3260 break;
3261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003262 }
3263 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003264
3265 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003266 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 }
3269
3270 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003271 * Receives notifications when applications are added/removed.
3272 */
3273 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3274 @Override
3275 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003276 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003277 String reason = intent.getStringExtra("reason");
3278 if (!"homekey".equals(reason)) {
3279 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003280 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003281 animate = false;
3282 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003283 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003284 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003285 }
3286 }
3287
3288 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003289 * Receives notifications whenever the appwidgets are reset.
3290 */
3291 private class AppWidgetResetObserver extends ContentObserver {
3292 public AppWidgetResetObserver() {
3293 super(new Handler());
3294 }
3295
3296 @Override
3297 public void onChange(boolean selfChange) {
3298 onAppWidgetReset();
3299 }
3300 }
3301
3302 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003303 * If the activity is currently paused, signal that we need to re-run the loader
3304 * in onResume.
3305 *
3306 * This needs to be called from incoming places where resources might have been loaded
3307 * while we are paused. That is becaues the Configuration might be wrong
3308 * when we're not running, and if it comes back to what it was when we
3309 * were paused, we are not restarted.
3310 *
3311 * Implementation of the method from LauncherModel.Callbacks.
3312 *
3313 * @return true if we are currently paused. The caller might be able to
3314 * skip some work in that case since we will come back again.
3315 */
3316 public boolean setLoadOnResume() {
3317 if (mPaused) {
3318 Log.i(TAG, "setLoadOnResume");
3319 mOnResumeNeedsLoad = true;
3320 return true;
3321 } else {
3322 return false;
3323 }
3324 }
3325
3326 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003327 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003330 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003331 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003332 } else {
3333 return SCREEN_COUNT / 2;
3334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003336
Winson Chunga12a2502010-12-20 14:41:35 -08003337 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003338 mAllAppsPagedView = view;
3339 }
3340
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 /**
3342 * Refreshes the shortcuts shown on the workspace.
3343 *
3344 * Implementation of the method from LauncherModel.Callbacks.
3345 */
3346 public void startBinding() {
3347 final Workspace workspace = mWorkspace;
3348 int count = workspace.getChildCount();
3349 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003350 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003351 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3352 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003354
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 if (DEBUG_USER_INTERFACE) {
3356 android.widget.Button finishButton = new android.widget.Button(this);
3357 finishButton.setText("Finish");
3358 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3359
3360 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3361 public void onClick(View v) {
3362 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003365 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003366
3367 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3368 // mDesktopItems -> AppWidgetInfo -> hostView).
3369 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003370 }
3371
3372 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 * Bind the items start-end from the list.
3374 *
3375 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003376 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3378
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003379 setLoadOnResume();
3380
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 final Workspace workspace = mWorkspace;
3382
3383 for (int i=start; i<end; i++) {
3384 final ItemInfo item = shortcuts.get(i);
3385 mDesktopItems.add(item);
3386 switch (item.itemType) {
3387 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3388 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003389 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3391 false);
3392 break;
3393 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3394 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003395 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003396 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003397 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3398 false);
3399 break;
3400 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3401 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3402 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003403 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 (LiveFolderInfo) item);
3405 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3406 false);
3407 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003409 }
3410
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412 }
3413
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 /**
3415 * Implementation of the method from LauncherModel.Callbacks.
3416 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003417 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003418 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003419 sFolders.clear();
3420 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422
3423 /**
3424 * Add the views for a widget to the workspace.
3425 *
3426 * Implementation of the method from LauncherModel.Callbacks.
3427 */
3428 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003429 setLoadOnResume();
3430
Daniel Sandler843e8602010-06-07 14:59:01 -04003431 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3432 if (DEBUG_WIDGETS) {
3433 Log.d(TAG, "bindAppWidget: " + item);
3434 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 final Workspace workspace = mWorkspace;
3436
3437 final int appWidgetId = item.appWidgetId;
3438 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003439 if (DEBUG_WIDGETS) {
3440 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3441 }
3442
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3444
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3446 item.hostView.setTag(item);
3447
3448 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3449 item.cellY, item.spanX, item.spanY, false);
3450
Adam Cohended9f8d2010-11-03 13:25:16 -07003451 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3452
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453 workspace.requestLayout();
3454
3455 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003456
3457 if (DEBUG_WIDGETS) {
3458 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3459 + (SystemClock.uptimeMillis()-start) + "ms");
3460 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003461 }
3462
3463 /**
3464 * Callback saying that there aren't any more items to bind.
3465 *
3466 * Implementation of the method from LauncherModel.Callbacks.
3467 */
3468 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003469 setLoadOnResume();
3470
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 if (mSavedState != null) {
3472 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003473 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 }
3475
3476 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3477 if (userFolders != null) {
3478 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003479 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 if (info != null) {
3481 openFolder(info);
3482 }
3483 }
3484 final Folder openFolder = mWorkspace.getOpenFolder();
3485 if (openFolder != null) {
3486 openFolder.requestFocus();
3487 }
3488 }
3489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 mSavedState = null;
3491 }
3492
3493 if (mSavedInstanceState != null) {
3494 super.onRestoreInstanceState(mSavedInstanceState);
3495 mSavedInstanceState = null;
3496 }
3497
Winson Chung32bb5e52011-01-31 14:51:56 -08003498 // Workaround a bug that occurs when rotating the device while the customization mode is
3499 // open, we trigger a new layout on all the CellLayout children.
3500 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3501 final int childCount = mWorkspace.getChildCount();
3502 for (int i = 0; i < childCount; ++i) {
3503 mWorkspace.getChildAt(i).requestLayout();
3504 }
3505 }
3506
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 mWorkspaceLoading = false;
3508 }
3509
3510 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003511 * Updates the icons on the launcher that are affected by changes to the package list
3512 * on the device.
3513 */
3514 private void updateIconsAffectedByPackageManagerChanges() {
3515 updateAppMarketIcon();
3516 updateGlobalSearchIcon();
3517 updateVoiceSearchIcon();
3518 }
3519
3520 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 * Add the icons for all apps.
3522 *
3523 * Implementation of the method from LauncherModel.Callbacks.
3524 */
3525 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003526 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003527 if (mCustomizePagedView != null) {
3528 mCustomizePagedView.setApps(apps);
3529 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003530 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 }
3532
3533 /**
3534 * A package was installed.
3535 *
3536 * Implementation of the method from LauncherModel.Callbacks.
3537 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003538 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003539 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003541 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003542 if (mCustomizePagedView != null) {
3543 mCustomizePagedView.addApps(apps);
3544 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003545 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 }
3547
3548 /**
3549 * A package was updated.
3550 *
3551 * Implementation of the method from LauncherModel.Callbacks.
3552 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003553 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003554 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003556 mWorkspace.updateShortcuts(apps);
3557 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003558 if (mCustomizePagedView != null) {
3559 mCustomizePagedView.updateApps(apps);
3560 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003561 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003562 }
3563
3564 /**
3565 * A package was uninstalled.
3566 *
3567 * Implementation of the method from LauncherModel.Callbacks.
3568 */
Joe Onorato36115782010-06-17 13:28:48 -04003569 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003571 if (permanent) {
3572 mWorkspace.removeItems(apps);
3573 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003574 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003575 if (mCustomizePagedView != null) {
3576 mCustomizePagedView.removeApps(apps);
3577 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003578 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 }
Joe Onoratobe386092009-11-17 17:32:16 -08003580
3581 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003582 * A number of packages were updated.
3583 */
3584 public void bindPackagesUpdated() {
3585 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003586 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003587 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003588 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003589 }
3590
Winson Chung400438b2011-01-16 17:53:48 -08003591 private int mapConfigurationOriActivityInfoOri(int configOri) {
3592 final Display d = getWindowManager().getDefaultDisplay();
3593 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3594 switch (d.getRotation()) {
3595 case Surface.ROTATION_0:
3596 case Surface.ROTATION_180:
3597 // We are currently in the same basic orientation as the natural orientation
3598 naturalOri = configOri;
3599 break;
3600 case Surface.ROTATION_90:
3601 case Surface.ROTATION_270:
3602 // We are currently in the other basic orientation to the natural orientation
3603 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3604 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3605 break;
3606 }
3607
3608 int[] oriMap = {
3609 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3610 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3611 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3612 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3613 };
3614 // Since the map starts at portrait, we need to offset if this device's natural orientation
3615 // is landscape.
3616 int indexOffset = 0;
3617 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3618 indexOffset = 1;
3619 }
3620 return oriMap[(d.getRotation() + indexOffset) % 4];
3621 }
3622 public void lockScreenOrientation() {
3623 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3624 .getConfiguration().orientation));
3625 }
3626 public void unlockScreenOrientation() {
3627 mHandler.postDelayed(new Runnable() {
3628 public void run() {
3629 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3630 }
3631 }, mRestoreScreenOrientationDelay);
3632 }
3633
Winson Chung80baf5a2010-08-09 16:03:15 -07003634 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003635 * Prints out out state for debugging.
3636 */
3637 public void dumpState() {
3638 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003639 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003640 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3641 Log.d(TAG, "mRestoring=" + mRestoring);
3642 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3643 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3644 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003645 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003646 mModel.dumpState();
3647 mAllAppsGrid.dumpState();
3648 Log.d(TAG, "END launcher2 dump state");
3649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003650}