blob: f43f31e93f81bf69ef934af4c1fe4c6ab30b8f15 [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 Jurkaaf91de02010-11-23 16:23:58 -080020import com.android.common.Search;
21import com.android.launcher.R;
Winson Chunga12a2502010-12-20 14:41:35 -080022import com.android.launcher2.CustomizePagedView.CustomizationType;
Michael Jurkaaf91de02010-11-23 16:23:58 -080023import 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 Jurka946ad472010-07-09 18:05:18 -070031import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.Dialog;
34import android.app.SearchManager;
35import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070036import android.app.WallpaperManager;
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 Chung68846fd2010-10-29 11:00:27 -070048import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070049import 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 Chung68846fd2010-10-29 11:00:27 -070052import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070053import 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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070094import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070096import android.widget.ImageView;
97import android.widget.LinearLayout;
98import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070099import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800100import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700101import android.widget.TextView;
102import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700103import android.widget.TabHost.OnTabChangeListener;
104import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Michael Jurkaaf91de02010-11-23 16:23:58 -0800106import java.io.DataInputStream;
107import java.io.DataOutputStream;
108import java.io.FileNotFoundException;
109import java.io.IOException;
110import java.util.ArrayList;
111import java.util.HashMap;
112import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800113
Adam Cohended9f8d2010-11-03 13:25:16 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800129 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700132 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
133 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
135 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700136 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 private static final int REQUEST_CREATE_SHORTCUT = 1;
139 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 private static final int REQUEST_PICK_APPLICATION = 6;
142 private static final int REQUEST_PICK_SHORTCUT = 7;
143 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700145 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
148
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800149 static final int SCREEN_COUNT = 5;
150 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Joe Onorato7c312c12009-08-13 21:36:53 -0700152 static final int DIALOG_CREATE_SHORTCUT = 1;
153 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
157 // Type: int
158 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700159 // Type: int
160 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: long
162 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: boolean
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
171 // Type: long
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
173
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 // tags for the customization tabs
175 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700176 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700177 private static final String SHORTCUTS_TAG = "shortcuts";
178 private static final String WALLPAPERS_TAG = "wallpapers";
179
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
181
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800183 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
184 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700185 private State mState = State.WORKSPACE;
186 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800191 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800193 private final BroadcastReceiver mCloseSystemDialogsReceiver
194 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800195 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private LayoutInflater mInflater;
198
Joe Onorato00acb122009-08-04 16:04:30 -0400199 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
Michael Jurka0280c3b2010-09-17 15:00:07 -0700205 private int mAddScreen = -1;
206 private int mAddIntersectCellX = -1;
207 private int mAddIntersectCellY = -1;
208 private int[] mAddDropPosition;
209 private int[] mTmpAddItemCellCoordinates = new int[2];
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private FolderInfo mFolderInfo;
212
Joe Onorato7c312c12009-08-13 21:36:53 -0700213 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700216 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700217 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700218
Winson Chunga12a2502010-12-20 14:41:35 -0800219 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private Bundle mSavedState;
223
224 private SpannableStringBuilder mDefaultKeySsb = null;
225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 private boolean mWorkspaceLoading = true;
227
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800228 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private boolean mRestoring;
230 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700231 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
233 private Bundle mSavedInstanceState;
234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800236 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800237 private boolean mUserPresent = true;
238 private boolean mVisible = false;
239 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400240
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700241 private static LocaleConfiguration sLocaleConfiguration = null;
242
Romain Guy84f296c2009-11-04 15:00:44 -0800243 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700244
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700245 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700246
Romain Guy1fbc1c82009-11-09 20:43:08 -0800247 private ImageView mPreviousView;
248 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500249
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400251 private String[] mHotseatConfig = null;
252 private Intent[] mHotseats = null;
253 private Drawable[] mHotseatIcons = null;
254 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400255
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700256 private Intent mAppMarketIntent = null;
257
Adam Cohended9f8d2010-11-03 13:25:16 -0700258 // Related to the auto-advancing of widgets
259 private final int ADVANCE_MSG = 1;
260 private final int mAdvanceInterval = 20000;
261 private final int mAdvanceStagger = 250;
262 private long mAutoAdvanceSentTime;
263 private long mAutoAdvanceTimeLeft = -1;
264 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
265 new HashMap<View, AppWidgetProviderInfo>();
266
Michael Jurka4ef207b2010-11-29 17:05:45 -0800267 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800268 private static Drawable.ConstantState sGlobalSearchIcon;
269 private static Drawable.ConstantState sVoiceSearchIcon;
270 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271
Winson Chunga12a2502010-12-20 14:41:35 -0800272 private CustomizationType getCustomizeFilterForTabTag(String tag) {
273 if (tag.equals(WIDGETS_TAG)) {
274 return CustomizationType.WidgetCustomization;
275 } else if (tag.equals(APPLICATIONS_TAG)) {
276 return CustomizationType.ApplicationCustomization;
277 } else if (tag.equals(WALLPAPERS_TAG)) {
278 return CustomizePagedView.CustomizationType.WallpaperCustomization;
279 } else if (tag.equals(SHORTCUTS_TAG)) {
280 return CustomizePagedView.CustomizationType.ShortcutCustomization;
281 }
282 return CustomizationType.WidgetCustomization;
283 }
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 @Override
286 protected void onCreate(Bundle savedInstanceState) {
287 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700288
Winson Chungaafa03c2010-06-11 17:34:16 -0700289 if (LauncherApplication.isInPlaceRotationEnabled()) {
290 // hide the status bar (temporary until we get the status bar design figured out)
291 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
292 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
293 }
294
Joe Onorato0589f0f2010-02-08 13:44:00 -0800295 LauncherApplication app = ((LauncherApplication)getApplication());
296 mModel = app.setLauncher(this);
297 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400298 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700300
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
303 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200306 android.os.Debug.startMethodTracing(
307 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400310 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 checkForLocaleChange();
312 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800314 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700315 if (mHomeCustomizationDrawer != null) {
316 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700317
Winson Chung80baf5a2010-08-09 16:03:15 -0700318 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700319 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
320 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 TabContentFactory contentFactory = new TabContentFactory() {
322 public View createTabContent(String tag) {
323 return mCustomizePagedView;
324 }
325 };
326
Winson Chung49767ae2010-11-29 14:48:30 -0800327
328 TextView tabView;
329 TabWidget tabWidget = (TabWidget)
330 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
331
332 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
333 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800335 .setIndicator(tabView).setContent(contentFactory));
336 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
337 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700338 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800339 .setIndicator(tabView).setContent(contentFactory));
340 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
341 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700342 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800343 .setIndicator(tabView).setContent(contentFactory));
344 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
345 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700346 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800347 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700348 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
349 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800350 final CustomizePagedView.CustomizationType newType =
351 getCustomizeFilterForTabTag(tabId);
352 if (newType != mCustomizePagedView.getCustomizationFilter()) {
353 // animate the changing of the tab content by fading pages in and out
354 final Resources res = getResources();
355 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
356 final float alpha = mCustomizePagedView.getAlpha();
357 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
358 "alpha", alpha, 0.0f);
359 alphaAnim.setDuration(duration);
360 alphaAnim.addListener(new AnimatorListenerAdapter() {
361 @Override
362 public void onAnimationEnd(Animator animation) {
363 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
364 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700365
Winson Chunga12a2502010-12-20 14:41:35 -0800366 final float alpha = mCustomizePagedView.getAlpha();
367 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
368 mCustomizePagedView, "alpha", alpha, 1.0f);
369 alphaAnim.setDuration(duration);
370 alphaAnim.start();
371 }
372 });
373 alphaAnim.start();
374 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700375 }
376 });
Michael Jurka946ad472010-07-09 18:05:18 -0700377 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 setupViews();
379
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800380 registerContentObservers();
381
Joe Onorato7c312c12009-08-13 21:36:53 -0700382 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 mSavedState = savedInstanceState;
385 restoreState(mSavedState);
386
Winson Chunga12a2502010-12-20 14:41:35 -0800387 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800388 if (mCustomizePagedView != null) {
389 mCustomizePagedView.update();
390 }
Winson Chunga12a2502010-12-20 14:41:35 -0800391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 if (PROFILE_STARTUP) {
393 android.os.Debug.stopMethodTracing();
394 }
395
396 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400397 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 }
399
400 // For handling default keys
401 mDefaultKeySsb = new SpannableStringBuilder();
402 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800403
404 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
405 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800406
407 // If we have a saved version of these external icons, we load them up immediately
408 if (LauncherApplication.isScreenXLarge()) {
409 if (sGlobalSearchIcon != null) {
410 updateGlobalSearchIcon(sGlobalSearchIcon);
411 }
412 if (sVoiceSearchIcon != null) {
413 updateVoiceSearchIcon(sVoiceSearchIcon);
414 }
415 if (sAppMarketIcon != null) {
416 updateAppMarketIcon(sAppMarketIcon);
417 }
418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 }
Romain Guycbb89e42009-06-08 15:52:54 -0700420
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800421 @Override
422 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
423 super.dispatchPopulateAccessibilityEvent(event);
424
425 // we want to take over text population so it is context dependent
426 event.getText().clear();
427 if (mState == State.ALL_APPS) {
428 event.getText().add(getString(R.string.all_apps_button_label));
429 } else if (mState == State.WORKSPACE) {
430 event.getText().add(getString(R.string.all_apps_home_button_label));
431 }
432
433 return true;
434 }
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700437 if (sLocaleConfiguration == null) {
438 new AsyncTask<Void, Void, LocaleConfiguration>() {
439 @Override
440 protected LocaleConfiguration doInBackground(Void... unused) {
441 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
442 readConfiguration(Launcher.this, localeConfiguration);
443 return localeConfiguration;
444 }
445
446 @Override
447 protected void onPostExecute(LocaleConfiguration result) {
448 sLocaleConfiguration = result;
449 checkForLocaleChange(); // recursive, but now with a locale configuration
450 }
451 }.execute();
452 return;
453 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 final Configuration configuration = getResources().getConfiguration();
456
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700457 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 final String locale = configuration.locale.toString();
459
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 final int mcc = configuration.mcc;
462
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 final int mnc = configuration.mnc;
465
Romain Guy84f296c2009-11-04 15:00:44 -0800466 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467
Romain Guy84f296c2009-11-04 15:00:44 -0800468 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700469 sLocaleConfiguration.locale = locale;
470 sLocaleConfiguration.mcc = mcc;
471 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700472
Joe Onorato0589f0f2010-02-08 13:44:00 -0800473 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400474 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700475
476 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
477 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700478 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479 public void run() {
480 writeConfiguration(Launcher.this, localeConfiguration);
481 }
482 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700483 }
484 }
485
486 private static class LocaleConfiguration {
487 public String locale;
488 public int mcc = -1;
489 public int mnc = -1;
490 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700491
Romain Guy98d01652009-06-30 16:21:04 -0700492 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
493 DataInputStream in = null;
494 try {
495 in = new DataInputStream(context.openFileInput(PREFERENCES));
496 configuration.locale = in.readUTF();
497 configuration.mcc = in.readInt();
498 configuration.mnc = in.readInt();
499 } catch (FileNotFoundException e) {
500 // Ignore
501 } catch (IOException e) {
502 // Ignore
503 } finally {
504 if (in != null) {
505 try {
506 in.close();
507 } catch (IOException e) {
508 // Ignore
509 }
510 }
511 }
512 }
513
514 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
515 DataOutputStream out = null;
516 try {
517 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
518 out.writeUTF(configuration.locale);
519 out.writeInt(configuration.mcc);
520 out.writeInt(configuration.mnc);
521 out.flush();
522 } catch (FileNotFoundException e) {
523 // Ignore
524 } catch (IOException e) {
525 //noinspection ResultOfMethodCallIgnored
526 context.getFileStreamPath(PREFERENCES).delete();
527 } finally {
528 if (out != null) {
529 try {
530 out.close();
531 } catch (IOException e) {
532 // Ignore
533 }
534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 }
536 }
537
538 static int getScreen() {
539 synchronized (sLock) {
540 return sScreen;
541 }
542 }
543
544 static void setScreen(int screen) {
545 synchronized (sLock) {
546 sScreen = screen;
547 }
548 }
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700551 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
553 Display display = getWindowManager().getDefaultDisplay();
Michael Jurka340c5f32010-10-21 16:49:19 -0700554 boolean isPortrait = display.getWidth() < display.getHeight();
555 // find width and height when in portrait mode
556 final int width = isPortrait ? display.getWidth() : display.getHeight();
557 final int height = isPortrait ? display.getHeight() : display.getWidth();
558 wpm.suggestDesiredDimensions((int) (Math.max(width, height) * 1.5f),
559 Math.max(width, height));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 // Note: This doesn't do all the client-id magic that BrowserProvider does
563 // in Browser. (http://b/2425179)
564 private Uri getDefaultBrowserUri() {
565 String url = getString(R.string.default_browser_url);
566 if (url.indexOf("{CID}") != -1) {
567 url = url.replace("{CID}", "android-google");
568 }
569 return Uri.parse(url);
570 }
571
572 // Load the Intent templates from arrays.xml to populate the hotseats. For
573 // each Intent, if it resolves to a single app, use that as the launch
574 // intent & use that app's label as the contentDescription. Otherwise,
575 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400576 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 if (mHotseatConfig == null) {
578 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
579 if (mHotseatConfig.length > 0) {
580 mHotseats = new Intent[mHotseatConfig.length];
581 mHotseatLabels = new CharSequence[mHotseatConfig.length];
582 mHotseatIcons = new Drawable[mHotseatConfig.length];
583 } else {
584 mHotseats = null;
585 mHotseatIcons = null;
586 mHotseatLabels = null;
587 }
588
589 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
590 for (int i=0; i<mHotseatConfig.length; i++) {
591 // load icon for this slot; currently unrelated to the actual activity
592 try {
593 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
594 } catch (ArrayIndexOutOfBoundsException ex) {
595 Log.w(TAG, "Missing hotseat_icons array item #" + i);
596 mHotseatIcons[i] = null;
597 }
598 }
599 hotseatIconDrawables.recycle();
600 }
601
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400602 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400603 for (int i=0; i<mHotseatConfig.length; i++) {
604 Intent intent = null;
605 if (mHotseatConfig[i].equals("*BROWSER*")) {
606 // magic value meaning "launch user's default web browser"
607 // replace it with a generic web request so we can see if there is indeed a default
608 String defaultUri = getString(R.string.default_browser_url);
609 intent = new Intent(
610 Intent.ACTION_VIEW,
611 ((defaultUri != null)
612 ? Uri.parse(defaultUri)
613 : getDefaultBrowserUri())
614 ).addCategory(Intent.CATEGORY_BROWSABLE);
615 // note: if the user launches this without a default set, she
616 // will always be taken to the default URL above; this is
617 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700618 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 // URL is unavoidably sent to the chosen app.
620 } else {
621 try {
622 intent = Intent.parseUri(mHotseatConfig[i], 0);
623 } catch (java.net.URISyntaxException ex) {
624 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
625 // bogus; leave intent=null
626 }
627 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700628
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400629 if (intent == null) {
630 mHotseats[i] = null;
631 mHotseatLabels[i] = getText(R.string.activity_not_found);
632 continue;
633 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400634
635 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 Log.d(TAG, "loadHotseats: hotseat " + i
637 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400638 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400639 + "]");
640 }
641
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
643 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400645 Log.d(TAG, "Best match for intent: " + bestMatch);
646 Log.d(TAG, "All matches: ");
647 for (ResolveInfo ri : allMatches) {
648 Log.d(TAG, " --> " + ri);
649 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400650 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400651 // did this resolve to a single app, or the resolver?
652 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700653 // can't find any activity to handle this. let's leave the
654 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 // to launch.
656 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400657
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400658 // set accessibility text to "Not installed"
659 mHotseatLabels[i] = getText(R.string.activity_not_found);
660 } else {
661 boolean found = false;
662 for (ResolveInfo ri : allMatches) {
663 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
664 && bestMatch.activityInfo.applicationInfo.packageName
665 .equals(ri.activityInfo.applicationInfo.packageName)) {
666 found = true;
667 break;
668 }
669 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700670
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400671 if (!found) {
672 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
673 // the bestMatch is probably the ResolveActivity, meaning the
674 // user has not yet selected a default
675 // so: we'll keep the original intent for now
676 mHotseats[i] = intent;
677
678 // set the accessibility text to "Select shortcut"
679 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
680 } else {
681 // we have an app!
682 // now reconstruct the intent to launch it through the front
683 // door
684 ComponentName com = new ComponentName(
685 bestMatch.activityInfo.applicationInfo.packageName,
686 bestMatch.activityInfo.name);
687 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
688
689 // load the app label for accessibility
690 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
691 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400692 }
693
694 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700695 Log.d(TAG, "loadHotseats: hotseat " + i
696 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400697 + ((mHotseats[i] == null)
698 ? "null"
699 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400700 + "] label=[" + mHotseatLabels[i]
701 + "]"
702 );
703 }
704 }
705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 @Override
708 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700709 mWaitingForResult = false;
710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // The pattern used here is that a user PICKs a specific application,
712 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
715 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700716
Michael Jurka0280c3b2010-09-17 15:00:07 -0700717 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 switch (requestCode) {
719 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700720 completeAddApplication(
721 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 break;
723 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800724 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
726 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 break;
729 case REQUEST_PICK_LIVE_FOLDER:
730 addLiveFolder(data);
731 break;
732 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700733 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
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_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700739 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700740 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700742 case REQUEST_PICK_WALLPAPER:
743 // We just wanted the activity result here so we can clear mWaitingForResult
744 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
Romain Guy18042c82009-11-06 11:44:55 -0800746 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
747 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
748 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700749 // Clean up the appWidgetId if we canceled
750 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
751 if (appWidgetId != -1) {
752 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
756
757 @Override
758 protected void onResume() {
759 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800760 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700761 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 mWorkspaceLoading = true;
763 mModel.startLoader(this, true);
764 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700765 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700767 // When we resume Launcher, a different Activity might be responsible for the app
768 // market intent, so refresh the icon
769 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 }
771
772 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 protected void onPause() {
774 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700775 // Some launcher layouts don't have a previous and next view
776 if (mPreviousView != null) {
777 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700778 }
779 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700780 dismissPreview(mNextView);
781 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700782 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800783 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700784 }
Romain Guycbb89e42009-06-08 15:52:54 -0700785
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700786 @Override
787 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 // Flag the loader to stop early before switching
789 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800790 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800791 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700792 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700793
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800794 // We can't hide the IME if it was forced open. So don't bother
795 /*
796 @Override
797 public void onWindowFocusChanged(boolean hasFocus) {
798 super.onWindowFocusChanged(hasFocus);
799
800 if (hasFocus) {
801 final InputMethodManager inputManager = (InputMethodManager)
802 getSystemService(Context.INPUT_METHOD_SERVICE);
803 WindowManager.LayoutParams lp = getWindow().getAttributes();
804 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
805 android.os.Handler()) {
806 protected void onReceiveResult(int resultCode, Bundle resultData) {
807 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
808 }
809 });
810 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
811 }
812 }
813 */
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 private boolean acceptFilter() {
816 final InputMethodManager inputManager = (InputMethodManager)
817 getSystemService(Context.INPUT_METHOD_SERVICE);
818 return !inputManager.isFullscreenMode();
819 }
820
821 @Override
822 public boolean onKeyDown(int keyCode, KeyEvent event) {
823 boolean handled = super.onKeyDown(keyCode, event);
824 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
825 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
826 keyCode, event);
827 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700828 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700829 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700830 // showSearchDialog()
831 // If there are multiple keystrokes before the search dialog takes focus,
832 // onSearchRequested() will be called for every keystroke,
833 // but it is idempotent, so it's fine.
834 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
836 }
837
Joe Onorato8a9625e2010-01-28 15:55:35 -0800838 // Eat the long press event so the keyboard doesn't come up.
839 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
840 return true;
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 return handled;
844 }
845
Karl Rosaen138a0412009-04-23 19:00:21 -0700846 private String getTypedText() {
847 return mDefaultKeySsb.toString();
848 }
849
850 private void clearTypedText() {
851 mDefaultKeySsb.clear();
852 mDefaultKeySsb.clearSpans();
853 Selection.setSelection(mDefaultKeySsb, 0);
854 }
855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700857 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
858 * State
859 */
860 private static State intToState(int stateOrdinal) {
861 State state = State.WORKSPACE;
862 final State[] stateValues = State.values();
863 for (int i = 0; i < stateValues.length; i++) {
864 if (stateValues[i].ordinal() == stateOrdinal) {
865 state = stateValues[i];
866 break;
867 }
868 }
869 return state;
870 }
871
872 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 * Restores the previous state, if it exists.
874 *
875 * @param savedState The previous state.
876 */
877 private void restoreState(Bundle savedState) {
878 if (savedState == null) {
879 return;
880 }
881
Michael Jurka883f55b2010-10-21 15:47:14 -0700882 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
883
884 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800885 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700886 } else if (state == State.CUSTOMIZE) {
887 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800888 }
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
891 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700892 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
895 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700898 mAddScreen = addScreen;
899 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
900 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 mRestoring = true;
902 }
903
904 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
905 if (renameFolder) {
906 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700907 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 mRestoring = true;
909 }
Winson Chunga12a2502010-12-20 14:41:35 -0800910
911 // Restore the current AllApps drawer tab
912 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
913 String curTab = savedState.getString("allapps_currentTab");
914 if (curTab != null) {
915 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
916 tabhost.setCurrentTabByTag(curTab);
917 }
918 int curPage = savedState.getInt("allapps_currentPage", -1);
919 if (curPage > -1) {
920 mAllAppsPagedView.setRestorePage(curPage);
921 }
922 }
923
924 // Restore the current customization drawer tab
925 if (mHomeCustomizationDrawer != null) {
926 String curTab = savedState.getString("customize_currentTab");
927 if (curTab != null) {
928 // We set this directly so that there is no delay before the tab is set
929 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
930 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
931 }
932
933 // Note: currently we do not restore the page for the customization tray because unlike
934 // AllApps, the page content can change drastically
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
938 /**
939 * Finds all the views we need and configure them properly.
940 */
941 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700942 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400943
Romain Guyb1b69f52009-08-10 15:10:15 -0700944 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400945 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
Joe Onorato7c312c12009-08-13 21:36:53 -0700947 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700948 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700949 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800950 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700951 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700952 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700953
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700954 if (LauncherApplication.isScreenXLarge()) {
955 // They need to be INVISIBLE initially so that they will be measured in the layout.
956 // Otherwise the animations are messed up when we show them for the first time.
957 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
958 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
959 }
960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500964 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700965
Joe Onorato7c312c12009-08-13 21:36:53 -0700966 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
967 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700969 View handleView = findViewById(R.id.all_apps_button);
970 if (handleView != null && handleView instanceof HandleView) {
971 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700972 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700973 mHandleView.setLauncher(this);
974 mHandleView.setOnClickListener(this);
975 mHandleView.setOnLongClickListener(this);
976 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800977
Winson Chung80baf5a2010-08-09 16:03:15 -0700978 if (mCustomizePagedView != null) {
979 mCustomizePagedView.setLauncher(this);
980 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700981 } else {
982 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
983 hotseatLeft.setContentDescription(mHotseatLabels[0]);
984 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
985 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
986 hotseatRight.setContentDescription(mHotseatLabels[1]);
987 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400988
Michael Jurkaaf442092010-06-10 17:01:57 -0700989 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
990 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800991
Michael Jurkaaf442092010-06-10 17:01:57 -0700992 Drawable previous = mPreviousView.getDrawable();
993 Drawable next = mNextView.getDrawable();
994 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
Michael Jurkaaf442092010-06-10 17:01:57 -0700996 mPreviousView.setHapticFeedbackEnabled(false);
997 mPreviousView.setOnLongClickListener(this);
998 mNextView.setHapticFeedbackEnabled(false);
999 mNextView.setOnLongClickListener(this);
1000 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001003 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005
1006 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001007 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -07001008 int deleteZoneHandleId;
Michael Jurkab8e14472010-12-20 16:06:10 -08001009
1010 final View allAppsButton = findViewById(R.id.all_apps_button);
1011 final View divider = findViewById(R.id.divider);
1012 final View configureButton = findViewById(R.id.configure_button);
1013
Michael Jurka54f7ac32010-08-02 13:56:46 -07001014 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001015 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001016 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001017 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001018 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001019 dragController.addDragListener(deleteZone);
1020
Adam Cohencdc30d52010-12-01 15:09:47 -08001021 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1022 if (allAppsDeleteZone != null) {
1023 allAppsDeleteZone.setLauncher(this);
1024 allAppsDeleteZone.setDragController(dragController);
1025 allAppsDeleteZone.setDragAndDropEnabled(false);
1026 dragController.addDragListener(allAppsDeleteZone);
1027 dragController.addDropTarget(allAppsDeleteZone);
1028 }
1029
1030 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1031 findViewById(R.id.all_apps_info_target);
1032 if (allAppsInfoTarget != null) {
1033 allAppsInfoTarget.setLauncher(this);
1034 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001035 allAppsInfoTarget.setDragAndDropEnabled(false);
1036 View marketButton = findViewById(R.id.market_button);
1037 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001038 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001039 }
1040 }
1041
Joe Onorato00acb122009-08-04 16:04:30 -04001042 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001043 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001044 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001045
Joe Onorato00acb122009-08-04 16:04:30 -04001046 // The order here is bottom to top.
1047 dragController.addDropTarget(workspace);
1048 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001049 if (allAppsInfoTarget != null) {
1050 dragController.addDropTarget(allAppsInfoTarget);
1051 }
1052 if (allAppsDeleteZone != null) {
1053 dragController.addDropTarget(allAppsDeleteZone);
1054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056
Romain Guy8a73c512009-11-09 19:19:59 -08001057 @SuppressWarnings({"UnusedDeclaration"})
1058 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001059 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001060 mWorkspace.scrollLeft();
1061 }
Romain Guy8a73c512009-11-09 19:19:59 -08001062 }
1063
1064 @SuppressWarnings({"UnusedDeclaration"})
1065 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001066 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001067 mWorkspace.scrollRight();
1068 }
Romain Guy8a73c512009-11-09 19:19:59 -08001069 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001070
1071 @SuppressWarnings({"UnusedDeclaration"})
1072 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001073 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001074
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001075 int index = -1;
1076 if (v.getId() == R.id.hotseat_left) {
1077 index = 0;
1078 } else if (v.getId() == R.id.hotseat_right) {
1079 index = 1;
1080 }
1081
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001082 // reload these every tap; you never know when they might change
1083 loadHotseats();
1084 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1085 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001086 startActivitySafely(
1087 mHotseats[index],
1088 "hotseat"
1089 );
1090 }
1091 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 /**
1094 * Creates a view representing a shortcut.
1095 *
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from R.layout.application.
1099 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001102 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104
1105 /**
1106 * Creates a view representing a shortcut inflated from the specified resource.
1107 *
1108 * @param layoutResId The id of the XML layout used to create the shortcut.
1109 * @param parent The group the shortcut belongs to.
1110 * @param info The data structure describing the shortcut.
1111 *
1112 * @return A View inflated from layoutResId.
1113 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001114 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001115 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1116 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 return favorite;
1119 }
1120
1121 /**
1122 * Add an application shortcut to the workspace.
1123 *
1124 * @param data The intent describing the application.
1125 * @param cellInfo The position on screen where to create the shortcut.
1126 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 void completeAddApplication(Context context, Intent data, int screen,
1128 int intersectCellX, int intersectCellY) {
1129 final int[] cellXY = mTmpAddItemCellCoordinates;
1130 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1131
1132 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1133 showOutOfSpaceMessage();
1134 return;
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
Joe Onorato0589f0f2010-02-08 13:44:00 -08001137 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1138 data, context);
1139
Romain Guy73b979d2009-06-09 12:57:21 -07001140 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001141 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001143 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1145 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001146 } else {
1147 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 }
1149 }
Romain Guycbb89e42009-06-08 15:52:54 -07001150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 /**
1152 * Add a shortcut to the workspace.
1153 *
1154 * @param data The intent describing the shortcut.
1155 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001157 private void completeAddShortcut(Intent data, int screen,
1158 int intersectCellX, int intersectCellY) {
1159 final int[] cellXY = mTmpAddItemCellCoordinates;
1160 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 int[] touchXY = null;
1163 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1164 touchXY = mAddDropPosition;
1165 }
1166 boolean foundCellSpan = false;
1167 if (touchXY != null) {
1168 // when dragging and dropping, just find the closest free spot
1169 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1170 int[] result = screenLayout.findNearestVacantArea(
1171 touchXY[0], touchXY[1], 1, 1, cellXY);
1172 foundCellSpan = (result != null);
1173 } else {
1174 foundCellSpan = layout.findCellForSpanThatIntersects(
1175 cellXY, 1, 1, intersectCellX, intersectCellY);
1176 }
1177
1178 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179 showOutOfSpaceMessage();
1180 return;
1181 }
1182
1183 final ShortcutInfo info = mModel.addShortcut(
1184 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185
1186 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190 }
1191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001195 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001196 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001199 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001200
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1203 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001206 // We have saved the position to which the widget was dragged-- this really only matters
1207 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001208 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001209
1210 // For now, we don't save the coordinate where we dropped the icon because we're not
1211 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1212 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001213 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001214 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001215 touchXY = mAddDropPosition;
1216 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001217 boolean foundCellSpan = false;
1218 if (touchXY != null) {
1219 // when dragging and dropping, just find the closest free spot
1220 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1221 int[] result = screenLayout.findNearestVacantArea(
1222 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001223 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001225 // if we long pressed on an empty cell to bring up a menu,
1226 // make sure we intersect the empty cell
1227 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1228 // findCellForSpanThatIntersects will just ignore them
1229 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1230 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001231 }
1232
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001234 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001236 return;
1237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 // Build Launcher-specific widget info and save to database
1240 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 launcherInfo.spanX = spanXY[0];
1242 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 LauncherModel.addItemToDatabase(this, launcherInfo,
1245 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001246 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247
1248 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001252 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001253
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001256
Michael Jurka0280c3b2010-09-17 15:00:07 -07001257 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001259
1260 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
1262 }
Romain Guycbb89e42009-06-08 15:52:54 -07001263
Adam Cohended9f8d2010-11-03 13:25:16 -07001264 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1265 @Override
1266 public void onReceive(Context context, Intent intent) {
1267 final String action = intent.getAction();
1268 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1269 mUserPresent = false;
1270 updateRunning();
1271 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1272 mUserPresent = true;
1273 updateRunning();
1274 }
1275 }
1276 };
1277
1278 @Override
1279 public void onAttachedToWindow() {
1280 super.onAttachedToWindow();
1281
1282 // Listen for broadcasts related to user-presence
1283 final IntentFilter filter = new IntentFilter();
1284 filter.addAction(Intent.ACTION_SCREEN_OFF);
1285 filter.addAction(Intent.ACTION_USER_PRESENT);
1286 registerReceiver(mReceiver, filter);
1287
Adam Cohend113e0c2010-11-11 10:48:05 -08001288 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 mVisible = true;
1290 }
1291
1292 @Override
1293 public void onDetachedFromWindow() {
1294 super.onDetachedFromWindow();
1295 mVisible = false;
1296
Adam Cohend113e0c2010-11-11 10:48:05 -08001297 if (mAttached) {
1298 unregisterReceiver(mReceiver);
1299 mAttached = false;
1300 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001301 updateRunning();
1302 }
1303
1304 public void onWindowVisibilityChanged(int visibility) {
1305 mVisible = visibility == View.VISIBLE;
1306 updateRunning();
1307 }
1308
1309 private void sendAdvanceMessage(long delay) {
1310 mHandler.removeMessages(ADVANCE_MSG);
1311 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1312 mHandler.sendMessageDelayed(msg, delay);
1313 mAutoAdvanceSentTime = System.currentTimeMillis();
1314 }
1315
1316 private void updateRunning() {
1317 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1318 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1319 mAutoAdvanceRunning = autoAdvanceRunning;
1320 if (autoAdvanceRunning) {
1321 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1322 sendAdvanceMessage(delay);
1323 } else {
1324 if (!mWidgetsToAdvance.isEmpty()) {
1325 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1326 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1327 }
1328 mHandler.removeMessages(ADVANCE_MSG);
1329 }
1330 }
1331 }
1332
1333 private final Handler mHandler = new Handler() {
1334 @Override
1335 public void handleMessage(Message msg) {
1336 if (msg.what == ADVANCE_MSG) {
1337 int i = 0;
1338 for (View key: mWidgetsToAdvance.keySet()) {
1339 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1340 final int delay = mAdvanceStagger * i;
1341 if (v instanceof Advanceable) {
1342 postDelayed(new Runnable() {
1343 public void run() {
1344 ((Advanceable) v).advance();
1345 }
1346 }, delay);
1347 }
1348 i++;
1349 }
1350 sendAdvanceMessage(mAdvanceInterval);
1351 }
1352 }
1353 };
1354
1355 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1356 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1357 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1358 if (v instanceof Advanceable) {
1359 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1360 ((Advanceable) v).willBeAdvancedByHost();
1361 updateRunning();
1362 }
1363 }
1364
1365 void removeWidgetToAutoAdvance(View hostView) {
1366 if (mWidgetsToAdvance.containsKey(hostView)) {
1367 mWidgetsToAdvance.remove(hostView);
1368 updateRunning();
1369 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 }
1371
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1373 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 launcherInfo.hostView = null;
1376 }
1377
Adam Cohended9f8d2010-11-03 13:25:16 -07001378 void showOutOfSpaceMessage() {
1379 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1380 }
1381
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001382 public LauncherAppWidgetHost getAppWidgetHost() {
1383 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
Romain Guycbb89e42009-06-08 15:52:54 -07001385
Winson Chunga9abd0e2010-10-27 17:18:37 -07001386 public LauncherModel getModel() {
1387 return mModel;
1388 }
1389
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001390 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001391 getWindow().closeAllPanels();
1392
1393 try {
1394 dismissDialog(DIALOG_CREATE_SHORTCUT);
1395 // Unlock the workspace if the dialog was showing
1396 } catch (Exception e) {
1397 // An exception is thrown if the dialog is not visible, which is fine
1398 }
1399
1400 try {
1401 dismissDialog(DIALOG_RENAME_FOLDER);
1402 // Unlock the workspace if the dialog was showing
1403 } catch (Exception e) {
1404 // An exception is thrown if the dialog is not visible, which is fine
1405 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001406
1407 // Whatever we were doing is hereby canceled.
1408 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001409 }
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 @Override
1412 protected void onNewIntent(Intent intent) {
1413 super.onNewIntent(intent);
1414
1415 // Close the menu
1416 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001417 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001418 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001419
Joe Onorato14f122b2009-11-19 14:06:36 -08001420 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1421 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001422
Michael Jurka4cb37242010-08-09 21:05:32 -07001423 // in all these cases, only animate if we're already on home
1424 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001425 mWorkspace.unshrink(alreadyOnHome);
1426 }
1427 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001428 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001429 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1430 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001431 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001432 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001433
Joe Onorato3a8820b2009-11-10 15:06:42 -08001434 final View v = getWindow().peekDecorView();
1435 if (v != null && v.getWindowToken() != null) {
1436 InputMethodManager imm = (InputMethodManager)getSystemService(
1437 INPUT_METHOD_SERVICE);
1438 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440 }
1441 }
1442
1443 @Override
1444 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1445 // Do not call super here
1446 mSavedInstanceState = savedInstanceState;
1447 }
1448
1449 @Override
1450 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001451 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452
1453 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1454 if (folders.size() > 0) {
1455 final int count = folders.size();
1456 long[] ids = new long[count];
1457 for (int i = 0; i < count; i++) {
1458 final FolderInfo info = folders.get(i).getInfo();
1459 ids[i] = info.id;
1460 }
1461 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1462 } else {
1463 super.onSaveInstanceState(outState);
1464 }
1465
Michael Jurka883f55b2010-10-21 15:47:14 -07001466 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468 if (mAddScreen > -1 && mWaitingForResult) {
1469 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1470 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1471 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 if (mFolderInfo != null && mWaitingForResult) {
1475 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1476 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1477 }
Michael Jurka946ad472010-07-09 18:05:18 -07001478
Winson Chung43b119d2010-12-06 16:45:05 -08001479 // Save the current AllApps drawer tab
1480 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1481 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1482 String currentTabTag = tabhost.getCurrentTabTag();
1483 if (currentTabTag != null) {
1484 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001485 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001486 }
1487 }
1488
1489 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001490 if (mHomeCustomizationDrawer != null) {
1491 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1492 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001493 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001494 }
1495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
1498 @Override
1499 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001505 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 }
1507
1508 TextKeyListener.getInstance().release();
1509
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001513
1514 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001515
Patrick Dubroyab962b72010-07-26 12:10:10 -07001516 // Some launcher layouts don't have a previous and next view
1517 if (mPreviousView != null) {
1518 dismissPreview(mPreviousView);
1519 }
1520 if (mNextView != null) {
1521 dismissPreview(mNextView);
1522 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001523
1524 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
1526
1527 @Override
1528 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001529 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 super.startActivityForResult(intent, requestCode);
1531 }
1532
1533 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001534 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001536
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001537 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Karl Rosaen138a0412009-04-23 19:00:21 -07001539 if (initialQuery == null) {
1540 // Use any text typed in the launcher as the initial query
1541 initialQuery = getTypedText();
1542 clearTypedText();
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 if (appSearchData == null) {
1545 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001546 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
Romain Guycbb89e42009-06-08 15:52:54 -07001548
Karl Rosaen138a0412009-04-23 19:00:21 -07001549 final SearchManager searchManager =
1550 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001551 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001552 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554
1555 @Override
1556 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001557 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558 return false;
1559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1564 .setIcon(android.R.drawable.ic_menu_add)
1565 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001566 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1567 .setIcon(android.R.drawable.ic_menu_manage)
1568 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001569 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 .setIcon(android.R.drawable.ic_menu_gallery)
1571 .setAlphabeticShortcut('W');
1572 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1573 .setIcon(android.R.drawable.ic_search_category_default)
1574 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1575 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1576 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1577 .setAlphabeticShortcut('N');
1578
1579 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001580 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1581 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582
1583 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1584 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1585 .setIntent(settings);
1586
1587 return true;
1588 }
1589
1590 @Override
1591 public boolean onPrepareOptionsMenu(Menu menu) {
1592 super.onPrepareOptionsMenu(menu);
1593
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001594 // If all apps is animating, don't show the menu, because we don't know
1595 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001596 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001597 return false;
1598 }
1599
1600 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001601 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1603 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1604
1605 // Disable add if the workspace is full.
1606 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001607 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1608 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610
1611 return true;
1612 }
1613
1614 @Override
1615 public boolean onOptionsItemSelected(MenuItem item) {
1616 switch (item.getItemId()) {
1617 case MENU_ADD:
1618 addItems();
1619 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001620 case MENU_MANAGE_APPS:
1621 manageApps();
1622 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 case MENU_WALLPAPER_SETTINGS:
1624 startWallpaper();
1625 return true;
1626 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001627 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 return true;
1629 case MENU_NOTIFICATIONS:
1630 showNotifications();
1631 return true;
1632 }
1633
1634 return super.onOptionsItemSelected(item);
1635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Karl Rosaen138a0412009-04-23 19:00:21 -07001637 /**
1638 * Indicates that we want global search for this activity by setting the globalSearch
1639 * argument for {@link #startSearch} to true.
1640 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001642 @Override
1643 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001644 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001645 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001646 }
1647
Joe Onorato9c1289c2009-08-17 11:03:03 -04001648 public boolean isWorkspaceLocked() {
1649 return mWorkspaceLoading || mWaitingForResult;
1650 }
1651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001653 if (LauncherApplication.isScreenXLarge()) {
1654 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001655 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001656 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001657 }
1658 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001659 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001660 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 }
1663
Michael Jurka0280c3b2010-09-17 15:00:07 -07001664 private void resetAddInfo() {
1665 mAddScreen = -1;
1666 mAddIntersectCellX = -1;
1667 mAddIntersectCellY = -1;
1668 mAddDropPosition = null;
1669 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001670
Winson Chung55cef262010-10-28 14:14:18 -07001671 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001672 resetAddInfo();
1673 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001674 mAddDropPosition = position;
1675
Michael Jurkaaf442092010-06-10 17:01:57 -07001676 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001677 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1678 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 }
1680
Winson Chung7ad01412010-09-27 11:33:03 -07001681 private void manageApps() {
1682 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1683 }
1684
Michael Jurkaaf442092010-06-10 17:01:57 -07001685 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001686 // TODO: catch bad widget exception when sent
1687 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001688 // TODO: Is this log message meaningful?
1689 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001690 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001691 }
1692
Winson Chung55cef262010-10-28 14:14:18 -07001693 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001694 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695
Bjorn Bringert7984c942009-12-09 15:38:25 +00001696 if (appWidget.configure != null) {
1697 // Launch over to configure widget, if needed
1698 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1699 intent.setComponent(appWidget.configure);
1700 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001701 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001702 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1703 intent.putExtra(
1704 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1705 info.mimeType);
1706
1707 final String mimeType = info.mimeType;
1708 final ClipData clipData = (ClipData) info.configurationData;
1709 final ClipDescription clipDesc = clipData.getDescription();
1710 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1711 if (clipDesc.getMimeType(i).equals(mimeType)) {
1712 final ClipData.Item item = clipData.getItem(i);
1713 final CharSequence stringData = item.getText();
1714 final Uri uriData = item.getUri();
1715 final Intent intentData = item.getIntent();
1716 final String key =
1717 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1718 if (uriData != null) {
1719 intent.putExtra(key, uriData);
1720 } else if (intentData != null) {
1721 intent.putExtra(key, intentData);
1722 } else if (stringData != null) {
1723 intent.putExtra(key, stringData);
1724 }
1725 break;
1726 }
1727 }
1728 }
Winson Chung55cef262010-10-28 14:14:18 -07001729 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001730
Romain Guy8e633c52010-03-04 12:51:36 -08001731 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001733 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001734 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
1736 }
Romain Guycbb89e42009-06-08 15:52:54 -07001737
Michael Jurka0280c3b2010-09-17 15:00:07 -07001738 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1739 resetAddInfo();
1740 mAddScreen = screen;
1741 mAddDropPosition = position;
1742
1743 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1744 createShortcutIntent.setComponent(componentName);
1745 processShortcut(createShortcutIntent);
1746 }
1747
Joe Onoratodeb98af2010-02-19 14:59:39 -08001748 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001749 // Handle case where user selected "Applications"
1750 String applicationName = getResources().getString(R.string.group_applications);
1751 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001752
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001753 if (applicationName != null && applicationName.equals(shortcutName)) {
1754 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1755 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001756
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001757 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1758 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001759 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001760 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001761 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001762 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765
Winson Chung24ab2f12010-09-16 14:10:47 -07001766 void processWallpaper(Intent intent) {
1767 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1768 }
1769
Michael Jurka0280c3b2010-09-17 15:00:07 -07001770 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1771 resetAddInfo();
1772 mAddScreen = screen;
1773 mAddDropPosition = position;
1774
1775 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1776 createFolderIntent.setComponent(componentName);
1777
1778 addLiveFolder(createFolderIntent);
1779 }
1780
1781 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001782 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001783 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001784 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001785
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001786 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001787 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001788 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001789 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792
Michael Jurka0280c3b2010-09-17 15:00:07 -07001793 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 UserFolderInfo folderInfo = new UserFolderInfo();
1795 folderInfo.title = getText(R.string.folder_name);
1796
Michael Jurka0280c3b2010-09-17 15:00:07 -07001797 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1798 final int[] cellXY = mTmpAddItemCellCoordinates;
1799 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1800 showOutOfSpaceMessage();
1801 return;
1802 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803
1804 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 LauncherModel.addItemToDatabase(this, folderInfo,
1806 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001807 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001808 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809
1810 // Create the view
1811 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001812 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1813 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001814 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
Romain Guycbb89e42009-06-08 15:52:54 -07001816
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001818 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 }
1820
Michael Jurka28750fb2010-09-24 17:43:49 -07001821 private void completeAddLiveFolder(
1822 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001823 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1824 final int[] cellXY = mTmpAddItemCellCoordinates;
1825 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1826 showOutOfSpaceMessage();
1827 return;
1828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829
Michael Jurka0280c3b2010-09-17 15:00:07 -07001830 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831
1832 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001834 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837 }
1838
1839 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841
1842 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1843 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1844
1845 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 Intent.ShortcutIconResource iconResource = null;
1847
1848 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1849 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1850 try {
1851 iconResource = (Intent.ShortcutIconResource) extra;
1852 final PackageManager packageManager = context.getPackageManager();
1853 Resources resources = packageManager.getResourcesForApplication(
1854 iconResource.packageName);
1855 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1856 icon = resources.getDrawable(id);
1857 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001858 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
1860 }
1861
1862 if (icon == null) {
1863 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1864 }
1865
1866 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001867 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 info.title = name;
1869 info.iconResource = iconResource;
1870 info.uri = data.getData();
1871 info.baseIntent = baseIntent;
1872 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1873 LiveFolders.DISPLAY_MODE_GRID);
1874
1875 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001876 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001877 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878
1879 return info;
1880 }
1881
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 private void showNotifications() {
1883 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1884 if (statusBar != null) {
1885 statusBar.expand();
1886 }
1887 }
1888
1889 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001890 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001892 Intent chooser = Intent.createChooser(pickWallpaper,
1893 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001894 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1895 // Removed in Eclair MR1
1896// WallpaperManager wm = (WallpaperManager)
1897// getSystemService(Context.WALLPAPER_SERVICE);
1898// WallpaperInfo wi = wm.getWallpaperInfo();
1899// if (wi != null && wi.getSettingsActivity() != null) {
1900// LabeledIntent li = new LabeledIntent(getPackageName(),
1901// R.string.configure_wallpaper, 0);
1902// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1903// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1904// }
Mike Clerona0618e42009-10-22 13:55:21 -07001905 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001908 /**
1909 * Registers various content observers. The current implementation registers
1910 * only a favorites observer to keep track of the favorites applications.
1911 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001912 private void registerContentObservers() {
1913 ContentResolver resolver = getContentResolver();
1914 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1915 true, mWidgetObserver);
1916 }
1917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 @Override
1919 public boolean dispatchKeyEvent(KeyEvent event) {
1920 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1921 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001923 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001924 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001925 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001926 dumpState();
1927 return true;
1928 }
1929 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001930 }
1931 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1932 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001933 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 return true;
1935 }
1936 }
1937
1938 return super.dispatchKeyEvent(event);
1939 }
1940
Joe Onorato88ec0992009-11-19 13:16:06 -08001941 @Override
1942 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001943 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1944 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001945 } else {
1946 closeFolder();
1947 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001948 // Some launcher layouts don't have a previous and next view
1949 if (mPreviousView != null) {
1950 dismissPreview(mPreviousView);
1951 dismissPreview(mNextView);
1952 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001953 }
1954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 private void closeFolder() {
1956 Folder folder = mWorkspace.getOpenFolder();
1957 if (folder != null) {
1958 closeFolder(folder);
1959 }
1960 }
1961
1962 void closeFolder(Folder folder) {
1963 folder.getInfo().opened = false;
1964 ViewGroup parent = (ViewGroup) folder.getParent();
1965 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001966 CellLayout cl = (CellLayout) parent;
1967 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001968 if (folder instanceof DropTarget) {
1969 // Live folders aren't DropTargets.
1970 mDragController.removeDropTarget((DropTarget)folder);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973 folder.onClose();
1974 }
1975
1976 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001977 * Re-listen when widgets are reset.
1978 */
1979 private void onAppWidgetReset() {
1980 mAppWidgetHost.startListening();
1981 }
1982
1983 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1985 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 private void unbindDesktopItems() {
1988 for (ItemInfo item: mDesktopItems) {
1989 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 /**
1994 * Launches the intent referred by the clicked shortcut.
1995 *
1996 * @param v The view representing the clicked shortcut.
1997 */
1998 public void onClick(View v) {
1999 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002000 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002002 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002003 int[] pos = new int[2];
2004 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002005 intent.setSourceBounds(new Rect(pos[0], pos[1],
2006 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002007 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 } else if (tag instanceof FolderInfo) {
2009 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002010 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002011 if (mState == State.ALL_APPS) {
2012 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002013 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002014 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002015 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
2017 }
2018
Michael Jurka0e260592010-06-30 17:07:39 -07002019 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002020 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002021 // clicking anywhere on the workspace causes the customization drawer to slide down
2022 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002023 return false;
2024 }
2025
Michael Jurkaaf442092010-06-10 17:01:57 -07002026 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002027 * Event handler for the search button
2028 *
2029 * @param v The view that was clicked.
2030 */
2031 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002032 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002033 // Use a custom animation for launching search
2034 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002035 }
2036
2037 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002038 * Event handler for the voice button
2039 *
2040 * @param v The view that was clicked.
2041 */
2042 public void onClickVoiceButton(View v) {
2043 startVoiceSearch();
2044 }
2045
2046 private void startVoiceSearch() {
2047 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2048 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2049 startActivity(intent);
2050 }
2051
2052 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002053 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002054 * enters home screen customization mode.
2055 *
2056 * @param v The view that was clicked.
2057 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002058 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002059 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002060 }
2061
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002062 /**
2063 * Event handler for the "grid" button that appears on the home screen, which
2064 * enters all apps mode.
2065 *
2066 * @param v The view that was clicked.
2067 */
2068 public void onClickAllAppsButton(View v) {
2069 showAllApps(true);
2070 }
2071
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002072 public void onClickAppMarketButton(View v) {
2073 if (mAppMarketIntent != null) {
2074 startActivitySafely(mAppMarketIntent, "app market");
2075 }
2076 }
2077
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002078 void startApplicationDetailsActivity(ComponentName componentName) {
2079 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002080 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2081 Uri.fromParts("package", packageName, null));
2082 startActivity(intent);
2083 }
2084
Patrick Dubroy5539af72010-09-07 15:22:01 -07002085 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2086 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2087 // System applications cannot be installed. For now, show a toast explaining that.
2088 // We may give them the option of disabling apps this way.
2089 int messageId = R.string.uninstall_system_app_text;
2090 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2091 } else {
2092 String packageName = appInfo.componentName.getPackageName();
2093 String className = appInfo.componentName.getClassName();
2094 Intent intent = new Intent(
2095 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2096 startActivity(intent);
2097 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002098 }
2099
Joe Onoratof984e852010-03-25 09:47:45 -07002100 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2102 try {
2103 startActivity(intent);
2104 } catch (ActivityNotFoundException e) {
2105 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002106 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 } catch (SecurityException e) {
2108 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002109 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002111 "or use the exported attribute for this activity. "
2112 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002115
Romain Guy8e633c52010-03-04 12:51:36 -08002116 void startActivityForResultSafely(Intent intent, int requestCode) {
2117 try {
2118 startActivityForResult(intent, requestCode);
2119 } catch (ActivityNotFoundException e) {
2120 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2121 } catch (SecurityException e) {
2122 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2123 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2124 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2125 "or use the exported attribute for this activity.", e);
2126 }
2127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128
2129 private void handleFolderClick(FolderInfo folderInfo) {
2130 if (!folderInfo.opened) {
2131 // Close any open folder
2132 closeFolder();
2133 // Open the requested folder
2134 openFolder(folderInfo);
2135 } else {
2136 // Find the open folder...
2137 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2138 int folderScreen;
2139 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002140 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 // .. and close it
2142 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002143 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 // Close any folder open on the current screen
2145 closeFolder();
2146 // Pull the folder onto this screen
2147 openFolder(folderInfo);
2148 }
2149 }
2150 }
2151 }
2152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002154 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 * is animated relative to the specified View. If the View is null, no animation
2156 * is played.
2157 *
2158 * @param folderInfo The FolderInfo describing the folder to open.
2159 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002160 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 Folder openFolder;
2162
2163 if (folderInfo instanceof UserFolderInfo) {
2164 openFolder = UserFolder.fromXml(this);
2165 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002166 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 } else {
2168 return;
2169 }
2170
Joe Onorato00acb122009-08-04 16:04:30 -04002171 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 openFolder.setLauncher(this);
2173
2174 openFolder.bind(folderInfo);
2175 folderInfo.opened = true;
2176
Winson Chungaafa03c2010-06-11 17:34:16 -07002177 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 openFolder.onOpen();
2180 }
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002183 switch (v.getId()) {
2184 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002185 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002186 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2187 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002188 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002189 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002190 return true;
2191 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002192 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002193 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2194 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002195 showPreviews(v);
2196 }
2197 return true;
2198 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002199 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002200 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2201 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2202 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002203 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002204 return true;
2205 }
2206
Joe Onorato9c1289c2009-08-17 11:03:03 -04002207 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 return false;
2209 }
2210
2211 if (!(v instanceof CellLayout)) {
2212 v = (View) v.getParent();
2213 }
2214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
Michael Jurka0280c3b2010-09-17 15:00:07 -07002216 resetAddInfo();
2217 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002219 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 return true;
2221 }
2222
Michael Jurka0280c3b2010-09-17 15:00:07 -07002223 final View itemUnderLongClick = longClickCellInfo.cell;
2224
Winson Chung304dcde2011-01-07 11:17:23 -08002225 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002226 if (itemUnderLongClick == null) {
2227 // User long pressed on empty space
2228 mWorkspace.setAllowLongPress(false);
2229 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2230 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002231 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002232 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 }
2234 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002237 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2238 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002239 mAddIntersectCellX = longClickCellInfo.cellX;
2240 mAddIntersectCellY = longClickCellInfo.cellY;
2241 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243 }
2244 }
2245 return true;
2246 }
2247
Romain Guye6b8e2f2009-11-10 11:56:55 -08002248 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002249 private void dismissPreview(final View v) {
2250 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002251 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002252 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2253 public void onDismiss() {
2254 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2255 int count = group.getChildCount();
2256 for (int i = 0; i < count; i++) {
2257 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2258 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002259 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2260 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2261
2262 v.setTag(R.id.workspace, null);
2263 v.setTag(R.id.icon, null);
2264 window.setOnDismissListener(null);
2265 }
2266 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002267 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002268 }
2269 v.setTag(null);
2270 }
2271
Romain Guyf8e6a802009-12-07 17:48:02 -08002272 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002273 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002274 }
2275
Romain Guya6abce82009-11-10 02:54:41 -08002276 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002277 final Resources resources = getResources();
2278 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002279
Romain Guya6abce82009-11-10 02:54:41 -08002280 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002281
Romain Guy9d31e9f2009-11-11 18:54:28 -08002282 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002283
Romain Guyf8e6a802009-12-07 17:48:02 -08002284 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002285 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002286 int extraW = (int) ((r.left + r.right) * max);
2287 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002288
2289 int aW = cell.getWidth() - extraW;
2290 float w = aW / max;
2291
2292 int width = cell.getWidth();
2293 int height = cell.getHeight();
2294 int x = cell.getLeftPadding();
2295 int y = cell.getTopPadding();
2296 width -= (x + cell.getRightPadding());
2297 height -= (y + cell.getBottomPadding());
2298
2299 float scale = w / width;
2300
2301 int count = end - start;
2302
2303 final float sWidth = width * scale;
2304 float sHeight = height * scale;
2305
Romain Guye6b8e2f2009-11-10 11:56:55 -08002306 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002307
Romain Guye6b8e2f2009-11-10 11:56:55 -08002308 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2309 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002310
2311 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002312 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002313 cell = (CellLayout) workspace.getChildAt(i);
2314
Romain Guyf8e6a802009-12-07 17:48:02 -08002315 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002316 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002317
2318 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002319 c.scale(scale, scale);
2320 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002321 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002322
Romain Guy9d31e9f2009-11-11 18:54:28 -08002323 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002324 image.setImageBitmap(bitmap);
2325 image.setTag(i);
2326 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002327 image.setOnFocusChangeListener(handler);
2328 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002329 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002330
2331 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002332 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2333
Winson Chungaafa03c2010-06-11 17:34:16 -07002334 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002335 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002336
2337 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002338 p.setContentView(preview);
2339 p.setWidth((int) (sWidth * count + extraW));
2340 p.setHeight((int) (sHeight + extraH));
2341 p.setAnimationStyle(R.style.AnimationPreview);
2342 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002343 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002344 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002345 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002346
Romain Guye6b8e2f2009-11-10 11:56:55 -08002347 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2348 public void onDismiss() {
2349 dismissPreview(anchor);
2350 }
2351 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002352
2353 anchor.setTag(p);
2354 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002355 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002356 }
2357
Romain Guy9d31e9f2009-11-11 18:54:28 -08002358 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002359 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002360
Romain Guye6b8e2f2009-11-10 11:56:55 -08002361 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002362 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002363 }
2364
2365 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002366 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002367 v.post(this);
2368 }
2369
2370 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002371 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002372 }
2373
2374 public void onFocusChange(View v, boolean hasFocus) {
2375 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002376 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002377 }
Romain Guya6abce82009-11-10 02:54:41 -08002378 }
2379 }
2380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 Workspace getWorkspace() {
2382 return mWorkspace;
2383 }
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 @Override
2386 protected Dialog onCreateDialog(int id) {
2387 switch (id) {
2388 case DIALOG_CREATE_SHORTCUT:
2389 return new CreateShortcut().createDialog();
2390 case DIALOG_RENAME_FOLDER:
2391 return new RenameFolder().createDialog();
2392 }
2393
2394 return super.onCreateDialog(id);
2395 }
2396
2397 @Override
2398 protected void onPrepareDialog(int id, Dialog dialog) {
2399 switch (id) {
2400 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 break;
2402 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002403 if (mFolderInfo != null) {
2404 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2405 final CharSequence text = mFolderInfo.title;
2406 input.setText(text);
2407 input.setSelection(0, text.length());
2408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 break;
2410 }
2411 }
2412
2413 void showRenameDialog(FolderInfo info) {
2414 mFolderInfo = info;
2415 mWaitingForResult = true;
2416 showDialog(DIALOG_RENAME_FOLDER);
2417 }
2418
Michael Jurka0280c3b2010-09-17 15:00:07 -07002419 private void showAddDialog(int intersectX, int intersectY) {
2420 resetAddInfo();
2421 mAddIntersectCellX = intersectX;
2422 mAddIntersectCellY = intersectY;
2423 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 mWaitingForResult = true;
2425 showDialog(DIALOG_CREATE_SHORTCUT);
2426 }
2427
Joe Onoratodeb98af2010-02-19 14:59:39 -08002428 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002429 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002430 Bundle bundle = new Bundle();
2431
2432 ArrayList<String> shortcutNames = new ArrayList<String>();
2433 shortcutNames.add(getString(R.string.group_applications));
2434 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2435
2436 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2437 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2438 R.drawable.ic_launcher_application));
2439 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2440
2441 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2442 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002443 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002444 pickIntent.putExtras(bundle);
2445
Joe Onoratodeb98af2010-02-19 14:59:39 -08002446 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 private class RenameFolder {
2450 private EditText mInput;
2451
2452 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2454 mInput = (EditText) layout.findViewById(R.id.folder_name);
2455
2456 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2457 builder.setIcon(0);
2458 builder.setTitle(getString(R.string.rename_folder_title));
2459 builder.setCancelable(true);
2460 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2461 public void onCancel(DialogInterface dialog) {
2462 cleanup();
2463 }
2464 });
2465 builder.setNegativeButton(getString(R.string.cancel_action),
2466 new Dialog.OnClickListener() {
2467 public void onClick(DialogInterface dialog, int which) {
2468 cleanup();
2469 }
2470 }
2471 );
2472 builder.setPositiveButton(getString(R.string.rename_action),
2473 new Dialog.OnClickListener() {
2474 public void onClick(DialogInterface dialog, int which) {
2475 changeFolderName();
2476 }
2477 }
2478 );
2479 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002480
2481 final AlertDialog dialog = builder.create();
2482 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2483 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002484 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002485 mInput.requestFocus();
2486 InputMethodManager inputManager = (InputMethodManager)
2487 getSystemService(Context.INPUT_METHOD_SERVICE);
2488 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002489 }
2490 });
2491
2492 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 }
2494
2495 private void changeFolderName() {
2496 final String name = mInput.getText().toString();
2497 if (!TextUtils.isEmpty(name)) {
2498 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002499 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 mFolderInfo.title = name;
2501 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2502
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002504 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002505 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 } else {
2507 final FolderIcon folderIcon = (FolderIcon)
2508 mWorkspace.getViewForTag(mFolderInfo);
2509 if (folderIcon != null) {
2510 folderIcon.setText(name);
2511 getWorkspace().requestLayout();
2512 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002513 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002514 mWorkspaceLoading = true;
2515 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 }
2517 }
2518 }
2519 cleanup();
2520 }
2521
2522 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 dismissDialog(DIALOG_RENAME_FOLDER);
2524 mWaitingForResult = false;
2525 mFolderInfo = null;
2526 }
2527 }
2528
Daniel Sandler843e8602010-06-07 14:59:01 -04002529 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2530 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002531 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002532 }
2533
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002534 // AllAppsView.Watcher
2535 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002536 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2537 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002538 mWorkspace.setVisibility(View.GONE);
2539 }
2540 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 private void showToolbarButton(View button) {
2543 button.setAlpha(1.0f);
2544 button.setVisibility(View.VISIBLE);
2545 button.setFocusable(true);
2546 button.setClickable(true);
2547 }
2548
2549 private void hideToolbarButton(View button) {
2550 button.setAlpha(0.0f);
2551 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2552 button.setVisibility(View.INVISIBLE);
2553 button.setFocusable(false);
2554 button.setClickable(false);
2555 }
2556
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002558 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002559 *
2560 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2561 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002562 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002563 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002564 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002565 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002566 final boolean showing = show;
2567 final boolean hiding = !show;
2568
2569 final int duration = show ?
2570 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2571 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2572
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002573 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002574 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2575 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002576 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002577 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002578 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002579 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002580 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002581 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002582 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002583 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002584 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 });
2586 seq.play(anim);
2587 } else {
2588 if (showing) {
2589 showToolbarButton(view);
2590 } else {
2591 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002592 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002593 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594 }
2595
2596 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 * Show/hide the appropriate toolbar buttons for newState.
2598 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 *
2600 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002601 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2602 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002604 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002605 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002607 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 final View configureButton = findViewById(R.id.configure_button);
2609
2610 switch (newState) {
2611 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002612 hideOrShowToolbarButton(true, searchButton, showSeq);
2613 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002614 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002615 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002616 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002617 mDeleteZone.setDragAndDropEnabled(true);
2618 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 break;
2620 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002621 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002622 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002623 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002624 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002625 mDeleteZone.setDragAndDropEnabled(false);
2626 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 break;
2628 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002629 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002630 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002631 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002632 hideOrShowToolbarButton(false, configureButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002633 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 break;
2635 }
2636 }
2637
2638 /**
2639 * Helper method for the cameraZoomIn/cameraZoomOut animations
2640 * @param view The view being animated
2641 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2642 * @param scaleFactor The scale factor used for the zoom
2643 */
2644 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2645 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002646
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002647 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002648 // Set pivotY so that at the starting zoom factor, the view is partially
2649 // visible. Modifying initialHeightFactor changes how much of the view is
2650 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002652 final float initialHeightFactor = 0.165f;
2653 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002654 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002655 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002656 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 }
2658 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002659
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002660 /**
2661 * Zoom the camera out from the workspace to reveal 'toView'.
2662 * Assumes that the view to show is anchored at either the very top or very bottom
2663 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002665 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002666 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002667 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002669
2670 final int duration = toAllApps ?
2671 res.getInteger(R.integer.config_allAppsZoomInTime) :
2672 res.getInteger(R.integer.config_customizeZoomInTime);
2673
2674 final float scale = toAllApps ?
2675 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2676 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2677
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002679
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 setPivotsForZoom(toView, toState, scale);
2681
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002682 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002683 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Adam Cohenf16e5712011-01-13 13:31:45 -08002684 toView.setAlpha(0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002686 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002688
2689 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002690 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2691 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2692 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
Adam Cohenf16e5712011-01-13 13:31:45 -08002693
Chet Haase472b2812010-10-14 07:02:04 -07002694 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002695
Adam Cohenf16e5712011-01-13 13:31:45 -08002696 if (toAllApps) {
2697 ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2698 PropertyValuesHolder.ofFloat("alpha", 1.0f));
2699 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2700 alphaAnim.setDuration(duration);
2701 alphaAnim.start();
2702 }
2703
Adam Cohen61033d32010-11-15 18:29:44 -08002704 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002705 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002706 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002707 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002708 // Prepare the position
2709 toView.setTranslationX(0.0f);
2710 toView.setTranslationY(0.0f);
2711 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002712 if (!toAllApps) {
2713 toView.setAlpha(1.0f);
2714 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002715 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002716 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002717 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002718 // If we don't set the final scale values here, if this animation is cancelled
2719 // it will have the wrong scale value and subsequent cameraPan animations will
2720 // not fix that
2721 toView.setScaleX(1.0f);
2722 toView.setScaleY(1.0f);
2723 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002724 });
2725
Chet Haaseb1254a62010-09-07 13:35:00 -07002726 AnimatorSet toolbarHideAnim = new AnimatorSet();
2727 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002728 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2729
2730 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002731 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002733 if (mStateAnimation != null) mStateAnimation.cancel();
2734 mStateAnimation = new AnimatorSet();
2735 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2736 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737
2738 // Show the new toolbar buttons just as the main animation is ending
2739 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002740 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2741 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742 } else {
2743 toView.setTranslationX(0.0f);
2744 toView.setTranslationY(0.0f);
2745 toView.setScaleX(1.0f);
2746 toView.setScaleY(1.0f);
2747 toView.setVisibility(View.VISIBLE);
2748 hideAndShowToolbarButtons(toState, null, null);
2749 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002750 }
2751
2752 /**
2753 * Zoom the camera back into the workspace, hiding 'fromView'.
2754 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002755 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2756 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002757 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002758 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002759 cameraZoomIn(fromState, animated, false);
2760 }
2761
2762 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002763 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002764 final boolean fromAllApps = (fromState == State.ALL_APPS);
2765
2766 int duration = fromAllApps ?
2767 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2768 res.getInteger(R.integer.config_customizeZoomOutTime);
2769
2770 float scaleFactor = fromAllApps ?
2771 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2772 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2773
2774 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002775
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002776 mCustomizePagedView.endChoiceMode();
2777 mAllAppsPagedView.endChoiceMode();
2778
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002779 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780
Michael Jurkad3ef3062010-11-23 16:23:58 -08002781 if (!springLoaded) {
2782 mWorkspace.unshrink(animated);
2783 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002784
2785 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002786 if (mStateAnimation != null) mStateAnimation.cancel();
2787 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002788 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2789 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2790 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2791 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002792 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2793
2794 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2795 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2796 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002797 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002798 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002799 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002800 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002801 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002802 }
2803 });
2804
Chet Haaseb1254a62010-09-07 13:35:00 -07002805 AnimatorSet toolbarHideAnim = new AnimatorSet();
2806 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002807 if (!springLoaded) {
2808 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2809 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002810
Adam Cohen61033d32010-11-15 18:29:44 -08002811 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002812
2813 // Show the new toolbar buttons at the very end of the whole animation
2814 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2815 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002816 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2817 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 } else {
2819 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002820 if (!springLoaded) {
2821 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2822 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002823 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002824 }
2825
2826 /**
2827 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2828 * This is the transition used on xlarge screens to go between all apps and
2829 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002830 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2831 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2832 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002833 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002835 final Resources res = getResources();
2836 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002837 final int workspaceHeight = mWorkspace.getHeight();
2838
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002839 final boolean fromAllApps = (fromState == State.ALL_APPS);
2840 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2841 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002842
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2844 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2845 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2846 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002847
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002848 mCustomizePagedView.endChoiceMode();
2849 mAllAppsPagedView.endChoiceMode();
2850
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002851 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002852 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002853 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002854 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002855 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002856
2857 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002858 if (mStateAnimation != null) mStateAnimation.cancel();
2859 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002860 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002861 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002862 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 toView.setVisibility(View.VISIBLE);
2864 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002865 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002866 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002867 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002868 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869 fromView.setVisibility(View.GONE);
2870 }
2871 });
2872
Chet Haaseb1254a62010-09-07 13:35:00 -07002873 AnimatorSet toolbarHideAnim = new AnimatorSet();
2874 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2876
Chet Haase472b2812010-10-14 07:02:04 -07002877 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2878 fromViewStartY, fromViewEndY);
2879 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002880 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2881 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2882 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2883 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2884 );
Chet Haase472b2812010-10-14 07:02:04 -07002885 fromAnim.setDuration(duration);
2886 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002887
2888 // Show the new toolbar buttons just as the main animation is ending
2889 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002890 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2891 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 } else {
2893 fromView.setY(fromViewEndY);
2894 fromView.setVisibility(View.GONE);
2895 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002896 toView.setScaleX(1.0f);
2897 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002898 toView.setVisibility(View.VISIBLE);
2899 hideAndShowToolbarButtons(toState, null, null);
2900 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002901 }
2902
2903 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002904 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002905 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002906 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002907
Michael Jurka79212d82010-07-30 16:36:20 -07002908 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002909 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002910 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002911 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002912 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002913 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002914 } else {
2915 mAllAppsGrid.zoom(1.0f, animated);
2916 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002917
Romain Guyc16fea72010-03-12 17:17:56 -08002918 ((View) mAllAppsGrid).setFocusable(true);
2919 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002920
Joe Onorato7c312c12009-08-13 21:36:53 -07002921 // TODO: fade these two too
2922 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002923
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002924 // Change the state *after* we've called all the transition code
2925 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002926
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002927 // send an accessibility event to announce the context change
2928 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2929 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002930
2931 void showWorkspace(boolean animated) {
2932 showWorkspace(animated, null);
2933 }
2934
2935 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002936 if (layout != null) {
2937 // always animated, but that's ok since we never specify a layout and
2938 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002939 mWorkspace.unshrink(layout);
2940 } else {
2941 mWorkspace.unshrink(animated);
2942 }
2943 if (mState == State.ALL_APPS) {
2944 closeAllApps(animated);
2945 } else if (mState == State.CUSTOMIZE) {
2946 hideCustomizationDrawer(animated);
2947 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002948
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002949 // Change the state *after* we've called all the transition code
2950 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002951
2952 // send an accessibility event to announce the context change
2953 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002954 }
2955
Michael Jurkad3ef3062010-11-23 16:23:58 -08002956 void enterSpringLoadedDragMode(CellLayout layout) {
2957 mWorkspace.enterSpringLoadedDragMode(layout);
2958 if (mState == State.ALL_APPS) {
2959 cameraZoomIn(State.ALL_APPS, true, true);
2960 mState = State.ALL_APPS_SPRING_LOADED;
2961 } else if (mState == State.CUSTOMIZE) {
2962 cameraZoomIn(State.CUSTOMIZE, true, true);
2963 mState = State.CUSTOMIZE_SPRING_LOADED;
2964 }/* else {
2965 // we're already in spring loaded mode; don't do anything
2966 }*/
2967 }
2968
2969 void exitSpringLoadedDragMode() {
2970 if (mState == State.ALL_APPS_SPRING_LOADED) {
2971 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2972 cameraZoomOut(State.ALL_APPS, true);
2973 mState = State.ALL_APPS;
2974 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2975 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2976 cameraZoomOut(State.CUSTOMIZE, true);
2977 mState = State.CUSTOMIZE;
2978 }/* else {
2979 // we're not in spring loaded mode; don't do anything
2980 }*/
2981 }
2982
Joe Onoratob2061212009-11-24 16:13:54 -05002983 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002984 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002985 * - Home from workspace
2986 * - from center screen
2987 * - from other screens
2988 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002989 * - from center screen
2990 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002991 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002992 * - from center screen
2993 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002994 * - Launch app from workspace and quit
2995 * - with back
2996 * - with home
2997 * - Launch app from all apps and quit
2998 * - with back
2999 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003000 * - Go to a screen that's not the default, then all
3001 * apps, and launch and app, and go back
3002 * - with back
3003 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003004 * - On workspace, long press power and go back
3005 * - with back
3006 * - with home
3007 * - On all apps, long press power and go back
3008 * - with back
3009 * - with home
3010 * - On workspace, power off
3011 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003012 * - Launch an app and turn off the screen while in that app
3013 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003014 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003015 * - From all apps
3016 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003017 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3018 * - From all apps
3019 * - From the center workspace
3020 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003021 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003022 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003023 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003024 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003025 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003026 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003027 } else {
3028 mAllAppsGrid.zoom(0.0f, animated);
3029 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003030 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003031 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003032 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003033 }
3034
Joe Onorato7c312c12009-08-13 21:36:53 -07003035 void lockAllApps() {
3036 // TODO
3037 }
3038
3039 void unlockAllApps() {
3040 // TODO
3041 }
3042
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003043 // Show the customization drawer (only exists in x-large configuration)
3044 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003045 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003046 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003047 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003048 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003049 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003050 // Change the state *after* we've called all the transition code
3051 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003052 }
3053
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003054 // Hide the customization drawer (only exists in x-large configuration)
3055 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003056 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003057 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003058 }
3059 }
3060
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003061 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3062 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3063 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003064 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003065 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003066
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003067 void onWorkspaceClick(CellLayout layout) {
3068 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003069 }
3070
Michael Jurka4ef207b2010-11-29 17:05:45 -08003071 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003072 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003073 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3074 ImageView button = (ImageView) findViewById(buttonId);
3075 Drawable toolbarIcon = null;
3076 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003077 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003078 // Look for the toolbar icon specified in the activity meta-data
3079 Bundle metaData = packageManager.getActivityInfo(
3080 activityName, PackageManager.GET_META_DATA).metaData;
3081 if (metaData != null) {
3082 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3083 if (iconResId != 0) {
3084 Resources res = packageManager.getResourcesForActivity(activityName);
3085 toolbarIcon = res.getDrawable(iconResId);
3086 }
3087 }
3088 } catch (NameNotFoundException e) {
3089 // Do nothing
3090 }
3091 // If we were unable to find the icon via the meta-data, use a generic one
3092 if (toolbarIcon == null) {
3093 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003094 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003095 } else {
3096 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003097 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 }
3099 }
3100
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003101 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003102 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003103 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003104 }
3105
Michael Jurka0423dcf2010-10-05 14:56:18 -07003106 private void updateGlobalSearchIcon() {
3107 if (LauncherApplication.isScreenXLarge()) {
3108 final SearchManager searchManager =
3109 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3110 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003111 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003112 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003113 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003114 } else {
3115 findViewById(R.id.search_button).setVisibility(View.GONE);
3116 }
3117 }
3118 }
3119
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003120 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003121 updateButtonWithDrawable(R.id.search_button, d);
3122 }
3123
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003124 private void updateVoiceSearchIcon() {
3125 if (LauncherApplication.isScreenXLarge()) {
3126 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3127 ComponentName activityName = intent.resolveActivity(getPackageManager());
3128 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003129 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003130 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3131 } else {
3132 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003133 }
3134 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003135 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003136
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003137 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003138 updateButtonWithDrawable(R.id.voice_button, d);
3139 }
3140
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003141 /**
3142 * Sets the app market icon (shown when all apps is visible on x-large screens)
3143 */
3144 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145 if (LauncherApplication.isScreenXLarge()) {
3146 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3147 // Find the app market activity by resolving an intent.
3148 // (If multiple app markets are installed, it will return the ResolverActivity.)
3149 ComponentName activityName = intent.resolveActivity(getPackageManager());
3150 if (activityName != null) {
3151 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003152 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003153 R.id.market_button, activityName, R.drawable.app_market_generic);
3154 }
3155 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003156 }
3157
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003158 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003159 updateButtonWithDrawable(R.id.market_button, d);
3160 }
3161
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003162 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003163 * Displays the shortcut creation dialog and launches, if necessary, the
3164 * appropriate activity.
3165 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003166 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003167 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3168 DialogInterface.OnShowListener {
3169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3176 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003177 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 builder.setInverseBackgroundForced(true);
3180
3181 AlertDialog dialog = builder.create();
3182 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003183 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003184 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 return dialog;
3187 }
3188
3189 public void onCancel(DialogInterface dialog) {
3190 mWaitingForResult = false;
3191 cleanup();
3192 }
3193
Romain Guycbb89e42009-06-08 15:52:54 -07003194 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003195 }
3196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003198 try {
3199 dismissDialog(DIALOG_CREATE_SHORTCUT);
3200 } catch (Exception e) {
3201 // An exception is thrown if the dialog is not visible, which is fine
3202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
3204
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003205 /**
3206 * Handle the action clicked in the "Add to home" dialog.
3207 */
3208 public void onClick(DialogInterface dialog, int which) {
3209 Resources res = getResources();
3210 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003211
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003212 switch (which) {
3213 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003214 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003215 break;
3216 }
Romain Guycbb89e42009-06-08 15:52:54 -07003217
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003218 case AddAdapter.ITEM_APPWIDGET: {
3219 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003221 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3222 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003223 // start the pick activity
3224 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3225 break;
3226 }
Romain Guycbb89e42009-06-08 15:52:54 -07003227
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003228 case AddAdapter.ITEM_LIVE_FOLDER: {
3229 // Insert extra item to handle inserting folder
3230 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003231
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003232 ArrayList<String> shortcutNames = new ArrayList<String>();
3233 shortcutNames.add(res.getString(R.string.group_folder));
3234 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003235
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003236 ArrayList<ShortcutIconResource> shortcutIcons =
3237 new ArrayList<ShortcutIconResource>();
3238 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3239 R.drawable.ic_launcher_folder));
3240 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3241
3242 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3243 pickIntent.putExtra(Intent.EXTRA_INTENT,
3244 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3245 pickIntent.putExtra(Intent.EXTRA_TITLE,
3246 getText(R.string.title_select_live_folder));
3247 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003248
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003249 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3250 break;
3251 }
3252
3253 case AddAdapter.ITEM_WALLPAPER: {
3254 startWallpaper();
3255 break;
3256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257 }
3258 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003259
3260 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003261 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003263 }
3264
3265 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003266 * Receives notifications when applications are added/removed.
3267 */
3268 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3269 @Override
3270 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003271 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003272 String reason = intent.getStringExtra("reason");
3273 if (!"homekey".equals(reason)) {
3274 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003275 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003276 animate = false;
3277 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003278 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003279 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003280 }
3281 }
3282
3283 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003284 * Receives notifications whenever the appwidgets are reset.
3285 */
3286 private class AppWidgetResetObserver extends ContentObserver {
3287 public AppWidgetResetObserver() {
3288 super(new Handler());
3289 }
3290
3291 @Override
3292 public void onChange(boolean selfChange) {
3293 onAppWidgetReset();
3294 }
3295 }
3296
3297 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003298 * If the activity is currently paused, signal that we need to re-run the loader
3299 * in onResume.
3300 *
3301 * This needs to be called from incoming places where resources might have been loaded
3302 * while we are paused. That is becaues the Configuration might be wrong
3303 * when we're not running, and if it comes back to what it was when we
3304 * were paused, we are not restarted.
3305 *
3306 * Implementation of the method from LauncherModel.Callbacks.
3307 *
3308 * @return true if we are currently paused. The caller might be able to
3309 * skip some work in that case since we will come back again.
3310 */
3311 public boolean setLoadOnResume() {
3312 if (mPaused) {
3313 Log.i(TAG, "setLoadOnResume");
3314 mOnResumeNeedsLoad = true;
3315 return true;
3316 } else {
3317 return false;
3318 }
3319 }
3320
3321 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003322 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003325 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003326 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003327 } else {
3328 return SCREEN_COUNT / 2;
3329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003331
Winson Chunga12a2502010-12-20 14:41:35 -08003332 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003333 mAllAppsPagedView = view;
3334 }
3335
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 /**
3337 * Refreshes the shortcuts shown on the workspace.
3338 *
3339 * Implementation of the method from LauncherModel.Callbacks.
3340 */
3341 public void startBinding() {
3342 final Workspace workspace = mWorkspace;
3343 int count = workspace.getChildCount();
3344 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003345 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003346 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3347 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003348
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 if (DEBUG_USER_INTERFACE) {
3350 android.widget.Button finishButton = new android.widget.Button(this);
3351 finishButton.setText("Finish");
3352 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3353
3354 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3355 public void onClick(View v) {
3356 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003357 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003359 }
3360 }
3361
3362 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 * Bind the items start-end from the list.
3364 *
3365 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003366 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3368
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003369 setLoadOnResume();
3370
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 final Workspace workspace = mWorkspace;
3372
3373 for (int i=start; i<end; i++) {
3374 final ItemInfo item = shortcuts.get(i);
3375 mDesktopItems.add(item);
3376 switch (item.itemType) {
3377 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3378 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003379 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3381 false);
3382 break;
3383 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3384 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003385 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003386 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3388 false);
3389 break;
3390 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3391 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3392 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003393 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 (LiveFolderInfo) item);
3395 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3396 false);
3397 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003399 }
3400
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003402 }
3403
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 /**
3405 * Implementation of the method from LauncherModel.Callbacks.
3406 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003407 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003408 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003409 sFolders.clear();
3410 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003411 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412
3413 /**
3414 * Add the views for a widget to the workspace.
3415 *
3416 * Implementation of the method from LauncherModel.Callbacks.
3417 */
3418 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003419 setLoadOnResume();
3420
Daniel Sandler843e8602010-06-07 14:59:01 -04003421 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3422 if (DEBUG_WIDGETS) {
3423 Log.d(TAG, "bindAppWidget: " + item);
3424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 final Workspace workspace = mWorkspace;
3426
3427 final int appWidgetId = item.appWidgetId;
3428 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003429 if (DEBUG_WIDGETS) {
3430 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3431 }
3432
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3434
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3436 item.hostView.setTag(item);
3437
3438 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3439 item.cellY, item.spanX, item.spanY, false);
3440
Adam Cohended9f8d2010-11-03 13:25:16 -07003441 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3442
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 workspace.requestLayout();
3444
3445 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003446
3447 if (DEBUG_WIDGETS) {
3448 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3449 + (SystemClock.uptimeMillis()-start) + "ms");
3450 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451 }
3452
3453 /**
3454 * Callback saying that there aren't any more items to bind.
3455 *
3456 * Implementation of the method from LauncherModel.Callbacks.
3457 */
3458 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003459 setLoadOnResume();
3460
Joe Onorato9c1289c2009-08-17 11:03:03 -04003461 if (mSavedState != null) {
3462 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003463 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 }
3465
3466 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3467 if (userFolders != null) {
3468 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003469 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 if (info != null) {
3471 openFolder(info);
3472 }
3473 }
3474 final Folder openFolder = mWorkspace.getOpenFolder();
3475 if (openFolder != null) {
3476 openFolder.requestFocus();
3477 }
3478 }
3479
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 mSavedState = null;
3481 }
3482
3483 if (mSavedInstanceState != null) {
3484 super.onRestoreInstanceState(mSavedInstanceState);
3485 mSavedInstanceState = null;
3486 }
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 mWorkspaceLoading = false;
3489 }
3490
3491 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003492 * Updates the icons on the launcher that are affected by changes to the package list
3493 * on the device.
3494 */
3495 private void updateIconsAffectedByPackageManagerChanges() {
3496 updateAppMarketIcon();
3497 updateGlobalSearchIcon();
3498 updateVoiceSearchIcon();
3499 }
3500
3501 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 * Add the icons for all apps.
3503 *
3504 * Implementation of the method from LauncherModel.Callbacks.
3505 */
3506 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003507 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003508 if (mCustomizePagedView != null) {
3509 mCustomizePagedView.setApps(apps);
3510 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003511 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 }
3513
3514 /**
3515 * A package was installed.
3516 *
3517 * Implementation of the method from LauncherModel.Callbacks.
3518 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003519 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003520 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003522 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003523 if (mCustomizePagedView != null) {
3524 mCustomizePagedView.addApps(apps);
3525 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003526 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
3528
3529 /**
3530 * A package was updated.
3531 *
3532 * Implementation of the method from LauncherModel.Callbacks.
3533 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003534 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003535 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003537 mWorkspace.updateShortcuts(apps);
3538 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003539 if (mCustomizePagedView != null) {
3540 mCustomizePagedView.updateApps(apps);
3541 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003542 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 }
3544
3545 /**
3546 * A package was uninstalled.
3547 *
3548 * Implementation of the method from LauncherModel.Callbacks.
3549 */
Joe Onorato36115782010-06-17 13:28:48 -04003550 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003552 if (permanent) {
3553 mWorkspace.removeItems(apps);
3554 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003555 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003556 if (mCustomizePagedView != null) {
3557 mCustomizePagedView.removeApps(apps);
3558 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003559 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 }
Joe Onoratobe386092009-11-17 17:32:16 -08003561
3562 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003563 * A number of packages were updated.
3564 */
3565 public void bindPackagesUpdated() {
3566 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003567 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003568 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003569 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003570 }
3571
3572 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003573 * Prints out out state for debugging.
3574 */
3575 public void dumpState() {
3576 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003577 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003578 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3579 Log.d(TAG, "mRestoring=" + mRestoring);
3580 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3581 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3582 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003583 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003584 mModel.dumpState();
3585 mAllAppsGrid.dumpState();
3586 Log.d(TAG, "END launcher2 dump state");
3587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003588}