blob: 3e0fcd5d29e8ef607fb6b3f24ac805cd0af8b8af [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080030import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070031import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.Dialog;
34import android.app.SearchManager;
35import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070036import android.appwidget.AppWidgetManager;
37import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080039import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070040import android.content.ClipData;
41import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040042import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080043import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080047import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080048import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080051import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080052import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070054import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040055import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080058import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080060import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070061import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040062import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070063import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020065import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080066import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070067import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040069import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080070import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070071import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070072import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080073import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.text.Selection;
75import android.text.SpannableStringBuilder;
76import android.text.TextUtils;
77import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070078import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080080import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070085import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080086import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080090import android.view.View.OnClickListener;
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 Jurkaafca9532011-02-16 14:50:35 -0800184 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800185 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
Michael Jurka838a4ca2011-02-07 13:33:06 -0800220 private View mButtonCluster;
221 private View mAllAppsButton;
222 private View mDivider;
223 private View mConfigureButton;
224
Winson Chunga12a2502010-12-20 14:41:35 -0800225 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700226 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Bundle mSavedState;
229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private Bundle mSavedInstanceState;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800242 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800243 private boolean mUserPresent = true;
244 private boolean mVisible = false;
245 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700247 private static LocaleConfiguration sLocaleConfiguration = null;
248
Romain Guy84f296c2009-11-04 15:00:44 -0800249 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700250
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700251 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800253 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800254 private ImageView mPreviousView;
255 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500256
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400257 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400258 private String[] mHotseatConfig = null;
259 private Intent[] mHotseats = null;
260 private Drawable[] mHotseatIcons = null;
261 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800279 private static Drawable.ConstantState sGlobalSearchIcon;
280 private static Drawable.ConstantState sVoiceSearchIcon;
281 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Michael Jurkaddd62e92011-02-16 17:49:14 -0800283 private BubbleTextView mWaitingForResume;
284
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285 private static ArrayList<PendingAddArguments> sPendingAddList
286 = new ArrayList<PendingAddArguments>();
287
288 private static class PendingAddArguments {
289 int requestCode;
290 Intent intent;
291 int screen;
292 int cellX;
293 int cellY;
294 }
295
Winson Chunga12a2502010-12-20 14:41:35 -0800296 private CustomizationType getCustomizeFilterForTabTag(String tag) {
297 if (tag.equals(WIDGETS_TAG)) {
298 return CustomizationType.WidgetCustomization;
299 } else if (tag.equals(APPLICATIONS_TAG)) {
300 return CustomizationType.ApplicationCustomization;
301 } else if (tag.equals(WALLPAPERS_TAG)) {
302 return CustomizePagedView.CustomizationType.WallpaperCustomization;
303 } else if (tag.equals(SHORTCUTS_TAG)) {
304 return CustomizePagedView.CustomizationType.ShortcutCustomization;
305 }
306 return CustomizationType.WidgetCustomization;
307 }
308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 @Override
310 protected void onCreate(Bundle savedInstanceState) {
311 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700312
Winson Chungaafa03c2010-06-11 17:34:16 -0700313 if (LauncherApplication.isInPlaceRotationEnabled()) {
314 // hide the status bar (temporary until we get the status bar design figured out)
315 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
316 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
317 }
318
Joe Onorato0589f0f2010-02-08 13:44:00 -0800319 LauncherApplication app = ((LauncherApplication)getApplication());
320 mModel = app.setLauncher(this);
321 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400322 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700324
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700325 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700326 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
327 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200330 android.os.Debug.startMethodTracing(
331 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
333
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400334 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800337 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700338 if (mHomeCustomizationDrawer != null) {
339 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700340
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700342 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800343 R.layout.customization_drawer_tab_contents, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700344 TabContentFactory contentFactory = new TabContentFactory() {
345 public View createTabContent(String tag) {
346 return mCustomizePagedView;
347 }
348 };
349
Winson Chung49767ae2010-11-29 14:48:30 -0800350
351 TextView tabView;
352 TabWidget tabWidget = (TabWidget)
353 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
354
355 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
356 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800358 .setIndicator(tabView).setContent(contentFactory));
359 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
360 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700361 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800362 .setIndicator(tabView).setContent(contentFactory));
363 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
364 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700365 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800366 .setIndicator(tabView).setContent(contentFactory));
367 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
368 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700369 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800370 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700371 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
372 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800373 final CustomizePagedView.CustomizationType newType =
374 getCustomizeFilterForTabTag(tabId);
375 if (newType != mCustomizePagedView.getCustomizationFilter()) {
376 // animate the changing of the tab content by fading pages in and out
377 final Resources res = getResources();
378 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
379 final float alpha = mCustomizePagedView.getAlpha();
380 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
381 "alpha", alpha, 0.0f);
382 alphaAnim.setDuration(duration);
383 alphaAnim.addListener(new AnimatorListenerAdapter() {
384 @Override
385 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800386 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 final float alpha = mCustomizePagedView.getAlpha();
389 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
390 mCustomizePagedView, "alpha", alpha, 1.0f);
391 alphaAnim.setDuration(duration);
392 alphaAnim.start();
393 }
394 });
395 alphaAnim.start();
396 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700397 }
398 });
Michael Jurka946ad472010-07-09 18:05:18 -0700399 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 setupViews();
401
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800402 registerContentObservers();
403
Joe Onorato7c312c12009-08-13 21:36:53 -0700404 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 mSavedState = savedInstanceState;
407 restoreState(mSavedState);
408
Winson Chunga12a2502010-12-20 14:41:35 -0800409 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800410 if (mCustomizePagedView != null) {
411 mCustomizePagedView.update();
412 }
Winson Chunga12a2502010-12-20 14:41:35 -0800413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 if (PROFILE_STARTUP) {
415 android.os.Debug.stopMethodTracing();
416 }
417
418 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400419 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421
422 // For handling default keys
423 mDefaultKeySsb = new SpannableStringBuilder();
424 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800425
426 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
427 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800428
429 // If we have a saved version of these external icons, we load them up immediately
430 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800431 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
432 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800433 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 }
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800437 @Override
438 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
439 super.dispatchPopulateAccessibilityEvent(event);
440
441 // we want to take over text population so it is context dependent
442 event.getText().clear();
443 if (mState == State.ALL_APPS) {
444 event.getText().add(getString(R.string.all_apps_button_label));
445 } else if (mState == State.WORKSPACE) {
446 event.getText().add(getString(R.string.all_apps_home_button_label));
447 }
448
449 return true;
450 }
451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700453 if (sLocaleConfiguration == null) {
454 new AsyncTask<Void, Void, LocaleConfiguration>() {
455 @Override
456 protected LocaleConfiguration doInBackground(Void... unused) {
457 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
458 readConfiguration(Launcher.this, localeConfiguration);
459 return localeConfiguration;
460 }
461
462 @Override
463 protected void onPostExecute(LocaleConfiguration result) {
464 sLocaleConfiguration = result;
465 checkForLocaleChange(); // recursive, but now with a locale configuration
466 }
467 }.execute();
468 return;
469 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 final Configuration configuration = getResources().getConfiguration();
472
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700473 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 final String locale = configuration.locale.toString();
475
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700476 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 final int mcc = configuration.mcc;
478
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final int mnc = configuration.mnc;
481
Romain Guy84f296c2009-11-04 15:00:44 -0800482 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483
Romain Guy84f296c2009-11-04 15:00:44 -0800484 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700485 sLocaleConfiguration.locale = locale;
486 sLocaleConfiguration.mcc = mcc;
487 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700488
Joe Onorato0589f0f2010-02-08 13:44:00 -0800489 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400490 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700491
492 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
493 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700494 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 public void run() {
496 writeConfiguration(Launcher.this, localeConfiguration);
497 }
498 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700499 }
500 }
501
502 private static class LocaleConfiguration {
503 public String locale;
504 public int mcc = -1;
505 public int mnc = -1;
506 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700507
Romain Guy98d01652009-06-30 16:21:04 -0700508 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
509 DataInputStream in = null;
510 try {
511 in = new DataInputStream(context.openFileInput(PREFERENCES));
512 configuration.locale = in.readUTF();
513 configuration.mcc = in.readInt();
514 configuration.mnc = in.readInt();
515 } catch (FileNotFoundException e) {
516 // Ignore
517 } catch (IOException e) {
518 // Ignore
519 } finally {
520 if (in != null) {
521 try {
522 in.close();
523 } catch (IOException e) {
524 // Ignore
525 }
526 }
527 }
528 }
529
530 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
531 DataOutputStream out = null;
532 try {
533 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
534 out.writeUTF(configuration.locale);
535 out.writeInt(configuration.mcc);
536 out.writeInt(configuration.mnc);
537 out.flush();
538 } catch (FileNotFoundException e) {
539 // Ignore
540 } catch (IOException e) {
541 //noinspection ResultOfMethodCallIgnored
542 context.getFileStreamPath(PREFERENCES).delete();
543 } finally {
544 if (out != null) {
545 try {
546 out.close();
547 } catch (IOException e) {
548 // Ignore
549 }
550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 }
552 }
553
554 static int getScreen() {
555 synchronized (sLock) {
556 return sScreen;
557 }
558 }
559
560 static void setScreen(int screen) {
561 synchronized (sLock) {
562 sScreen = screen;
563 }
564 }
565
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400566 // Note: This doesn't do all the client-id magic that BrowserProvider does
567 // in Browser. (http://b/2425179)
568 private Uri getDefaultBrowserUri() {
569 String url = getString(R.string.default_browser_url);
570 if (url.indexOf("{CID}") != -1) {
571 url = url.replace("{CID}", "android-google");
572 }
573 return Uri.parse(url);
574 }
575
576 // Load the Intent templates from arrays.xml to populate the hotseats. For
577 // each Intent, if it resolves to a single app, use that as the launch
578 // intent & use that app's label as the contentDescription. Otherwise,
579 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400581 if (mHotseatConfig == null) {
582 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
583 if (mHotseatConfig.length > 0) {
584 mHotseats = new Intent[mHotseatConfig.length];
585 mHotseatLabels = new CharSequence[mHotseatConfig.length];
586 mHotseatIcons = new Drawable[mHotseatConfig.length];
587 } else {
588 mHotseats = null;
589 mHotseatIcons = null;
590 mHotseatLabels = null;
591 }
592
593 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
594 for (int i=0; i<mHotseatConfig.length; i++) {
595 // load icon for this slot; currently unrelated to the actual activity
596 try {
597 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
598 } catch (ArrayIndexOutOfBoundsException ex) {
599 Log.w(TAG, "Missing hotseat_icons array item #" + i);
600 mHotseatIcons[i] = null;
601 }
602 }
603 hotseatIconDrawables.recycle();
604 }
605
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400606 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400607 for (int i=0; i<mHotseatConfig.length; i++) {
608 Intent intent = null;
609 if (mHotseatConfig[i].equals("*BROWSER*")) {
610 // magic value meaning "launch user's default web browser"
611 // replace it with a generic web request so we can see if there is indeed a default
612 String defaultUri = getString(R.string.default_browser_url);
613 intent = new Intent(
614 Intent.ACTION_VIEW,
615 ((defaultUri != null)
616 ? Uri.parse(defaultUri)
617 : getDefaultBrowserUri())
618 ).addCategory(Intent.CATEGORY_BROWSABLE);
619 // note: if the user launches this without a default set, she
620 // will always be taken to the default URL above; this is
621 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700622 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400623 // URL is unavoidably sent to the chosen app.
624 } else {
625 try {
626 intent = Intent.parseUri(mHotseatConfig[i], 0);
627 } catch (java.net.URISyntaxException ex) {
628 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
629 // bogus; leave intent=null
630 }
631 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700632
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400633 if (intent == null) {
634 mHotseats[i] = null;
635 mHotseatLabels[i] = getText(R.string.activity_not_found);
636 continue;
637 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638
639 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700640 Log.d(TAG, "loadHotseats: hotseat " + i
641 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400643 + "]");
644 }
645
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400646 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
647 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700648 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400649 Log.d(TAG, "Best match for intent: " + bestMatch);
650 Log.d(TAG, "All matches: ");
651 for (ResolveInfo ri : allMatches) {
652 Log.d(TAG, " --> " + ri);
653 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400654 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 // did this resolve to a single app, or the resolver?
656 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700657 // can't find any activity to handle this. let's leave the
658 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400659 // to launch.
660 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400661
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400662 // set accessibility text to "Not installed"
663 mHotseatLabels[i] = getText(R.string.activity_not_found);
664 } else {
665 boolean found = false;
666 for (ResolveInfo ri : allMatches) {
667 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
668 && bestMatch.activityInfo.applicationInfo.packageName
669 .equals(ri.activityInfo.applicationInfo.packageName)) {
670 found = true;
671 break;
672 }
673 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700674
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400675 if (!found) {
676 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
677 // the bestMatch is probably the ResolveActivity, meaning the
678 // user has not yet selected a default
679 // so: we'll keep the original intent for now
680 mHotseats[i] = intent;
681
682 // set the accessibility text to "Select shortcut"
683 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
684 } else {
685 // we have an app!
686 // now reconstruct the intent to launch it through the front
687 // door
688 ComponentName com = new ComponentName(
689 bestMatch.activityInfo.applicationInfo.packageName,
690 bestMatch.activityInfo.name);
691 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
692
693 // load the app label for accessibility
694 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
695 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400696 }
697
698 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700699 Log.d(TAG, "loadHotseats: hotseat " + i
700 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400701 + ((mHotseats[i] == null)
702 ? "null"
703 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400704 + "] label=[" + mHotseatLabels[i]
705 + "]"
706 );
707 }
708 }
709 }
710
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800711 private void completeAdd(PendingAddArguments args) {
712 switch (args.requestCode) {
713 case REQUEST_PICK_APPLICATION:
714 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
715 break;
716 case REQUEST_PICK_SHORTCUT:
717 processShortcut(args.intent);
718 break;
719 case REQUEST_CREATE_SHORTCUT:
720 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
721 break;
722 case REQUEST_PICK_LIVE_FOLDER:
723 addLiveFolder(args.intent);
724 break;
725 case REQUEST_CREATE_LIVE_FOLDER:
726 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
727 break;
728 case REQUEST_PICK_APPWIDGET:
729 addAppWidgetFromPick(args.intent);
730 break;
731 case REQUEST_CREATE_APPWIDGET:
732 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
733 completeAddAppWidget(appWidgetId, args.screen);
734 break;
735 case REQUEST_PICK_WALLPAPER:
736 // We just wanted the activity result here so we can clear mWaitingForResult
737 break;
738 }
739 }
740
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700743 mWaitingForResult = false;
744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 // The pattern used here is that a user PICKs a specific application,
746 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
749 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700750
Michael Jurka0280c3b2010-09-17 15:00:07 -0700751 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 final PendingAddArguments args = new PendingAddArguments();
753 args.requestCode = requestCode;
754 args.intent = data;
755 args.screen = mAddScreen;
756 args.cellX = mAddIntersectCellX;
757 args.cellY = mAddIntersectCellY;
758
759 // If the loader is still running, defer the add until it is done.
760 if (isWorkspaceLocked()) {
761 sPendingAddList.add(args);
762 } else {
763 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
Romain Guy18042c82009-11-06 11:44:55 -0800765 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
766 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
767 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700768 // Clean up the appWidgetId if we canceled
769 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
770 if (appWidgetId != -1) {
771 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775
776 @Override
777 protected void onResume() {
778 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800779 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700780 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781 mWorkspaceLoading = true;
782 mModel.startLoader(this, true);
783 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700784 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800786 if (mWaitingForResume != null) {
787 mWaitingForResume.setStayPressed(false);
788 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700789 // When we resume Launcher, a different Activity might be responsible for the app
790 // market intent, so refresh the icon
791 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 }
793
794 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700795 protected void onPause() {
796 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700797 // Some launcher layouts don't have a previous and next view
798 if (mPreviousView != null) {
799 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700800 }
801 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700802 dismissPreview(mNextView);
803 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700804 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800805 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700806 }
Romain Guycbb89e42009-06-08 15:52:54 -0700807
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700808 @Override
809 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 // Flag the loader to stop early before switching
811 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800812 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800813 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700814 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700815
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800816 // We can't hide the IME if it was forced open. So don't bother
817 /*
818 @Override
819 public void onWindowFocusChanged(boolean hasFocus) {
820 super.onWindowFocusChanged(hasFocus);
821
822 if (hasFocus) {
823 final InputMethodManager inputManager = (InputMethodManager)
824 getSystemService(Context.INPUT_METHOD_SERVICE);
825 WindowManager.LayoutParams lp = getWindow().getAttributes();
826 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
827 android.os.Handler()) {
828 protected void onReceiveResult(int resultCode, Bundle resultData) {
829 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
830 }
831 });
832 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
833 }
834 }
835 */
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 private boolean acceptFilter() {
838 final InputMethodManager inputManager = (InputMethodManager)
839 getSystemService(Context.INPUT_METHOD_SERVICE);
840 return !inputManager.isFullscreenMode();
841 }
842
843 @Override
844 public boolean onKeyDown(int keyCode, KeyEvent event) {
845 boolean handled = super.onKeyDown(keyCode, event);
846 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
847 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
848 keyCode, event);
849 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700850 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700851 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700852 // showSearchDialog()
853 // If there are multiple keystrokes before the search dialog takes focus,
854 // onSearchRequested() will be called for every keystroke,
855 // but it is idempotent, so it's fine.
856 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
858 }
859
Joe Onorato8a9625e2010-01-28 15:55:35 -0800860 // Eat the long press event so the keyboard doesn't come up.
861 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
862 return true;
863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 return handled;
866 }
867
Karl Rosaen138a0412009-04-23 19:00:21 -0700868 private String getTypedText() {
869 return mDefaultKeySsb.toString();
870 }
871
872 private void clearTypedText() {
873 mDefaultKeySsb.clear();
874 mDefaultKeySsb.clearSpans();
875 Selection.setSelection(mDefaultKeySsb, 0);
876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700879 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
880 * State
881 */
882 private static State intToState(int stateOrdinal) {
883 State state = State.WORKSPACE;
884 final State[] stateValues = State.values();
885 for (int i = 0; i < stateValues.length; i++) {
886 if (stateValues[i].ordinal() == stateOrdinal) {
887 state = stateValues[i];
888 break;
889 }
890 }
891 return state;
892 }
893
894 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 * Restores the previous state, if it exists.
896 *
897 * @param savedState The previous state.
898 */
899 private void restoreState(Bundle savedState) {
900 if (savedState == null) {
901 return;
902 }
903
Michael Jurka883f55b2010-10-21 15:47:14 -0700904 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
905
906 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800907 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700908 } else if (state == State.CUSTOMIZE) {
909 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800910 }
911
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
913 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700914 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 }
916
917 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700920 mAddScreen = addScreen;
921 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
922 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 mRestoring = true;
924 }
925
926 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
927 if (renameFolder) {
928 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700929 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 mRestoring = true;
931 }
Winson Chunga12a2502010-12-20 14:41:35 -0800932
933 // Restore the current AllApps drawer tab
934 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
935 String curTab = savedState.getString("allapps_currentTab");
936 if (curTab != null) {
937 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
938 tabhost.setCurrentTabByTag(curTab);
939 }
940 int curPage = savedState.getInt("allapps_currentPage", -1);
941 if (curPage > -1) {
942 mAllAppsPagedView.setRestorePage(curPage);
943 }
944 }
945
946 // Restore the current customization drawer tab
947 if (mHomeCustomizationDrawer != null) {
948 String curTab = savedState.getString("customize_currentTab");
949 if (curTab != null) {
950 // We set this directly so that there is no delay before the tab is set
951 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
952 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
953 }
954
955 // Note: currently we do not restore the page for the customization tray because unlike
956 // AllApps, the page content can change drastically
957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 /**
961 * Finds all the views we need and configure them properly.
962 */
963 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700964 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400965
Romain Guyb1b69f52009-08-10 15:10:15 -0700966 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400967 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968
Joe Onorato7c312c12009-08-13 21:36:53 -0700969 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700970 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700971 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800972 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700973 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700974 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700975
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700976 if (LauncherApplication.isScreenXLarge()) {
977 // They need to be INVISIBLE initially so that they will be measured in the layout.
978 // Otherwise the animations are messed up when we show them for the first time.
979 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
980 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
981 }
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500986 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700987
Joe Onorato7c312c12009-08-13 21:36:53 -0700988 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
989 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700991 View handleView = findViewById(R.id.all_apps_button);
992 if (handleView != null && handleView instanceof HandleView) {
993 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700994 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700995 mHandleView.setLauncher(this);
996 mHandleView.setOnClickListener(this);
997 mHandleView.setOnLongClickListener(this);
998 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800999
Winson Chung80baf5a2010-08-09 16:03:15 -07001000 if (mCustomizePagedView != null) {
1001 mCustomizePagedView.setLauncher(this);
1002 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -07001003 } else {
1004 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
1005 hotseatLeft.setContentDescription(mHotseatLabels[0]);
1006 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
1007 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
1008 hotseatRight.setContentDescription(mHotseatLabels[1]);
1009 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001010
Michael Jurkaaf442092010-06-10 17:01:57 -07001011 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
1012 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001013
Michael Jurkaaf442092010-06-10 17:01:57 -07001014 Drawable previous = mPreviousView.getDrawable();
1015 Drawable next = mNextView.getDrawable();
1016 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017
Michael Jurkaaf442092010-06-10 17:01:57 -07001018 mPreviousView.setHapticFeedbackEnabled(false);
1019 mPreviousView.setOnLongClickListener(this);
1020 mNextView.setHapticFeedbackEnabled(false);
1021 mNextView.setOnLongClickListener(this);
1022 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001025 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001027 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028
1029 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001030 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001031
1032 final View allAppsButton = findViewById(R.id.all_apps_button);
1033 final View divider = findViewById(R.id.divider);
1034 final View configureButton = findViewById(R.id.configure_button);
1035
Michael Jurka54f7ac32010-08-02 13:56:46 -07001036 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001037 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001038 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001039 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001040 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001041 dragController.addDragListener(deleteZone);
1042
Adam Cohencdc30d52010-12-01 15:09:47 -08001043 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1044 if (allAppsDeleteZone != null) {
1045 allAppsDeleteZone.setLauncher(this);
1046 allAppsDeleteZone.setDragController(dragController);
1047 allAppsDeleteZone.setDragAndDropEnabled(false);
1048 dragController.addDragListener(allAppsDeleteZone);
1049 dragController.addDropTarget(allAppsDeleteZone);
1050 }
1051
1052 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1053 findViewById(R.id.all_apps_info_target);
1054 if (allAppsInfoTarget != null) {
1055 allAppsInfoTarget.setLauncher(this);
1056 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001057 allAppsInfoTarget.setDragAndDropEnabled(false);
1058 View marketButton = findViewById(R.id.market_button);
1059 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001060 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08001061 marketButton.setOnClickListener(new OnClickListener() {
1062 public void onClick(View v) {
1063 onClickAppMarketButton(v);
1064 }
1065 });
Adam Cohencdc30d52010-12-01 15:09:47 -08001066 }
1067 }
1068
Joe Onorato00acb122009-08-04 16:04:30 -04001069 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001070 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001071 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001072
Joe Onorato00acb122009-08-04 16:04:30 -04001073 // The order here is bottom to top.
1074 dragController.addDropTarget(workspace);
1075 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001076 if (allAppsInfoTarget != null) {
1077 dragController.addDropTarget(allAppsInfoTarget);
1078 }
1079 if (allAppsDeleteZone != null) {
1080 dragController.addDropTarget(allAppsDeleteZone);
1081 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001082 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1083
1084 mAllAppsButton = findViewById(R.id.all_apps_button);
1085 mDivider = findViewById(R.id.divider);
1086 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001087
1088 // We had previously set these click handlers in XML, but the first time we launched
1089 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1090 // found the right handler. Setting the handlers directly here eliminates that cost.
1091 if (mConfigureButton != null) {
1092 mConfigureButton.setOnClickListener(new OnClickListener() {
1093 public void onClick(View v) {
1094 onClickConfigureButton(v);
1095 }
1096 });
1097 }
1098 if (mDivider != null) {
1099 mDivider.setOnClickListener(new OnClickListener() {
1100 public void onClick(View v) {
1101 onClickAllAppsButton(v);
1102 }
1103 });
1104 }
1105 if (mAllAppsButton != null) {
1106 mAllAppsButton.setOnClickListener(new OnClickListener() {
1107 public void onClick(View v) {
1108 onClickAllAppsButton(v);
1109 }
1110 });
1111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113
Romain Guy8a73c512009-11-09 19:19:59 -08001114 @SuppressWarnings({"UnusedDeclaration"})
1115 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001116 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001117 mWorkspace.scrollLeft();
1118 }
Romain Guy8a73c512009-11-09 19:19:59 -08001119 }
1120
1121 @SuppressWarnings({"UnusedDeclaration"})
1122 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001123 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001124 mWorkspace.scrollRight();
1125 }
Romain Guy8a73c512009-11-09 19:19:59 -08001126 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001127
1128 @SuppressWarnings({"UnusedDeclaration"})
1129 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001130 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001131
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001132 int index = -1;
1133 if (v.getId() == R.id.hotseat_left) {
1134 index = 0;
1135 } else if (v.getId() == R.id.hotseat_right) {
1136 index = 1;
1137 }
1138
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001139 // reload these every tap; you never know when they might change
1140 loadHotseats();
1141 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001142 startActivitySafely(
1143 mHotseats[index],
1144 "hotseat"
1145 );
1146 }
1147 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 /**
1150 * Creates a view representing a shortcut.
1151 *
1152 * @param info The data structure describing the shortcut.
1153 *
1154 * @return A View inflated from R.layout.application.
1155 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001156 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001158 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160
1161 /**
1162 * Creates a view representing a shortcut inflated from the specified resource.
1163 *
1164 * @param layoutResId The id of the XML layout used to create the shortcut.
1165 * @param parent The group the shortcut belongs to.
1166 * @param info The data structure describing the shortcut.
1167 *
1168 * @return A View inflated from layoutResId.
1169 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001170 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001171 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1172 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 return favorite;
1175 }
1176
1177 /**
1178 * Add an application shortcut to the workspace.
1179 *
1180 * @param data The intent describing the application.
1181 * @param cellInfo The position on screen where to create the shortcut.
1182 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001183 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 int intersectCellX, int intersectCellY) {
1185 final int[] cellXY = mTmpAddItemCellCoordinates;
1186 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1187
1188 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1189 showOutOfSpaceMessage();
1190 return;
1191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001193 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001194
Romain Guy73b979d2009-06-09 12:57:21 -07001195 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001196 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001198 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001199 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1200 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001201 } else {
1202 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
1204 }
Romain Guycbb89e42009-06-08 15:52:54 -07001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 /**
1207 * Add a shortcut to the workspace.
1208 *
1209 * @param data The intent describing the shortcut.
1210 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001212 private void completeAddShortcut(Intent data, int screen,
1213 int intersectCellX, int intersectCellY) {
1214 final int[] cellXY = mTmpAddItemCellCoordinates;
1215 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001217 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001218 boolean foundCellSpan = false;
1219 if (touchXY != null) {
1220 // when dragging and dropping, just find the closest free spot
1221 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1222 int[] result = screenLayout.findNearestVacantArea(
1223 touchXY[0], touchXY[1], 1, 1, cellXY);
1224 foundCellSpan = (result != null);
1225 } else {
1226 foundCellSpan = layout.findCellForSpanThatIntersects(
1227 cellXY, 1, 1, intersectCellX, intersectCellY);
1228 }
1229
1230 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 showOutOfSpaceMessage();
1232 return;
1233 }
1234
1235 final ShortcutInfo info = mModel.addShortcut(
1236 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
1238 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242 }
1243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001247 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001248 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 */
Winson Chungf7640c82011-02-28 13:47:29 -08001250 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001254 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1255 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001258 // We have saved the position to which the widget was dragged-- this really only matters
1259 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001261
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001262 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 boolean foundCellSpan = false;
1264 if (touchXY != null) {
1265 // when dragging and dropping, just find the closest free spot
1266 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1267 int[] result = screenLayout.findNearestVacantArea(
1268 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001269 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001270 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001271 // if we long pressed on an empty cell to bring up a menu,
1272 // make sure we intersect the empty cell
1273 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1274 // findCellForSpanThatIntersects will just ignore them
1275 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1276 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001277 }
1278
Michael Jurka0280c3b2010-09-17 15:00:07 -07001279 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001280 if (appWidgetId != -1) {
1281 // Deleting an app widget ID is a void call but writes to disk before returning
1282 // to the caller...
1283 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1284 new Thread("deleteAppWidgetId") {
1285 public void run() {
1286 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1287 }
1288 }.start();
1289 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001290 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001291 return;
1292 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001294 // Build Launcher-specific widget info and save to database
1295 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001296 launcherInfo.spanX = spanXY[0];
1297 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 LauncherModel.addItemToDatabase(this, launcherInfo,
1300 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302
1303 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001307 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001308
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001309 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001311
Michael Jurka0280c3b2010-09-17 15:00:07 -07001312 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001313 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001314
1315 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317 }
Romain Guycbb89e42009-06-08 15:52:54 -07001318
Adam Cohended9f8d2010-11-03 13:25:16 -07001319 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1320 @Override
1321 public void onReceive(Context context, Intent intent) {
1322 final String action = intent.getAction();
1323 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1324 mUserPresent = false;
1325 updateRunning();
1326 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1327 mUserPresent = true;
1328 updateRunning();
1329 }
1330 }
1331 };
1332
1333 @Override
1334 public void onAttachedToWindow() {
1335 super.onAttachedToWindow();
1336
1337 // Listen for broadcasts related to user-presence
1338 final IntentFilter filter = new IntentFilter();
1339 filter.addAction(Intent.ACTION_SCREEN_OFF);
1340 filter.addAction(Intent.ACTION_USER_PRESENT);
1341 registerReceiver(mReceiver, filter);
1342
Adam Cohend113e0c2010-11-11 10:48:05 -08001343 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001344 mVisible = true;
1345 }
1346
1347 @Override
1348 public void onDetachedFromWindow() {
1349 super.onDetachedFromWindow();
1350 mVisible = false;
1351
Adam Cohend113e0c2010-11-11 10:48:05 -08001352 if (mAttached) {
1353 unregisterReceiver(mReceiver);
1354 mAttached = false;
1355 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001356 updateRunning();
1357 }
1358
1359 public void onWindowVisibilityChanged(int visibility) {
1360 mVisible = visibility == View.VISIBLE;
1361 updateRunning();
1362 }
1363
1364 private void sendAdvanceMessage(long delay) {
1365 mHandler.removeMessages(ADVANCE_MSG);
1366 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1367 mHandler.sendMessageDelayed(msg, delay);
1368 mAutoAdvanceSentTime = System.currentTimeMillis();
1369 }
1370
1371 private void updateRunning() {
1372 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1373 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1374 mAutoAdvanceRunning = autoAdvanceRunning;
1375 if (autoAdvanceRunning) {
1376 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1377 sendAdvanceMessage(delay);
1378 } else {
1379 if (!mWidgetsToAdvance.isEmpty()) {
1380 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1381 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1382 }
1383 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001384 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001385 }
1386 }
1387 }
1388
1389 private final Handler mHandler = new Handler() {
1390 @Override
1391 public void handleMessage(Message msg) {
1392 if (msg.what == ADVANCE_MSG) {
1393 int i = 0;
1394 for (View key: mWidgetsToAdvance.keySet()) {
1395 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1396 final int delay = mAdvanceStagger * i;
1397 if (v instanceof Advanceable) {
1398 postDelayed(new Runnable() {
1399 public void run() {
1400 ((Advanceable) v).advance();
1401 }
1402 }, delay);
1403 }
1404 i++;
1405 }
1406 sendAdvanceMessage(mAdvanceInterval);
1407 }
1408 }
1409 };
1410
1411 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1412 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1413 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1414 if (v instanceof Advanceable) {
1415 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001416 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001417 updateRunning();
1418 }
1419 }
1420
1421 void removeWidgetToAutoAdvance(View hostView) {
1422 if (mWidgetsToAdvance.containsKey(hostView)) {
1423 mWidgetsToAdvance.remove(hostView);
1424 updateRunning();
1425 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 }
1427
Joe Onorato9c1289c2009-08-17 11:03:03 -04001428 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1429 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001430 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 launcherInfo.hostView = null;
1432 }
1433
Adam Cohended9f8d2010-11-03 13:25:16 -07001434 void showOutOfSpaceMessage() {
1435 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1436 }
1437
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001438 public LauncherAppWidgetHost getAppWidgetHost() {
1439 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
Romain Guycbb89e42009-06-08 15:52:54 -07001441
Winson Chunga9abd0e2010-10-27 17:18:37 -07001442 public LauncherModel getModel() {
1443 return mModel;
1444 }
1445
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001446 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001447 getWindow().closeAllPanels();
1448
1449 try {
1450 dismissDialog(DIALOG_CREATE_SHORTCUT);
1451 // Unlock the workspace if the dialog was showing
1452 } catch (Exception e) {
1453 // An exception is thrown if the dialog is not visible, which is fine
1454 }
1455
1456 try {
1457 dismissDialog(DIALOG_RENAME_FOLDER);
1458 // Unlock the workspace if the dialog was showing
1459 } catch (Exception e) {
1460 // An exception is thrown if the dialog is not visible, which is fine
1461 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001462
1463 // Whatever we were doing is hereby canceled.
1464 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001465 }
1466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 @Override
1468 protected void onNewIntent(Intent intent) {
1469 super.onNewIntent(intent);
1470
1471 // Close the menu
1472 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001473 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001474 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001475
Joe Onorato14f122b2009-11-19 14:06:36 -08001476 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1477 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001478
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001479 // In all these cases, only animate if we're already on home
1480
Michael Jurka4cb37242010-08-09 21:05:32 -07001481 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001482 mWorkspace.unshrink(alreadyOnHome);
1483 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001484
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001485 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001486 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001487 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001488 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001489
Joe Onorato3a8820b2009-11-10 15:06:42 -08001490 final View v = getWindow().peekDecorView();
1491 if (v != null && v.getWindowToken() != null) {
1492 InputMethodManager imm = (InputMethodManager)getSystemService(
1493 INPUT_METHOD_SERVICE);
1494 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496 }
1497 }
1498
1499 @Override
1500 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1501 // Do not call super here
1502 mSavedInstanceState = savedInstanceState;
1503 }
1504
1505 @Override
1506 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001507 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508
1509 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1510 if (folders.size() > 0) {
1511 final int count = folders.size();
1512 long[] ids = new long[count];
1513 for (int i = 0; i < count; i++) {
1514 final FolderInfo info = folders.get(i).getInfo();
1515 ids[i] = info.id;
1516 }
1517 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1518 } else {
1519 super.onSaveInstanceState(outState);
1520 }
1521
Michael Jurka883f55b2010-10-21 15:47:14 -07001522 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 if (mAddScreen > -1 && mWaitingForResult) {
1525 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1526 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1527 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
1530 if (mFolderInfo != null && mWaitingForResult) {
1531 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1532 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1533 }
Michael Jurka946ad472010-07-09 18:05:18 -07001534
Winson Chung43b119d2010-12-06 16:45:05 -08001535 // Save the current AllApps drawer tab
1536 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1537 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1538 String currentTabTag = tabhost.getCurrentTabTag();
1539 if (currentTabTag != null) {
1540 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001541 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001542 }
1543 }
1544
1545 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001546 if (mHomeCustomizationDrawer != null) {
1547 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1548 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001549 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001550 }
1551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
1554 @Override
1555 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001559 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001561 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001563 mAppWidgetHost = null;
1564
1565 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
1567 TextKeyListener.getInstance().release();
1568
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001572
1573 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001574
Patrick Dubroyab962b72010-07-26 12:10:10 -07001575 // Some launcher layouts don't have a previous and next view
1576 if (mPreviousView != null) {
1577 dismissPreview(mPreviousView);
1578 }
1579 if (mNextView != null) {
1580 dismissPreview(mNextView);
1581 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001582
1583 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001584
1585 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1586 mWorkspace.removeAllViews();
1587 mWorkspace = null;
1588 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001589
1590 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592
1593 @Override
1594 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001595 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 super.startActivityForResult(intent, requestCode);
1597 }
1598
1599 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001600 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001602
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001603 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001604
Karl Rosaen138a0412009-04-23 19:00:21 -07001605 if (initialQuery == null) {
1606 // Use any text typed in the launcher as the initial query
1607 initialQuery = getTypedText();
1608 clearTypedText();
1609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 if (appSearchData == null) {
1611 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001612 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 }
Romain Guycbb89e42009-06-08 15:52:54 -07001614
Karl Rosaen138a0412009-04-23 19:00:21 -07001615 final SearchManager searchManager =
1616 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001617 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001618 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
1620
1621 @Override
1622 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001623 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 return false;
1625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626
1627 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1630 .setIcon(android.R.drawable.ic_menu_add)
1631 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001632 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1633 .setIcon(android.R.drawable.ic_menu_manage)
1634 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001635 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 .setIcon(android.R.drawable.ic_menu_gallery)
1637 .setAlphabeticShortcut('W');
1638 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1639 .setIcon(android.R.drawable.ic_search_category_default)
1640 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1641 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1642 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1643 .setAlphabeticShortcut('N');
1644
1645 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001646 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1647 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
1649 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1650 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1651 .setIntent(settings);
1652
1653 return true;
1654 }
1655
1656 @Override
1657 public boolean onPrepareOptionsMenu(Menu menu) {
1658 super.onPrepareOptionsMenu(menu);
1659
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001660 // If all apps is animating, don't show the menu, because we don't know
1661 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001662 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001663 return false;
1664 }
1665
1666 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001667 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001668 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1669 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1670
1671 // Disable add if the workspace is full.
1672 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001673 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1674 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
1677 return true;
1678 }
1679
1680 @Override
1681 public boolean onOptionsItemSelected(MenuItem item) {
1682 switch (item.getItemId()) {
1683 case MENU_ADD:
1684 addItems();
1685 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001686 case MENU_MANAGE_APPS:
1687 manageApps();
1688 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 case MENU_WALLPAPER_SETTINGS:
1690 startWallpaper();
1691 return true;
1692 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001693 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 return true;
1695 case MENU_NOTIFICATIONS:
1696 showNotifications();
1697 return true;
1698 }
1699
1700 return super.onOptionsItemSelected(item);
1701 }
Romain Guycbb89e42009-06-08 15:52:54 -07001702
Karl Rosaen138a0412009-04-23 19:00:21 -07001703 /**
1704 * Indicates that we want global search for this activity by setting the globalSearch
1705 * argument for {@link #startSearch} to true.
1706 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001708 @Override
1709 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001710 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001711 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001712 }
1713
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714 public boolean isWorkspaceLocked() {
1715 return mWorkspaceLoading || mWaitingForResult;
1716 }
1717
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001718 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1719 private boolean isPreviewVisible() {
1720 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1721 (mNextView != null && mNextView.getTag() != null);
1722 }
1723
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001725 if (LauncherApplication.isScreenXLarge()) {
1726 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001727 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001728 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001729 }
1730 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001731 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001732 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
1735
Michael Jurka0280c3b2010-09-17 15:00:07 -07001736 private void resetAddInfo() {
1737 mAddScreen = -1;
1738 mAddIntersectCellX = -1;
1739 mAddIntersectCellY = -1;
1740 mAddDropPosition = null;
1741 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001742
Winson Chung55cef262010-10-28 14:14:18 -07001743 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001744 resetAddInfo();
1745 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001746 mAddDropPosition = position;
1747
Michael Jurkaaf442092010-06-10 17:01:57 -07001748 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001749 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1750 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001751 }
1752
Winson Chung7ad01412010-09-27 11:33:03 -07001753 private void manageApps() {
1754 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1755 }
1756
Michael Jurkaaf442092010-06-10 17:01:57 -07001757 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001758 // TODO: catch bad widget exception when sent
1759 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001760 // TODO: Is this log message meaningful?
1761 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001762 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001763 }
1764
Winson Chung55cef262010-10-28 14:14:18 -07001765 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001766 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767
Bjorn Bringert7984c942009-12-09 15:38:25 +00001768 if (appWidget.configure != null) {
1769 // Launch over to configure widget, if needed
1770 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1771 intent.setComponent(appWidget.configure);
1772 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001773 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001774 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1775 intent.putExtra(
1776 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1777 info.mimeType);
1778
1779 final String mimeType = info.mimeType;
1780 final ClipData clipData = (ClipData) info.configurationData;
1781 final ClipDescription clipDesc = clipData.getDescription();
1782 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1783 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001784 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001785 final CharSequence stringData = item.getText();
1786 final Uri uriData = item.getUri();
1787 final Intent intentData = item.getIntent();
1788 final String key =
1789 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1790 if (uriData != null) {
1791 intent.putExtra(key, uriData);
1792 } else if (intentData != null) {
1793 intent.putExtra(key, intentData);
1794 } else if (stringData != null) {
1795 intent.putExtra(key, stringData);
1796 }
1797 break;
1798 }
1799 }
1800 }
Winson Chung55cef262010-10-28 14:14:18 -07001801 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001802
Romain Guy8e633c52010-03-04 12:51:36 -08001803 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001805 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 }
1808 }
Romain Guycbb89e42009-06-08 15:52:54 -07001809
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1811 resetAddInfo();
1812 mAddScreen = screen;
1813 mAddDropPosition = position;
1814
1815 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1816 createShortcutIntent.setComponent(componentName);
1817 processShortcut(createShortcutIntent);
1818 }
1819
Joe Onoratodeb98af2010-02-19 14:59:39 -08001820 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001821 // Handle case where user selected "Applications"
1822 String applicationName = getResources().getString(R.string.group_applications);
1823 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001824
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001825 if (applicationName != null && applicationName.equals(shortcutName)) {
1826 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1827 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001828
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001829 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1830 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001831 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001832 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001833 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001834 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837
Winson Chung24ab2f12010-09-16 14:10:47 -07001838 void processWallpaper(Intent intent) {
1839 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1840 }
1841
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1843 resetAddInfo();
1844 mAddScreen = screen;
1845 mAddDropPosition = position;
1846
1847 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1848 createFolderIntent.setComponent(componentName);
1849
1850 addLiveFolder(createFolderIntent);
1851 }
1852
1853 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001854 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001855 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001856 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001857
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001858 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001859 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001860 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001861 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 UserFolderInfo folderInfo = new UserFolderInfo();
1867 folderInfo.title = getText(R.string.folder_name);
1868
Michael Jurka0280c3b2010-09-17 15:00:07 -07001869 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1870 final int[] cellXY = mTmpAddItemCellCoordinates;
1871 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1872 showOutOfSpaceMessage();
1873 return;
1874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875
1876 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 LauncherModel.addItemToDatabase(this, folderInfo,
1878 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001879 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001880 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881
1882 // Create the view
1883 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001884 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1885 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001886 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
Romain Guycbb89e42009-06-08 15:52:54 -07001888
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001890 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001891 }
1892
Michael Jurka28750fb2010-09-24 17:43:49 -07001893 private void completeAddLiveFolder(
1894 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001895 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1896 final int[] cellXY = mTmpAddItemCellCoordinates;
1897 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1898 showOutOfSpaceMessage();
1899 return;
1900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901
Michael Jurka0280c3b2010-09-17 15:00:07 -07001902 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
1904 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001906 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001907 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909 }
1910
1911 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001912 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913
1914 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1915 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1916
1917 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 Intent.ShortcutIconResource iconResource = null;
1919
1920 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1921 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1922 try {
1923 iconResource = (Intent.ShortcutIconResource) extra;
1924 final PackageManager packageManager = context.getPackageManager();
1925 Resources resources = packageManager.getResourcesForApplication(
1926 iconResource.packageName);
1927 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1928 icon = resources.getDrawable(id);
1929 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001930 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932 }
1933
1934 if (icon == null) {
1935 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1936 }
1937
1938 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001939 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 info.title = name;
1941 info.iconResource = iconResource;
1942 info.uri = data.getData();
1943 info.baseIntent = baseIntent;
1944 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1945 LiveFolders.DISPLAY_MODE_GRID);
1946
1947 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001949 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950
1951 return info;
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 private void showNotifications() {
1955 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1956 if (statusBar != null) {
1957 statusBar.expand();
1958 }
1959 }
1960
1961 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001962 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001964 Intent chooser = Intent.createChooser(pickWallpaper,
1965 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001966 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1967 // Removed in Eclair MR1
1968// WallpaperManager wm = (WallpaperManager)
1969// getSystemService(Context.WALLPAPER_SERVICE);
1970// WallpaperInfo wi = wm.getWallpaperInfo();
1971// if (wi != null && wi.getSettingsActivity() != null) {
1972// LabeledIntent li = new LabeledIntent(getPackageName(),
1973// R.string.configure_wallpaper, 0);
1974// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1975// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1976// }
Mike Clerona0618e42009-10-22 13:55:21 -07001977 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001980 /**
1981 * Registers various content observers. The current implementation registers
1982 * only a favorites observer to keep track of the favorites applications.
1983 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001984 private void registerContentObservers() {
1985 ContentResolver resolver = getContentResolver();
1986 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1987 true, mWidgetObserver);
1988 }
1989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 @Override
1991 public boolean dispatchKeyEvent(KeyEvent event) {
1992 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1993 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001995 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001996 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001997 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001998 dumpState();
1999 return true;
2000 }
2001 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002002 }
2003 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2004 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002005 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 return true;
2007 }
2008 }
2009
2010 return super.dispatchKeyEvent(event);
2011 }
2012
Joe Onorato88ec0992009-11-19 13:16:06 -08002013 @Override
2014 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002015 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
2016 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002017 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08002018 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002019 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07002020 dismissPreview(mPreviousView);
2021 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002022 } else {
2023 // Back button is a no-op here, but give at least some feedback for the button press
2024 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002025 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002026 }
2027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 private void closeFolder() {
2029 Folder folder = mWorkspace.getOpenFolder();
2030 if (folder != null) {
2031 closeFolder(folder);
2032 }
2033 }
2034
2035 void closeFolder(Folder folder) {
2036 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08002037 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07002039 CellLayout cl = (CellLayout) parent;
2040 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05002041 if (folder instanceof DropTarget) {
2042 // Live folders aren't DropTargets.
2043 mDragController.removeDropTarget((DropTarget)folder);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046 folder.onClose();
2047 }
2048
2049 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002050 * Re-listen when widgets are reset.
2051 */
2052 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002053 if (mAppWidgetHost != null) {
2054 mAppWidgetHost.startListening();
2055 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002056 }
2057
2058 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2060 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 private void unbindDesktopItems() {
2063 for (ItemInfo item: mDesktopItems) {
2064 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 /**
2070 * Launches the intent referred by the clicked shortcut.
2071 *
2072 * @param v The view representing the clicked shortcut.
2073 */
2074 public void onClick(View v) {
2075 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002076 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002078 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002079 int[] pos = new int[2];
2080 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002081 intent.setSourceBounds(new Rect(pos[0], pos[1],
2082 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002083 boolean success = startActivitySafely(intent, tag);
2084
2085 if (success && v instanceof BubbleTextView) {
2086 mWaitingForResume = (BubbleTextView) v;
2087 mWaitingForResume.setStayPressed(true);
2088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 } else if (tag instanceof FolderInfo) {
2090 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002091 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002092 if (mState == State.ALL_APPS) {
2093 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002094 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002095 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 }
2098 }
2099
Michael Jurka0e260592010-06-30 17:07:39 -07002100 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002101 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002102 // clicking anywhere on the workspace causes the customization drawer to slide down
2103 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002104 return false;
2105 }
2106
Michael Jurkaaf442092010-06-10 17:01:57 -07002107 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002108 * Event handler for the search button
2109 *
2110 * @param v The view that was clicked.
2111 */
2112 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002113 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002114 // Use a custom animation for launching search
2115 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002116 }
2117
2118 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002119 * Event handler for the voice button
2120 *
2121 * @param v The view that was clicked.
2122 */
2123 public void onClickVoiceButton(View v) {
2124 startVoiceSearch();
2125 }
2126
2127 private void startVoiceSearch() {
2128 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2129 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2130 startActivity(intent);
2131 }
2132
2133 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002134 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002135 * enters home screen customization mode.
2136 *
2137 * @param v The view that was clicked.
2138 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002139 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002140 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002141 }
2142
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002143 /**
2144 * Event handler for the "grid" button that appears on the home screen, which
2145 * enters all apps mode.
2146 *
2147 * @param v The view that was clicked.
2148 */
2149 public void onClickAllAppsButton(View v) {
2150 showAllApps(true);
2151 }
2152
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002153 public void onClickAppMarketButton(View v) {
2154 if (mAppMarketIntent != null) {
2155 startActivitySafely(mAppMarketIntent, "app market");
2156 }
2157 }
2158
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002159 void startApplicationDetailsActivity(ComponentName componentName) {
2160 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002161 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2162 Uri.fromParts("package", packageName, null));
2163 startActivity(intent);
2164 }
2165
Patrick Dubroy5539af72010-09-07 15:22:01 -07002166 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2167 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2168 // System applications cannot be installed. For now, show a toast explaining that.
2169 // We may give them the option of disabling apps this way.
2170 int messageId = R.string.uninstall_system_app_text;
2171 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2172 } else {
2173 String packageName = appInfo.componentName.getPackageName();
2174 String className = appInfo.componentName.getClassName();
2175 Intent intent = new Intent(
2176 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2177 startActivity(intent);
2178 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002179 }
2180
Michael Jurkaddd62e92011-02-16 17:49:14 -08002181 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2183 try {
2184 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002185 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 } catch (ActivityNotFoundException e) {
2187 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002188 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 } catch (SecurityException e) {
2190 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002191 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002193 "or use the exported attribute for this activity. "
2194 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002196 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002198
Romain Guy8e633c52010-03-04 12:51:36 -08002199 void startActivityForResultSafely(Intent intent, int requestCode) {
2200 try {
2201 startActivityForResult(intent, requestCode);
2202 } catch (ActivityNotFoundException e) {
2203 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2204 } catch (SecurityException e) {
2205 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2206 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2207 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2208 "or use the exported attribute for this activity.", e);
2209 }
2210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211
2212 private void handleFolderClick(FolderInfo folderInfo) {
2213 if (!folderInfo.opened) {
2214 // Close any open folder
2215 closeFolder();
2216 // Open the requested folder
2217 openFolder(folderInfo);
2218 } else {
2219 // Find the open folder...
2220 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2221 int folderScreen;
2222 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002223 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 // .. and close it
2225 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002226 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 // Close any folder open on the current screen
2228 closeFolder();
2229 // Pull the folder onto this screen
2230 openFolder(folderInfo);
2231 }
2232 }
2233 }
2234 }
2235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002237 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 * is animated relative to the specified View. If the View is null, no animation
2239 * is played.
2240 *
2241 * @param folderInfo The FolderInfo describing the folder to open.
2242 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002243 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 Folder openFolder;
2245
2246 if (folderInfo instanceof UserFolderInfo) {
2247 openFolder = UserFolder.fromXml(this);
2248 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002249 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 } else {
2251 return;
2252 }
2253
Joe Onorato00acb122009-08-04 16:04:30 -04002254 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 openFolder.setLauncher(this);
2256
2257 openFolder.bind(folderInfo);
2258 folderInfo.opened = true;
2259
Winson Chungaafa03c2010-06-11 17:34:16 -07002260 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 openFolder.onOpen();
2263 }
2264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002266 if (mState != State.WORKSPACE) {
2267 return false;
2268 }
2269
Romain Guy1fbc1c82009-11-09 20:43:08 -08002270 switch (v.getId()) {
2271 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002272 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002273 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2274 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002275 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002276 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002277 return true;
2278 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002279 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002280 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2281 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002282 showPreviews(v);
2283 }
2284 return true;
2285 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002286 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002287 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2288 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2289 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002290 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002291 return true;
2292 }
2293
Joe Onorato9c1289c2009-08-17 11:03:03 -04002294 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 return false;
2296 }
2297
2298 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002299 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 }
2301
Michael Jurka0280c3b2010-09-17 15:00:07 -07002302 resetAddInfo();
2303 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002305 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 return true;
2307 }
2308
Michael Jurka0280c3b2010-09-17 15:00:07 -07002309 final View itemUnderLongClick = longClickCellInfo.cell;
2310
Winson Chung304dcde2011-01-07 11:17:23 -08002311 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312 if (itemUnderLongClick == null) {
2313 // User long pressed on empty space
2314 mWorkspace.setAllowLongPress(false);
2315 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2316 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002317 if (LauncherApplication.isScreenXLarge()) {
2318 addItems();
2319 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002320 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 }
2322 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002323 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002325 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2326 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002327 mAddIntersectCellX = longClickCellInfo.cellX;
2328 mAddIntersectCellY = longClickCellInfo.cellY;
2329 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 }
2331 }
2332 }
2333 return true;
2334 }
2335
Romain Guye6b8e2f2009-11-10 11:56:55 -08002336 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002337 private void dismissPreview(final View v) {
2338 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002339 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002340 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2341 public void onDismiss() {
2342 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2343 int count = group.getChildCount();
2344 for (int i = 0; i < count; i++) {
2345 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2346 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002347 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2348 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2349
2350 v.setTag(R.id.workspace, null);
2351 v.setTag(R.id.icon, null);
2352 window.setOnDismissListener(null);
2353 }
2354 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002355 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002356 }
2357 v.setTag(null);
2358 }
2359
Romain Guyf8e6a802009-12-07 17:48:02 -08002360 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002361 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002362 }
2363
Romain Guya6abce82009-11-10 02:54:41 -08002364 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002365 final Resources resources = getResources();
2366 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002367
Romain Guya6abce82009-11-10 02:54:41 -08002368 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002369
Romain Guy9d31e9f2009-11-11 18:54:28 -08002370 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002371
Romain Guyf8e6a802009-12-07 17:48:02 -08002372 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002374 int extraW = (int) ((r.left + r.right) * max);
2375 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002376
2377 int aW = cell.getWidth() - extraW;
2378 float w = aW / max;
2379
2380 int width = cell.getWidth();
2381 int height = cell.getHeight();
2382 int x = cell.getLeftPadding();
2383 int y = cell.getTopPadding();
2384 width -= (x + cell.getRightPadding());
2385 height -= (y + cell.getBottomPadding());
2386
2387 float scale = w / width;
2388
2389 int count = end - start;
2390
2391 final float sWidth = width * scale;
2392 float sHeight = height * scale;
2393
Romain Guye6b8e2f2009-11-10 11:56:55 -08002394 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002395
Romain Guye6b8e2f2009-11-10 11:56:55 -08002396 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2397 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002398
2399 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002400 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002401 cell = (CellLayout) workspace.getChildAt(i);
2402
Romain Guyf8e6a802009-12-07 17:48:02 -08002403 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002404 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002405
2406 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002407 c.scale(scale, scale);
2408 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002409 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002410
Romain Guy9d31e9f2009-11-11 18:54:28 -08002411 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002412 image.setImageBitmap(bitmap);
2413 image.setTag(i);
2414 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002415 image.setOnFocusChangeListener(handler);
2416 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002417 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002418
2419 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002420 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2421
Winson Chungaafa03c2010-06-11 17:34:16 -07002422 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002423 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002424
2425 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002426 p.setContentView(preview);
2427 p.setWidth((int) (sWidth * count + extraW));
2428 p.setHeight((int) (sHeight + extraH));
2429 p.setAnimationStyle(R.style.AnimationPreview);
2430 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002431 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002432 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002433 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002434
Romain Guye6b8e2f2009-11-10 11:56:55 -08002435 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2436 public void onDismiss() {
2437 dismissPreview(anchor);
2438 }
2439 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002440
2441 anchor.setTag(p);
2442 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002443 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002444 }
2445
Romain Guy9d31e9f2009-11-11 18:54:28 -08002446 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002447 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002448
Romain Guye6b8e2f2009-11-10 11:56:55 -08002449 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002450 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002451 }
2452
2453 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002454 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002455 v.post(this);
2456 }
2457
2458 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002459 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002460 }
2461
2462 public void onFocusChange(View v, boolean hasFocus) {
2463 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002464 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002465 }
Romain Guya6abce82009-11-10 02:54:41 -08002466 }
2467 }
2468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 Workspace getWorkspace() {
2470 return mWorkspace;
2471 }
2472
Michael Jurka3c38d562011-02-18 19:22:12 -08002473 TabHost getCustomizationDrawer() {
2474 return mHomeCustomizationDrawer;
2475 }
2476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 @Override
2478 protected Dialog onCreateDialog(int id) {
2479 switch (id) {
2480 case DIALOG_CREATE_SHORTCUT:
2481 return new CreateShortcut().createDialog();
2482 case DIALOG_RENAME_FOLDER:
2483 return new RenameFolder().createDialog();
2484 }
2485
2486 return super.onCreateDialog(id);
2487 }
2488
2489 @Override
2490 protected void onPrepareDialog(int id, Dialog dialog) {
2491 switch (id) {
2492 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 break;
2494 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002495 if (mFolderInfo != null) {
2496 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2497 final CharSequence text = mFolderInfo.title;
2498 input.setText(text);
2499 input.setSelection(0, text.length());
2500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 break;
2502 }
2503 }
2504
2505 void showRenameDialog(FolderInfo info) {
2506 mFolderInfo = info;
2507 mWaitingForResult = true;
2508 showDialog(DIALOG_RENAME_FOLDER);
2509 }
2510
Michael Jurka0280c3b2010-09-17 15:00:07 -07002511 private void showAddDialog(int intersectX, int intersectY) {
2512 resetAddInfo();
2513 mAddIntersectCellX = intersectX;
2514 mAddIntersectCellY = intersectY;
2515 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 mWaitingForResult = true;
2517 showDialog(DIALOG_CREATE_SHORTCUT);
2518 }
2519
Joe Onoratodeb98af2010-02-19 14:59:39 -08002520 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002521 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002522 Bundle bundle = new Bundle();
2523
2524 ArrayList<String> shortcutNames = new ArrayList<String>();
2525 shortcutNames.add(getString(R.string.group_applications));
2526 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2527
2528 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2529 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2530 R.drawable.ic_launcher_application));
2531 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2532
2533 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2534 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002535 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002536 pickIntent.putExtras(bundle);
2537
Joe Onoratodeb98af2010-02-19 14:59:39 -08002538 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002539 }
2540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 private class RenameFolder {
2542 private EditText mInput;
2543
2544 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2546 mInput = (EditText) layout.findViewById(R.id.folder_name);
2547
2548 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2549 builder.setIcon(0);
2550 builder.setTitle(getString(R.string.rename_folder_title));
2551 builder.setCancelable(true);
2552 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2553 public void onCancel(DialogInterface dialog) {
2554 cleanup();
2555 }
2556 });
2557 builder.setNegativeButton(getString(R.string.cancel_action),
2558 new Dialog.OnClickListener() {
2559 public void onClick(DialogInterface dialog, int which) {
2560 cleanup();
2561 }
2562 }
2563 );
2564 builder.setPositiveButton(getString(R.string.rename_action),
2565 new Dialog.OnClickListener() {
2566 public void onClick(DialogInterface dialog, int which) {
2567 changeFolderName();
2568 }
2569 }
2570 );
2571 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002572
2573 final AlertDialog dialog = builder.create();
2574 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2575 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002576 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002577 mInput.requestFocus();
2578 InputMethodManager inputManager = (InputMethodManager)
2579 getSystemService(Context.INPUT_METHOD_SERVICE);
2580 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002581 }
2582 });
2583
2584 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 }
2586
2587 private void changeFolderName() {
2588 final String name = mInput.getText().toString();
2589 if (!TextUtils.isEmpty(name)) {
2590 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002591 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 mFolderInfo.title = name;
2593 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2594
Joe Onorato9c1289c2009-08-17 11:03:03 -04002595 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002596 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002597 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 } else {
2599 final FolderIcon folderIcon = (FolderIcon)
2600 mWorkspace.getViewForTag(mFolderInfo);
2601 if (folderIcon != null) {
2602 folderIcon.setText(name);
2603 getWorkspace().requestLayout();
2604 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002605 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002606 mWorkspaceLoading = true;
2607 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 }
2609 }
2610 }
2611 cleanup();
2612 }
2613
2614 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 dismissDialog(DIALOG_RENAME_FOLDER);
2616 mWaitingForResult = false;
2617 mFolderInfo = null;
2618 }
2619 }
2620
Daniel Sandler843e8602010-06-07 14:59:01 -04002621 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2622 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002623 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002624 }
2625
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002626 // AllAppsView.Watcher
2627 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002628 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2629 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002630 mWorkspace.setVisibility(View.GONE);
2631 }
2632 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633
Michael Jurkaafca9532011-02-16 14:50:35 -08002634 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635 button.setVisibility(View.VISIBLE);
2636 button.setFocusable(true);
2637 button.setClickable(true);
2638 }
2639
2640 private void hideToolbarButton(View button) {
2641 button.setAlpha(0.0f);
2642 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2643 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002644 }
2645
2646 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002647 button.setFocusable(false);
2648 button.setClickable(false);
2649 }
2650
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002652 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 *
2654 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2655 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002656 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002659 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 final boolean showing = show;
2661 final boolean hiding = !show;
2662
2663 final int duration = show ?
2664 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2665 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2666
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002667 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002668 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002669 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002670 anim.addUpdateListener(new AnimatorUpdateListener() {
2671 public void onAnimationUpdate(ValueAnimator animation) {
2672 view.setAlpha((Float) animation.getAnimatedValue());
2673 }
2674 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002675 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002676 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002677 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002678 if (showing) showAndEnableToolbarButton(view);
2679 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002681 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002682 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002683 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002684 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002685 });
2686 seq.play(anim);
2687 } else {
2688 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002689 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002690 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002692 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002693 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002694 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002695 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696 }
2697
2698 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002699 * Show/hide the appropriate toolbar buttons for newState.
2700 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002701 *
2702 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002703 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2704 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002706 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002707 switch (newState) {
2708 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002709 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2710 mDeleteZone.setOverlappingViews(
2711 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002712 mDeleteZone.setDragAndDropEnabled(true);
2713 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002714 break;
2715 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002716 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002717 mDeleteZone.setDragAndDropEnabled(false);
2718 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 break;
2720 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002721 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002722 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002723 break;
2724 }
2725 }
2726
2727 /**
2728 * Helper method for the cameraZoomIn/cameraZoomOut animations
2729 * @param view The view being animated
2730 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2731 * @param scaleFactor The scale factor used for the zoom
2732 */
2733 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2734 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002735
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002736 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002737 // Set pivotY so that at the starting zoom factor, the view is partially
2738 // visible. Modifying initialHeightFactor changes how much of the view is
2739 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002740 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002741 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002742 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002743 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002744 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002745 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 }
2747 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002748
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002749 /**
2750 * Zoom the camera out from the workspace to reveal 'toView'.
2751 * Assumes that the view to show is anchored at either the very top or very bottom
2752 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002753 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002754 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002756 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002757 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002758
2759 final int duration = toAllApps ?
2760 res.getInteger(R.integer.config_allAppsZoomInTime) :
2761 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002762 final int fadeDuration = toAllApps ?
2763 res.getInteger(R.integer.config_allAppsFadeInTime) :
2764 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002765
2766 final float scale = toAllApps ?
2767 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2768 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2769
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002770 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002771
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002772 setPivotsForZoom(toView, toState, scale);
2773
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002774 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002775 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002776 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002777 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002778 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002779
2780 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002781 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2782 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2783 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2784 public void onAnimationUpdate(ValueAnimator animation) {
2785 final float b = (Float) animation.getAnimatedValue();
2786 final float a = 1f - b;
2787 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002788 toView.setFastScaleX(a * scale + b * 1f);
2789 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002790 }
2791 });
Adam Cohen61033d32010-11-15 18:29:44 -08002792
Adam Cohenf16e5712011-01-13 13:31:45 -08002793 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002794 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002795 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002796 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002797 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2798 public void onAnimationUpdate(ValueAnimator animation) {
2799 final float b = (Float) animation.getAnimatedValue();
2800 final float a = 1f - b;
2801 // don't need to invalidate because we do so above
2802 toView.setFastAlpha(a * 0f + b * 1f);
2803 }
2804 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002805 alphaAnim.start();
2806 }
2807
Michael Jurka2763be32011-02-24 11:19:57 -08002808 if (toView instanceof LauncherAnimatable) {
2809 ((LauncherAnimatable) toView).onLauncherAnimationStart();
Michael Jurkabfadaad2011-01-31 21:35:39 -08002810 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002811 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002812 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002813 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 // Prepare the position
2815 toView.setTranslationX(0.0f);
2816 toView.setTranslationY(0.0f);
2817 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002818 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002819 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002820 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002821 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002822 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002823 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002824 // If we don't set the final scale values here, if this animation is cancelled
2825 // it will have the wrong scale value and subsequent cameraPan animations will
2826 // not fix that
2827 toView.setScaleX(1.0f);
2828 toView.setScaleY(1.0f);
Michael Jurka2763be32011-02-24 11:19:57 -08002829 if (toView instanceof LauncherAnimatable) {
2830 ((LauncherAnimatable) toView).onLauncherAnimationEnd();
2831 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002832 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002833 });
2834
Chet Haaseb1254a62010-09-07 13:35:00 -07002835 AnimatorSet toolbarHideAnim = new AnimatorSet();
2836 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2838
2839 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002840 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002841
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002842 if (mStateAnimation != null) mStateAnimation.cancel();
2843 mStateAnimation = new AnimatorSet();
2844 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2845 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846
2847 // Show the new toolbar buttons just as the main animation is ending
2848 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002849 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2850 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002851 } else {
2852 toView.setTranslationX(0.0f);
2853 toView.setTranslationY(0.0f);
2854 toView.setScaleX(1.0f);
2855 toView.setScaleY(1.0f);
2856 toView.setVisibility(View.VISIBLE);
2857 hideAndShowToolbarButtons(toState, null, null);
2858 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002859 }
2860
2861 /**
2862 * Zoom the camera back into the workspace, hiding 'fromView'.
2863 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002864 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2865 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002868 cameraZoomIn(fromState, animated, false);
2869 }
2870
2871 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002872 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002873 final boolean fromAllApps = (fromState == State.ALL_APPS);
2874
2875 int duration = fromAllApps ?
2876 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2877 res.getInteger(R.integer.config_customizeZoomOutTime);
2878
Michael Jurka742574b2011-02-02 23:51:01 -08002879 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002880 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2881 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2882
2883 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002884
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002885 mCustomizePagedView.endChoiceMode();
2886 mAllAppsPagedView.endChoiceMode();
2887
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002888 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002889
Michael Jurkad3ef3062010-11-23 16:23:58 -08002890 if (!springLoaded) {
2891 mWorkspace.unshrink(animated);
2892 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002893
2894 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002895 if (mStateAnimation != null) mStateAnimation.cancel();
2896 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002897
Michael Jurka742574b2011-02-02 23:51:01 -08002898 final float oldScaleX = fromView.getScaleX();
2899 final float oldScaleY = fromView.getScaleY();
2900
2901 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2902 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2903 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2904 public void onAnimationUpdate(ValueAnimator animation) {
2905 final float b = (Float) animation.getAnimatedValue();
2906 final float a = 1f - b;
2907 ((View)fromView.getParent()).fastInvalidate();
2908 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2909 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2910 }
2911 });
2912 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002913 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002914 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002915 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2916 public void onAnimationUpdate(ValueAnimator animation) {
2917 final float b = (Float) animation.getAnimatedValue();
2918 final float a = 1f - b;
2919 // don't need to invalidate because we do so above
2920 fromView.setFastAlpha(a * 1f + b * 0f);
2921 }
2922 });
Michael Jurka2763be32011-02-24 11:19:57 -08002923 if (fromView instanceof LauncherAnimatable) {
2924 ((LauncherAnimatable) fromView).onLauncherAnimationStart();
2925 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002926 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002927 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002928 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002929 fromView.setVisibility(View.GONE);
Michael Jurka2763be32011-02-24 11:19:57 -08002930 if (fromView instanceof LauncherAnimatable) {
2931 ((LauncherAnimatable) fromView).onLauncherAnimationEnd();
2932 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002933 }
2934 });
2935
Chet Haaseb1254a62010-09-07 13:35:00 -07002936 AnimatorSet toolbarHideAnim = new AnimatorSet();
2937 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002938 if (!springLoaded) {
2939 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2940 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002941
Adam Cohen61033d32010-11-15 18:29:44 -08002942 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002943
2944 // Show the new toolbar buttons at the very end of the whole animation
2945 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2946 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002947 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2948 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002949 } else {
2950 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002951 if (!springLoaded) {
2952 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2953 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002954 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002955 }
2956
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002957 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002958 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002959 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002960 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002961
Michael Jurka79212d82010-07-30 16:36:20 -07002962 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002963 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002964 } else {
2965 mAllAppsGrid.zoom(1.0f, animated);
2966 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002967
Romain Guyc16fea72010-03-12 17:17:56 -08002968 ((View) mAllAppsGrid).setFocusable(true);
2969 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002970
Joe Onorato7c312c12009-08-13 21:36:53 -07002971 // TODO: fade these two too
2972 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002973
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002974 // Change the state *after* we've called all the transition code
2975 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002976
Winson Chung5fb63472011-02-02 17:03:37 -08002977 // Pause the auto-advance of widgets until we are out of AllApps
2978 mUserPresent = false;
2979 updateRunning();
2980
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002981 // send an accessibility event to announce the context change
2982 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2983 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002984
2985 void showWorkspace(boolean animated) {
2986 showWorkspace(animated, null);
2987 }
2988
2989 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002990 if (layout != null) {
2991 // always animated, but that's ok since we never specify a layout and
2992 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002993 mWorkspace.unshrink(layout);
2994 } else {
2995 mWorkspace.unshrink(animated);
2996 }
2997 if (mState == State.ALL_APPS) {
2998 closeAllApps(animated);
2999 } else if (mState == State.CUSTOMIZE) {
3000 hideCustomizationDrawer(animated);
3001 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003002
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003003 // Change the state *after* we've called all the transition code
3004 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003005
Winson Chung5fb63472011-02-02 17:03:37 -08003006 // Resume the auto-advance of widgets
3007 mUserPresent = true;
3008 updateRunning();
3009
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003010 // send an accessibility event to announce the context change
3011 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04003012 }
3013
Michael Jurkad3ef3062010-11-23 16:23:58 -08003014 void enterSpringLoadedDragMode(CellLayout layout) {
3015 mWorkspace.enterSpringLoadedDragMode(layout);
3016 if (mState == State.ALL_APPS) {
3017 cameraZoomIn(State.ALL_APPS, true, true);
3018 mState = State.ALL_APPS_SPRING_LOADED;
3019 } else if (mState == State.CUSTOMIZE) {
3020 cameraZoomIn(State.CUSTOMIZE, true, true);
3021 mState = State.CUSTOMIZE_SPRING_LOADED;
3022 }/* else {
3023 // we're already in spring loaded mode; don't do anything
3024 }*/
3025 }
3026
3027 void exitSpringLoadedDragMode() {
3028 if (mState == State.ALL_APPS_SPRING_LOADED) {
3029 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3030 cameraZoomOut(State.ALL_APPS, true);
3031 mState = State.ALL_APPS;
3032 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3033 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3034 cameraZoomOut(State.CUSTOMIZE, true);
3035 mState = State.CUSTOMIZE;
3036 }/* else {
3037 // we're not in spring loaded mode; don't do anything
3038 }*/
3039 }
3040
Joe Onoratob2061212009-11-24 16:13:54 -05003041 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003042 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003043 * - Home from workspace
3044 * - from center screen
3045 * - from other screens
3046 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003047 * - from center screen
3048 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003049 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003050 * - from center screen
3051 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003052 * - Launch app from workspace and quit
3053 * - with back
3054 * - with home
3055 * - Launch app from all apps and quit
3056 * - with back
3057 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003058 * - Go to a screen that's not the default, then all
3059 * apps, and launch and app, and go back
3060 * - with back
3061 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003062 * - On workspace, long press power and go back
3063 * - with back
3064 * - with home
3065 * - On all apps, long press power and go back
3066 * - with back
3067 * - with home
3068 * - On workspace, power off
3069 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003070 * - Launch an app and turn off the screen while in that app
3071 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003072 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003073 * - From all apps
3074 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003075 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3076 * - From all apps
3077 * - From the center workspace
3078 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003079 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003080 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003081 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003082 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003083 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003084 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003085 } else {
3086 mAllAppsGrid.zoom(0.0f, animated);
3087 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003088 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003089 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003090 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003091 }
3092
Joe Onorato7c312c12009-08-13 21:36:53 -07003093 void lockAllApps() {
3094 // TODO
3095 }
3096
3097 void unlockAllApps() {
3098 // TODO
3099 }
3100
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003101 // Show the customization drawer (only exists in x-large configuration)
3102 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003103 if (mState != State.WORKSPACE) {
3104 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003105 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003106
3107 cameraZoomOut(State.CUSTOMIZE, animated);
3108
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003109 // Change the state *after* we've called all the transition code
3110 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003111
3112 // Pause the auto-advance of widgets until we are out of Customization drawer
3113 mUserPresent = false;
3114 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003115 }
3116
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003117 // Hide the customization drawer (only exists in x-large configuration)
3118 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003119 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003120 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003121 }
3122 }
3123
Patrick Dubroy5f445422011-02-18 14:35:21 -08003124 /**
3125 * Add an item from all apps or customize onto the given workspace screen.
3126 * If layout is null, add to the current screen.
3127 */
3128 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003129 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3130 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003131 } else {
3132 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003133 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003134 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003135
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003136 void onWorkspaceClick(CellLayout layout) {
3137 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003138 }
3139
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003140 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003141 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003142 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003143 // Look for the toolbar icon specified in the activity meta-data
3144 Bundle metaData = packageManager.getActivityInfo(
3145 activityName, PackageManager.GET_META_DATA).metaData;
3146 if (metaData != null) {
3147 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3148 if (iconResId != 0) {
3149 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003150 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003151 }
3152 }
3153 } catch (NameNotFoundException e) {
3154 // Do nothing
3155 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003156 return null;
3157 }
3158
3159 // if successful in getting icon, return it; otherwise, set button to use default drawable
3160 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3161 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3162 TextView button = (TextView) findViewById(buttonId);
3163 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3164
3165 // If we were unable to find the icon via the meta-data, use a generic one
3166 if (toolbarIcon == null) {
3167 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3168 return null;
3169 } else {
3170 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3171 return toolbarIcon.getConstantState();
3172 }
3173 }
3174
3175 // if successful in getting icon, return it; otherwise, set button to use default drawable
3176 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3177 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3178 ImageView button = (ImageView) findViewById(buttonId);
3179 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3180
Michael Jurka0423dcf2010-10-05 14:56:18 -07003181 // If we were unable to find the icon via the meta-data, use a generic one
3182 if (toolbarIcon == null) {
3183 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003184 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003185 } else {
3186 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003187 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003188 }
3189 }
3190
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003191 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3192 TextView button = (TextView) findViewById(buttonId);
3193 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3194 }
3195
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003196 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003197 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003198 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003199 }
3200
Michael Jurka0423dcf2010-10-05 14:56:18 -07003201 private void updateGlobalSearchIcon() {
3202 if (LauncherApplication.isScreenXLarge()) {
3203 final SearchManager searchManager =
3204 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3205 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003206 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003207 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003208 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003209 } else {
3210 findViewById(R.id.search_button).setVisibility(View.GONE);
3211 }
3212 }
3213 }
3214
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003215 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003216 updateButtonWithDrawable(R.id.search_button, d);
3217 }
3218
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003219 private void updateVoiceSearchIcon() {
3220 if (LauncherApplication.isScreenXLarge()) {
3221 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3222 ComponentName activityName = intent.resolveActivity(getPackageManager());
3223 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003224 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003225 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3226 } else {
3227 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003228 }
3229 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003230 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003231
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003232 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003233 updateButtonWithDrawable(R.id.voice_button, d);
3234 }
3235
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003236 /**
3237 * Sets the app market icon (shown when all apps is visible on x-large screens)
3238 */
3239 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003240 if (LauncherApplication.isScreenXLarge()) {
3241 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3242 // Find the app market activity by resolving an intent.
3243 // (If multiple app markets are installed, it will return the ResolverActivity.)
3244 ComponentName activityName = intent.resolveActivity(getPackageManager());
3245 if (activityName != null) {
3246 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003247 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003248 R.id.market_button, activityName, R.drawable.app_market_generic);
3249 }
3250 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003251 }
3252
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003253 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003254 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003255 }
3256
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003257 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003258 * Displays the shortcut creation dialog and launches, if necessary, the
3259 * appropriate activity.
3260 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003261 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003262 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3263 DialogInterface.OnShowListener {
3264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003265 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3271 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003272 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003274 builder.setInverseBackgroundForced(true);
3275
3276 AlertDialog dialog = builder.create();
3277 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003278 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003279 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003281 return dialog;
3282 }
3283
3284 public void onCancel(DialogInterface dialog) {
3285 mWaitingForResult = false;
3286 cleanup();
3287 }
3288
Romain Guycbb89e42009-06-08 15:52:54 -07003289 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003290 }
3291
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003292 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003293 try {
3294 dismissDialog(DIALOG_CREATE_SHORTCUT);
3295 } catch (Exception e) {
3296 // An exception is thrown if the dialog is not visible, which is fine
3297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298 }
3299
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003300 /**
3301 * Handle the action clicked in the "Add to home" dialog.
3302 */
3303 public void onClick(DialogInterface dialog, int which) {
3304 Resources res = getResources();
3305 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003306
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003307 switch (which) {
3308 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003309 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003310 break;
3311 }
Romain Guycbb89e42009-06-08 15:52:54 -07003312
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003313 case AddAdapter.ITEM_APPWIDGET: {
3314 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003315
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003316 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3317 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003318 // start the pick activity
3319 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3320 break;
3321 }
Romain Guycbb89e42009-06-08 15:52:54 -07003322
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003323 case AddAdapter.ITEM_LIVE_FOLDER: {
3324 // Insert extra item to handle inserting folder
3325 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003326
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003327 ArrayList<String> shortcutNames = new ArrayList<String>();
3328 shortcutNames.add(res.getString(R.string.group_folder));
3329 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003330
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003331 ArrayList<ShortcutIconResource> shortcutIcons =
3332 new ArrayList<ShortcutIconResource>();
3333 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3334 R.drawable.ic_launcher_folder));
3335 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3336
3337 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3338 pickIntent.putExtra(Intent.EXTRA_INTENT,
3339 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3340 pickIntent.putExtra(Intent.EXTRA_TITLE,
3341 getText(R.string.title_select_live_folder));
3342 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003343
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003344 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3345 break;
3346 }
3347
3348 case AddAdapter.ITEM_WALLPAPER: {
3349 startWallpaper();
3350 break;
3351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003352 }
3353 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003354
3355 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003356 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003358 }
3359
3360 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003361 * Receives notifications when applications are added/removed.
3362 */
3363 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3364 @Override
3365 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003366 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003367 String reason = intent.getStringExtra("reason");
3368 if (!"homekey".equals(reason)) {
3369 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003370 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003371 animate = false;
3372 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003373 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003374 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003375 }
3376 }
3377
3378 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003379 * Receives notifications whenever the appwidgets are reset.
3380 */
3381 private class AppWidgetResetObserver extends ContentObserver {
3382 public AppWidgetResetObserver() {
3383 super(new Handler());
3384 }
3385
3386 @Override
3387 public void onChange(boolean selfChange) {
3388 onAppWidgetReset();
3389 }
3390 }
3391
3392 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003393 * If the activity is currently paused, signal that we need to re-run the loader
3394 * in onResume.
3395 *
3396 * This needs to be called from incoming places where resources might have been loaded
3397 * while we are paused. That is becaues the Configuration might be wrong
3398 * when we're not running, and if it comes back to what it was when we
3399 * were paused, we are not restarted.
3400 *
3401 * Implementation of the method from LauncherModel.Callbacks.
3402 *
3403 * @return true if we are currently paused. The caller might be able to
3404 * skip some work in that case since we will come back again.
3405 */
3406 public boolean setLoadOnResume() {
3407 if (mPaused) {
3408 Log.i(TAG, "setLoadOnResume");
3409 mOnResumeNeedsLoad = true;
3410 return true;
3411 } else {
3412 return false;
3413 }
3414 }
3415
3416 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003418 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003420 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003421 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003422 } else {
3423 return SCREEN_COUNT / 2;
3424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003426
Winson Chunga12a2502010-12-20 14:41:35 -08003427 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003428 mAllAppsPagedView = view;
3429 }
3430
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 /**
3432 * Refreshes the shortcuts shown on the workspace.
3433 *
3434 * Implementation of the method from LauncherModel.Callbacks.
3435 */
3436 public void startBinding() {
3437 final Workspace workspace = mWorkspace;
3438 int count = workspace.getChildCount();
3439 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003440 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003441 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3442 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003444
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 if (DEBUG_USER_INTERFACE) {
3446 android.widget.Button finishButton = new android.widget.Button(this);
3447 finishButton.setText("Finish");
3448 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3449
3450 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3451 public void onClick(View v) {
3452 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003455 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003456
3457 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3458 // mDesktopItems -> AppWidgetInfo -> hostView).
3459 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003460 }
3461
3462 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 * Bind the items start-end from the list.
3464 *
3465 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003466 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3468
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003469 setLoadOnResume();
3470
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 final Workspace workspace = mWorkspace;
3472
3473 for (int i=start; i<end; i++) {
3474 final ItemInfo item = shortcuts.get(i);
3475 mDesktopItems.add(item);
3476 switch (item.itemType) {
3477 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3478 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003479 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3481 false);
3482 break;
3483 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3484 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003485 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003486 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3488 false);
3489 break;
3490 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3491 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3492 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003493 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 (LiveFolderInfo) item);
3495 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3496 false);
3497 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003499 }
3500
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502 }
3503
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 /**
3505 * Implementation of the method from LauncherModel.Callbacks.
3506 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003507 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003508 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003509 sFolders.clear();
3510 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003511 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512
3513 /**
3514 * Add the views for a widget to the workspace.
3515 *
3516 * Implementation of the method from LauncherModel.Callbacks.
3517 */
3518 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003519 setLoadOnResume();
3520
Daniel Sandler843e8602010-06-07 14:59:01 -04003521 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3522 if (DEBUG_WIDGETS) {
3523 Log.d(TAG, "bindAppWidget: " + item);
3524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 final Workspace workspace = mWorkspace;
3526
3527 final int appWidgetId = item.appWidgetId;
3528 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003529 if (DEBUG_WIDGETS) {
3530 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3531 }
3532
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3534
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3536 item.hostView.setTag(item);
3537
3538 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3539 item.cellY, item.spanX, item.spanY, false);
3540
Adam Cohended9f8d2010-11-03 13:25:16 -07003541 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3542
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 workspace.requestLayout();
3544
3545 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003546
3547 if (DEBUG_WIDGETS) {
3548 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3549 + (SystemClock.uptimeMillis()-start) + "ms");
3550 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 }
3552
3553 /**
3554 * Callback saying that there aren't any more items to bind.
3555 *
3556 * Implementation of the method from LauncherModel.Callbacks.
3557 */
3558 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003559 setLoadOnResume();
3560
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 if (mSavedState != null) {
3562 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003563 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 }
3565
3566 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3567 if (userFolders != null) {
3568 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003569 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 if (info != null) {
3571 openFolder(info);
3572 }
3573 }
3574 final Folder openFolder = mWorkspace.getOpenFolder();
3575 if (openFolder != null) {
3576 openFolder.requestFocus();
3577 }
3578 }
3579
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 mSavedState = null;
3581 }
3582
3583 if (mSavedInstanceState != null) {
3584 super.onRestoreInstanceState(mSavedInstanceState);
3585 mSavedInstanceState = null;
3586 }
3587
Winson Chung32bb5e52011-01-31 14:51:56 -08003588 // Workaround a bug that occurs when rotating the device while the customization mode is
3589 // open, we trigger a new layout on all the CellLayout children.
3590 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3591 final int childCount = mWorkspace.getChildCount();
3592 for (int i = 0; i < childCount; ++i) {
3593 mWorkspace.getChildAt(i).requestLayout();
3594 }
3595 }
3596
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003598
3599 // If we received the result of any pending adds while the loader was running (e.g. the
3600 // widget configuration forced an orientation change), process them now.
3601 for (int i = 0; i < sPendingAddList.size(); i++) {
3602 completeAdd(sPendingAddList.get(i));
3603 }
3604 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 }
3606
3607 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003608 * Updates the icons on the launcher that are affected by changes to the package list
3609 * on the device.
3610 */
3611 private void updateIconsAffectedByPackageManagerChanges() {
3612 updateAppMarketIcon();
3613 updateGlobalSearchIcon();
3614 updateVoiceSearchIcon();
3615 }
3616
3617 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 * Add the icons for all apps.
3619 *
3620 * Implementation of the method from LauncherModel.Callbacks.
3621 */
3622 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003623 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003624 if (mCustomizePagedView != null) {
3625 mCustomizePagedView.setApps(apps);
3626 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003627 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 }
3629
3630 /**
3631 * A package was installed.
3632 *
3633 * Implementation of the method from LauncherModel.Callbacks.
3634 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003635 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003636 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003638 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003639 if (mCustomizePagedView != null) {
3640 mCustomizePagedView.addApps(apps);
3641 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003642 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 }
3644
3645 /**
3646 * A package was updated.
3647 *
3648 * Implementation of the method from LauncherModel.Callbacks.
3649 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003650 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003651 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003653 if (mWorkspace != null) {
3654 mWorkspace.updateShortcuts(apps);
3655 }
3656 if (mAllAppsGrid != null) {
3657 mAllAppsGrid.updateApps(apps);
3658 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003659 if (mCustomizePagedView != null) {
3660 mCustomizePagedView.updateApps(apps);
3661 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003662 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 }
3664
3665 /**
3666 * A package was uninstalled.
3667 *
3668 * Implementation of the method from LauncherModel.Callbacks.
3669 */
Joe Onorato36115782010-06-17 13:28:48 -04003670 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003672 if (permanent) {
3673 mWorkspace.removeItems(apps);
3674 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003675 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003676 if (mCustomizePagedView != null) {
3677 mCustomizePagedView.removeApps(apps);
3678 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003679 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 }
Joe Onoratobe386092009-11-17 17:32:16 -08003681
3682 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003683 * A number of packages were updated.
3684 */
3685 public void bindPackagesUpdated() {
3686 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003687 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003688 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003689 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003690 }
3691
Winson Chung400438b2011-01-16 17:53:48 -08003692 private int mapConfigurationOriActivityInfoOri(int configOri) {
3693 final Display d = getWindowManager().getDefaultDisplay();
3694 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3695 switch (d.getRotation()) {
3696 case Surface.ROTATION_0:
3697 case Surface.ROTATION_180:
3698 // We are currently in the same basic orientation as the natural orientation
3699 naturalOri = configOri;
3700 break;
3701 case Surface.ROTATION_90:
3702 case Surface.ROTATION_270:
3703 // We are currently in the other basic orientation to the natural orientation
3704 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3705 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3706 break;
3707 }
3708
3709 int[] oriMap = {
3710 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3711 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3712 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3713 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3714 };
3715 // Since the map starts at portrait, we need to offset if this device's natural orientation
3716 // is landscape.
3717 int indexOffset = 0;
3718 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3719 indexOffset = 1;
3720 }
3721 return oriMap[(d.getRotation() + indexOffset) % 4];
3722 }
3723 public void lockScreenOrientation() {
3724 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3725 .getConfiguration().orientation));
3726 }
3727 public void unlockScreenOrientation() {
3728 mHandler.postDelayed(new Runnable() {
3729 public void run() {
3730 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3731 }
3732 }, mRestoreScreenOrientationDelay);
3733 }
3734
Winson Chung80baf5a2010-08-09 16:03:15 -07003735 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003736 * Prints out out state for debugging.
3737 */
3738 public void dumpState() {
3739 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003740 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003741 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3742 Log.d(TAG, "mRestoring=" + mRestoring);
3743 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3744 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3745 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003746 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003747 mModel.dumpState();
3748 mAllAppsGrid.dumpState();
3749 Log.d(TAG, "END launcher2 dump state");
3750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003751}
Michael Jurka2763be32011-02-24 11:19:57 -08003752
3753interface LauncherAnimatable {
3754 void onLauncherAnimationStart();
3755 void onLauncherAnimationEnd();
3756}