blob: 745ae2e43b73de056d40005e09c03bba360d80f5 [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() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001974 if (mAppWidgetHost != null) {
1975 mAppWidgetHost.startListening();
1976 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001977 }
1978
1979 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001980 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1981 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 private void unbindDesktopItems() {
1984 for (ItemInfo item: mDesktopItems) {
1985 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001987 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 /**
1991 * Launches the intent referred by the clicked shortcut.
1992 *
1993 * @param v The view representing the clicked shortcut.
1994 */
1995 public void onClick(View v) {
1996 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001997 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001999 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002000 int[] pos = new int[2];
2001 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002002 intent.setSourceBounds(new Rect(pos[0], pos[1],
2003 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002004 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 } else if (tag instanceof FolderInfo) {
2006 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002007 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002008 if (mState == State.ALL_APPS) {
2009 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002010 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002011 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014 }
2015
Michael Jurka0e260592010-06-30 17:07:39 -07002016 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002017 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002018 // clicking anywhere on the workspace causes the customization drawer to slide down
2019 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002020 return false;
2021 }
2022
Michael Jurkaaf442092010-06-10 17:01:57 -07002023 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002024 * Event handler for the search button
2025 *
2026 * @param v The view that was clicked.
2027 */
2028 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002029 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002030 // Use a custom animation for launching search
2031 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002032 }
2033
2034 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002035 * Event handler for the voice button
2036 *
2037 * @param v The view that was clicked.
2038 */
2039 public void onClickVoiceButton(View v) {
2040 startVoiceSearch();
2041 }
2042
2043 private void startVoiceSearch() {
2044 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2045 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2046 startActivity(intent);
2047 }
2048
2049 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002050 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002051 * enters home screen customization mode.
2052 *
2053 * @param v The view that was clicked.
2054 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002055 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002056 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002057 }
2058
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002059 /**
2060 * Event handler for the "grid" button that appears on the home screen, which
2061 * enters all apps mode.
2062 *
2063 * @param v The view that was clicked.
2064 */
2065 public void onClickAllAppsButton(View v) {
2066 showAllApps(true);
2067 }
2068
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002069 public void onClickAppMarketButton(View v) {
2070 if (mAppMarketIntent != null) {
2071 startActivitySafely(mAppMarketIntent, "app market");
2072 }
2073 }
2074
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002075 void startApplicationDetailsActivity(ComponentName componentName) {
2076 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002077 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2078 Uri.fromParts("package", packageName, null));
2079 startActivity(intent);
2080 }
2081
Patrick Dubroy5539af72010-09-07 15:22:01 -07002082 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2083 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2084 // System applications cannot be installed. For now, show a toast explaining that.
2085 // We may give them the option of disabling apps this way.
2086 int messageId = R.string.uninstall_system_app_text;
2087 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2088 } else {
2089 String packageName = appInfo.componentName.getPackageName();
2090 String className = appInfo.componentName.getClassName();
2091 Intent intent = new Intent(
2092 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2093 startActivity(intent);
2094 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002095 }
2096
Joe Onoratof984e852010-03-25 09:47:45 -07002097 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2099 try {
2100 startActivity(intent);
2101 } catch (ActivityNotFoundException e) {
2102 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002103 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 } catch (SecurityException e) {
2105 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002106 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002108 "or use the exported attribute for this activity. "
2109 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002112
Romain Guy8e633c52010-03-04 12:51:36 -08002113 void startActivityForResultSafely(Intent intent, int requestCode) {
2114 try {
2115 startActivityForResult(intent, requestCode);
2116 } catch (ActivityNotFoundException e) {
2117 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2118 } catch (SecurityException e) {
2119 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2120 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2121 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2122 "or use the exported attribute for this activity.", e);
2123 }
2124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125
2126 private void handleFolderClick(FolderInfo folderInfo) {
2127 if (!folderInfo.opened) {
2128 // Close any open folder
2129 closeFolder();
2130 // Open the requested folder
2131 openFolder(folderInfo);
2132 } else {
2133 // Find the open folder...
2134 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2135 int folderScreen;
2136 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002137 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 // .. and close it
2139 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002140 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 // Close any folder open on the current screen
2142 closeFolder();
2143 // Pull the folder onto this screen
2144 openFolder(folderInfo);
2145 }
2146 }
2147 }
2148 }
2149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002151 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 * is animated relative to the specified View. If the View is null, no animation
2153 * is played.
2154 *
2155 * @param folderInfo The FolderInfo describing the folder to open.
2156 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002157 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 Folder openFolder;
2159
2160 if (folderInfo instanceof UserFolderInfo) {
2161 openFolder = UserFolder.fromXml(this);
2162 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002163 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 } else {
2165 return;
2166 }
2167
Joe Onorato00acb122009-08-04 16:04:30 -04002168 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 openFolder.setLauncher(this);
2170
2171 openFolder.bind(folderInfo);
2172 folderInfo.opened = true;
2173
Winson Chungaafa03c2010-06-11 17:34:16 -07002174 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 openFolder.onOpen();
2177 }
2178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002180 switch (v.getId()) {
2181 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002182 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002183 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2184 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002185 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002186 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002187 return true;
2188 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002189 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002190 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2191 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002192 showPreviews(v);
2193 }
2194 return true;
2195 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002196 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002197 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2198 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2199 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002200 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002201 return true;
2202 }
2203
Joe Onorato9c1289c2009-08-17 11:03:03 -04002204 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 return false;
2206 }
2207
2208 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002209 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212
Michael Jurka0280c3b2010-09-17 15:00:07 -07002213 resetAddInfo();
2214 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002216 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 return true;
2218 }
2219
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 final View itemUnderLongClick = longClickCellInfo.cell;
2221
Winson Chung304dcde2011-01-07 11:17:23 -08002222 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002223 if (itemUnderLongClick == null) {
2224 // User long pressed on empty space
2225 mWorkspace.setAllowLongPress(false);
2226 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2227 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002228 if (LauncherApplication.isScreenXLarge()) {
2229 addItems();
2230 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002231 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002236 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2237 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 mAddIntersectCellX = longClickCellInfo.cellX;
2239 mAddIntersectCellY = longClickCellInfo.cellY;
2240 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 }
2242 }
2243 }
2244 return true;
2245 }
2246
Romain Guye6b8e2f2009-11-10 11:56:55 -08002247 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002248 private void dismissPreview(final View v) {
2249 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002250 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002251 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2252 public void onDismiss() {
2253 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2254 int count = group.getChildCount();
2255 for (int i = 0; i < count; i++) {
2256 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2257 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002258 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2259 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2260
2261 v.setTag(R.id.workspace, null);
2262 v.setTag(R.id.icon, null);
2263 window.setOnDismissListener(null);
2264 }
2265 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002266 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002267 }
2268 v.setTag(null);
2269 }
2270
Romain Guyf8e6a802009-12-07 17:48:02 -08002271 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002272 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002273 }
2274
Romain Guya6abce82009-11-10 02:54:41 -08002275 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002276 final Resources resources = getResources();
2277 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002278
Romain Guya6abce82009-11-10 02:54:41 -08002279 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002280
Romain Guy9d31e9f2009-11-11 18:54:28 -08002281 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002282
Romain Guyf8e6a802009-12-07 17:48:02 -08002283 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002284 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002285 int extraW = (int) ((r.left + r.right) * max);
2286 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002287
2288 int aW = cell.getWidth() - extraW;
2289 float w = aW / max;
2290
2291 int width = cell.getWidth();
2292 int height = cell.getHeight();
2293 int x = cell.getLeftPadding();
2294 int y = cell.getTopPadding();
2295 width -= (x + cell.getRightPadding());
2296 height -= (y + cell.getBottomPadding());
2297
2298 float scale = w / width;
2299
2300 int count = end - start;
2301
2302 final float sWidth = width * scale;
2303 float sHeight = height * scale;
2304
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002306
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2308 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002309
2310 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002311 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002312 cell = (CellLayout) workspace.getChildAt(i);
2313
Romain Guyf8e6a802009-12-07 17:48:02 -08002314 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002315 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002316
2317 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002318 c.scale(scale, scale);
2319 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002320 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002321
Romain Guy9d31e9f2009-11-11 18:54:28 -08002322 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002323 image.setImageBitmap(bitmap);
2324 image.setTag(i);
2325 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002326 image.setOnFocusChangeListener(handler);
2327 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002328 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002329
2330 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2332
Winson Chungaafa03c2010-06-11 17:34:16 -07002333 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002334 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002335
2336 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002337 p.setContentView(preview);
2338 p.setWidth((int) (sWidth * count + extraW));
2339 p.setHeight((int) (sHeight + extraH));
2340 p.setAnimationStyle(R.style.AnimationPreview);
2341 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002342 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002343 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002344 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002345
Romain Guye6b8e2f2009-11-10 11:56:55 -08002346 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2347 public void onDismiss() {
2348 dismissPreview(anchor);
2349 }
2350 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002351
2352 anchor.setTag(p);
2353 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002354 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002355 }
2356
Romain Guy9d31e9f2009-11-11 18:54:28 -08002357 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002358 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002359
Romain Guye6b8e2f2009-11-10 11:56:55 -08002360 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002361 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002362 }
2363
2364 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002365 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002366 v.post(this);
2367 }
2368
2369 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002370 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002371 }
2372
2373 public void onFocusChange(View v, boolean hasFocus) {
2374 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002375 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 }
Romain Guya6abce82009-11-10 02:54:41 -08002377 }
2378 }
2379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 Workspace getWorkspace() {
2381 return mWorkspace;
2382 }
2383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 @Override
2385 protected Dialog onCreateDialog(int id) {
2386 switch (id) {
2387 case DIALOG_CREATE_SHORTCUT:
2388 return new CreateShortcut().createDialog();
2389 case DIALOG_RENAME_FOLDER:
2390 return new RenameFolder().createDialog();
2391 }
2392
2393 return super.onCreateDialog(id);
2394 }
2395
2396 @Override
2397 protected void onPrepareDialog(int id, Dialog dialog) {
2398 switch (id) {
2399 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 break;
2401 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002402 if (mFolderInfo != null) {
2403 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2404 final CharSequence text = mFolderInfo.title;
2405 input.setText(text);
2406 input.setSelection(0, text.length());
2407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 break;
2409 }
2410 }
2411
2412 void showRenameDialog(FolderInfo info) {
2413 mFolderInfo = info;
2414 mWaitingForResult = true;
2415 showDialog(DIALOG_RENAME_FOLDER);
2416 }
2417
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 private void showAddDialog(int intersectX, int intersectY) {
2419 resetAddInfo();
2420 mAddIntersectCellX = intersectX;
2421 mAddIntersectCellY = intersectY;
2422 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 mWaitingForResult = true;
2424 showDialog(DIALOG_CREATE_SHORTCUT);
2425 }
2426
Joe Onoratodeb98af2010-02-19 14:59:39 -08002427 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002428 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002429 Bundle bundle = new Bundle();
2430
2431 ArrayList<String> shortcutNames = new ArrayList<String>();
2432 shortcutNames.add(getString(R.string.group_applications));
2433 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2434
2435 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2436 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2437 R.drawable.ic_launcher_application));
2438 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2439
2440 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2441 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002442 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002443 pickIntent.putExtras(bundle);
2444
Joe Onoratodeb98af2010-02-19 14:59:39 -08002445 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002446 }
2447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 private class RenameFolder {
2449 private EditText mInput;
2450
2451 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2453 mInput = (EditText) layout.findViewById(R.id.folder_name);
2454
2455 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2456 builder.setIcon(0);
2457 builder.setTitle(getString(R.string.rename_folder_title));
2458 builder.setCancelable(true);
2459 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2460 public void onCancel(DialogInterface dialog) {
2461 cleanup();
2462 }
2463 });
2464 builder.setNegativeButton(getString(R.string.cancel_action),
2465 new Dialog.OnClickListener() {
2466 public void onClick(DialogInterface dialog, int which) {
2467 cleanup();
2468 }
2469 }
2470 );
2471 builder.setPositiveButton(getString(R.string.rename_action),
2472 new Dialog.OnClickListener() {
2473 public void onClick(DialogInterface dialog, int which) {
2474 changeFolderName();
2475 }
2476 }
2477 );
2478 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002479
2480 final AlertDialog dialog = builder.create();
2481 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2482 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002483 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002484 mInput.requestFocus();
2485 InputMethodManager inputManager = (InputMethodManager)
2486 getSystemService(Context.INPUT_METHOD_SERVICE);
2487 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002488 }
2489 });
2490
2491 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 }
2493
2494 private void changeFolderName() {
2495 final String name = mInput.getText().toString();
2496 if (!TextUtils.isEmpty(name)) {
2497 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002498 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 mFolderInfo.title = name;
2500 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2501
Joe Onorato9c1289c2009-08-17 11:03:03 -04002502 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002503 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002504 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 } else {
2506 final FolderIcon folderIcon = (FolderIcon)
2507 mWorkspace.getViewForTag(mFolderInfo);
2508 if (folderIcon != null) {
2509 folderIcon.setText(name);
2510 getWorkspace().requestLayout();
2511 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002512 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002513 mWorkspaceLoading = true;
2514 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 }
2516 }
2517 }
2518 cleanup();
2519 }
2520
2521 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 dismissDialog(DIALOG_RENAME_FOLDER);
2523 mWaitingForResult = false;
2524 mFolderInfo = null;
2525 }
2526 }
2527
Daniel Sandler843e8602010-06-07 14:59:01 -04002528 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2529 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002530 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002531 }
2532
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002533 // AllAppsView.Watcher
2534 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002535 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2536 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002537 mWorkspace.setVisibility(View.GONE);
2538 }
2539 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 private void showToolbarButton(View button) {
2542 button.setAlpha(1.0f);
2543 button.setVisibility(View.VISIBLE);
2544 button.setFocusable(true);
2545 button.setClickable(true);
2546 }
2547
2548 private void hideToolbarButton(View button) {
2549 button.setAlpha(0.0f);
2550 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2551 button.setVisibility(View.INVISIBLE);
2552 button.setFocusable(false);
2553 button.setClickable(false);
2554 }
2555
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002556 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002558 *
2559 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2560 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002561 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002564 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 final boolean showing = show;
2566 final boolean hiding = !show;
2567
2568 final int duration = show ?
2569 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2570 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2571
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002573 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2574 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002575 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002576 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002577 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002579 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002580 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002581 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584 });
2585 seq.play(anim);
2586 } else {
2587 if (showing) {
2588 showToolbarButton(view);
2589 } else {
2590 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002592 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 }
2594
2595 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 * Show/hide the appropriate toolbar buttons for newState.
2597 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 *
2599 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002600 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2601 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002603 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Michael Jurkae90cd232011-01-26 16:13:55 -08002604 final View buttonCluster = findViewById(R.id.all_apps_button_cluster);
2605
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002607 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 final View configureButton = findViewById(R.id.configure_button);
2609
2610 switch (newState) {
2611 case WORKSPACE:
Michael Jurkae90cd232011-01-26 16:13:55 -08002612 hideOrShowToolbarButton(true, buttonCluster, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002613 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002614 mDeleteZone.setDragAndDropEnabled(true);
2615 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 break;
2617 case ALL_APPS:
Michael Jurkae90cd232011-01-26 16:13:55 -08002618 hideOrShowToolbarButton(false, buttonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002619 mDeleteZone.setDragAndDropEnabled(false);
2620 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002621 break;
2622 case CUSTOMIZE:
Michael Jurkae90cd232011-01-26 16:13:55 -08002623 hideOrShowToolbarButton(false, buttonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002624 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002625 break;
2626 }
2627 }
2628
2629 /**
2630 * Helper method for the cameraZoomIn/cameraZoomOut animations
2631 * @param view The view being animated
2632 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2633 * @param scaleFactor The scale factor used for the zoom
2634 */
2635 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2636 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002637
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002639 // Set pivotY so that at the starting zoom factor, the view is partially
2640 // visible. Modifying initialHeightFactor changes how much of the view is
2641 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002643 final float initialHeightFactor = 0.165f;
2644 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002645 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002646 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002647 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 }
2649 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002650
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002651 /**
2652 * Zoom the camera out from the workspace to reveal 'toView'.
2653 * Assumes that the view to show is anchored at either the very top or very bottom
2654 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002656 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002659 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002660
2661 final int duration = toAllApps ?
2662 res.getInteger(R.integer.config_allAppsZoomInTime) :
2663 res.getInteger(R.integer.config_customizeZoomInTime);
2664
2665 final float scale = toAllApps ?
2666 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2667 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2668
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002670
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 setPivotsForZoom(toView, toState, scale);
2672
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002673 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002674 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002676 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678
2679 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002680 final float oldScaleX = toView.getScaleX();
2681 final float oldScaleY = toView.getScaleY();
Adam Cohenf16e5712011-01-13 13:31:45 -08002682
Michael Jurka742574b2011-02-02 23:51:01 -08002683 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2684 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2685 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2686 public void onAnimationUpdate(ValueAnimator animation) {
2687 final float b = (Float) animation.getAnimatedValue();
2688 final float a = 1f - b;
2689 ((View) toView.getParent()).fastInvalidate();
2690 toView.setFastScaleX(a * oldScaleX + b * 1f);
2691 toView.setFastScaleY(a * oldScaleY + b * 1f);
2692 }
2693 });
Adam Cohen61033d32010-11-15 18:29:44 -08002694
Adam Cohenf16e5712011-01-13 13:31:45 -08002695 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002696 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002697 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Adam Cohen860f4c52011-01-27 20:16:13 -08002698 alphaAnim.setInterpolator(new DecelerateInterpolator(1.0f));
Michael Jurka742574b2011-02-02 23:51:01 -08002699 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2700 public void onAnimationUpdate(ValueAnimator animation) {
2701 final float b = (Float) animation.getAnimatedValue();
2702 final float a = 1f - b;
2703 // don't need to invalidate because we do so above
2704 toView.setFastAlpha(a * 0f + b * 1f);
2705 }
2706 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002707 alphaAnim.start();
2708 }
2709
Michael Jurkabfadaad2011-01-31 21:35:39 -08002710 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2711 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2712 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2713 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002714 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002715 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002716 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 // Prepare the position
2718 toView.setTranslationX(0.0f);
2719 toView.setTranslationY(0.0f);
2720 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002721 if (!toAllApps) {
2722 toView.setAlpha(1.0f);
2723 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002724 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002725 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002726 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002727 // If we don't set the final scale values here, if this animation is cancelled
2728 // it will have the wrong scale value and subsequent cameraPan animations will
2729 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002730 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002731 toView.setScaleX(1.0f);
2732 toView.setScaleY(1.0f);
2733 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734 });
2735
Chet Haaseb1254a62010-09-07 13:35:00 -07002736 AnimatorSet toolbarHideAnim = new AnimatorSet();
2737 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2739
2740 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002741 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002743 if (mStateAnimation != null) mStateAnimation.cancel();
2744 mStateAnimation = new AnimatorSet();
2745 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2746 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002747
2748 // Show the new toolbar buttons just as the main animation is ending
2749 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002750 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2751 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752 } else {
2753 toView.setTranslationX(0.0f);
2754 toView.setTranslationY(0.0f);
2755 toView.setScaleX(1.0f);
2756 toView.setScaleY(1.0f);
2757 toView.setVisibility(View.VISIBLE);
2758 hideAndShowToolbarButtons(toState, null, null);
2759 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 }
2761
2762 /**
2763 * Zoom the camera back into the workspace, hiding 'fromView'.
2764 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002765 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2766 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002767 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002768 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002769 cameraZoomIn(fromState, animated, false);
2770 }
2771
2772 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002773 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002774 final boolean fromAllApps = (fromState == State.ALL_APPS);
2775
2776 int duration = fromAllApps ?
2777 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2778 res.getInteger(R.integer.config_customizeZoomOutTime);
2779
Michael Jurka742574b2011-02-02 23:51:01 -08002780 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002781 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2782 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2783
2784 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002785
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002786 mCustomizePagedView.endChoiceMode();
2787 mAllAppsPagedView.endChoiceMode();
2788
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002789 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002790
Michael Jurkad3ef3062010-11-23 16:23:58 -08002791 if (!springLoaded) {
2792 mWorkspace.unshrink(animated);
2793 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794
2795 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002796 if (mStateAnimation != null) mStateAnimation.cancel();
2797 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002798
Michael Jurka742574b2011-02-02 23:51:01 -08002799 final float oldScaleX = fromView.getScaleX();
2800 final float oldScaleY = fromView.getScaleY();
2801
2802 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2803 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2804 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2805 public void onAnimationUpdate(ValueAnimator animation) {
2806 final float b = (Float) animation.getAnimatedValue();
2807 final float a = 1f - b;
2808 ((View)fromView.getParent()).fastInvalidate();
2809 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2810 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2811 }
2812 });
2813 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002814 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohen860f4c52011-01-27 20:16:13 -08002815 alphaAnim.setInterpolator(new DecelerateInterpolator(2.0f));
Michael Jurka742574b2011-02-02 23:51:01 -08002816 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2817 public void onAnimationUpdate(ValueAnimator animation) {
2818 final float b = (Float) animation.getAnimatedValue();
2819 final float a = 1f - b;
2820 // don't need to invalidate because we do so above
2821 fromView.setFastAlpha(a * 1f + b * 0f);
2822 }
2823 });
2824
Michael Jurkacf6125c2011-01-28 15:20:01 -08002825 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002826 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002827 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002828 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002830 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002831 }
2832 });
2833
Chet Haaseb1254a62010-09-07 13:35:00 -07002834 AnimatorSet toolbarHideAnim = new AnimatorSet();
2835 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002836 if (!springLoaded) {
2837 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2838 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839
Adam Cohen61033d32010-11-15 18:29:44 -08002840 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002841
2842 // Show the new toolbar buttons at the very end of the whole animation
2843 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2844 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2846 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 } else {
2848 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002849 if (!springLoaded) {
2850 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2851 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002853 }
2854
2855 /**
2856 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2857 * This is the transition used on xlarge screens to go between all apps and
2858 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002859 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2860 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2861 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002864 final Resources res = getResources();
2865 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 final int workspaceHeight = mWorkspace.getHeight();
2867
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002868 final boolean fromAllApps = (fromState == State.ALL_APPS);
2869 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2870 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002871
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002872 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2873 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2874 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2875 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002876
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002877 mCustomizePagedView.endChoiceMode();
2878 mAllAppsPagedView.endChoiceMode();
2879
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002880 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002881 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002882 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002883 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002884 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002885
2886 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002887 if (mStateAnimation != null) mStateAnimation.cancel();
2888 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002889 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002890 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002891 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002892 toView.setVisibility(View.VISIBLE);
2893 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002894 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002896 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002897 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002898 fromView.setVisibility(View.GONE);
2899 }
2900 });
2901
Chet Haaseb1254a62010-09-07 13:35:00 -07002902 AnimatorSet toolbarHideAnim = new AnimatorSet();
2903 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2905
Chet Haase472b2812010-10-14 07:02:04 -07002906 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2907 fromViewStartY, fromViewEndY);
2908 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002909 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2910 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2911 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2912 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2913 );
Chet Haase472b2812010-10-14 07:02:04 -07002914 fromAnim.setDuration(duration);
2915 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916
2917 // Show the new toolbar buttons just as the main animation is ending
2918 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002919 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2920 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002921 } else {
2922 fromView.setY(fromViewEndY);
2923 fromView.setVisibility(View.GONE);
2924 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002925 toView.setScaleX(1.0f);
2926 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002927 toView.setVisibility(View.VISIBLE);
2928 hideAndShowToolbarButtons(toState, null, null);
2929 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002930 }
2931
2932 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002933 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002934 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002935 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002936
Michael Jurka79212d82010-07-30 16:36:20 -07002937 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002938 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002939 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002940 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002941 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002942 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002943 } else {
2944 mAllAppsGrid.zoom(1.0f, animated);
2945 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002946
Romain Guyc16fea72010-03-12 17:17:56 -08002947 ((View) mAllAppsGrid).setFocusable(true);
2948 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002949
Joe Onorato7c312c12009-08-13 21:36:53 -07002950 // TODO: fade these two too
2951 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002952
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953 // Change the state *after* we've called all the transition code
2954 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002955
Winson Chung5fb63472011-02-02 17:03:37 -08002956 // Pause the auto-advance of widgets until we are out of AllApps
2957 mUserPresent = false;
2958 updateRunning();
2959
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002960 // send an accessibility event to announce the context change
2961 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2962 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002963
2964 void showWorkspace(boolean animated) {
2965 showWorkspace(animated, null);
2966 }
2967
2968 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002969 if (layout != null) {
2970 // always animated, but that's ok since we never specify a layout and
2971 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002972 mWorkspace.unshrink(layout);
2973 } else {
2974 mWorkspace.unshrink(animated);
2975 }
2976 if (mState == State.ALL_APPS) {
2977 closeAllApps(animated);
2978 } else if (mState == State.CUSTOMIZE) {
2979 hideCustomizationDrawer(animated);
2980 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002981
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002982 // Change the state *after* we've called all the transition code
2983 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002984
Winson Chung5fb63472011-02-02 17:03:37 -08002985 // Resume the auto-advance of widgets
2986 mUserPresent = true;
2987 updateRunning();
2988
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002989 // send an accessibility event to announce the context change
2990 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002991 }
2992
Michael Jurkad3ef3062010-11-23 16:23:58 -08002993 void enterSpringLoadedDragMode(CellLayout layout) {
2994 mWorkspace.enterSpringLoadedDragMode(layout);
2995 if (mState == State.ALL_APPS) {
2996 cameraZoomIn(State.ALL_APPS, true, true);
2997 mState = State.ALL_APPS_SPRING_LOADED;
2998 } else if (mState == State.CUSTOMIZE) {
2999 cameraZoomIn(State.CUSTOMIZE, true, true);
3000 mState = State.CUSTOMIZE_SPRING_LOADED;
3001 }/* else {
3002 // we're already in spring loaded mode; don't do anything
3003 }*/
3004 }
3005
3006 void exitSpringLoadedDragMode() {
3007 if (mState == State.ALL_APPS_SPRING_LOADED) {
3008 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3009 cameraZoomOut(State.ALL_APPS, true);
3010 mState = State.ALL_APPS;
3011 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3012 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3013 cameraZoomOut(State.CUSTOMIZE, true);
3014 mState = State.CUSTOMIZE;
3015 }/* else {
3016 // we're not in spring loaded mode; don't do anything
3017 }*/
3018 }
3019
Joe Onoratob2061212009-11-24 16:13:54 -05003020 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003021 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003022 * - Home from workspace
3023 * - from center screen
3024 * - from other screens
3025 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003026 * - from center screen
3027 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003028 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003029 * - from center screen
3030 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003031 * - Launch app from workspace and quit
3032 * - with back
3033 * - with home
3034 * - Launch app from all apps and quit
3035 * - with back
3036 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003037 * - Go to a screen that's not the default, then all
3038 * apps, and launch and app, and go back
3039 * - with back
3040 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003041 * - On workspace, long press power and go back
3042 * - with back
3043 * - with home
3044 * - On all apps, long press power and go back
3045 * - with back
3046 * - with home
3047 * - On workspace, power off
3048 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003049 * - Launch an app and turn off the screen while in that app
3050 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003051 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003052 * - From all apps
3053 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003054 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3055 * - From all apps
3056 * - From the center workspace
3057 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003058 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003059 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003060 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003061 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003062 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003063 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003064 } else {
3065 mAllAppsGrid.zoom(0.0f, animated);
3066 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003067 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003068 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003069 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003070 }
3071
Joe Onorato7c312c12009-08-13 21:36:53 -07003072 void lockAllApps() {
3073 // TODO
3074 }
3075
3076 void unlockAllApps() {
3077 // TODO
3078 }
3079
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003080 // Show the customization drawer (only exists in x-large configuration)
3081 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003082 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003083 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003084 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003085 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003086 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003087 // Change the state *after* we've called all the transition code
3088 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003089
3090 // Pause the auto-advance of widgets until we are out of Customization drawer
3091 mUserPresent = false;
3092 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003093 }
3094
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003095 // Hide the customization drawer (only exists in x-large configuration)
3096 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003097 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003098 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003099 }
3100 }
3101
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003102 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3103 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3104 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003105 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003106 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003107
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003108 void onWorkspaceClick(CellLayout layout) {
3109 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003110 }
3111
Michael Jurka4ef207b2010-11-29 17:05:45 -08003112 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003113 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3115 ImageView button = (ImageView) findViewById(buttonId);
3116 Drawable toolbarIcon = null;
3117 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003118 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003119 // Look for the toolbar icon specified in the activity meta-data
3120 Bundle metaData = packageManager.getActivityInfo(
3121 activityName, PackageManager.GET_META_DATA).metaData;
3122 if (metaData != null) {
3123 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3124 if (iconResId != 0) {
3125 Resources res = packageManager.getResourcesForActivity(activityName);
3126 toolbarIcon = res.getDrawable(iconResId);
3127 }
3128 }
3129 } catch (NameNotFoundException e) {
3130 // Do nothing
3131 }
3132 // If we were unable to find the icon via the meta-data, use a generic one
3133 if (toolbarIcon == null) {
3134 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003135 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003136 } else {
3137 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003138 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003139 }
3140 }
3141
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003142 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003144 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003145 }
3146
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147 private void updateGlobalSearchIcon() {
3148 if (LauncherApplication.isScreenXLarge()) {
3149 final SearchManager searchManager =
3150 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3151 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003152 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003153 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003154 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003155 } else {
3156 findViewById(R.id.search_button).setVisibility(View.GONE);
3157 }
3158 }
3159 }
3160
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003161 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003162 updateButtonWithDrawable(R.id.search_button, d);
3163 }
3164
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003165 private void updateVoiceSearchIcon() {
3166 if (LauncherApplication.isScreenXLarge()) {
3167 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3168 ComponentName activityName = intent.resolveActivity(getPackageManager());
3169 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003170 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003171 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3172 } else {
3173 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003174 }
3175 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003176 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003177
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003178 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003179 updateButtonWithDrawable(R.id.voice_button, d);
3180 }
3181
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003182 /**
3183 * Sets the app market icon (shown when all apps is visible on x-large screens)
3184 */
3185 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003186 if (LauncherApplication.isScreenXLarge()) {
3187 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3188 // Find the app market activity by resolving an intent.
3189 // (If multiple app markets are installed, it will return the ResolverActivity.)
3190 ComponentName activityName = intent.resolveActivity(getPackageManager());
3191 if (activityName != null) {
3192 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003193 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003194 R.id.market_button, activityName, R.drawable.app_market_generic);
3195 }
3196 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003197 }
3198
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003199 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003200 updateButtonWithDrawable(R.id.market_button, d);
3201 }
3202
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003203 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 * Displays the shortcut creation dialog and launches, if necessary, the
3205 * appropriate activity.
3206 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003207 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003208 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3209 DialogInterface.OnShowListener {
3210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3217 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003218 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 builder.setInverseBackgroundForced(true);
3221
3222 AlertDialog dialog = builder.create();
3223 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003224 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003225 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 return dialog;
3228 }
3229
3230 public void onCancel(DialogInterface dialog) {
3231 mWaitingForResult = false;
3232 cleanup();
3233 }
3234
Romain Guycbb89e42009-06-08 15:52:54 -07003235 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003236 }
3237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003239 try {
3240 dismissDialog(DIALOG_CREATE_SHORTCUT);
3241 } catch (Exception e) {
3242 // An exception is thrown if the dialog is not visible, which is fine
3243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 }
3245
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003246 /**
3247 * Handle the action clicked in the "Add to home" dialog.
3248 */
3249 public void onClick(DialogInterface dialog, int which) {
3250 Resources res = getResources();
3251 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003252
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003253 switch (which) {
3254 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003255 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003256 break;
3257 }
Romain Guycbb89e42009-06-08 15:52:54 -07003258
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003259 case AddAdapter.ITEM_APPWIDGET: {
3260 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003261
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003262 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3263 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003264 // start the pick activity
3265 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3266 break;
3267 }
Romain Guycbb89e42009-06-08 15:52:54 -07003268
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003269 case AddAdapter.ITEM_LIVE_FOLDER: {
3270 // Insert extra item to handle inserting folder
3271 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003272
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003273 ArrayList<String> shortcutNames = new ArrayList<String>();
3274 shortcutNames.add(res.getString(R.string.group_folder));
3275 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003276
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003277 ArrayList<ShortcutIconResource> shortcutIcons =
3278 new ArrayList<ShortcutIconResource>();
3279 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3280 R.drawable.ic_launcher_folder));
3281 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3282
3283 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3284 pickIntent.putExtra(Intent.EXTRA_INTENT,
3285 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3286 pickIntent.putExtra(Intent.EXTRA_TITLE,
3287 getText(R.string.title_select_live_folder));
3288 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3291 break;
3292 }
3293
3294 case AddAdapter.ITEM_WALLPAPER: {
3295 startWallpaper();
3296 break;
3297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298 }
3299 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003300
3301 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003302 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003304 }
3305
3306 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003307 * Receives notifications when applications are added/removed.
3308 */
3309 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3310 @Override
3311 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003312 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003313 String reason = intent.getStringExtra("reason");
3314 if (!"homekey".equals(reason)) {
3315 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003316 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003317 animate = false;
3318 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003319 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003320 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003321 }
3322 }
3323
3324 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003325 * Receives notifications whenever the appwidgets are reset.
3326 */
3327 private class AppWidgetResetObserver extends ContentObserver {
3328 public AppWidgetResetObserver() {
3329 super(new Handler());
3330 }
3331
3332 @Override
3333 public void onChange(boolean selfChange) {
3334 onAppWidgetReset();
3335 }
3336 }
3337
3338 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003339 * If the activity is currently paused, signal that we need to re-run the loader
3340 * in onResume.
3341 *
3342 * This needs to be called from incoming places where resources might have been loaded
3343 * while we are paused. That is becaues the Configuration might be wrong
3344 * when we're not running, and if it comes back to what it was when we
3345 * were paused, we are not restarted.
3346 *
3347 * Implementation of the method from LauncherModel.Callbacks.
3348 *
3349 * @return true if we are currently paused. The caller might be able to
3350 * skip some work in that case since we will come back again.
3351 */
3352 public boolean setLoadOnResume() {
3353 if (mPaused) {
3354 Log.i(TAG, "setLoadOnResume");
3355 mOnResumeNeedsLoad = true;
3356 return true;
3357 } else {
3358 return false;
3359 }
3360 }
3361
3362 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003364 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003366 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003367 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003368 } else {
3369 return SCREEN_COUNT / 2;
3370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003372
Winson Chunga12a2502010-12-20 14:41:35 -08003373 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003374 mAllAppsPagedView = view;
3375 }
3376
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 /**
3378 * Refreshes the shortcuts shown on the workspace.
3379 *
3380 * Implementation of the method from LauncherModel.Callbacks.
3381 */
3382 public void startBinding() {
3383 final Workspace workspace = mWorkspace;
3384 int count = workspace.getChildCount();
3385 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003386 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003387 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3388 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003390
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 if (DEBUG_USER_INTERFACE) {
3392 android.widget.Button finishButton = new android.widget.Button(this);
3393 finishButton.setText("Finish");
3394 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3395
3396 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3397 public void onClick(View v) {
3398 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003399 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003401 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003402
3403 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3404 // mDesktopItems -> AppWidgetInfo -> hostView).
3405 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406 }
3407
3408 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 * Bind the items start-end from the list.
3410 *
3411 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3414
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003415 setLoadOnResume();
3416
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 final Workspace workspace = mWorkspace;
3418
3419 for (int i=start; i<end; i++) {
3420 final ItemInfo item = shortcuts.get(i);
3421 mDesktopItems.add(item);
3422 switch (item.itemType) {
3423 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3424 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003425 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3427 false);
3428 break;
3429 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3430 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003431 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003432 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3434 false);
3435 break;
3436 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3437 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3438 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003439 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 (LiveFolderInfo) item);
3441 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3442 false);
3443 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003448 }
3449
Joe Onorato9c1289c2009-08-17 11:03:03 -04003450 /**
3451 * Implementation of the method from LauncherModel.Callbacks.
3452 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003453 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003454 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003455 sFolders.clear();
3456 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458
3459 /**
3460 * Add the views for a widget to the workspace.
3461 *
3462 * Implementation of the method from LauncherModel.Callbacks.
3463 */
3464 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003465 setLoadOnResume();
3466
Daniel Sandler843e8602010-06-07 14:59:01 -04003467 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3468 if (DEBUG_WIDGETS) {
3469 Log.d(TAG, "bindAppWidget: " + item);
3470 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 final Workspace workspace = mWorkspace;
3472
3473 final int appWidgetId = item.appWidgetId;
3474 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003475 if (DEBUG_WIDGETS) {
3476 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3477 }
3478
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3480
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3482 item.hostView.setTag(item);
3483
3484 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3485 item.cellY, item.spanX, item.spanY, false);
3486
Adam Cohended9f8d2010-11-03 13:25:16 -07003487 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 workspace.requestLayout();
3490
3491 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003492
3493 if (DEBUG_WIDGETS) {
3494 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3495 + (SystemClock.uptimeMillis()-start) + "ms");
3496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 }
3498
3499 /**
3500 * Callback saying that there aren't any more items to bind.
3501 *
3502 * Implementation of the method from LauncherModel.Callbacks.
3503 */
3504 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003505 setLoadOnResume();
3506
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 if (mSavedState != null) {
3508 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003509 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 }
3511
3512 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3513 if (userFolders != null) {
3514 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003515 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 if (info != null) {
3517 openFolder(info);
3518 }
3519 }
3520 final Folder openFolder = mWorkspace.getOpenFolder();
3521 if (openFolder != null) {
3522 openFolder.requestFocus();
3523 }
3524 }
3525
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 mSavedState = null;
3527 }
3528
3529 if (mSavedInstanceState != null) {
3530 super.onRestoreInstanceState(mSavedInstanceState);
3531 mSavedInstanceState = null;
3532 }
3533
Winson Chung32bb5e52011-01-31 14:51:56 -08003534 // Workaround a bug that occurs when rotating the device while the customization mode is
3535 // open, we trigger a new layout on all the CellLayout children.
3536 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3537 final int childCount = mWorkspace.getChildCount();
3538 for (int i = 0; i < childCount; ++i) {
3539 mWorkspace.getChildAt(i).requestLayout();
3540 }
3541 }
3542
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 mWorkspaceLoading = false;
3544 }
3545
3546 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003547 * Updates the icons on the launcher that are affected by changes to the package list
3548 * on the device.
3549 */
3550 private void updateIconsAffectedByPackageManagerChanges() {
3551 updateAppMarketIcon();
3552 updateGlobalSearchIcon();
3553 updateVoiceSearchIcon();
3554 }
3555
3556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 * Add the icons for all apps.
3558 *
3559 * Implementation of the method from LauncherModel.Callbacks.
3560 */
3561 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003562 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003563 if (mCustomizePagedView != null) {
3564 mCustomizePagedView.setApps(apps);
3565 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003566 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
3568
3569 /**
3570 * A package was installed.
3571 *
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003574 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003575 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003577 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003578 if (mCustomizePagedView != null) {
3579 mCustomizePagedView.addApps(apps);
3580 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003581 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 }
3583
3584 /**
3585 * A package was updated.
3586 *
3587 * Implementation of the method from LauncherModel.Callbacks.
3588 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003589 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003590 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003592 mWorkspace.updateShortcuts(apps);
3593 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003594 if (mCustomizePagedView != null) {
3595 mCustomizePagedView.updateApps(apps);
3596 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003597 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 }
3599
3600 /**
3601 * A package was uninstalled.
3602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 */
Joe Onorato36115782010-06-17 13:28:48 -04003605 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003607 if (permanent) {
3608 mWorkspace.removeItems(apps);
3609 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003610 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003611 if (mCustomizePagedView != null) {
3612 mCustomizePagedView.removeApps(apps);
3613 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003614 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 }
Joe Onoratobe386092009-11-17 17:32:16 -08003616
3617 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003618 * A number of packages were updated.
3619 */
3620 public void bindPackagesUpdated() {
3621 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003622 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003623 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003625 }
3626
Winson Chung400438b2011-01-16 17:53:48 -08003627 private int mapConfigurationOriActivityInfoOri(int configOri) {
3628 final Display d = getWindowManager().getDefaultDisplay();
3629 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3630 switch (d.getRotation()) {
3631 case Surface.ROTATION_0:
3632 case Surface.ROTATION_180:
3633 // We are currently in the same basic orientation as the natural orientation
3634 naturalOri = configOri;
3635 break;
3636 case Surface.ROTATION_90:
3637 case Surface.ROTATION_270:
3638 // We are currently in the other basic orientation to the natural orientation
3639 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3640 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3641 break;
3642 }
3643
3644 int[] oriMap = {
3645 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3646 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3647 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3648 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3649 };
3650 // Since the map starts at portrait, we need to offset if this device's natural orientation
3651 // is landscape.
3652 int indexOffset = 0;
3653 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3654 indexOffset = 1;
3655 }
3656 return oriMap[(d.getRotation() + indexOffset) % 4];
3657 }
3658 public void lockScreenOrientation() {
3659 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3660 .getConfiguration().orientation));
3661 }
3662 public void unlockScreenOrientation() {
3663 mHandler.postDelayed(new Runnable() {
3664 public void run() {
3665 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3666 }
3667 }, mRestoreScreenOrientationDelay);
3668 }
3669
Winson Chung80baf5a2010-08-09 16:03:15 -07003670 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003671 * Prints out out state for debugging.
3672 */
3673 public void dumpState() {
3674 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003675 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003676 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3677 Log.d(TAG, "mRestoring=" + mRestoring);
3678 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3679 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3680 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003681 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003682 mModel.dumpState();
3683 mAllAppsGrid.dumpState();
3684 Log.d(TAG, "END launcher2 dump state");
3685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003686}