blob: 6f407488253f2822469560a135d0a7b568fff474 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.CustomizePagedView.CustomizationType;
23import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070029import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080031import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080048import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080049import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080052import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040056import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080059import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080061import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040063import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070064import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020066import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080067import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070068import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080074import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080087import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Adam Cohen860f4c52011-01-27 20:16:13 -080091import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800101import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800104import android.widget.TabHost.OnTabChangeListener;
105import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700106
Michael Jurka742574b2011-02-02 23:51:01 -0800107import java.io.DataInputStream;
108import java.io.DataOutputStream;
109import java.io.FileNotFoundException;
110import java.io.IOException;
111import java.util.ArrayList;
112import java.util.HashMap;
113import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800114
Adam Cohended9f8d2010-11-03 13:25:16 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
121 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400127 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800130 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700133 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
134 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
136 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700137 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 private static final int REQUEST_CREATE_SHORTCUT = 1;
140 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final int REQUEST_PICK_APPLICATION = 6;
143 private static final int REQUEST_PICK_SHORTCUT = 7;
144 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
149
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800150 static final int SCREEN_COUNT = 5;
151 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Joe Onorato7c312c12009-08-13 21:36:53 -0700153 static final int DIALOG_CREATE_SHORTCUT = 1;
154 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Romain Guy98d01652009-06-30 16:21:04 -0700156 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
158 // Type: int
159 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700160 // Type: int
161 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: long
163 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: boolean
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
172 // Type: long
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
174
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 // tags for the customization tabs
176 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700177 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 private static final String SHORTCUTS_TAG = "shortcuts";
179 private static final String WALLPAPERS_TAG = "wallpapers";
180
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700181 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
182
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800184 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
185 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700186 private State mState = State.WORKSPACE;
187 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
Michael Jurka0280c3b2010-09-17 15:00:07 -0700206 private int mAddScreen = -1;
207 private int mAddIntersectCellX = -1;
208 private int mAddIntersectCellY = -1;
209 private int[] mAddDropPosition;
210 private int[] mTmpAddItemCellCoordinates = new int[2];
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private FolderInfo mFolderInfo;
213
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700216 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700217 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700219
Winson Chunga12a2502010-12-20 14:41:35 -0800220 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700221 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private Bundle mSavedState;
224
225 private SpannableStringBuilder mDefaultKeySsb = null;
226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 private boolean mWorkspaceLoading = true;
228
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800229 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private boolean mRestoring;
231 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700232 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
234 private Bundle mSavedInstanceState;
235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800237 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800238 private boolean mUserPresent = true;
239 private boolean mVisible = false;
240 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700242 private static LocaleConfiguration sLocaleConfiguration = null;
243
Romain Guy84f296c2009-11-04 15:00:44 -0800244 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700245
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700246 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700247
Romain Guy1fbc1c82009-11-09 20:43:08 -0800248 private ImageView mPreviousView;
249 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500250
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400251 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400252 private String[] mHotseatConfig = null;
253 private Intent[] mHotseats = null;
254 private Drawable[] mHotseatIcons = null;
255 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400256
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700257 private Intent mAppMarketIntent = null;
258
Adam Cohended9f8d2010-11-03 13:25:16 -0700259 // Related to the auto-advancing of widgets
260 private final int ADVANCE_MSG = 1;
261 private final int mAdvanceInterval = 20000;
262 private final int mAdvanceStagger = 250;
263 private long mAutoAdvanceSentTime;
264 private long mAutoAdvanceTimeLeft = -1;
265 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
266 new HashMap<View, AppWidgetProviderInfo>();
267
Winson Chung400438b2011-01-16 17:53:48 -0800268 // Determines how long to wait after a rotation before restoring the screen orientation to
269 // match the sensor state.
270 private final int mRestoreScreenOrientationDelay = 500;
271
Michael Jurka4ef207b2010-11-29 17:05:45 -0800272 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800273 private static Drawable.ConstantState sGlobalSearchIcon;
274 private static Drawable.ConstantState sVoiceSearchIcon;
275 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800276
Winson Chunga12a2502010-12-20 14:41:35 -0800277 private CustomizationType getCustomizeFilterForTabTag(String tag) {
278 if (tag.equals(WIDGETS_TAG)) {
279 return CustomizationType.WidgetCustomization;
280 } else if (tag.equals(APPLICATIONS_TAG)) {
281 return CustomizationType.ApplicationCustomization;
282 } else if (tag.equals(WALLPAPERS_TAG)) {
283 return CustomizePagedView.CustomizationType.WallpaperCustomization;
284 } else if (tag.equals(SHORTCUTS_TAG)) {
285 return CustomizePagedView.CustomizationType.ShortcutCustomization;
286 }
287 return CustomizationType.WidgetCustomization;
288 }
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 @Override
291 protected void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700293
Winson Chungaafa03c2010-06-11 17:34:16 -0700294 if (LauncherApplication.isInPlaceRotationEnabled()) {
295 // hide the status bar (temporary until we get the status bar design figured out)
296 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
297 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
298 }
299
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 LauncherApplication app = ((LauncherApplication)getApplication());
301 mModel = app.setLauncher(this);
302 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400303 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700306 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700307 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
308 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200311 android.os.Debug.startMethodTracing(
312 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 }
314
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400315 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800318 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700319 if (mHomeCustomizationDrawer != null) {
320 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700321
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700323 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
324 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 TabContentFactory contentFactory = new TabContentFactory() {
326 public View createTabContent(String tag) {
327 return mCustomizePagedView;
328 }
329 };
330
Winson Chung49767ae2010-11-29 14:48:30 -0800331
332 TextView tabView;
333 TabWidget tabWidget = (TabWidget)
334 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
335
336 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
337 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700338 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_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.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700342 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_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.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800347 .setIndicator(tabView).setContent(contentFactory));
348 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
349 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700350 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800351 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700352 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
353 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800354 final CustomizePagedView.CustomizationType newType =
355 getCustomizeFilterForTabTag(tabId);
356 if (newType != mCustomizePagedView.getCustomizationFilter()) {
357 // animate the changing of the tab content by fading pages in and out
358 final Resources res = getResources();
359 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
360 final float alpha = mCustomizePagedView.getAlpha();
361 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
362 "alpha", alpha, 0.0f);
363 alphaAnim.setDuration(duration);
364 alphaAnim.addListener(new AnimatorListenerAdapter() {
365 @Override
366 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800367 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700368
Winson Chunga12a2502010-12-20 14:41:35 -0800369 final float alpha = mCustomizePagedView.getAlpha();
370 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
371 mCustomizePagedView, "alpha", alpha, 1.0f);
372 alphaAnim.setDuration(duration);
373 alphaAnim.start();
374 }
375 });
376 alphaAnim.start();
377 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700378 }
379 });
Michael Jurka946ad472010-07-09 18:05:18 -0700380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setupViews();
382
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800383 registerContentObservers();
384
Joe Onorato7c312c12009-08-13 21:36:53 -0700385 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 mSavedState = savedInstanceState;
388 restoreState(mSavedState);
389
Winson Chunga12a2502010-12-20 14:41:35 -0800390 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800391 if (mCustomizePagedView != null) {
392 mCustomizePagedView.update();
393 }
Winson Chunga12a2502010-12-20 14:41:35 -0800394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 if (PROFILE_STARTUP) {
396 android.os.Debug.stopMethodTracing();
397 }
398
399 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400400 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 // For handling default keys
404 mDefaultKeySsb = new SpannableStringBuilder();
405 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800406
407 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
408 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800409
410 // If we have a saved version of these external icons, we load them up immediately
411 if (LauncherApplication.isScreenXLarge()) {
412 if (sGlobalSearchIcon != null) {
413 updateGlobalSearchIcon(sGlobalSearchIcon);
414 }
415 if (sVoiceSearchIcon != null) {
416 updateVoiceSearchIcon(sVoiceSearchIcon);
417 }
418 if (sAppMarketIcon != null) {
419 updateAppMarketIcon(sAppMarketIcon);
420 }
421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 }
Romain Guycbb89e42009-06-08 15:52:54 -0700423
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800424 @Override
425 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
426 super.dispatchPopulateAccessibilityEvent(event);
427
428 // we want to take over text population so it is context dependent
429 event.getText().clear();
430 if (mState == State.ALL_APPS) {
431 event.getText().add(getString(R.string.all_apps_button_label));
432 } else if (mState == State.WORKSPACE) {
433 event.getText().add(getString(R.string.all_apps_home_button_label));
434 }
435
436 return true;
437 }
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700440 if (sLocaleConfiguration == null) {
441 new AsyncTask<Void, Void, LocaleConfiguration>() {
442 @Override
443 protected LocaleConfiguration doInBackground(Void... unused) {
444 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
445 readConfiguration(Launcher.this, localeConfiguration);
446 return localeConfiguration;
447 }
448
449 @Override
450 protected void onPostExecute(LocaleConfiguration result) {
451 sLocaleConfiguration = result;
452 checkForLocaleChange(); // recursive, but now with a locale configuration
453 }
454 }.execute();
455 return;
456 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 final Configuration configuration = getResources().getConfiguration();
459
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 final String locale = configuration.locale.toString();
462
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 final int mcc = configuration.mcc;
465
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700466 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 final int mnc = configuration.mnc;
468
Romain Guy84f296c2009-11-04 15:00:44 -0800469 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470
Romain Guy84f296c2009-11-04 15:00:44 -0800471 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 sLocaleConfiguration.locale = locale;
473 sLocaleConfiguration.mcc = mcc;
474 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700475
Joe Onorato0589f0f2010-02-08 13:44:00 -0800476 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400477 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478
479 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
480 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700481 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 public void run() {
483 writeConfiguration(Launcher.this, localeConfiguration);
484 }
485 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700486 }
487 }
488
489 private static class LocaleConfiguration {
490 public String locale;
491 public int mcc = -1;
492 public int mnc = -1;
493 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700494
Romain Guy98d01652009-06-30 16:21:04 -0700495 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
496 DataInputStream in = null;
497 try {
498 in = new DataInputStream(context.openFileInput(PREFERENCES));
499 configuration.locale = in.readUTF();
500 configuration.mcc = in.readInt();
501 configuration.mnc = in.readInt();
502 } catch (FileNotFoundException e) {
503 // Ignore
504 } catch (IOException e) {
505 // Ignore
506 } finally {
507 if (in != null) {
508 try {
509 in.close();
510 } catch (IOException e) {
511 // Ignore
512 }
513 }
514 }
515 }
516
517 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
518 DataOutputStream out = null;
519 try {
520 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
521 out.writeUTF(configuration.locale);
522 out.writeInt(configuration.mcc);
523 out.writeInt(configuration.mnc);
524 out.flush();
525 } catch (FileNotFoundException e) {
526 // Ignore
527 } catch (IOException e) {
528 //noinspection ResultOfMethodCallIgnored
529 context.getFileStreamPath(PREFERENCES).delete();
530 } finally {
531 if (out != null) {
532 try {
533 out.close();
534 } catch (IOException e) {
535 // Ignore
536 }
537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 }
539 }
540
541 static int getScreen() {
542 synchronized (sLock) {
543 return sScreen;
544 }
545 }
546
547 static void setScreen(int screen) {
548 synchronized (sLock) {
549 sScreen = screen;
550 }
551 }
552
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400553 // Note: This doesn't do all the client-id magic that BrowserProvider does
554 // in Browser. (http://b/2425179)
555 private Uri getDefaultBrowserUri() {
556 String url = getString(R.string.default_browser_url);
557 if (url.indexOf("{CID}") != -1) {
558 url = url.replace("{CID}", "android-google");
559 }
560 return Uri.parse(url);
561 }
562
563 // Load the Intent templates from arrays.xml to populate the hotseats. For
564 // each Intent, if it resolves to a single app, use that as the launch
565 // intent & use that app's label as the contentDescription. Otherwise,
566 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400567 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 if (mHotseatConfig == null) {
569 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
570 if (mHotseatConfig.length > 0) {
571 mHotseats = new Intent[mHotseatConfig.length];
572 mHotseatLabels = new CharSequence[mHotseatConfig.length];
573 mHotseatIcons = new Drawable[mHotseatConfig.length];
574 } else {
575 mHotseats = null;
576 mHotseatIcons = null;
577 mHotseatLabels = null;
578 }
579
580 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
581 for (int i=0; i<mHotseatConfig.length; i++) {
582 // load icon for this slot; currently unrelated to the actual activity
583 try {
584 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
585 } catch (ArrayIndexOutOfBoundsException ex) {
586 Log.w(TAG, "Missing hotseat_icons array item #" + i);
587 mHotseatIcons[i] = null;
588 }
589 }
590 hotseatIconDrawables.recycle();
591 }
592
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400593 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400594 for (int i=0; i<mHotseatConfig.length; i++) {
595 Intent intent = null;
596 if (mHotseatConfig[i].equals("*BROWSER*")) {
597 // magic value meaning "launch user's default web browser"
598 // replace it with a generic web request so we can see if there is indeed a default
599 String defaultUri = getString(R.string.default_browser_url);
600 intent = new Intent(
601 Intent.ACTION_VIEW,
602 ((defaultUri != null)
603 ? Uri.parse(defaultUri)
604 : getDefaultBrowserUri())
605 ).addCategory(Intent.CATEGORY_BROWSABLE);
606 // note: if the user launches this without a default set, she
607 // will always be taken to the default URL above; this is
608 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700609 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400610 // URL is unavoidably sent to the chosen app.
611 } else {
612 try {
613 intent = Intent.parseUri(mHotseatConfig[i], 0);
614 } catch (java.net.URISyntaxException ex) {
615 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
616 // bogus; leave intent=null
617 }
618 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700619
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400620 if (intent == null) {
621 mHotseats[i] = null;
622 mHotseatLabels[i] = getText(R.string.activity_not_found);
623 continue;
624 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625
626 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 Log.d(TAG, "loadHotseats: hotseat " + i
628 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400629 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400630 + "]");
631 }
632
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400633 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
634 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400636 Log.d(TAG, "Best match for intent: " + bestMatch);
637 Log.d(TAG, "All matches: ");
638 for (ResolveInfo ri : allMatches) {
639 Log.d(TAG, " --> " + ri);
640 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400641 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 // did this resolve to a single app, or the resolver?
643 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700644 // can't find any activity to handle this. let's leave the
645 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400646 // to launch.
647 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400648
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400649 // set accessibility text to "Not installed"
650 mHotseatLabels[i] = getText(R.string.activity_not_found);
651 } else {
652 boolean found = false;
653 for (ResolveInfo ri : allMatches) {
654 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
655 && bestMatch.activityInfo.applicationInfo.packageName
656 .equals(ri.activityInfo.applicationInfo.packageName)) {
657 found = true;
658 break;
659 }
660 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700661
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400662 if (!found) {
663 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
664 // the bestMatch is probably the ResolveActivity, meaning the
665 // user has not yet selected a default
666 // so: we'll keep the original intent for now
667 mHotseats[i] = intent;
668
669 // set the accessibility text to "Select shortcut"
670 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
671 } else {
672 // we have an app!
673 // now reconstruct the intent to launch it through the front
674 // door
675 ComponentName com = new ComponentName(
676 bestMatch.activityInfo.applicationInfo.packageName,
677 bestMatch.activityInfo.name);
678 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
679
680 // load the app label for accessibility
681 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
682 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400683 }
684
685 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700686 Log.d(TAG, "loadHotseats: hotseat " + i
687 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400688 + ((mHotseats[i] == null)
689 ? "null"
690 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400691 + "] label=[" + mHotseatLabels[i]
692 + "]"
693 );
694 }
695 }
696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 @Override
699 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700700 mWaitingForResult = false;
701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 // The pattern used here is that a user PICKs a specific application,
703 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700704
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
706 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700707
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 switch (requestCode) {
710 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700711 completeAddApplication(
712 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 break;
714 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800715 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 break;
717 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700718 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 break;
720 case REQUEST_PICK_LIVE_FOLDER:
721 addLiveFolder(data);
722 break;
723 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700724 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700730 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700731 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700733 case REQUEST_PICK_WALLPAPER:
734 // We just wanted the activity result here so we can clear mWaitingForResult
735 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
Romain Guy18042c82009-11-06 11:44:55 -0800737 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
738 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
739 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700740 // Clean up the appWidgetId if we canceled
741 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
742 if (appWidgetId != -1) {
743 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
747
748 @Override
749 protected void onResume() {
750 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800751 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700752 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 mWorkspaceLoading = true;
754 mModel.startLoader(this, true);
755 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700756 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700758 // When we resume Launcher, a different Activity might be responsible for the app
759 // market intent, so refresh the icon
760 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762
763 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700764 protected void onPause() {
765 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 // Some launcher layouts don't have a previous and next view
767 if (mPreviousView != null) {
768 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700769 }
770 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 dismissPreview(mNextView);
772 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700773 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800774 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 }
Romain Guycbb89e42009-06-08 15:52:54 -0700776
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700777 @Override
778 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 // Flag the loader to stop early before switching
780 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800781 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800782 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700783 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700784
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800785 // We can't hide the IME if it was forced open. So don't bother
786 /*
787 @Override
788 public void onWindowFocusChanged(boolean hasFocus) {
789 super.onWindowFocusChanged(hasFocus);
790
791 if (hasFocus) {
792 final InputMethodManager inputManager = (InputMethodManager)
793 getSystemService(Context.INPUT_METHOD_SERVICE);
794 WindowManager.LayoutParams lp = getWindow().getAttributes();
795 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
796 android.os.Handler()) {
797 protected void onReceiveResult(int resultCode, Bundle resultData) {
798 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
799 }
800 });
801 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
802 }
803 }
804 */
805
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 private boolean acceptFilter() {
807 final InputMethodManager inputManager = (InputMethodManager)
808 getSystemService(Context.INPUT_METHOD_SERVICE);
809 return !inputManager.isFullscreenMode();
810 }
811
812 @Override
813 public boolean onKeyDown(int keyCode, KeyEvent event) {
814 boolean handled = super.onKeyDown(keyCode, event);
815 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
816 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
817 keyCode, event);
818 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700819 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700820 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700821 // showSearchDialog()
822 // If there are multiple keystrokes before the search dialog takes focus,
823 // onSearchRequested() will be called for every keystroke,
824 // but it is idempotent, so it's fine.
825 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827 }
828
Joe Onorato8a9625e2010-01-28 15:55:35 -0800829 // Eat the long press event so the keyboard doesn't come up.
830 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
831 return true;
832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 return handled;
835 }
836
Karl Rosaen138a0412009-04-23 19:00:21 -0700837 private String getTypedText() {
838 return mDefaultKeySsb.toString();
839 }
840
841 private void clearTypedText() {
842 mDefaultKeySsb.clear();
843 mDefaultKeySsb.clearSpans();
844 Selection.setSelection(mDefaultKeySsb, 0);
845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700848 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
849 * State
850 */
851 private static State intToState(int stateOrdinal) {
852 State state = State.WORKSPACE;
853 final State[] stateValues = State.values();
854 for (int i = 0; i < stateValues.length; i++) {
855 if (stateValues[i].ordinal() == stateOrdinal) {
856 state = stateValues[i];
857 break;
858 }
859 }
860 return state;
861 }
862
863 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 * Restores the previous state, if it exists.
865 *
866 * @param savedState The previous state.
867 */
868 private void restoreState(Bundle savedState) {
869 if (savedState == null) {
870 return;
871 }
872
Michael Jurka883f55b2010-10-21 15:47:14 -0700873 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
874
875 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800876 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700877 } else if (state == State.CUSTOMIZE) {
878 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800879 }
880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
882 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700883 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885
886 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889 mAddScreen = addScreen;
890 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
891 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 mRestoring = true;
893 }
894
895 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
896 if (renameFolder) {
897 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700898 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 mRestoring = true;
900 }
Winson Chunga12a2502010-12-20 14:41:35 -0800901
902 // Restore the current AllApps drawer tab
903 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
904 String curTab = savedState.getString("allapps_currentTab");
905 if (curTab != null) {
906 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
907 tabhost.setCurrentTabByTag(curTab);
908 }
909 int curPage = savedState.getInt("allapps_currentPage", -1);
910 if (curPage > -1) {
911 mAllAppsPagedView.setRestorePage(curPage);
912 }
913 }
914
915 // Restore the current customization drawer tab
916 if (mHomeCustomizationDrawer != null) {
917 String curTab = savedState.getString("customize_currentTab");
918 if (curTab != null) {
919 // We set this directly so that there is no delay before the tab is set
920 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
921 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
922 }
923
924 // Note: currently we do not restore the page for the customization tray because unlike
925 // AllApps, the page content can change drastically
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928
929 /**
930 * Finds all the views we need and configure them properly.
931 */
932 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700933 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400934
Romain Guyb1b69f52009-08-10 15:10:15 -0700935 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400936 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937
Joe Onorato7c312c12009-08-13 21:36:53 -0700938 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700939 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700940 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800941 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700942 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700943 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700944
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700945 if (LauncherApplication.isScreenXLarge()) {
946 // They need to be INVISIBLE initially so that they will be measured in the layout.
947 // Otherwise the animations are messed up when we show them for the first time.
948 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
949 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
950 }
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500955 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700956
Joe Onorato7c312c12009-08-13 21:36:53 -0700957 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
958 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700960 View handleView = findViewById(R.id.all_apps_button);
961 if (handleView != null && handleView instanceof HandleView) {
962 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700963 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700964 mHandleView.setLauncher(this);
965 mHandleView.setOnClickListener(this);
966 mHandleView.setOnLongClickListener(this);
967 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800968
Winson Chung80baf5a2010-08-09 16:03:15 -0700969 if (mCustomizePagedView != null) {
970 mCustomizePagedView.setLauncher(this);
971 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700972 } else {
973 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
974 hotseatLeft.setContentDescription(mHotseatLabels[0]);
975 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
976 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
977 hotseatRight.setContentDescription(mHotseatLabels[1]);
978 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400979
Michael Jurkaaf442092010-06-10 17:01:57 -0700980 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
981 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800982
Michael Jurkaaf442092010-06-10 17:01:57 -0700983 Drawable previous = mPreviousView.getDrawable();
984 Drawable next = mNextView.getDrawable();
985 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986
Michael Jurkaaf442092010-06-10 17:01:57 -0700987 mPreviousView.setHapticFeedbackEnabled(false);
988 mPreviousView.setOnLongClickListener(this);
989 mNextView.setHapticFeedbackEnabled(false);
990 mNextView.setOnLongClickListener(this);
991 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400994 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800996 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997
998 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400999 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001000
1001 final View allAppsButton = findViewById(R.id.all_apps_button);
1002 final View divider = findViewById(R.id.divider);
1003 final View configureButton = findViewById(R.id.configure_button);
1004
Michael Jurka54f7ac32010-08-02 13:56:46 -07001005 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001006 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001007 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001008 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001009 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001010 dragController.addDragListener(deleteZone);
1011
Adam Cohencdc30d52010-12-01 15:09:47 -08001012 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1013 if (allAppsDeleteZone != null) {
1014 allAppsDeleteZone.setLauncher(this);
1015 allAppsDeleteZone.setDragController(dragController);
1016 allAppsDeleteZone.setDragAndDropEnabled(false);
1017 dragController.addDragListener(allAppsDeleteZone);
1018 dragController.addDropTarget(allAppsDeleteZone);
1019 }
1020
1021 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1022 findViewById(R.id.all_apps_info_target);
1023 if (allAppsInfoTarget != null) {
1024 allAppsInfoTarget.setLauncher(this);
1025 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001026 allAppsInfoTarget.setDragAndDropEnabled(false);
1027 View marketButton = findViewById(R.id.market_button);
1028 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001029 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001030 }
1031 }
1032
Joe Onorato00acb122009-08-04 16:04:30 -04001033 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001034 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001035 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001036
Joe Onorato00acb122009-08-04 16:04:30 -04001037 // The order here is bottom to top.
1038 dragController.addDropTarget(workspace);
1039 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001040 if (allAppsInfoTarget != null) {
1041 dragController.addDropTarget(allAppsInfoTarget);
1042 }
1043 if (allAppsDeleteZone != null) {
1044 dragController.addDropTarget(allAppsDeleteZone);
1045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047
Romain Guy8a73c512009-11-09 19:19:59 -08001048 @SuppressWarnings({"UnusedDeclaration"})
1049 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001050 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001051 mWorkspace.scrollLeft();
1052 }
Romain Guy8a73c512009-11-09 19:19:59 -08001053 }
1054
1055 @SuppressWarnings({"UnusedDeclaration"})
1056 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001057 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001058 mWorkspace.scrollRight();
1059 }
Romain Guy8a73c512009-11-09 19:19:59 -08001060 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001061
1062 @SuppressWarnings({"UnusedDeclaration"})
1063 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001064 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001065
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001066 int index = -1;
1067 if (v.getId() == R.id.hotseat_left) {
1068 index = 0;
1069 } else if (v.getId() == R.id.hotseat_right) {
1070 index = 1;
1071 }
1072
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001073 // reload these every tap; you never know when they might change
1074 loadHotseats();
1075 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001076 startActivitySafely(
1077 mHotseats[index],
1078 "hotseat"
1079 );
1080 }
1081 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 /**
1084 * Creates a view representing a shortcut.
1085 *
1086 * @param info The data structure describing the shortcut.
1087 *
1088 * @return A View inflated from R.layout.application.
1089 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001092 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
1094
1095 /**
1096 * Creates a view representing a shortcut inflated from the specified resource.
1097 *
1098 * @param layoutResId The id of the XML layout used to create the shortcut.
1099 * @param parent The group the shortcut belongs to.
1100 * @param info The data structure describing the shortcut.
1101 *
1102 * @return A View inflated from layoutResId.
1103 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001104 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001105 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1106 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 return favorite;
1109 }
1110
1111 /**
1112 * Add an application shortcut to the workspace.
1113 *
1114 * @param data The intent describing the application.
1115 * @param cellInfo The position on screen where to create the shortcut.
1116 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001117 void completeAddApplication(Context context, Intent data, int screen,
1118 int intersectCellX, int intersectCellY) {
1119 final int[] cellXY = mTmpAddItemCellCoordinates;
1120 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1121
1122 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1123 showOutOfSpaceMessage();
1124 return;
1125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126
Joe Onorato0589f0f2010-02-08 13:44:00 -08001127 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1128 data, context);
1129
Romain Guy73b979d2009-06-09 12:57:21 -07001130 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001131 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1135 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001136 } else {
1137 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 /**
1142 * Add a shortcut to the workspace.
1143 *
1144 * @param data The intent describing the shortcut.
1145 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 private void completeAddShortcut(Intent data, int screen,
1148 int intersectCellX, int intersectCellY) {
1149 final int[] cellXY = mTmpAddItemCellCoordinates;
1150 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001152 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001153 boolean foundCellSpan = false;
1154 if (touchXY != null) {
1155 // when dragging and dropping, just find the closest free spot
1156 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1157 int[] result = screenLayout.findNearestVacantArea(
1158 touchXY[0], touchXY[1], 1, 1, cellXY);
1159 foundCellSpan = (result != null);
1160 } else {
1161 foundCellSpan = layout.findCellForSpanThatIntersects(
1162 cellXY, 1, 1, intersectCellX, intersectCellY);
1163 }
1164
1165 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 showOutOfSpaceMessage();
1167 return;
1168 }
1169
1170 final ShortcutInfo info = mModel.addShortcut(
1171 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
1173 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001182 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1190 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001193 // We have saved the position to which the widget was dragged-- this really only matters
1194 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001196
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001197 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 boolean foundCellSpan = false;
1199 if (touchXY != null) {
1200 // when dragging and dropping, just find the closest free spot
1201 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1202 int[] result = screenLayout.findNearestVacantArea(
1203 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001204 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001206 // if we long pressed on an empty cell to bring up a menu,
1207 // make sure we intersect the empty cell
1208 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1209 // findCellForSpanThatIntersects will just ignore them
1210 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1211 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001212 }
1213
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001215 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001217 return;
1218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220 // Build Launcher-specific widget info and save to database
1221 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 launcherInfo.spanX = spanXY[0];
1223 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 LauncherModel.addItemToDatabase(this, launcherInfo,
1226 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
1229 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001234
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001237
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001240
1241 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243 }
Romain Guycbb89e42009-06-08 15:52:54 -07001244
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1246 @Override
1247 public void onReceive(Context context, Intent intent) {
1248 final String action = intent.getAction();
1249 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1250 mUserPresent = false;
1251 updateRunning();
1252 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1253 mUserPresent = true;
1254 updateRunning();
1255 }
1256 }
1257 };
1258
1259 @Override
1260 public void onAttachedToWindow() {
1261 super.onAttachedToWindow();
1262
1263 // Listen for broadcasts related to user-presence
1264 final IntentFilter filter = new IntentFilter();
1265 filter.addAction(Intent.ACTION_SCREEN_OFF);
1266 filter.addAction(Intent.ACTION_USER_PRESENT);
1267 registerReceiver(mReceiver, filter);
1268
Adam Cohend113e0c2010-11-11 10:48:05 -08001269 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001270 mVisible = true;
1271 }
1272
1273 @Override
1274 public void onDetachedFromWindow() {
1275 super.onDetachedFromWindow();
1276 mVisible = false;
1277
Adam Cohend113e0c2010-11-11 10:48:05 -08001278 if (mAttached) {
1279 unregisterReceiver(mReceiver);
1280 mAttached = false;
1281 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001282 updateRunning();
1283 }
1284
1285 public void onWindowVisibilityChanged(int visibility) {
1286 mVisible = visibility == View.VISIBLE;
1287 updateRunning();
1288 }
1289
1290 private void sendAdvanceMessage(long delay) {
1291 mHandler.removeMessages(ADVANCE_MSG);
1292 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1293 mHandler.sendMessageDelayed(msg, delay);
1294 mAutoAdvanceSentTime = System.currentTimeMillis();
1295 }
1296
1297 private void updateRunning() {
1298 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1299 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1300 mAutoAdvanceRunning = autoAdvanceRunning;
1301 if (autoAdvanceRunning) {
1302 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1303 sendAdvanceMessage(delay);
1304 } else {
1305 if (!mWidgetsToAdvance.isEmpty()) {
1306 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1307 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1308 }
1309 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001310 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001311 }
1312 }
1313 }
1314
1315 private final Handler mHandler = new Handler() {
1316 @Override
1317 public void handleMessage(Message msg) {
1318 if (msg.what == ADVANCE_MSG) {
1319 int i = 0;
1320 for (View key: mWidgetsToAdvance.keySet()) {
1321 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1322 final int delay = mAdvanceStagger * i;
1323 if (v instanceof Advanceable) {
1324 postDelayed(new Runnable() {
1325 public void run() {
1326 ((Advanceable) v).advance();
1327 }
1328 }, delay);
1329 }
1330 i++;
1331 }
1332 sendAdvanceMessage(mAdvanceInterval);
1333 }
1334 }
1335 };
1336
1337 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1338 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1339 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1340 if (v instanceof Advanceable) {
1341 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001342 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001343 updateRunning();
1344 }
1345 }
1346
1347 void removeWidgetToAutoAdvance(View hostView) {
1348 if (mWidgetsToAdvance.containsKey(hostView)) {
1349 mWidgetsToAdvance.remove(hostView);
1350 updateRunning();
1351 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 }
1353
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1355 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001356 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 launcherInfo.hostView = null;
1358 }
1359
Adam Cohended9f8d2010-11-03 13:25:16 -07001360 void showOutOfSpaceMessage() {
1361 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1362 }
1363
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001364 public LauncherAppWidgetHost getAppWidgetHost() {
1365 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
Romain Guycbb89e42009-06-08 15:52:54 -07001367
Winson Chunga9abd0e2010-10-27 17:18:37 -07001368 public LauncherModel getModel() {
1369 return mModel;
1370 }
1371
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001373 getWindow().closeAllPanels();
1374
1375 try {
1376 dismissDialog(DIALOG_CREATE_SHORTCUT);
1377 // Unlock the workspace if the dialog was showing
1378 } catch (Exception e) {
1379 // An exception is thrown if the dialog is not visible, which is fine
1380 }
1381
1382 try {
1383 dismissDialog(DIALOG_RENAME_FOLDER);
1384 // Unlock the workspace if the dialog was showing
1385 } catch (Exception e) {
1386 // An exception is thrown if the dialog is not visible, which is fine
1387 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001388
1389 // Whatever we were doing is hereby canceled.
1390 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001391 }
1392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 @Override
1394 protected void onNewIntent(Intent intent) {
1395 super.onNewIntent(intent);
1396
1397 // Close the menu
1398 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001399 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001400 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001401
Joe Onorato14f122b2009-11-19 14:06:36 -08001402 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1403 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001404
Michael Jurka4cb37242010-08-09 21:05:32 -07001405 // in all these cases, only animate if we're already on home
1406 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001407 mWorkspace.unshrink(alreadyOnHome);
1408 }
1409 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001410 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001411 boolean animate = alreadyOnHome &&
1412 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1413 mWorkspace.moveToDefaultScreen(animate);
1414 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001415 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001416 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001417
Joe Onorato3a8820b2009-11-10 15:06:42 -08001418 final View v = getWindow().peekDecorView();
1419 if (v != null && v.getWindowToken() != null) {
1420 InputMethodManager imm = (InputMethodManager)getSystemService(
1421 INPUT_METHOD_SERVICE);
1422 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424 }
1425 }
1426
1427 @Override
1428 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1429 // Do not call super here
1430 mSavedInstanceState = savedInstanceState;
1431 }
1432
1433 @Override
1434 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001435 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436
1437 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1438 if (folders.size() > 0) {
1439 final int count = folders.size();
1440 long[] ids = new long[count];
1441 for (int i = 0; i < count; i++) {
1442 final FolderInfo info = folders.get(i).getInfo();
1443 ids[i] = info.id;
1444 }
1445 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1446 } else {
1447 super.onSaveInstanceState(outState);
1448 }
1449
Michael Jurka883f55b2010-10-21 15:47:14 -07001450 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
Michael Jurka0280c3b2010-09-17 15:00:07 -07001452 if (mAddScreen > -1 && mWaitingForResult) {
1453 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1454 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1455 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457
1458 if (mFolderInfo != null && mWaitingForResult) {
1459 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1460 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1461 }
Michael Jurka946ad472010-07-09 18:05:18 -07001462
Winson Chung43b119d2010-12-06 16:45:05 -08001463 // Save the current AllApps drawer tab
1464 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1465 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1466 String currentTabTag = tabhost.getCurrentTabTag();
1467 if (currentTabTag != null) {
1468 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001469 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001470 }
1471 }
1472
1473 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001474 if (mHomeCustomizationDrawer != null) {
1475 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1476 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001477 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001478 }
1479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 @Override
1483 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001487 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001489 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001491 mAppWidgetHost = null;
1492
1493 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
1495 TextKeyListener.getInstance().release();
1496
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001500
1501 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001502
Patrick Dubroyab962b72010-07-26 12:10:10 -07001503 // Some launcher layouts don't have a previous and next view
1504 if (mPreviousView != null) {
1505 dismissPreview(mPreviousView);
1506 }
1507 if (mNextView != null) {
1508 dismissPreview(mNextView);
1509 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001510
1511 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001512
1513 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1514 mWorkspace.removeAllViews();
1515 mWorkspace = null;
1516 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001517
1518 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
1521 @Override
1522 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001523 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 super.startActivityForResult(intent, requestCode);
1525 }
1526
1527 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001528 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001530
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001531 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 if (initialQuery == null) {
1534 // Use any text typed in the launcher as the initial query
1535 initialQuery = getTypedText();
1536 clearTypedText();
1537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 if (appSearchData == null) {
1539 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001540 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
Romain Guycbb89e42009-06-08 15:52:54 -07001542
Karl Rosaen138a0412009-04-23 19:00:21 -07001543 final SearchManager searchManager =
1544 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001545 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001546 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548
1549 @Override
1550 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001551 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 return false;
1553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
1555 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1558 .setIcon(android.R.drawable.ic_menu_add)
1559 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001560 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1561 .setIcon(android.R.drawable.ic_menu_manage)
1562 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001563 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 .setIcon(android.R.drawable.ic_menu_gallery)
1565 .setAlphabeticShortcut('W');
1566 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1567 .setIcon(android.R.drawable.ic_search_category_default)
1568 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1569 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1570 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1571 .setAlphabeticShortcut('N');
1572
1573 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001574 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1575 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576
1577 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1578 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1579 .setIntent(settings);
1580
1581 return true;
1582 }
1583
1584 @Override
1585 public boolean onPrepareOptionsMenu(Menu menu) {
1586 super.onPrepareOptionsMenu(menu);
1587
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001588 // If all apps is animating, don't show the menu, because we don't know
1589 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001590 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001591 return false;
1592 }
1593
1594 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001595 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001596 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1597 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1598
1599 // Disable add if the workspace is full.
1600 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001601 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1602 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604
1605 return true;
1606 }
1607
1608 @Override
1609 public boolean onOptionsItemSelected(MenuItem item) {
1610 switch (item.getItemId()) {
1611 case MENU_ADD:
1612 addItems();
1613 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001614 case MENU_MANAGE_APPS:
1615 manageApps();
1616 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 case MENU_WALLPAPER_SETTINGS:
1618 startWallpaper();
1619 return true;
1620 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001621 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 return true;
1623 case MENU_NOTIFICATIONS:
1624 showNotifications();
1625 return true;
1626 }
1627
1628 return super.onOptionsItemSelected(item);
1629 }
Romain Guycbb89e42009-06-08 15:52:54 -07001630
Karl Rosaen138a0412009-04-23 19:00:21 -07001631 /**
1632 * Indicates that we want global search for this activity by setting the globalSearch
1633 * argument for {@link #startSearch} to true.
1634 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001636 @Override
1637 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001638 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001639 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001640 }
1641
Joe Onorato9c1289c2009-08-17 11:03:03 -04001642 public boolean isWorkspaceLocked() {
1643 return mWorkspaceLoading || mWaitingForResult;
1644 }
1645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001647 if (LauncherApplication.isScreenXLarge()) {
1648 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001649 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001650 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001651 }
1652 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001653 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001654 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
Michael Jurka0280c3b2010-09-17 15:00:07 -07001658 private void resetAddInfo() {
1659 mAddScreen = -1;
1660 mAddIntersectCellX = -1;
1661 mAddIntersectCellY = -1;
1662 mAddDropPosition = null;
1663 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001664
Winson Chung55cef262010-10-28 14:14:18 -07001665 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001666 resetAddInfo();
1667 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001668 mAddDropPosition = position;
1669
Michael Jurkaaf442092010-06-10 17:01:57 -07001670 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001671 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1672 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001673 }
1674
Winson Chung7ad01412010-09-27 11:33:03 -07001675 private void manageApps() {
1676 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1677 }
1678
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001680 // TODO: catch bad widget exception when sent
1681 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001682 // TODO: Is this log message meaningful?
1683 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001684 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001685 }
1686
Winson Chung55cef262010-10-28 14:14:18 -07001687 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001688 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689
Bjorn Bringert7984c942009-12-09 15:38:25 +00001690 if (appWidget.configure != null) {
1691 // Launch over to configure widget, if needed
1692 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1693 intent.setComponent(appWidget.configure);
1694 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001695 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001696 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1697 intent.putExtra(
1698 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1699 info.mimeType);
1700
1701 final String mimeType = info.mimeType;
1702 final ClipData clipData = (ClipData) info.configurationData;
1703 final ClipDescription clipDesc = clipData.getDescription();
1704 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1705 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001706 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001707 final CharSequence stringData = item.getText();
1708 final Uri uriData = item.getUri();
1709 final Intent intentData = item.getIntent();
1710 final String key =
1711 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1712 if (uriData != null) {
1713 intent.putExtra(key, uriData);
1714 } else if (intentData != null) {
1715 intent.putExtra(key, intentData);
1716 } else if (stringData != null) {
1717 intent.putExtra(key, stringData);
1718 }
1719 break;
1720 }
1721 }
1722 }
Winson Chung55cef262010-10-28 14:14:18 -07001723 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001724
Romain Guy8e633c52010-03-04 12:51:36 -08001725 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001727 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001728 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730 }
Romain Guycbb89e42009-06-08 15:52:54 -07001731
Michael Jurka0280c3b2010-09-17 15:00:07 -07001732 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1733 resetAddInfo();
1734 mAddScreen = screen;
1735 mAddDropPosition = position;
1736
1737 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1738 createShortcutIntent.setComponent(componentName);
1739 processShortcut(createShortcutIntent);
1740 }
1741
Joe Onoratodeb98af2010-02-19 14:59:39 -08001742 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001743 // Handle case where user selected "Applications"
1744 String applicationName = getResources().getString(R.string.group_applications);
1745 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001746
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001747 if (applicationName != null && applicationName.equals(shortcutName)) {
1748 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1749 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001751 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1752 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001753 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001754 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001755 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001756 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
1759
Winson Chung24ab2f12010-09-16 14:10:47 -07001760 void processWallpaper(Intent intent) {
1761 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1762 }
1763
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1765 resetAddInfo();
1766 mAddScreen = screen;
1767 mAddDropPosition = position;
1768
1769 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1770 createFolderIntent.setComponent(componentName);
1771
1772 addLiveFolder(createFolderIntent);
1773 }
1774
1775 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001776 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001777 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001778 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001779
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001780 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001781 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001782 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001783 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
1786
Michael Jurka0280c3b2010-09-17 15:00:07 -07001787 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 UserFolderInfo folderInfo = new UserFolderInfo();
1789 folderInfo.title = getText(R.string.folder_name);
1790
Michael Jurka0280c3b2010-09-17 15:00:07 -07001791 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1792 final int[] cellXY = mTmpAddItemCellCoordinates;
1793 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1794 showOutOfSpaceMessage();
1795 return;
1796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797
1798 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001799 LauncherModel.addItemToDatabase(this, folderInfo,
1800 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001801 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001802 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803
1804 // Create the view
1805 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001806 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1807 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001808 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001812 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 }
1814
Michael Jurka28750fb2010-09-24 17:43:49 -07001815 private void completeAddLiveFolder(
1816 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1818 final int[] cellXY = mTmpAddItemCellCoordinates;
1819 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1820 showOutOfSpaceMessage();
1821 return;
1822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823
Michael Jurka0280c3b2010-09-17 15:00:07 -07001824 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825
1826 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001828 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 }
1831 }
1832
1833 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835
1836 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1837 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1838
1839 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 Intent.ShortcutIconResource iconResource = null;
1841
1842 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1843 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1844 try {
1845 iconResource = (Intent.ShortcutIconResource) extra;
1846 final PackageManager packageManager = context.getPackageManager();
1847 Resources resources = packageManager.getResourcesForApplication(
1848 iconResource.packageName);
1849 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1850 icon = resources.getDrawable(id);
1851 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001852 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
1854 }
1855
1856 if (icon == null) {
1857 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1858 }
1859
1860 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001861 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 info.title = name;
1863 info.iconResource = iconResource;
1864 info.uri = data.getData();
1865 info.baseIntent = baseIntent;
1866 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1867 LiveFolders.DISPLAY_MODE_GRID);
1868
1869 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001870 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001871 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872
1873 return info;
1874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 private void showNotifications() {
1877 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1878 if (statusBar != null) {
1879 statusBar.expand();
1880 }
1881 }
1882
1883 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001884 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001886 Intent chooser = Intent.createChooser(pickWallpaper,
1887 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001888 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1889 // Removed in Eclair MR1
1890// WallpaperManager wm = (WallpaperManager)
1891// getSystemService(Context.WALLPAPER_SERVICE);
1892// WallpaperInfo wi = wm.getWallpaperInfo();
1893// if (wi != null && wi.getSettingsActivity() != null) {
1894// LabeledIntent li = new LabeledIntent(getPackageName(),
1895// R.string.configure_wallpaper, 0);
1896// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1897// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1898// }
Mike Clerona0618e42009-10-22 13:55:21 -07001899 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 }
1901
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001902 /**
1903 * Registers various content observers. The current implementation registers
1904 * only a favorites observer to keep track of the favorites applications.
1905 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001906 private void registerContentObservers() {
1907 ContentResolver resolver = getContentResolver();
1908 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1909 true, mWidgetObserver);
1910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 @Override
1913 public boolean dispatchKeyEvent(KeyEvent event) {
1914 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1915 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001917 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001918 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001919 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001920 dumpState();
1921 return true;
1922 }
1923 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001924 }
1925 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1926 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001927 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 return true;
1929 }
1930 }
1931
1932 return super.dispatchKeyEvent(event);
1933 }
1934
Joe Onorato88ec0992009-11-19 13:16:06 -08001935 @Override
1936 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001937 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1938 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001939 } else {
1940 closeFolder();
1941 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001942 // Some launcher layouts don't have a previous and next view
1943 if (mPreviousView != null) {
1944 dismissPreview(mPreviousView);
1945 dismissPreview(mNextView);
1946 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001947 }
1948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 private void closeFolder() {
1950 Folder folder = mWorkspace.getOpenFolder();
1951 if (folder != null) {
1952 closeFolder(folder);
1953 }
1954 }
1955
1956 void closeFolder(Folder folder) {
1957 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001958 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001960 CellLayout cl = (CellLayout) parent;
1961 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001962 if (folder instanceof DropTarget) {
1963 // Live folders aren't DropTargets.
1964 mDragController.removeDropTarget((DropTarget)folder);
1965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
1967 folder.onClose();
1968 }
1969
1970 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001971 * Re-listen when widgets are reset.
1972 */
1973 private void onAppWidgetReset() {
1974 mAppWidgetHost.startListening();
1975 }
1976
1977 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1979 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001981 private void unbindDesktopItems() {
1982 for (ItemInfo item: mDesktopItems) {
1983 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001985 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 /**
1989 * Launches the intent referred by the clicked shortcut.
1990 *
1991 * @param v The view representing the clicked shortcut.
1992 */
1993 public void onClick(View v) {
1994 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001995 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001997 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001998 int[] pos = new int[2];
1999 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002000 intent.setSourceBounds(new Rect(pos[0], pos[1],
2001 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002002 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 } else if (tag instanceof FolderInfo) {
2004 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002005 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002006 if (mState == State.ALL_APPS) {
2007 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002008 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002009 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012 }
2013
Michael Jurka0e260592010-06-30 17:07:39 -07002014 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002015 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002016 // clicking anywhere on the workspace causes the customization drawer to slide down
2017 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002018 return false;
2019 }
2020
Michael Jurkaaf442092010-06-10 17:01:57 -07002021 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002022 * Event handler for the search button
2023 *
2024 * @param v The view that was clicked.
2025 */
2026 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002027 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002028 // Use a custom animation for launching search
2029 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002030 }
2031
2032 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002033 * Event handler for the voice button
2034 *
2035 * @param v The view that was clicked.
2036 */
2037 public void onClickVoiceButton(View v) {
2038 startVoiceSearch();
2039 }
2040
2041 private void startVoiceSearch() {
2042 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2043 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2044 startActivity(intent);
2045 }
2046
2047 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002048 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002049 * enters home screen customization mode.
2050 *
2051 * @param v The view that was clicked.
2052 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002053 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002054 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002055 }
2056
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002057 /**
2058 * Event handler for the "grid" button that appears on the home screen, which
2059 * enters all apps mode.
2060 *
2061 * @param v The view that was clicked.
2062 */
2063 public void onClickAllAppsButton(View v) {
2064 showAllApps(true);
2065 }
2066
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002067 public void onClickAppMarketButton(View v) {
2068 if (mAppMarketIntent != null) {
2069 startActivitySafely(mAppMarketIntent, "app market");
2070 }
2071 }
2072
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002073 void startApplicationDetailsActivity(ComponentName componentName) {
2074 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002075 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2076 Uri.fromParts("package", packageName, null));
2077 startActivity(intent);
2078 }
2079
Patrick Dubroy5539af72010-09-07 15:22:01 -07002080 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2081 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2082 // System applications cannot be installed. For now, show a toast explaining that.
2083 // We may give them the option of disabling apps this way.
2084 int messageId = R.string.uninstall_system_app_text;
2085 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2086 } else {
2087 String packageName = appInfo.componentName.getPackageName();
2088 String className = appInfo.componentName.getClassName();
2089 Intent intent = new Intent(
2090 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2091 startActivity(intent);
2092 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002093 }
2094
Joe Onoratof984e852010-03-25 09:47:45 -07002095 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2097 try {
2098 startActivity(intent);
2099 } catch (ActivityNotFoundException e) {
2100 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002101 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 } catch (SecurityException e) {
2103 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002104 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002106 "or use the exported attribute for this activity. "
2107 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002110
Romain Guy8e633c52010-03-04 12:51:36 -08002111 void startActivityForResultSafely(Intent intent, int requestCode) {
2112 try {
2113 startActivityForResult(intent, requestCode);
2114 } catch (ActivityNotFoundException e) {
2115 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2116 } catch (SecurityException e) {
2117 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2118 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2119 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2120 "or use the exported attribute for this activity.", e);
2121 }
2122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123
2124 private void handleFolderClick(FolderInfo folderInfo) {
2125 if (!folderInfo.opened) {
2126 // Close any open folder
2127 closeFolder();
2128 // Open the requested folder
2129 openFolder(folderInfo);
2130 } else {
2131 // Find the open folder...
2132 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2133 int folderScreen;
2134 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002135 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 // .. and close it
2137 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002138 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 // Close any folder open on the current screen
2140 closeFolder();
2141 // Pull the folder onto this screen
2142 openFolder(folderInfo);
2143 }
2144 }
2145 }
2146 }
2147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002149 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 * is animated relative to the specified View. If the View is null, no animation
2151 * is played.
2152 *
2153 * @param folderInfo The FolderInfo describing the folder to open.
2154 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002155 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 Folder openFolder;
2157
2158 if (folderInfo instanceof UserFolderInfo) {
2159 openFolder = UserFolder.fromXml(this);
2160 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002161 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 } else {
2163 return;
2164 }
2165
Joe Onorato00acb122009-08-04 16:04:30 -04002166 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 openFolder.setLauncher(this);
2168
2169 openFolder.bind(folderInfo);
2170 folderInfo.opened = true;
2171
Winson Chungaafa03c2010-06-11 17:34:16 -07002172 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 openFolder.onOpen();
2175 }
2176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002178 switch (v.getId()) {
2179 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002180 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002181 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2182 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002183 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002184 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002185 return true;
2186 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002187 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002188 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2189 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002190 showPreviews(v);
2191 }
2192 return true;
2193 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002194 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002195 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2196 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2197 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002198 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002199 return true;
2200 }
2201
Joe Onorato9c1289c2009-08-17 11:03:03 -04002202 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 return false;
2204 }
2205
2206 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002207 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 }
2209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210
Michael Jurka0280c3b2010-09-17 15:00:07 -07002211 resetAddInfo();
2212 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002214 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 return true;
2216 }
2217
Michael Jurka0280c3b2010-09-17 15:00:07 -07002218 final View itemUnderLongClick = longClickCellInfo.cell;
2219
Winson Chung304dcde2011-01-07 11:17:23 -08002220 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002221 if (itemUnderLongClick == null) {
2222 // User long pressed on empty space
2223 mWorkspace.setAllowLongPress(false);
2224 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2225 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002226 if (LauncherApplication.isScreenXLarge()) {
2227 addItems();
2228 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002229 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 }
2231 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002232 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002234 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2235 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002236 mAddIntersectCellX = longClickCellInfo.cellX;
2237 mAddIntersectCellY = longClickCellInfo.cellY;
2238 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 }
2240 }
2241 }
2242 return true;
2243 }
2244
Romain Guye6b8e2f2009-11-10 11:56:55 -08002245 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002246 private void dismissPreview(final View v) {
2247 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002248 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002249 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2250 public void onDismiss() {
2251 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2252 int count = group.getChildCount();
2253 for (int i = 0; i < count; i++) {
2254 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2255 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002256 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2257 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2258
2259 v.setTag(R.id.workspace, null);
2260 v.setTag(R.id.icon, null);
2261 window.setOnDismissListener(null);
2262 }
2263 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002264 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002265 }
2266 v.setTag(null);
2267 }
2268
Romain Guyf8e6a802009-12-07 17:48:02 -08002269 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002270 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002271 }
2272
Romain Guya6abce82009-11-10 02:54:41 -08002273 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002274 final Resources resources = getResources();
2275 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002276
Romain Guya6abce82009-11-10 02:54:41 -08002277 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002278
Romain Guy9d31e9f2009-11-11 18:54:28 -08002279 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002280
Romain Guyf8e6a802009-12-07 17:48:02 -08002281 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002282 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002283 int extraW = (int) ((r.left + r.right) * max);
2284 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002285
2286 int aW = cell.getWidth() - extraW;
2287 float w = aW / max;
2288
2289 int width = cell.getWidth();
2290 int height = cell.getHeight();
2291 int x = cell.getLeftPadding();
2292 int y = cell.getTopPadding();
2293 width -= (x + cell.getRightPadding());
2294 height -= (y + cell.getBottomPadding());
2295
2296 float scale = w / width;
2297
2298 int count = end - start;
2299
2300 final float sWidth = width * scale;
2301 float sHeight = height * scale;
2302
Romain Guye6b8e2f2009-11-10 11:56:55 -08002303 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002304
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2306 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002307
2308 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002309 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002310 cell = (CellLayout) workspace.getChildAt(i);
2311
Romain Guyf8e6a802009-12-07 17:48:02 -08002312 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002314
2315 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002316 c.scale(scale, scale);
2317 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002318 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002319
Romain Guy9d31e9f2009-11-11 18:54:28 -08002320 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002321 image.setImageBitmap(bitmap);
2322 image.setTag(i);
2323 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002324 image.setOnFocusChangeListener(handler);
2325 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002326 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002327
2328 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002329 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2330
Winson Chungaafa03c2010-06-11 17:34:16 -07002331 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002332 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002333
2334 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002335 p.setContentView(preview);
2336 p.setWidth((int) (sWidth * count + extraW));
2337 p.setHeight((int) (sHeight + extraH));
2338 p.setAnimationStyle(R.style.AnimationPreview);
2339 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002340 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002341 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002342 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002343
Romain Guye6b8e2f2009-11-10 11:56:55 -08002344 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2345 public void onDismiss() {
2346 dismissPreview(anchor);
2347 }
2348 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002349
2350 anchor.setTag(p);
2351 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002352 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002353 }
2354
Romain Guy9d31e9f2009-11-11 18:54:28 -08002355 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002356 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002357
Romain Guye6b8e2f2009-11-10 11:56:55 -08002358 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002359 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002360 }
2361
2362 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002363 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002364 v.post(this);
2365 }
2366
2367 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002368 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002369 }
2370
2371 public void onFocusChange(View v, boolean hasFocus) {
2372 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002373 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002374 }
Romain Guya6abce82009-11-10 02:54:41 -08002375 }
2376 }
2377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 Workspace getWorkspace() {
2379 return mWorkspace;
2380 }
2381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 @Override
2383 protected Dialog onCreateDialog(int id) {
2384 switch (id) {
2385 case DIALOG_CREATE_SHORTCUT:
2386 return new CreateShortcut().createDialog();
2387 case DIALOG_RENAME_FOLDER:
2388 return new RenameFolder().createDialog();
2389 }
2390
2391 return super.onCreateDialog(id);
2392 }
2393
2394 @Override
2395 protected void onPrepareDialog(int id, Dialog dialog) {
2396 switch (id) {
2397 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 break;
2399 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002400 if (mFolderInfo != null) {
2401 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2402 final CharSequence text = mFolderInfo.title;
2403 input.setText(text);
2404 input.setSelection(0, text.length());
2405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 break;
2407 }
2408 }
2409
2410 void showRenameDialog(FolderInfo info) {
2411 mFolderInfo = info;
2412 mWaitingForResult = true;
2413 showDialog(DIALOG_RENAME_FOLDER);
2414 }
2415
Michael Jurka0280c3b2010-09-17 15:00:07 -07002416 private void showAddDialog(int intersectX, int intersectY) {
2417 resetAddInfo();
2418 mAddIntersectCellX = intersectX;
2419 mAddIntersectCellY = intersectY;
2420 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 mWaitingForResult = true;
2422 showDialog(DIALOG_CREATE_SHORTCUT);
2423 }
2424
Joe Onoratodeb98af2010-02-19 14:59:39 -08002425 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002426 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002427 Bundle bundle = new Bundle();
2428
2429 ArrayList<String> shortcutNames = new ArrayList<String>();
2430 shortcutNames.add(getString(R.string.group_applications));
2431 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2432
2433 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2434 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2435 R.drawable.ic_launcher_application));
2436 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2437
2438 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2439 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002440 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002441 pickIntent.putExtras(bundle);
2442
Joe Onoratodeb98af2010-02-19 14:59:39 -08002443 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002444 }
2445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 private class RenameFolder {
2447 private EditText mInput;
2448
2449 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2451 mInput = (EditText) layout.findViewById(R.id.folder_name);
2452
2453 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2454 builder.setIcon(0);
2455 builder.setTitle(getString(R.string.rename_folder_title));
2456 builder.setCancelable(true);
2457 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2458 public void onCancel(DialogInterface dialog) {
2459 cleanup();
2460 }
2461 });
2462 builder.setNegativeButton(getString(R.string.cancel_action),
2463 new Dialog.OnClickListener() {
2464 public void onClick(DialogInterface dialog, int which) {
2465 cleanup();
2466 }
2467 }
2468 );
2469 builder.setPositiveButton(getString(R.string.rename_action),
2470 new Dialog.OnClickListener() {
2471 public void onClick(DialogInterface dialog, int which) {
2472 changeFolderName();
2473 }
2474 }
2475 );
2476 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002477
2478 final AlertDialog dialog = builder.create();
2479 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2480 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002481 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002482 mInput.requestFocus();
2483 InputMethodManager inputManager = (InputMethodManager)
2484 getSystemService(Context.INPUT_METHOD_SERVICE);
2485 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002486 }
2487 });
2488
2489 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 }
2491
2492 private void changeFolderName() {
2493 final String name = mInput.getText().toString();
2494 if (!TextUtils.isEmpty(name)) {
2495 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002496 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 mFolderInfo.title = name;
2498 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2499
Joe Onorato9c1289c2009-08-17 11:03:03 -04002500 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002501 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002502 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 } else {
2504 final FolderIcon folderIcon = (FolderIcon)
2505 mWorkspace.getViewForTag(mFolderInfo);
2506 if (folderIcon != null) {
2507 folderIcon.setText(name);
2508 getWorkspace().requestLayout();
2509 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002510 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 mWorkspaceLoading = true;
2512 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 }
2514 }
2515 }
2516 cleanup();
2517 }
2518
2519 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 dismissDialog(DIALOG_RENAME_FOLDER);
2521 mWaitingForResult = false;
2522 mFolderInfo = null;
2523 }
2524 }
2525
Daniel Sandler843e8602010-06-07 14:59:01 -04002526 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2527 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002528 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002529 }
2530
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002531 // AllAppsView.Watcher
2532 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002533 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2534 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002535 mWorkspace.setVisibility(View.GONE);
2536 }
2537 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002539 private void showToolbarButton(View button) {
2540 button.setAlpha(1.0f);
2541 button.setVisibility(View.VISIBLE);
2542 button.setFocusable(true);
2543 button.setClickable(true);
2544 }
2545
2546 private void hideToolbarButton(View button) {
2547 button.setAlpha(0.0f);
2548 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2549 button.setVisibility(View.INVISIBLE);
2550 button.setFocusable(false);
2551 button.setClickable(false);
2552 }
2553
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002554 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002555 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002556 *
2557 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2558 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002559 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002562 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 final boolean showing = show;
2564 final boolean hiding = !show;
2565
2566 final int duration = show ?
2567 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2568 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2569
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002570 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002571 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2572 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002573 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002574 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002575 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002578 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002579 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002581 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 });
2583 seq.play(anim);
2584 } else {
2585 if (showing) {
2586 showToolbarButton(view);
2587 } else {
2588 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002589 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 }
2592
2593 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002594 * Show/hide the appropriate toolbar buttons for newState.
2595 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 *
2597 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002598 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2599 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002601 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Michael Jurkae90cd232011-01-26 16:13:55 -08002602 final View buttonCluster = findViewById(R.id.all_apps_button_cluster);
2603
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002605 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 final View configureButton = findViewById(R.id.configure_button);
2607
2608 switch (newState) {
2609 case WORKSPACE:
Michael Jurkae90cd232011-01-26 16:13:55 -08002610 hideOrShowToolbarButton(true, buttonCluster, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002611 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002612 mDeleteZone.setDragAndDropEnabled(true);
2613 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 break;
2615 case ALL_APPS:
Michael Jurkae90cd232011-01-26 16:13:55 -08002616 hideOrShowToolbarButton(false, buttonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002617 mDeleteZone.setDragAndDropEnabled(false);
2618 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 break;
2620 case CUSTOMIZE:
Michael Jurkae90cd232011-01-26 16:13:55 -08002621 hideOrShowToolbarButton(false, buttonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002622 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 break;
2624 }
2625 }
2626
2627 /**
2628 * Helper method for the cameraZoomIn/cameraZoomOut animations
2629 * @param view The view being animated
2630 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2631 * @param scaleFactor The scale factor used for the zoom
2632 */
2633 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2634 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002635
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002637 // Set pivotY so that at the starting zoom factor, the view is partially
2638 // visible. Modifying initialHeightFactor changes how much of the view is
2639 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002641 final float initialHeightFactor = 0.165f;
2642 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002644 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002645 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 }
2647 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002648
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002649 /**
2650 * Zoom the camera out from the workspace to reveal 'toView'.
2651 * Assumes that the view to show is anchored at either the very top or very bottom
2652 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002654 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002656 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002658
2659 final int duration = toAllApps ?
2660 res.getInteger(R.integer.config_allAppsZoomInTime) :
2661 res.getInteger(R.integer.config_customizeZoomInTime);
2662
2663 final float scale = toAllApps ?
2664 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2665 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2666
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002667 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002668
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 setPivotsForZoom(toView, toState, scale);
2670
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002671 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002672 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002674 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002676
2677 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002678 final float oldScaleX = toView.getScaleX();
2679 final float oldScaleY = toView.getScaleY();
Adam Cohenf16e5712011-01-13 13:31:45 -08002680
Michael Jurka742574b2011-02-02 23:51:01 -08002681 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2682 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2683 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2684 public void onAnimationUpdate(ValueAnimator animation) {
2685 final float b = (Float) animation.getAnimatedValue();
2686 final float a = 1f - b;
2687 ((View) toView.getParent()).fastInvalidate();
2688 toView.setFastScaleX(a * oldScaleX + b * 1f);
2689 toView.setFastScaleY(a * oldScaleY + b * 1f);
2690 }
2691 });
Adam Cohen61033d32010-11-15 18:29:44 -08002692
Adam Cohenf16e5712011-01-13 13:31:45 -08002693 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002694 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002695 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Adam Cohen860f4c52011-01-27 20:16:13 -08002696 alphaAnim.setInterpolator(new DecelerateInterpolator(1.0f));
Michael Jurka742574b2011-02-02 23:51:01 -08002697 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2698 public void onAnimationUpdate(ValueAnimator animation) {
2699 final float b = (Float) animation.getAnimatedValue();
2700 final float a = 1f - b;
2701 // don't need to invalidate because we do so above
2702 toView.setFastAlpha(a * 0f + b * 1f);
2703 }
2704 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002705 alphaAnim.start();
2706 }
2707
Michael Jurkabfadaad2011-01-31 21:35:39 -08002708 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2709 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2710 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2711 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002712 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002713 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002714 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002715 // Prepare the position
2716 toView.setTranslationX(0.0f);
2717 toView.setTranslationY(0.0f);
2718 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002719 if (!toAllApps) {
2720 toView.setAlpha(1.0f);
2721 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002723 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002724 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002725 // If we don't set the final scale values here, if this animation is cancelled
2726 // it will have the wrong scale value and subsequent cameraPan animations will
2727 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002728 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002729 toView.setScaleX(1.0f);
2730 toView.setScaleY(1.0f);
2731 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732 });
2733
Chet Haaseb1254a62010-09-07 13:35:00 -07002734 AnimatorSet toolbarHideAnim = new AnimatorSet();
2735 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2737
2738 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002739 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002740
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002741 if (mStateAnimation != null) mStateAnimation.cancel();
2742 mStateAnimation = new AnimatorSet();
2743 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2744 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002745
2746 // Show the new toolbar buttons just as the main animation is ending
2747 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002748 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2749 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002750 } else {
2751 toView.setTranslationX(0.0f);
2752 toView.setTranslationY(0.0f);
2753 toView.setScaleX(1.0f);
2754 toView.setScaleY(1.0f);
2755 toView.setVisibility(View.VISIBLE);
2756 hideAndShowToolbarButtons(toState, null, null);
2757 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002758 }
2759
2760 /**
2761 * Zoom the camera back into the workspace, hiding 'fromView'.
2762 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002763 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2764 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002766 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002767 cameraZoomIn(fromState, animated, false);
2768 }
2769
2770 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002772 final boolean fromAllApps = (fromState == State.ALL_APPS);
2773
2774 int duration = fromAllApps ?
2775 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2776 res.getInteger(R.integer.config_customizeZoomOutTime);
2777
Michael Jurka742574b2011-02-02 23:51:01 -08002778 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002779 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2780 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2781
2782 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002783
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002784 mCustomizePagedView.endChoiceMode();
2785 mAllAppsPagedView.endChoiceMode();
2786
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002787 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002788
Michael Jurkad3ef3062010-11-23 16:23:58 -08002789 if (!springLoaded) {
2790 mWorkspace.unshrink(animated);
2791 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792
2793 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002794 if (mStateAnimation != null) mStateAnimation.cancel();
2795 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002796
Michael Jurka742574b2011-02-02 23:51:01 -08002797 final float oldScaleX = fromView.getScaleX();
2798 final float oldScaleY = fromView.getScaleY();
2799
2800 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2801 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2802 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2803 public void onAnimationUpdate(ValueAnimator animation) {
2804 final float b = (Float) animation.getAnimatedValue();
2805 final float a = 1f - b;
2806 ((View)fromView.getParent()).fastInvalidate();
2807 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2808 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2809 }
2810 });
2811 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002812 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohen860f4c52011-01-27 20:16:13 -08002813 alphaAnim.setInterpolator(new DecelerateInterpolator(2.0f));
Michael Jurka742574b2011-02-02 23:51:01 -08002814 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2815 public void onAnimationUpdate(ValueAnimator animation) {
2816 final float b = (Float) animation.getAnimatedValue();
2817 final float a = 1f - b;
2818 // don't need to invalidate because we do so above
2819 fromView.setFastAlpha(a * 1f + b * 0f);
2820 }
2821 });
2822
Michael Jurkacf6125c2011-01-28 15:20:01 -08002823 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002824 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002825 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002826 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002827 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002828 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 }
2830 });
2831
Chet Haaseb1254a62010-09-07 13:35:00 -07002832 AnimatorSet toolbarHideAnim = new AnimatorSet();
2833 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002834 if (!springLoaded) {
2835 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2836 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837
Adam Cohen61033d32010-11-15 18:29:44 -08002838 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839
2840 // Show the new toolbar buttons at the very end of the whole animation
2841 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2842 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002843 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2844 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 } else {
2846 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002847 if (!springLoaded) {
2848 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2849 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002850 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002851 }
2852
2853 /**
2854 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2855 * This is the transition used on xlarge screens to go between all apps and
2856 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002857 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2858 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2859 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002860 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002862 final Resources res = getResources();
2863 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002864 final int workspaceHeight = mWorkspace.getHeight();
2865
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002866 final boolean fromAllApps = (fromState == State.ALL_APPS);
2867 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2868 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002869
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002870 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2871 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2872 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2873 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002874
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002875 mCustomizePagedView.endChoiceMode();
2876 mAllAppsPagedView.endChoiceMode();
2877
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002878 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002879 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002880 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002881 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002882 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002883
2884 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002885 if (mStateAnimation != null) mStateAnimation.cancel();
2886 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002887 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002888 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002889 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890 toView.setVisibility(View.VISIBLE);
2891 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002892 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002893 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002894 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002895 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896 fromView.setVisibility(View.GONE);
2897 }
2898 });
2899
Chet Haaseb1254a62010-09-07 13:35:00 -07002900 AnimatorSet toolbarHideAnim = new AnimatorSet();
2901 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002902 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2903
Chet Haase472b2812010-10-14 07:02:04 -07002904 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2905 fromViewStartY, fromViewEndY);
2906 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002907 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2908 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2909 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2910 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2911 );
Chet Haase472b2812010-10-14 07:02:04 -07002912 fromAnim.setDuration(duration);
2913 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002914
2915 // Show the new toolbar buttons just as the main animation is ending
2916 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002917 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2918 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002919 } else {
2920 fromView.setY(fromViewEndY);
2921 fromView.setVisibility(View.GONE);
2922 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002923 toView.setScaleX(1.0f);
2924 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002925 toView.setVisibility(View.VISIBLE);
2926 hideAndShowToolbarButtons(toState, null, null);
2927 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002928 }
2929
2930 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002931 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002932 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002933 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002934
Michael Jurka79212d82010-07-30 16:36:20 -07002935 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002936 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002937 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002938 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002939 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002940 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002941 } else {
2942 mAllAppsGrid.zoom(1.0f, animated);
2943 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002944
Romain Guyc16fea72010-03-12 17:17:56 -08002945 ((View) mAllAppsGrid).setFocusable(true);
2946 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002947
Joe Onorato7c312c12009-08-13 21:36:53 -07002948 // TODO: fade these two too
2949 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002950
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002951 // Change the state *after* we've called all the transition code
2952 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953
Winson Chung5fb63472011-02-02 17:03:37 -08002954 // Pause the auto-advance of widgets until we are out of AllApps
2955 mUserPresent = false;
2956 updateRunning();
2957
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002958 // send an accessibility event to announce the context change
2959 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2960 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002961
2962 void showWorkspace(boolean animated) {
2963 showWorkspace(animated, null);
2964 }
2965
2966 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002967 if (layout != null) {
2968 // always animated, but that's ok since we never specify a layout and
2969 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002970 mWorkspace.unshrink(layout);
2971 } else {
2972 mWorkspace.unshrink(animated);
2973 }
2974 if (mState == State.ALL_APPS) {
2975 closeAllApps(animated);
2976 } else if (mState == State.CUSTOMIZE) {
2977 hideCustomizationDrawer(animated);
2978 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002979
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002980 // Change the state *after* we've called all the transition code
2981 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002982
Winson Chung5fb63472011-02-02 17:03:37 -08002983 // Resume the auto-advance of widgets
2984 mUserPresent = true;
2985 updateRunning();
2986
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002987 // send an accessibility event to announce the context change
2988 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002989 }
2990
Michael Jurkad3ef3062010-11-23 16:23:58 -08002991 void enterSpringLoadedDragMode(CellLayout layout) {
2992 mWorkspace.enterSpringLoadedDragMode(layout);
2993 if (mState == State.ALL_APPS) {
2994 cameraZoomIn(State.ALL_APPS, true, true);
2995 mState = State.ALL_APPS_SPRING_LOADED;
2996 } else if (mState == State.CUSTOMIZE) {
2997 cameraZoomIn(State.CUSTOMIZE, true, true);
2998 mState = State.CUSTOMIZE_SPRING_LOADED;
2999 }/* else {
3000 // we're already in spring loaded mode; don't do anything
3001 }*/
3002 }
3003
3004 void exitSpringLoadedDragMode() {
3005 if (mState == State.ALL_APPS_SPRING_LOADED) {
3006 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3007 cameraZoomOut(State.ALL_APPS, true);
3008 mState = State.ALL_APPS;
3009 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3010 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3011 cameraZoomOut(State.CUSTOMIZE, true);
3012 mState = State.CUSTOMIZE;
3013 }/* else {
3014 // we're not in spring loaded mode; don't do anything
3015 }*/
3016 }
3017
Joe Onoratob2061212009-11-24 16:13:54 -05003018 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003019 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003020 * - Home from workspace
3021 * - from center screen
3022 * - from other screens
3023 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003024 * - from center screen
3025 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003026 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003027 * - from center screen
3028 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003029 * - Launch app from workspace and quit
3030 * - with back
3031 * - with home
3032 * - Launch app from all apps and quit
3033 * - with back
3034 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003035 * - Go to a screen that's not the default, then all
3036 * apps, and launch and app, and go back
3037 * - with back
3038 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003039 * - On workspace, long press power and go back
3040 * - with back
3041 * - with home
3042 * - On all apps, long press power and go back
3043 * - with back
3044 * - with home
3045 * - On workspace, power off
3046 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003047 * - Launch an app and turn off the screen while in that app
3048 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003049 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003050 * - From all apps
3051 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003052 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3053 * - From all apps
3054 * - From the center workspace
3055 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003056 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003057 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003058 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003059 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003060 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003061 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003062 } else {
3063 mAllAppsGrid.zoom(0.0f, animated);
3064 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003065 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003066 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003067 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003068 }
3069
Joe Onorato7c312c12009-08-13 21:36:53 -07003070 void lockAllApps() {
3071 // TODO
3072 }
3073
3074 void unlockAllApps() {
3075 // TODO
3076 }
3077
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003078 // Show the customization drawer (only exists in x-large configuration)
3079 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003080 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003081 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003082 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003083 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003084 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003085 // Change the state *after* we've called all the transition code
3086 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003087
3088 // Pause the auto-advance of widgets until we are out of Customization drawer
3089 mUserPresent = false;
3090 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003091 }
3092
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003093 // Hide the customization drawer (only exists in x-large configuration)
3094 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003095 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003096 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003097 }
3098 }
3099
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003100 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3101 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3102 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003103 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003104 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003105
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003106 void onWorkspaceClick(CellLayout layout) {
3107 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003108 }
3109
Michael Jurka4ef207b2010-11-29 17:05:45 -08003110 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003111 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003112 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3113 ImageView button = (ImageView) findViewById(buttonId);
3114 Drawable toolbarIcon = null;
3115 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003116 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003117 // Look for the toolbar icon specified in the activity meta-data
3118 Bundle metaData = packageManager.getActivityInfo(
3119 activityName, PackageManager.GET_META_DATA).metaData;
3120 if (metaData != null) {
3121 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3122 if (iconResId != 0) {
3123 Resources res = packageManager.getResourcesForActivity(activityName);
3124 toolbarIcon = res.getDrawable(iconResId);
3125 }
3126 }
3127 } catch (NameNotFoundException e) {
3128 // Do nothing
3129 }
3130 // If we were unable to find the icon via the meta-data, use a generic one
3131 if (toolbarIcon == null) {
3132 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003133 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003134 } else {
3135 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003136 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003137 }
3138 }
3139
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003140 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003142 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 }
3144
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145 private void updateGlobalSearchIcon() {
3146 if (LauncherApplication.isScreenXLarge()) {
3147 final SearchManager searchManager =
3148 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3149 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003150 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003151 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003152 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003153 } else {
3154 findViewById(R.id.search_button).setVisibility(View.GONE);
3155 }
3156 }
3157 }
3158
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003159 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003160 updateButtonWithDrawable(R.id.search_button, d);
3161 }
3162
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003163 private void updateVoiceSearchIcon() {
3164 if (LauncherApplication.isScreenXLarge()) {
3165 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3166 ComponentName activityName = intent.resolveActivity(getPackageManager());
3167 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003168 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003169 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3170 } else {
3171 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003172 }
3173 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003174 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003175
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003176 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003177 updateButtonWithDrawable(R.id.voice_button, d);
3178 }
3179
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003180 /**
3181 * Sets the app market icon (shown when all apps is visible on x-large screens)
3182 */
3183 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003184 if (LauncherApplication.isScreenXLarge()) {
3185 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3186 // Find the app market activity by resolving an intent.
3187 // (If multiple app markets are installed, it will return the ResolverActivity.)
3188 ComponentName activityName = intent.resolveActivity(getPackageManager());
3189 if (activityName != null) {
3190 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003191 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003192 R.id.market_button, activityName, R.drawable.app_market_generic);
3193 }
3194 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003195 }
3196
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003197 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003198 updateButtonWithDrawable(R.id.market_button, d);
3199 }
3200
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003201 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 * Displays the shortcut creation dialog and launches, if necessary, the
3203 * appropriate activity.
3204 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003205 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003206 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3207 DialogInterface.OnShowListener {
3208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3215 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003216 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 builder.setInverseBackgroundForced(true);
3219
3220 AlertDialog dialog = builder.create();
3221 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003222 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003223 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 return dialog;
3226 }
3227
3228 public void onCancel(DialogInterface dialog) {
3229 mWaitingForResult = false;
3230 cleanup();
3231 }
3232
Romain Guycbb89e42009-06-08 15:52:54 -07003233 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003234 }
3235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003237 try {
3238 dismissDialog(DIALOG_CREATE_SHORTCUT);
3239 } catch (Exception e) {
3240 // An exception is thrown if the dialog is not visible, which is fine
3241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 }
3243
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003244 /**
3245 * Handle the action clicked in the "Add to home" dialog.
3246 */
3247 public void onClick(DialogInterface dialog, int which) {
3248 Resources res = getResources();
3249 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003250
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003251 switch (which) {
3252 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003253 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003254 break;
3255 }
Romain Guycbb89e42009-06-08 15:52:54 -07003256
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003257 case AddAdapter.ITEM_APPWIDGET: {
3258 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003259
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003260 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3261 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003262 // start the pick activity
3263 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3264 break;
3265 }
Romain Guycbb89e42009-06-08 15:52:54 -07003266
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003267 case AddAdapter.ITEM_LIVE_FOLDER: {
3268 // Insert extra item to handle inserting folder
3269 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003270
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003271 ArrayList<String> shortcutNames = new ArrayList<String>();
3272 shortcutNames.add(res.getString(R.string.group_folder));
3273 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003274
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003275 ArrayList<ShortcutIconResource> shortcutIcons =
3276 new ArrayList<ShortcutIconResource>();
3277 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3278 R.drawable.ic_launcher_folder));
3279 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3280
3281 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3282 pickIntent.putExtra(Intent.EXTRA_INTENT,
3283 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3284 pickIntent.putExtra(Intent.EXTRA_TITLE,
3285 getText(R.string.title_select_live_folder));
3286 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003287
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003288 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3289 break;
3290 }
3291
3292 case AddAdapter.ITEM_WALLPAPER: {
3293 startWallpaper();
3294 break;
3295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003296 }
3297 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003298
3299 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003300 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003302 }
3303
3304 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003305 * Receives notifications when applications are added/removed.
3306 */
3307 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3308 @Override
3309 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003310 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003311 String reason = intent.getStringExtra("reason");
3312 if (!"homekey".equals(reason)) {
3313 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003314 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003315 animate = false;
3316 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003317 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003318 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003319 }
3320 }
3321
3322 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003323 * Receives notifications whenever the appwidgets are reset.
3324 */
3325 private class AppWidgetResetObserver extends ContentObserver {
3326 public AppWidgetResetObserver() {
3327 super(new Handler());
3328 }
3329
3330 @Override
3331 public void onChange(boolean selfChange) {
3332 onAppWidgetReset();
3333 }
3334 }
3335
3336 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003337 * If the activity is currently paused, signal that we need to re-run the loader
3338 * in onResume.
3339 *
3340 * This needs to be called from incoming places where resources might have been loaded
3341 * while we are paused. That is becaues the Configuration might be wrong
3342 * when we're not running, and if it comes back to what it was when we
3343 * were paused, we are not restarted.
3344 *
3345 * Implementation of the method from LauncherModel.Callbacks.
3346 *
3347 * @return true if we are currently paused. The caller might be able to
3348 * skip some work in that case since we will come back again.
3349 */
3350 public boolean setLoadOnResume() {
3351 if (mPaused) {
3352 Log.i(TAG, "setLoadOnResume");
3353 mOnResumeNeedsLoad = true;
3354 return true;
3355 } else {
3356 return false;
3357 }
3358 }
3359
3360 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003362 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003364 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003365 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003366 } else {
3367 return SCREEN_COUNT / 2;
3368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003370
Winson Chunga12a2502010-12-20 14:41:35 -08003371 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003372 mAllAppsPagedView = view;
3373 }
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 /**
3376 * Refreshes the shortcuts shown on the workspace.
3377 *
3378 * Implementation of the method from LauncherModel.Callbacks.
3379 */
3380 public void startBinding() {
3381 final Workspace workspace = mWorkspace;
3382 int count = workspace.getChildCount();
3383 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003384 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003385 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3386 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003388
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 if (DEBUG_USER_INTERFACE) {
3390 android.widget.Button finishButton = new android.widget.Button(this);
3391 finishButton.setText("Finish");
3392 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3393
3394 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3395 public void onClick(View v) {
3396 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003397 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003399 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003400
3401 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3402 // mDesktopItems -> AppWidgetInfo -> hostView).
3403 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003404 }
3405
3406 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 * Bind the items start-end from the list.
3408 *
3409 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003410 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3412
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003413 setLoadOnResume();
3414
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 final Workspace workspace = mWorkspace;
3416
3417 for (int i=start; i<end; i++) {
3418 final ItemInfo item = shortcuts.get(i);
3419 mDesktopItems.add(item);
3420 switch (item.itemType) {
3421 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3422 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003423 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3425 false);
3426 break;
3427 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3428 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003429 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003430 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3432 false);
3433 break;
3434 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3435 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3436 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003437 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 (LiveFolderInfo) item);
3439 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3440 false);
3441 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003443 }
3444
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003446 }
3447
Joe Onorato9c1289c2009-08-17 11:03:03 -04003448 /**
3449 * Implementation of the method from LauncherModel.Callbacks.
3450 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003451 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003452 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003453 sFolders.clear();
3454 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003455 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456
3457 /**
3458 * Add the views for a widget to the workspace.
3459 *
3460 * Implementation of the method from LauncherModel.Callbacks.
3461 */
3462 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003463 setLoadOnResume();
3464
Daniel Sandler843e8602010-06-07 14:59:01 -04003465 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3466 if (DEBUG_WIDGETS) {
3467 Log.d(TAG, "bindAppWidget: " + item);
3468 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003469 final Workspace workspace = mWorkspace;
3470
3471 final int appWidgetId = item.appWidgetId;
3472 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003473 if (DEBUG_WIDGETS) {
3474 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3475 }
3476
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3478
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3480 item.hostView.setTag(item);
3481
3482 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3483 item.cellY, item.spanX, item.spanY, false);
3484
Adam Cohended9f8d2010-11-03 13:25:16 -07003485 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3486
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 workspace.requestLayout();
3488
3489 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003490
3491 if (DEBUG_WIDGETS) {
3492 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3493 + (SystemClock.uptimeMillis()-start) + "ms");
3494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 }
3496
3497 /**
3498 * Callback saying that there aren't any more items to bind.
3499 *
3500 * Implementation of the method from LauncherModel.Callbacks.
3501 */
3502 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003503 setLoadOnResume();
3504
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 if (mSavedState != null) {
3506 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003507 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 }
3509
3510 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3511 if (userFolders != null) {
3512 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003513 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 if (info != null) {
3515 openFolder(info);
3516 }
3517 }
3518 final Folder openFolder = mWorkspace.getOpenFolder();
3519 if (openFolder != null) {
3520 openFolder.requestFocus();
3521 }
3522 }
3523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 mSavedState = null;
3525 }
3526
3527 if (mSavedInstanceState != null) {
3528 super.onRestoreInstanceState(mSavedInstanceState);
3529 mSavedInstanceState = null;
3530 }
3531
Winson Chung32bb5e52011-01-31 14:51:56 -08003532 // Workaround a bug that occurs when rotating the device while the customization mode is
3533 // open, we trigger a new layout on all the CellLayout children.
3534 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3535 final int childCount = mWorkspace.getChildCount();
3536 for (int i = 0; i < childCount; ++i) {
3537 mWorkspace.getChildAt(i).requestLayout();
3538 }
3539 }
3540
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541 mWorkspaceLoading = false;
3542 }
3543
3544 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003545 * Updates the icons on the launcher that are affected by changes to the package list
3546 * on the device.
3547 */
3548 private void updateIconsAffectedByPackageManagerChanges() {
3549 updateAppMarketIcon();
3550 updateGlobalSearchIcon();
3551 updateVoiceSearchIcon();
3552 }
3553
3554 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 * Add the icons for all apps.
3556 *
3557 * Implementation of the method from LauncherModel.Callbacks.
3558 */
3559 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003560 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003561 if (mCustomizePagedView != null) {
3562 mCustomizePagedView.setApps(apps);
3563 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003564 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565 }
3566
3567 /**
3568 * A package was installed.
3569 *
3570 * Implementation of the method from LauncherModel.Callbacks.
3571 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003572 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003573 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003575 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003576 if (mCustomizePagedView != null) {
3577 mCustomizePagedView.addApps(apps);
3578 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003579 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 }
3581
3582 /**
3583 * A package was updated.
3584 *
3585 * Implementation of the method from LauncherModel.Callbacks.
3586 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003587 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003588 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003590 mWorkspace.updateShortcuts(apps);
3591 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003592 if (mCustomizePagedView != null) {
3593 mCustomizePagedView.updateApps(apps);
3594 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003595 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003596 }
3597
3598 /**
3599 * A package was uninstalled.
3600 *
3601 * Implementation of the method from LauncherModel.Callbacks.
3602 */
Joe Onorato36115782010-06-17 13:28:48 -04003603 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003605 if (permanent) {
3606 mWorkspace.removeItems(apps);
3607 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003608 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003609 if (mCustomizePagedView != null) {
3610 mCustomizePagedView.removeApps(apps);
3611 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003612 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 }
Joe Onoratobe386092009-11-17 17:32:16 -08003614
3615 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003616 * A number of packages were updated.
3617 */
3618 public void bindPackagesUpdated() {
3619 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003620 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003621 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003622 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003623 }
3624
Winson Chung400438b2011-01-16 17:53:48 -08003625 private int mapConfigurationOriActivityInfoOri(int configOri) {
3626 final Display d = getWindowManager().getDefaultDisplay();
3627 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3628 switch (d.getRotation()) {
3629 case Surface.ROTATION_0:
3630 case Surface.ROTATION_180:
3631 // We are currently in the same basic orientation as the natural orientation
3632 naturalOri = configOri;
3633 break;
3634 case Surface.ROTATION_90:
3635 case Surface.ROTATION_270:
3636 // We are currently in the other basic orientation to the natural orientation
3637 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3638 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3639 break;
3640 }
3641
3642 int[] oriMap = {
3643 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3644 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3645 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3646 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3647 };
3648 // Since the map starts at portrait, we need to offset if this device's natural orientation
3649 // is landscape.
3650 int indexOffset = 0;
3651 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3652 indexOffset = 1;
3653 }
3654 return oriMap[(d.getRotation() + indexOffset) % 4];
3655 }
3656 public void lockScreenOrientation() {
3657 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3658 .getConfiguration().orientation));
3659 }
3660 public void unlockScreenOrientation() {
3661 mHandler.postDelayed(new Runnable() {
3662 public void run() {
3663 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3664 }
3665 }, mRestoreScreenOrientationDelay);
3666 }
3667
Winson Chung80baf5a2010-08-09 16:03:15 -07003668 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003669 * Prints out out state for debugging.
3670 */
3671 public void dumpState() {
3672 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003673 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003674 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3675 Log.d(TAG, "mRestoring=" + mRestoring);
3676 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3677 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3678 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003679 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003680 mModel.dumpState();
3681 mAllAppsGrid.dumpState();
3682 Log.d(TAG, "END launcher2 dump state");
3683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003684}