blob: 40cd74b04c6f51bfc9521b7903cc1b5fbf118d01 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurkaaf91de02010-11-23 16:23:58 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080025import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070027import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070028import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070030import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.Dialog;
33import android.app.SearchManager;
34import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070035import android.app.WallpaperManager;
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 Chung68846fd2010-10-29 11:00:27 -070047import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070048import 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 Chung68846fd2010-10-29 11:00:27 -070051import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070052import 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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.View;
87import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070088import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070089import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070093import android.widget.ImageView;
94import android.widget.LinearLayout;
95import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070096import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080097import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -070098import android.widget.TextView;
99import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700100import android.widget.TabHost.OnTabChangeListener;
101import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700102
Michael Jurkaaf91de02010-11-23 16:23:58 -0800103import java.io.DataInputStream;
104import java.io.DataOutputStream;
105import java.io.FileNotFoundException;
106import java.io.IOException;
107import java.util.ArrayList;
108import java.util.HashMap;
109import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800110
Adam Cohended9f8d2010-11-03 13:25:16 -0700111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112/**
113 * Default launcher application.
114 */
Michael Jurka946ad472010-07-09 18:05:18 -0700115public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700116 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
117 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800118 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700119 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400122 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800126 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
127
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700129 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
130 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
132 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700133 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
136 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 private static final int REQUEST_PICK_APPLICATION = 6;
139 private static final int REQUEST_PICK_SHORTCUT = 7;
140 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700142 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
145
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800146 static final int SCREEN_COUNT = 5;
147 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Joe Onorato7c312c12009-08-13 21:36:53 -0700149 static final int DIALOG_CREATE_SHORTCUT = 1;
150 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Romain Guy98d01652009-06-30 16:21:04 -0700152 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
154 // Type: int
155 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700156 // Type: int
157 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: long
159 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: boolean
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
168 // Type: long
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
170
Winson Chung80baf5a2010-08-09 16:03:15 -0700171 // tags for the customization tabs
172 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700173 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700174 private static final String SHORTCUTS_TAG = "shortcuts";
175 private static final String WALLPAPERS_TAG = "wallpapers";
176
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700177 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
178
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700179 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800180 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
181 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700182 private State mState = State.WORKSPACE;
183 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800188 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800190 private final BroadcastReceiver mCloseSystemDialogsReceiver
191 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800192 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private LayoutInflater mInflater;
195
Joe Onorato00acb122009-08-04 16:04:30 -0400196 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700199 private AppWidgetManager mAppWidgetManager;
200 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
Michael Jurka0280c3b2010-09-17 15:00:07 -0700202 private int mAddScreen = -1;
203 private int mAddIntersectCellX = -1;
204 private int mAddIntersectCellY = -1;
205 private int[] mAddDropPosition;
206 private int[] mTmpAddItemCellCoordinates = new int[2];
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private FolderInfo mFolderInfo;
209
Joe Onorato7c312c12009-08-13 21:36:53 -0700210 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700211 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700212 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700213 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700214 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700215
216 private PagedView mAllAppsPagedView = null;
217 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Bundle mSavedState;
220
221 private SpannableStringBuilder mDefaultKeySsb = null;
222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 private boolean mWorkspaceLoading = true;
224
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800225 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private boolean mRestoring;
227 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700228 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
230 private Bundle mSavedInstanceState;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800233 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800234 private boolean mUserPresent = true;
235 private boolean mVisible = false;
236 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700238 private static LocaleConfiguration sLocaleConfiguration = null;
239
Romain Guy84f296c2009-11-04 15:00:44 -0800240 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700241
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700242 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Romain Guy1fbc1c82009-11-09 20:43:08 -0800244 private ImageView mPreviousView;
245 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500246
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400247 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400248 private String[] mHotseatConfig = null;
249 private Intent[] mHotseats = null;
250 private Drawable[] mHotseatIcons = null;
251 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400252
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700253 private Intent mAppMarketIntent = null;
254
Adam Cohended9f8d2010-11-03 13:25:16 -0700255 // Related to the auto-advancing of widgets
256 private final int ADVANCE_MSG = 1;
257 private final int mAdvanceInterval = 20000;
258 private final int mAdvanceStagger = 250;
259 private long mAutoAdvanceSentTime;
260 private long mAutoAdvanceTimeLeft = -1;
261 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
262 new HashMap<View, AppWidgetProviderInfo>();
263
Michael Jurka4ef207b2010-11-29 17:05:45 -0800264 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800265 private static Drawable.ConstantState sGlobalSearchIcon;
266 private static Drawable.ConstantState sVoiceSearchIcon;
267 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 @Override
270 protected void onCreate(Bundle savedInstanceState) {
271 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700272
Winson Chungaafa03c2010-06-11 17:34:16 -0700273 if (LauncherApplication.isInPlaceRotationEnabled()) {
274 // hide the status bar (temporary until we get the status bar design figured out)
275 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
276 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
277 }
278
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 LauncherApplication app = ((LauncherApplication)getApplication());
280 mModel = app.setLauncher(this);
281 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400282 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700284
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700285 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700286 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
287 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200290 android.os.Debug.startMethodTracing(
291 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400294 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 checkForLocaleChange();
296 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800298 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700299 if (mHomeCustomizationDrawer != null) {
300 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700301
Winson Chung80baf5a2010-08-09 16:03:15 -0700302 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700303 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
304 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 TabContentFactory contentFactory = new TabContentFactory() {
306 public View createTabContent(String tag) {
307 return mCustomizePagedView;
308 }
309 };
310
Winson Chung49767ae2010-11-29 14:48:30 -0800311
312 TextView tabView;
313 TabWidget tabWidget = (TabWidget)
314 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
315
316 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
317 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700318 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800319 .setIndicator(tabView).setContent(contentFactory));
320 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
321 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700322 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800323 .setIndicator(tabView).setContent(contentFactory));
324 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
325 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700326 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800327 .setIndicator(tabView).setContent(contentFactory));
328 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
329 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700330 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800331 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700332 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
333 public void onTabChanged(String tabId) {
334 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800335 final Resources res = getResources();
336 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700338 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700339 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700340 alphaAnim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -0800341 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700342 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -0800343 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700344 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
345 if (tag == WIDGETS_TAG) {
346 mCustomizePagedView.setCustomizationFilter(
347 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700348 } else if (tag == APPLICATIONS_TAG) {
349 mCustomizePagedView.setCustomizationFilter(
350 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700351 } else if (tag == WALLPAPERS_TAG) {
352 mCustomizePagedView.setCustomizationFilter(
353 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700354 } else if (tag == SHORTCUTS_TAG) {
355 mCustomizePagedView.setCustomizationFilter(
356 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700357 }
358
359 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700360 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700361 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700362 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700363 alphaAnim.start();
364 }
365 });
366 alphaAnim.start();
367 }
368 });
Michael Jurka946ad472010-07-09 18:05:18 -0700369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 setupViews();
371
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800372 registerContentObservers();
373
Joe Onorato7c312c12009-08-13 21:36:53 -0700374 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 mSavedState = savedInstanceState;
377 restoreState(mSavedState);
378
379 if (PROFILE_STARTUP) {
380 android.os.Debug.stopMethodTracing();
381 }
382
383 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386
387 // For handling default keys
388 mDefaultKeySsb = new SpannableStringBuilder();
389 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800390
391 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
392 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800393
394 // If we have a saved version of these external icons, we load them up immediately
395 if (LauncherApplication.isScreenXLarge()) {
396 if (sGlobalSearchIcon != null) {
397 updateGlobalSearchIcon(sGlobalSearchIcon);
398 }
399 if (sVoiceSearchIcon != null) {
400 updateVoiceSearchIcon(sVoiceSearchIcon);
401 }
402 if (sAppMarketIcon != null) {
403 updateAppMarketIcon(sAppMarketIcon);
404 }
405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
Romain Guycbb89e42009-06-08 15:52:54 -0700407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409 if (sLocaleConfiguration == null) {
410 new AsyncTask<Void, Void, LocaleConfiguration>() {
411 @Override
412 protected LocaleConfiguration doInBackground(Void... unused) {
413 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
414 readConfiguration(Launcher.this, localeConfiguration);
415 return localeConfiguration;
416 }
417
418 @Override
419 protected void onPostExecute(LocaleConfiguration result) {
420 sLocaleConfiguration = result;
421 checkForLocaleChange(); // recursive, but now with a locale configuration
422 }
423 }.execute();
424 return;
425 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 final Configuration configuration = getResources().getConfiguration();
428
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700429 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 final String locale = configuration.locale.toString();
431
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 final int mcc = configuration.mcc;
434
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700435 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 final int mnc = configuration.mnc;
437
Romain Guy84f296c2009-11-04 15:00:44 -0800438 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439
Romain Guy84f296c2009-11-04 15:00:44 -0800440 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700441 sLocaleConfiguration.locale = locale;
442 sLocaleConfiguration.mcc = mcc;
443 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700444
Joe Onorato0589f0f2010-02-08 13:44:00 -0800445 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400446 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700447
448 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
449 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700450 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700451 public void run() {
452 writeConfiguration(Launcher.this, localeConfiguration);
453 }
454 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700455 }
456 }
457
458 private static class LocaleConfiguration {
459 public String locale;
460 public int mcc = -1;
461 public int mnc = -1;
462 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700463
Romain Guy98d01652009-06-30 16:21:04 -0700464 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
465 DataInputStream in = null;
466 try {
467 in = new DataInputStream(context.openFileInput(PREFERENCES));
468 configuration.locale = in.readUTF();
469 configuration.mcc = in.readInt();
470 configuration.mnc = in.readInt();
471 } catch (FileNotFoundException e) {
472 // Ignore
473 } catch (IOException e) {
474 // Ignore
475 } finally {
476 if (in != null) {
477 try {
478 in.close();
479 } catch (IOException e) {
480 // Ignore
481 }
482 }
483 }
484 }
485
486 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
487 DataOutputStream out = null;
488 try {
489 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
490 out.writeUTF(configuration.locale);
491 out.writeInt(configuration.mcc);
492 out.writeInt(configuration.mnc);
493 out.flush();
494 } catch (FileNotFoundException e) {
495 // Ignore
496 } catch (IOException e) {
497 //noinspection ResultOfMethodCallIgnored
498 context.getFileStreamPath(PREFERENCES).delete();
499 } finally {
500 if (out != null) {
501 try {
502 out.close();
503 } catch (IOException e) {
504 // Ignore
505 }
506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 }
508 }
509
510 static int getScreen() {
511 synchronized (sLock) {
512 return sScreen;
513 }
514 }
515
516 static void setScreen(int screen) {
517 synchronized (sLock) {
518 sScreen = screen;
519 }
520 }
521
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700523 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524
525 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700526 // TODO: Put back when we decide about scrolling the wallpaper
527 // boolean isPortrait = display.getWidth() < display.getHeight();
528 // final int width = isPortrait ? display.getWidth() : display.getHeight();
529 // final int height = isPortrait ? display.getHeight() : display.getWidth();
530 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
531 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 }
533
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400534 // Note: This doesn't do all the client-id magic that BrowserProvider does
535 // in Browser. (http://b/2425179)
536 private Uri getDefaultBrowserUri() {
537 String url = getString(R.string.default_browser_url);
538 if (url.indexOf("{CID}") != -1) {
539 url = url.replace("{CID}", "android-google");
540 }
541 return Uri.parse(url);
542 }
543
544 // Load the Intent templates from arrays.xml to populate the hotseats. For
545 // each Intent, if it resolves to a single app, use that as the launch
546 // intent & use that app's label as the contentDescription. Otherwise,
547 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400548 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 if (mHotseatConfig == null) {
550 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
551 if (mHotseatConfig.length > 0) {
552 mHotseats = new Intent[mHotseatConfig.length];
553 mHotseatLabels = new CharSequence[mHotseatConfig.length];
554 mHotseatIcons = new Drawable[mHotseatConfig.length];
555 } else {
556 mHotseats = null;
557 mHotseatIcons = null;
558 mHotseatLabels = null;
559 }
560
561 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
562 for (int i=0; i<mHotseatConfig.length; i++) {
563 // load icon for this slot; currently unrelated to the actual activity
564 try {
565 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
566 } catch (ArrayIndexOutOfBoundsException ex) {
567 Log.w(TAG, "Missing hotseat_icons array item #" + i);
568 mHotseatIcons[i] = null;
569 }
570 }
571 hotseatIconDrawables.recycle();
572 }
573
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400574 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 for (int i=0; i<mHotseatConfig.length; i++) {
576 Intent intent = null;
577 if (mHotseatConfig[i].equals("*BROWSER*")) {
578 // magic value meaning "launch user's default web browser"
579 // replace it with a generic web request so we can see if there is indeed a default
580 String defaultUri = getString(R.string.default_browser_url);
581 intent = new Intent(
582 Intent.ACTION_VIEW,
583 ((defaultUri != null)
584 ? Uri.parse(defaultUri)
585 : getDefaultBrowserUri())
586 ).addCategory(Intent.CATEGORY_BROWSABLE);
587 // note: if the user launches this without a default set, she
588 // will always be taken to the default URL above; this is
589 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700590 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 // URL is unavoidably sent to the chosen app.
592 } else {
593 try {
594 intent = Intent.parseUri(mHotseatConfig[i], 0);
595 } catch (java.net.URISyntaxException ex) {
596 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
597 // bogus; leave intent=null
598 }
599 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700600
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400601 if (intent == null) {
602 mHotseats[i] = null;
603 mHotseatLabels[i] = getText(R.string.activity_not_found);
604 continue;
605 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400606
607 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700608 Log.d(TAG, "loadHotseats: hotseat " + i
609 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400610 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400611 + "]");
612 }
613
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400614 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
615 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400617 Log.d(TAG, "Best match for intent: " + bestMatch);
618 Log.d(TAG, "All matches: ");
619 for (ResolveInfo ri : allMatches) {
620 Log.d(TAG, " --> " + ri);
621 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400622 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400623 // did this resolve to a single app, or the resolver?
624 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 // can't find any activity to handle this. let's leave the
626 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400627 // to launch.
628 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400629
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400630 // set accessibility text to "Not installed"
631 mHotseatLabels[i] = getText(R.string.activity_not_found);
632 } else {
633 boolean found = false;
634 for (ResolveInfo ri : allMatches) {
635 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
636 && bestMatch.activityInfo.applicationInfo.packageName
637 .equals(ri.activityInfo.applicationInfo.packageName)) {
638 found = true;
639 break;
640 }
641 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700642
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400643 if (!found) {
644 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
645 // the bestMatch is probably the ResolveActivity, meaning the
646 // user has not yet selected a default
647 // so: we'll keep the original intent for now
648 mHotseats[i] = intent;
649
650 // set the accessibility text to "Select shortcut"
651 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
652 } else {
653 // we have an app!
654 // now reconstruct the intent to launch it through the front
655 // door
656 ComponentName com = new ComponentName(
657 bestMatch.activityInfo.applicationInfo.packageName,
658 bestMatch.activityInfo.name);
659 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
660
661 // load the app label for accessibility
662 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
663 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400664 }
665
666 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700667 Log.d(TAG, "loadHotseats: hotseat " + i
668 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400669 + ((mHotseats[i] == null)
670 ? "null"
671 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400672 + "] label=[" + mHotseatLabels[i]
673 + "]"
674 );
675 }
676 }
677 }
678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 @Override
680 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700681 mWaitingForResult = false;
682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 // The pattern used here is that a user PICKs a specific application,
684 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
687 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700688
Michael Jurka0280c3b2010-09-17 15:00:07 -0700689 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 switch (requestCode) {
691 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700692 completeAddApplication(
693 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 break;
695 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800696 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 break;
698 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700699 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 break;
701 case REQUEST_PICK_LIVE_FOLDER:
702 addLiveFolder(data);
703 break;
704 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700708 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700712 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700714 case REQUEST_PICK_WALLPAPER:
715 // We just wanted the activity result here so we can clear mWaitingForResult
716 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
Romain Guy18042c82009-11-06 11:44:55 -0800718 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
719 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
720 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 // Clean up the appWidgetId if we canceled
722 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
723 if (appWidgetId != -1) {
724 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
728
729 @Override
730 protected void onResume() {
731 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800732 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700733 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 mWorkspaceLoading = true;
735 mModel.startLoader(this, true);
736 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700737 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700739 // When we resume Launcher, a different Activity might be responsible for the app
740 // market intent, so refresh the icon
741 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743
744 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700745 protected void onPause() {
746 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700747 // Some launcher layouts don't have a previous and next view
748 if (mPreviousView != null) {
749 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700750 }
751 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700752 dismissPreview(mNextView);
753 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700754 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800755 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700756 }
Romain Guycbb89e42009-06-08 15:52:54 -0700757
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700758 @Override
759 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 // Flag the loader to stop early before switching
761 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800762 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800763 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700764 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800766 // We can't hide the IME if it was forced open. So don't bother
767 /*
768 @Override
769 public void onWindowFocusChanged(boolean hasFocus) {
770 super.onWindowFocusChanged(hasFocus);
771
772 if (hasFocus) {
773 final InputMethodManager inputManager = (InputMethodManager)
774 getSystemService(Context.INPUT_METHOD_SERVICE);
775 WindowManager.LayoutParams lp = getWindow().getAttributes();
776 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
777 android.os.Handler()) {
778 protected void onReceiveResult(int resultCode, Bundle resultData) {
779 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
780 }
781 });
782 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
783 }
784 }
785 */
786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 private boolean acceptFilter() {
788 final InputMethodManager inputManager = (InputMethodManager)
789 getSystemService(Context.INPUT_METHOD_SERVICE);
790 return !inputManager.isFullscreenMode();
791 }
792
793 @Override
794 public boolean onKeyDown(int keyCode, KeyEvent event) {
795 boolean handled = super.onKeyDown(keyCode, event);
796 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
797 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
798 keyCode, event);
799 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700800 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700801 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700802 // showSearchDialog()
803 // If there are multiple keystrokes before the search dialog takes focus,
804 // onSearchRequested() will be called for every keystroke,
805 // but it is idempotent, so it's fine.
806 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808 }
809
Joe Onorato8a9625e2010-01-28 15:55:35 -0800810 // Eat the long press event so the keyboard doesn't come up.
811 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
812 return true;
813 }
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 return handled;
816 }
817
Karl Rosaen138a0412009-04-23 19:00:21 -0700818 private String getTypedText() {
819 return mDefaultKeySsb.toString();
820 }
821
822 private void clearTypedText() {
823 mDefaultKeySsb.clear();
824 mDefaultKeySsb.clearSpans();
825 Selection.setSelection(mDefaultKeySsb, 0);
826 }
827
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700829 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
830 * State
831 */
832 private static State intToState(int stateOrdinal) {
833 State state = State.WORKSPACE;
834 final State[] stateValues = State.values();
835 for (int i = 0; i < stateValues.length; i++) {
836 if (stateValues[i].ordinal() == stateOrdinal) {
837 state = stateValues[i];
838 break;
839 }
840 }
841 return state;
842 }
843
844 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 * Restores the previous state, if it exists.
846 *
847 * @param savedState The previous state.
848 */
849 private void restoreState(Bundle savedState) {
850 if (savedState == null) {
851 return;
852 }
853
Michael Jurka883f55b2010-10-21 15:47:14 -0700854 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
855
856 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800857 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700858 } else if (state == State.CUSTOMIZE) {
859 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800860 }
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
863 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700864 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 }
866
867 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700870 mAddScreen = addScreen;
871 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
872 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 mRestoring = true;
874 }
875
876 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
877 if (renameFolder) {
878 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700879 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 mRestoring = true;
881 }
882 }
883
884 /**
885 * Finds all the views we need and configure them properly.
886 */
887 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700888 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400889
Romain Guyb1b69f52009-08-10 15:10:15 -0700890 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400891 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892
Joe Onorato7c312c12009-08-13 21:36:53 -0700893 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700894 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700895 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800896 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700897 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700898 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700899
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700900 if (LauncherApplication.isScreenXLarge()) {
901 // They need to be INVISIBLE initially so that they will be measured in the layout.
902 // Otherwise the animations are messed up when we show them for the first time.
903 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
904 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
905 }
906
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500910 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700911
Joe Onorato7c312c12009-08-13 21:36:53 -0700912 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
913 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700915 View handleView = findViewById(R.id.all_apps_button);
916 if (handleView != null && handleView instanceof HandleView) {
917 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700918 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700919 mHandleView.setLauncher(this);
920 mHandleView.setOnClickListener(this);
921 mHandleView.setOnLongClickListener(this);
922 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800923
Winson Chung80baf5a2010-08-09 16:03:15 -0700924 if (mCustomizePagedView != null) {
925 mCustomizePagedView.setLauncher(this);
926 mCustomizePagedView.setDragController(dragController);
927 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700928 } else {
929 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
930 hotseatLeft.setContentDescription(mHotseatLabels[0]);
931 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
932 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
933 hotseatRight.setContentDescription(mHotseatLabels[1]);
934 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400935
Michael Jurkaaf442092010-06-10 17:01:57 -0700936 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
937 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800938
Michael Jurkaaf442092010-06-10 17:01:57 -0700939 Drawable previous = mPreviousView.getDrawable();
940 Drawable next = mNextView.getDrawable();
941 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
Michael Jurkaaf442092010-06-10 17:01:57 -0700943 mPreviousView.setHapticFeedbackEnabled(false);
944 mPreviousView.setOnLongClickListener(this);
945 mNextView.setHapticFeedbackEnabled(false);
946 mNextView.setOnLongClickListener(this);
947 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400950 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952
953 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400954 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700955 int deleteZoneHandleId;
956 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700957 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700958 } else {
959 deleteZoneHandleId = R.id.all_apps_button_cluster;
960 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700961 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700962 dragController.addDragListener(deleteZone);
963
Adam Cohencdc30d52010-12-01 15:09:47 -0800964 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
965 if (allAppsDeleteZone != null) {
966 allAppsDeleteZone.setLauncher(this);
967 allAppsDeleteZone.setDragController(dragController);
968 allAppsDeleteZone.setDragAndDropEnabled(false);
969 dragController.addDragListener(allAppsDeleteZone);
970 dragController.addDropTarget(allAppsDeleteZone);
971 }
972
973 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
974 findViewById(R.id.all_apps_info_target);
975 if (allAppsInfoTarget != null) {
976 allAppsInfoTarget.setLauncher(this);
977 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800978 allAppsInfoTarget.setDragAndDropEnabled(false);
979 View marketButton = findViewById(R.id.market_button);
980 if (marketButton != null) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800981 allAppsInfoTarget.setHandle(marketButton);
982 }
983 }
984
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700985 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
986 if (infoButton != null) {
987 infoButton.setLauncher(this);
988 infoButton.setHandle(findViewById(R.id.configure_button));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700989 dragController.addDragListener(infoButton);
990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Joe Onorato00acb122009-08-04 16:04:30 -0400992 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400993 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800994 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700995
Joe Onorato00acb122009-08-04 16:04:30 -0400996 // The order here is bottom to top.
997 dragController.addDropTarget(workspace);
998 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700999 if (infoButton != null) {
1000 dragController.addDropTarget(infoButton);
1001 }
Adam Cohencdc30d52010-12-01 15:09:47 -08001002 if (allAppsInfoTarget != null) {
1003 dragController.addDropTarget(allAppsInfoTarget);
1004 }
1005 if (allAppsDeleteZone != null) {
1006 dragController.addDropTarget(allAppsDeleteZone);
1007 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 }
1009
Romain Guy8a73c512009-11-09 19:19:59 -08001010 @SuppressWarnings({"UnusedDeclaration"})
1011 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001012 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001013 mWorkspace.scrollLeft();
1014 }
Romain Guy8a73c512009-11-09 19:19:59 -08001015 }
1016
1017 @SuppressWarnings({"UnusedDeclaration"})
1018 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001019 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001020 mWorkspace.scrollRight();
1021 }
Romain Guy8a73c512009-11-09 19:19:59 -08001022 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001023
1024 @SuppressWarnings({"UnusedDeclaration"})
1025 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001026 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001027
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001028 int index = -1;
1029 if (v.getId() == R.id.hotseat_left) {
1030 index = 0;
1031 } else if (v.getId() == R.id.hotseat_right) {
1032 index = 1;
1033 }
1034
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001035 // reload these every tap; you never know when they might change
1036 loadHotseats();
1037 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1038 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001039 startActivitySafely(
1040 mHotseats[index],
1041 "hotseat"
1042 );
1043 }
1044 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 /**
1047 * Creates a view representing a shortcut.
1048 *
1049 * @param info The data structure describing the shortcut.
1050 *
1051 * @return A View inflated from R.layout.application.
1052 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001053 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001055 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
1058 /**
1059 * Creates a view representing a shortcut inflated from the specified resource.
1060 *
1061 * @param layoutResId The id of the XML layout used to create the shortcut.
1062 * @param parent The group the shortcut belongs to.
1063 * @param info The data structure describing the shortcut.
1064 *
1065 * @return A View inflated from layoutResId.
1066 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001067 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001068 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1069 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 return favorite;
1072 }
1073
1074 /**
1075 * Add an application shortcut to the workspace.
1076 *
1077 * @param data The intent describing the application.
1078 * @param cellInfo The position on screen where to create the shortcut.
1079 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 void completeAddApplication(Context context, Intent data, int screen,
1081 int intersectCellX, int intersectCellY) {
1082 final int[] cellXY = mTmpAddItemCellCoordinates;
1083 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1084
1085 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1086 showOutOfSpaceMessage();
1087 return;
1088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1091 data, context);
1092
Romain Guy73b979d2009-06-09 12:57:21 -07001093 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001094 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001096 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1098 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001099 } else {
1100 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
1102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 /**
1105 * Add a shortcut to the workspace.
1106 *
1107 * @param data The intent describing the shortcut.
1108 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 private void completeAddShortcut(Intent data, int screen,
1111 int intersectCellX, int intersectCellY) {
1112 final int[] cellXY = mTmpAddItemCellCoordinates;
1113 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Michael Jurkad3ef3062010-11-23 16:23:58 -08001115 int[] touchXY = null;
1116 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1117 touchXY = mAddDropPosition;
1118 }
1119 boolean foundCellSpan = false;
1120 if (touchXY != null) {
1121 // when dragging and dropping, just find the closest free spot
1122 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1123 int[] result = screenLayout.findNearestVacantArea(
1124 touchXY[0], touchXY[1], 1, 1, cellXY);
1125 foundCellSpan = (result != null);
1126 } else {
1127 foundCellSpan = layout.findCellForSpanThatIntersects(
1128 cellXY, 1, 1, intersectCellX, intersectCellY);
1129 }
1130
1131 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001132 showOutOfSpaceMessage();
1133 return;
1134 }
1135
1136 final ShortcutInfo info = mModel.addShortcut(
1137 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138
1139 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143 }
1144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001148 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001149 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001151 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001152 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001153
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001154 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001155 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1156 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001159 // We have saved the position to which the widget was dragged-- this really only matters
1160 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001162
1163 // For now, we don't save the coordinate where we dropped the icon because we're not
1164 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1165 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001166 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001167 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001168 touchXY = mAddDropPosition;
1169 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001170 boolean foundCellSpan = false;
1171 if (touchXY != null) {
1172 // when dragging and dropping, just find the closest free spot
1173 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1174 int[] result = screenLayout.findNearestVacantArea(
1175 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001176 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001177 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001178 // if we long pressed on an empty cell to bring up a menu,
1179 // make sure we intersect the empty cell
1180 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1181 // findCellForSpanThatIntersects will just ignore them
1182 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1183 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001184 }
1185
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001187 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001189 return;
1190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 // Build Launcher-specific widget info and save to database
1193 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194 launcherInfo.spanX = spanXY[0];
1195 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 LauncherModel.addItemToDatabase(this, launcherInfo,
1198 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001199 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200
1201 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001205 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001206
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001209
Michael Jurka0280c3b2010-09-17 15:00:07 -07001210 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001212
1213 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 }
1215 }
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Adam Cohended9f8d2010-11-03 13:25:16 -07001217 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1218 @Override
1219 public void onReceive(Context context, Intent intent) {
1220 final String action = intent.getAction();
1221 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1222 mUserPresent = false;
1223 updateRunning();
1224 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1225 mUserPresent = true;
1226 updateRunning();
1227 }
1228 }
1229 };
1230
1231 @Override
1232 public void onAttachedToWindow() {
1233 super.onAttachedToWindow();
1234
1235 // Listen for broadcasts related to user-presence
1236 final IntentFilter filter = new IntentFilter();
1237 filter.addAction(Intent.ACTION_SCREEN_OFF);
1238 filter.addAction(Intent.ACTION_USER_PRESENT);
1239 registerReceiver(mReceiver, filter);
1240
Adam Cohend113e0c2010-11-11 10:48:05 -08001241 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001242 mVisible = true;
1243 }
1244
1245 @Override
1246 public void onDetachedFromWindow() {
1247 super.onDetachedFromWindow();
1248 mVisible = false;
1249
Adam Cohend113e0c2010-11-11 10:48:05 -08001250 if (mAttached) {
1251 unregisterReceiver(mReceiver);
1252 mAttached = false;
1253 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001254 updateRunning();
1255 }
1256
1257 public void onWindowVisibilityChanged(int visibility) {
1258 mVisible = visibility == View.VISIBLE;
1259 updateRunning();
1260 }
1261
1262 private void sendAdvanceMessage(long delay) {
1263 mHandler.removeMessages(ADVANCE_MSG);
1264 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1265 mHandler.sendMessageDelayed(msg, delay);
1266 mAutoAdvanceSentTime = System.currentTimeMillis();
1267 }
1268
1269 private void updateRunning() {
1270 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1271 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1272 mAutoAdvanceRunning = autoAdvanceRunning;
1273 if (autoAdvanceRunning) {
1274 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1275 sendAdvanceMessage(delay);
1276 } else {
1277 if (!mWidgetsToAdvance.isEmpty()) {
1278 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1279 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1280 }
1281 mHandler.removeMessages(ADVANCE_MSG);
1282 }
1283 }
1284 }
1285
1286 private final Handler mHandler = new Handler() {
1287 @Override
1288 public void handleMessage(Message msg) {
1289 if (msg.what == ADVANCE_MSG) {
1290 int i = 0;
1291 for (View key: mWidgetsToAdvance.keySet()) {
1292 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1293 final int delay = mAdvanceStagger * i;
1294 if (v instanceof Advanceable) {
1295 postDelayed(new Runnable() {
1296 public void run() {
1297 ((Advanceable) v).advance();
1298 }
1299 }, delay);
1300 }
1301 i++;
1302 }
1303 sendAdvanceMessage(mAdvanceInterval);
1304 }
1305 }
1306 };
1307
1308 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1309 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1310 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1311 if (v instanceof Advanceable) {
1312 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1313 ((Advanceable) v).willBeAdvancedByHost();
1314 updateRunning();
1315 }
1316 }
1317
1318 void removeWidgetToAutoAdvance(View hostView) {
1319 if (mWidgetsToAdvance.containsKey(hostView)) {
1320 mWidgetsToAdvance.remove(hostView);
1321 updateRunning();
1322 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323 }
1324
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1326 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001327 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 launcherInfo.hostView = null;
1329 }
1330
Adam Cohended9f8d2010-11-03 13:25:16 -07001331 void showOutOfSpaceMessage() {
1332 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1333 }
1334
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001335 public LauncherAppWidgetHost getAppWidgetHost() {
1336 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
Romain Guycbb89e42009-06-08 15:52:54 -07001338
Winson Chunga9abd0e2010-10-27 17:18:37 -07001339 public LauncherModel getModel() {
1340 return mModel;
1341 }
1342
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001343 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001344 getWindow().closeAllPanels();
1345
1346 try {
1347 dismissDialog(DIALOG_CREATE_SHORTCUT);
1348 // Unlock the workspace if the dialog was showing
1349 } catch (Exception e) {
1350 // An exception is thrown if the dialog is not visible, which is fine
1351 }
1352
1353 try {
1354 dismissDialog(DIALOG_RENAME_FOLDER);
1355 // Unlock the workspace if the dialog was showing
1356 } catch (Exception e) {
1357 // An exception is thrown if the dialog is not visible, which is fine
1358 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001359
1360 // Whatever we were doing is hereby canceled.
1361 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001362 }
1363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 @Override
1365 protected void onNewIntent(Intent intent) {
1366 super.onNewIntent(intent);
1367
1368 // Close the menu
1369 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001370 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001371 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001372
Joe Onorato14f122b2009-11-19 14:06:36 -08001373 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1374 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001375
Michael Jurka4cb37242010-08-09 21:05:32 -07001376 // in all these cases, only animate if we're already on home
1377 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001378 mWorkspace.unshrink(alreadyOnHome);
1379 }
1380 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001381 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001382 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1383 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001384 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001385 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001386
Joe Onorato3a8820b2009-11-10 15:06:42 -08001387 final View v = getWindow().peekDecorView();
1388 if (v != null && v.getWindowToken() != null) {
1389 InputMethodManager imm = (InputMethodManager)getSystemService(
1390 INPUT_METHOD_SERVICE);
1391 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393 }
1394 }
1395
1396 @Override
1397 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1398 // Do not call super here
1399 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001400
Winson Chung43b119d2010-12-06 16:45:05 -08001401 // Restore the current AllApps drawer tab
1402 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1403 String cur = savedInstanceState.getString("allapps_currentTab");
1404 if (cur != null) {
1405 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1406 tabhost.setCurrentTabByTag(cur);
1407 }
1408 }
1409
1410 // Restore the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001411 if (mHomeCustomizationDrawer != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001412 String cur = savedInstanceState.getString("customize_currentTab");
Michael Jurka946ad472010-07-09 18:05:18 -07001413 if (cur != null) {
1414 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1415 }
1416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418
1419 @Override
1420 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001421 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422
1423 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1424 if (folders.size() > 0) {
1425 final int count = folders.size();
1426 long[] ids = new long[count];
1427 for (int i = 0; i < count; i++) {
1428 final FolderInfo info = folders.get(i).getInfo();
1429 ids[i] = info.id;
1430 }
1431 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1432 } else {
1433 super.onSaveInstanceState(outState);
1434 }
1435
Michael Jurka883f55b2010-10-21 15:47:14 -07001436 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437
Michael Jurka0280c3b2010-09-17 15:00:07 -07001438 if (mAddScreen > -1 && mWaitingForResult) {
1439 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1441 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443
1444 if (mFolderInfo != null && mWaitingForResult) {
1445 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1446 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1447 }
Michael Jurka946ad472010-07-09 18:05:18 -07001448
Winson Chung43b119d2010-12-06 16:45:05 -08001449 // Save the current AllApps drawer tab
1450 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1451 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1452 String currentTabTag = tabhost.getCurrentTabTag();
1453 if (currentTabTag != null) {
1454 outState.putString("allapps_currentTab", currentTabTag);
1455 }
1456 }
1457
1458 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001459 if (mHomeCustomizationDrawer != null) {
1460 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1461 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001462 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001463 }
1464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466
1467 @Override
1468 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001472 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001474 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476
1477 TextKeyListener.getInstance().release();
1478
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001482
1483 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001484
Patrick Dubroyab962b72010-07-26 12:10:10 -07001485 // Some launcher layouts don't have a previous and next view
1486 if (mPreviousView != null) {
1487 dismissPreview(mPreviousView);
1488 }
1489 if (mNextView != null) {
1490 dismissPreview(mNextView);
1491 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001492
1493 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 }
1495
1496 @Override
1497 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001498 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 super.startActivityForResult(intent, requestCode);
1500 }
1501
1502 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001503 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001505
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001506 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001507
Karl Rosaen138a0412009-04-23 19:00:21 -07001508 if (initialQuery == null) {
1509 // Use any text typed in the launcher as the initial query
1510 initialQuery = getTypedText();
1511 clearTypedText();
1512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 if (appSearchData == null) {
1514 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001515 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Karl Rosaen138a0412009-04-23 19:00:21 -07001518 final SearchManager searchManager =
1519 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001520 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001521 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
1523
1524 @Override
1525 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001526 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001527 return false;
1528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1533 .setIcon(android.R.drawable.ic_menu_add)
1534 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001535 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1536 .setIcon(android.R.drawable.ic_menu_manage)
1537 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001538 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 .setIcon(android.R.drawable.ic_menu_gallery)
1540 .setAlphabeticShortcut('W');
1541 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1542 .setIcon(android.R.drawable.ic_search_category_default)
1543 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1544 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1545 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1546 .setAlphabeticShortcut('N');
1547
1548 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001549 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1550 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1553 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1554 .setIntent(settings);
1555
1556 return true;
1557 }
1558
1559 @Override
1560 public boolean onPrepareOptionsMenu(Menu menu) {
1561 super.onPrepareOptionsMenu(menu);
1562
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001563 // If all apps is animating, don't show the menu, because we don't know
1564 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001565 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001566 return false;
1567 }
1568
1569 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001570 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001571 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1572 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1573
1574 // Disable add if the workspace is full.
1575 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001576 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1577 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
1580 return true;
1581 }
1582
1583 @Override
1584 public boolean onOptionsItemSelected(MenuItem item) {
1585 switch (item.getItemId()) {
1586 case MENU_ADD:
1587 addItems();
1588 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001589 case MENU_MANAGE_APPS:
1590 manageApps();
1591 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 case MENU_WALLPAPER_SETTINGS:
1593 startWallpaper();
1594 return true;
1595 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001596 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 return true;
1598 case MENU_NOTIFICATIONS:
1599 showNotifications();
1600 return true;
1601 }
1602
1603 return super.onOptionsItemSelected(item);
1604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Karl Rosaen138a0412009-04-23 19:00:21 -07001606 /**
1607 * Indicates that we want global search for this activity by setting the globalSearch
1608 * argument for {@link #startSearch} to true.
1609 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001611 @Override
1612 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001613 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001614 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001615 }
1616
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 public boolean isWorkspaceLocked() {
1618 return mWorkspaceLoading || mWaitingForResult;
1619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001622 if (LauncherApplication.isScreenXLarge()) {
1623 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001624 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001625 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001626 }
1627 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001628 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001629 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 }
1632
Michael Jurka0280c3b2010-09-17 15:00:07 -07001633 private void resetAddInfo() {
1634 mAddScreen = -1;
1635 mAddIntersectCellX = -1;
1636 mAddIntersectCellY = -1;
1637 mAddDropPosition = null;
1638 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001639
Winson Chung55cef262010-10-28 14:14:18 -07001640 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001641 resetAddInfo();
1642 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001643 mAddDropPosition = position;
1644
Michael Jurkaaf442092010-06-10 17:01:57 -07001645 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001646 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1647 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001648 }
1649
Winson Chung7ad01412010-09-27 11:33:03 -07001650 private void manageApps() {
1651 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1652 }
1653
Michael Jurkaaf442092010-06-10 17:01:57 -07001654 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001655 // TODO: catch bad widget exception when sent
1656 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001657 // TODO: Is this log message meaningful?
1658 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001659 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001660 }
1661
Winson Chung55cef262010-10-28 14:14:18 -07001662 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001663 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664
Bjorn Bringert7984c942009-12-09 15:38:25 +00001665 if (appWidget.configure != null) {
1666 // Launch over to configure widget, if needed
1667 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1668 intent.setComponent(appWidget.configure);
1669 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001670 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001671 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1672 intent.putExtra(
1673 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1674 info.mimeType);
1675
1676 final String mimeType = info.mimeType;
1677 final ClipData clipData = (ClipData) info.configurationData;
1678 final ClipDescription clipDesc = clipData.getDescription();
1679 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1680 if (clipDesc.getMimeType(i).equals(mimeType)) {
1681 final ClipData.Item item = clipData.getItem(i);
1682 final CharSequence stringData = item.getText();
1683 final Uri uriData = item.getUri();
1684 final Intent intentData = item.getIntent();
1685 final String key =
1686 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1687 if (uriData != null) {
1688 intent.putExtra(key, uriData);
1689 } else if (intentData != null) {
1690 intent.putExtra(key, intentData);
1691 } else if (stringData != null) {
1692 intent.putExtra(key, stringData);
1693 }
1694 break;
1695 }
1696 }
1697 }
Winson Chung55cef262010-10-28 14:14:18 -07001698 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001699
Romain Guy8e633c52010-03-04 12:51:36 -08001700 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001702 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001703 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705 }
Romain Guycbb89e42009-06-08 15:52:54 -07001706
Michael Jurka0280c3b2010-09-17 15:00:07 -07001707 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1708 resetAddInfo();
1709 mAddScreen = screen;
1710 mAddDropPosition = position;
1711
1712 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1713 createShortcutIntent.setComponent(componentName);
1714 processShortcut(createShortcutIntent);
1715 }
1716
Joe Onoratodeb98af2010-02-19 14:59:39 -08001717 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001718 // Handle case where user selected "Applications"
1719 String applicationName = getResources().getString(R.string.group_applications);
1720 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001722 if (applicationName != null && applicationName.equals(shortcutName)) {
1723 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1724 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001725
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001726 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1727 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001728 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001729 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001730 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001731 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 }
1734
Winson Chung24ab2f12010-09-16 14:10:47 -07001735 void processWallpaper(Intent intent) {
1736 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1737 }
1738
Michael Jurka0280c3b2010-09-17 15:00:07 -07001739 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1740 resetAddInfo();
1741 mAddScreen = screen;
1742 mAddDropPosition = position;
1743
1744 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1745 createFolderIntent.setComponent(componentName);
1746
1747 addLiveFolder(createFolderIntent);
1748 }
1749
1750 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001751 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001752 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001753 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001754
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001755 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001756 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001757 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001758 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
1761
Michael Jurka0280c3b2010-09-17 15:00:07 -07001762 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 UserFolderInfo folderInfo = new UserFolderInfo();
1764 folderInfo.title = getText(R.string.folder_name);
1765
Michael Jurka0280c3b2010-09-17 15:00:07 -07001766 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1767 final int[] cellXY = mTmpAddItemCellCoordinates;
1768 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1769 showOutOfSpaceMessage();
1770 return;
1771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772
1773 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774 LauncherModel.addItemToDatabase(this, folderInfo,
1775 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001777 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778
1779 // Create the view
1780 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001781 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1782 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001783 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 }
Romain Guycbb89e42009-06-08 15:52:54 -07001785
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001787 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788 }
1789
Michael Jurka28750fb2010-09-24 17:43:49 -07001790 private void completeAddLiveFolder(
1791 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001792 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1793 final int[] cellXY = mTmpAddItemCellCoordinates;
1794 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1795 showOutOfSpaceMessage();
1796 return;
1797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798
Michael Jurka0280c3b2010-09-17 15:00:07 -07001799 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800
1801 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001803 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001804 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806 }
1807
1808 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001809 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810
1811 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1812 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1813
1814 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 Intent.ShortcutIconResource iconResource = null;
1816
1817 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1818 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1819 try {
1820 iconResource = (Intent.ShortcutIconResource) extra;
1821 final PackageManager packageManager = context.getPackageManager();
1822 Resources resources = packageManager.getResourcesForApplication(
1823 iconResource.packageName);
1824 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1825 icon = resources.getDrawable(id);
1826 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001827 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
1829 }
1830
1831 if (icon == null) {
1832 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1833 }
1834
1835 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001836 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 info.title = name;
1838 info.iconResource = iconResource;
1839 info.uri = data.getData();
1840 info.baseIntent = baseIntent;
1841 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1842 LiveFolders.DISPLAY_MODE_GRID);
1843
1844 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001846 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847
1848 return info;
1849 }
1850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 private void showNotifications() {
1852 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1853 if (statusBar != null) {
1854 statusBar.expand();
1855 }
1856 }
1857
1858 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001859 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001861 Intent chooser = Intent.createChooser(pickWallpaper,
1862 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001863 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1864 // Removed in Eclair MR1
1865// WallpaperManager wm = (WallpaperManager)
1866// getSystemService(Context.WALLPAPER_SERVICE);
1867// WallpaperInfo wi = wm.getWallpaperInfo();
1868// if (wi != null && wi.getSettingsActivity() != null) {
1869// LabeledIntent li = new LabeledIntent(getPackageName(),
1870// R.string.configure_wallpaper, 0);
1871// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1872// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1873// }
Mike Clerona0618e42009-10-22 13:55:21 -07001874 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 }
1876
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001877 /**
1878 * Registers various content observers. The current implementation registers
1879 * only a favorites observer to keep track of the favorites applications.
1880 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001881 private void registerContentObservers() {
1882 ContentResolver resolver = getContentResolver();
1883 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1884 true, mWidgetObserver);
1885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 @Override
1888 public boolean dispatchKeyEvent(KeyEvent event) {
1889 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1890 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001892 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001893 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001894 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001895 dumpState();
1896 return true;
1897 }
1898 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001899 }
1900 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1901 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001902 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 return true;
1904 }
1905 }
1906
1907 return super.dispatchKeyEvent(event);
1908 }
1909
Joe Onorato88ec0992009-11-19 13:16:06 -08001910 @Override
1911 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001912 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1913 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001914 } else {
1915 closeFolder();
1916 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001917 // Some launcher layouts don't have a previous and next view
1918 if (mPreviousView != null) {
1919 dismissPreview(mPreviousView);
1920 dismissPreview(mNextView);
1921 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001922 }
1923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 private void closeFolder() {
1925 Folder folder = mWorkspace.getOpenFolder();
1926 if (folder != null) {
1927 closeFolder(folder);
1928 }
1929 }
1930
1931 void closeFolder(Folder folder) {
1932 folder.getInfo().opened = false;
1933 ViewGroup parent = (ViewGroup) folder.getParent();
1934 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001935 CellLayout cl = (CellLayout) parent;
1936 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001937 if (folder instanceof DropTarget) {
1938 // Live folders aren't DropTargets.
1939 mDragController.removeDropTarget((DropTarget)folder);
1940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942 folder.onClose();
1943 }
1944
1945 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001946 * Re-listen when widgets are reset.
1947 */
1948 private void onAppWidgetReset() {
1949 mAppWidgetHost.startListening();
1950 }
1951
1952 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1954 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001956 private void unbindDesktopItems() {
1957 for (ItemInfo item: mDesktopItems) {
1958 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 /**
1963 * Launches the intent referred by the clicked shortcut.
1964 *
1965 * @param v The view representing the clicked shortcut.
1966 */
1967 public void onClick(View v) {
1968 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001969 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001971 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001972 int[] pos = new int[2];
1973 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001974 intent.setSourceBounds(new Rect(pos[0], pos[1],
1975 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001976 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 } else if (tag instanceof FolderInfo) {
1978 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001979 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001980 if (mState == State.ALL_APPS) {
1981 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001982 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001983 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 }
1986 }
1987
Michael Jurka0e260592010-06-30 17:07:39 -07001988 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001989 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001990 // clicking anywhere on the workspace causes the customization drawer to slide down
1991 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001992 return false;
1993 }
1994
Michael Jurkaaf442092010-06-10 17:01:57 -07001995 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001996 * Event handler for the search button
1997 *
1998 * @param v The view that was clicked.
1999 */
2000 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002001 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002002 // Use a custom animation for launching search
2003 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002004 }
2005
2006 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002007 * Event handler for the voice button
2008 *
2009 * @param v The view that was clicked.
2010 */
2011 public void onClickVoiceButton(View v) {
2012 startVoiceSearch();
2013 }
2014
2015 private void startVoiceSearch() {
2016 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2017 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2018 startActivity(intent);
2019 }
2020
2021 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002022 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002023 * enters home screen customization mode.
2024 *
2025 * @param v The view that was clicked.
2026 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002027 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002028 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002029 }
2030
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002031 /**
2032 * Event handler for the "grid" button that appears on the home screen, which
2033 * enters all apps mode.
2034 *
2035 * @param v The view that was clicked.
2036 */
2037 public void onClickAllAppsButton(View v) {
2038 showAllApps(true);
2039 }
2040
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002041 public void onClickAppMarketButton(View v) {
2042 if (mAppMarketIntent != null) {
2043 startActivitySafely(mAppMarketIntent, "app market");
2044 }
2045 }
2046
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002047 void startApplicationDetailsActivity(ComponentName componentName) {
2048 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002049 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2050 Uri.fromParts("package", packageName, null));
2051 startActivity(intent);
2052 }
2053
Patrick Dubroy5539af72010-09-07 15:22:01 -07002054 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2055 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2056 // System applications cannot be installed. For now, show a toast explaining that.
2057 // We may give them the option of disabling apps this way.
2058 int messageId = R.string.uninstall_system_app_text;
2059 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2060 } else {
2061 String packageName = appInfo.componentName.getPackageName();
2062 String className = appInfo.componentName.getClassName();
2063 Intent intent = new Intent(
2064 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2065 startActivity(intent);
2066 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002067 }
2068
Joe Onoratof984e852010-03-25 09:47:45 -07002069 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2071 try {
2072 startActivity(intent);
2073 } catch (ActivityNotFoundException e) {
2074 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002075 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 } catch (SecurityException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002078 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002080 "or use the exported attribute for this activity. "
2081 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 }
2083 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002084
Romain Guy8e633c52010-03-04 12:51:36 -08002085 void startActivityForResultSafely(Intent intent, int requestCode) {
2086 try {
2087 startActivityForResult(intent, requestCode);
2088 } catch (ActivityNotFoundException e) {
2089 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2090 } catch (SecurityException e) {
2091 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2092 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2093 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2094 "or use the exported attribute for this activity.", e);
2095 }
2096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097
2098 private void handleFolderClick(FolderInfo folderInfo) {
2099 if (!folderInfo.opened) {
2100 // Close any open folder
2101 closeFolder();
2102 // Open the requested folder
2103 openFolder(folderInfo);
2104 } else {
2105 // Find the open folder...
2106 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2107 int folderScreen;
2108 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002109 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 // .. and close it
2111 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002112 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 // Close any folder open on the current screen
2114 closeFolder();
2115 // Pull the folder onto this screen
2116 openFolder(folderInfo);
2117 }
2118 }
2119 }
2120 }
2121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002123 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 * is animated relative to the specified View. If the View is null, no animation
2125 * is played.
2126 *
2127 * @param folderInfo The FolderInfo describing the folder to open.
2128 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002129 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 Folder openFolder;
2131
2132 if (folderInfo instanceof UserFolderInfo) {
2133 openFolder = UserFolder.fromXml(this);
2134 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002135 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 } else {
2137 return;
2138 }
2139
Joe Onorato00acb122009-08-04 16:04:30 -04002140 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 openFolder.setLauncher(this);
2142
2143 openFolder.bind(folderInfo);
2144 folderInfo.opened = true;
2145
Winson Chungaafa03c2010-06-11 17:34:16 -07002146 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 openFolder.onOpen();
2149 }
2150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002152 switch (v.getId()) {
2153 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002154 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002155 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2156 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002157 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002158 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002159 return true;
2160 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002161 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002162 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2163 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002164 showPreviews(v);
2165 }
2166 return true;
2167 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002168 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002169 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2170 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2171 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002172 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002173 return true;
2174 }
2175
Joe Onorato9c1289c2009-08-17 11:03:03 -04002176 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 return false;
2178 }
2179
2180 if (!(v instanceof CellLayout)) {
2181 v = (View) v.getParent();
2182 }
2183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184
Michael Jurka0280c3b2010-09-17 15:00:07 -07002185 resetAddInfo();
2186 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002188 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 return true;
2190 }
2191
Michael Jurka0280c3b2010-09-17 15:00:07 -07002192 final View itemUnderLongClick = longClickCellInfo.cell;
2193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002195 if (itemUnderLongClick == null) {
2196 // User long pressed on empty space
2197 mWorkspace.setAllowLongPress(false);
2198 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2199 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002200 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002201 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
2203 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002204 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002206 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2207 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002208 mAddIntersectCellX = longClickCellInfo.cellX;
2209 mAddIntersectCellY = longClickCellInfo.cellY;
2210 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 }
2212 }
2213 }
2214 return true;
2215 }
2216
Romain Guye6b8e2f2009-11-10 11:56:55 -08002217 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002218 private void dismissPreview(final View v) {
2219 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002220 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002221 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2222 public void onDismiss() {
2223 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2224 int count = group.getChildCount();
2225 for (int i = 0; i < count; i++) {
2226 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2227 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002228 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2229 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2230
2231 v.setTag(R.id.workspace, null);
2232 v.setTag(R.id.icon, null);
2233 window.setOnDismissListener(null);
2234 }
2235 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002236 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002237 }
2238 v.setTag(null);
2239 }
2240
Romain Guyf8e6a802009-12-07 17:48:02 -08002241 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002242 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002243 }
2244
Romain Guya6abce82009-11-10 02:54:41 -08002245 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002246 final Resources resources = getResources();
2247 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002248
Romain Guya6abce82009-11-10 02:54:41 -08002249 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002250
Romain Guy9d31e9f2009-11-11 18:54:28 -08002251 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002252
Romain Guyf8e6a802009-12-07 17:48:02 -08002253 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002254 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002255 int extraW = (int) ((r.left + r.right) * max);
2256 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002257
2258 int aW = cell.getWidth() - extraW;
2259 float w = aW / max;
2260
2261 int width = cell.getWidth();
2262 int height = cell.getHeight();
2263 int x = cell.getLeftPadding();
2264 int y = cell.getTopPadding();
2265 width -= (x + cell.getRightPadding());
2266 height -= (y + cell.getBottomPadding());
2267
2268 float scale = w / width;
2269
2270 int count = end - start;
2271
2272 final float sWidth = width * scale;
2273 float sHeight = height * scale;
2274
Romain Guye6b8e2f2009-11-10 11:56:55 -08002275 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002276
Romain Guye6b8e2f2009-11-10 11:56:55 -08002277 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2278 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002279
2280 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002281 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002282 cell = (CellLayout) workspace.getChildAt(i);
2283
Romain Guyf8e6a802009-12-07 17:48:02 -08002284 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002285 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002286
2287 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002288 c.scale(scale, scale);
2289 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002290 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002291
Romain Guy9d31e9f2009-11-11 18:54:28 -08002292 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002293 image.setImageBitmap(bitmap);
2294 image.setTag(i);
2295 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002296 image.setOnFocusChangeListener(handler);
2297 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002298 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002299
2300 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002301 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2302
Winson Chungaafa03c2010-06-11 17:34:16 -07002303 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002304 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002305
2306 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 p.setContentView(preview);
2308 p.setWidth((int) (sWidth * count + extraW));
2309 p.setHeight((int) (sHeight + extraH));
2310 p.setAnimationStyle(R.style.AnimationPreview);
2311 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002312 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002313 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002314 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002315
Romain Guye6b8e2f2009-11-10 11:56:55 -08002316 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2317 public void onDismiss() {
2318 dismissPreview(anchor);
2319 }
2320 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002321
2322 anchor.setTag(p);
2323 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002324 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002325 }
2326
Romain Guy9d31e9f2009-11-11 18:54:28 -08002327 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002328 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002329
Romain Guye6b8e2f2009-11-10 11:56:55 -08002330 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002331 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002332 }
2333
2334 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002335 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002336 v.post(this);
2337 }
2338
2339 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002340 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002341 }
2342
2343 public void onFocusChange(View v, boolean hasFocus) {
2344 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002345 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002346 }
Romain Guya6abce82009-11-10 02:54:41 -08002347 }
2348 }
2349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 Workspace getWorkspace() {
2351 return mWorkspace;
2352 }
2353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 @Override
2355 protected Dialog onCreateDialog(int id) {
2356 switch (id) {
2357 case DIALOG_CREATE_SHORTCUT:
2358 return new CreateShortcut().createDialog();
2359 case DIALOG_RENAME_FOLDER:
2360 return new RenameFolder().createDialog();
2361 }
2362
2363 return super.onCreateDialog(id);
2364 }
2365
2366 @Override
2367 protected void onPrepareDialog(int id, Dialog dialog) {
2368 switch (id) {
2369 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 break;
2371 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002372 if (mFolderInfo != null) {
2373 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2374 final CharSequence text = mFolderInfo.title;
2375 input.setText(text);
2376 input.setSelection(0, text.length());
2377 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 break;
2379 }
2380 }
2381
2382 void showRenameDialog(FolderInfo info) {
2383 mFolderInfo = info;
2384 mWaitingForResult = true;
2385 showDialog(DIALOG_RENAME_FOLDER);
2386 }
2387
Michael Jurka0280c3b2010-09-17 15:00:07 -07002388 private void showAddDialog(int intersectX, int intersectY) {
2389 resetAddInfo();
2390 mAddIntersectCellX = intersectX;
2391 mAddIntersectCellY = intersectY;
2392 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 mWaitingForResult = true;
2394 showDialog(DIALOG_CREATE_SHORTCUT);
2395 }
2396
Joe Onoratodeb98af2010-02-19 14:59:39 -08002397 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002398 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002399 Bundle bundle = new Bundle();
2400
2401 ArrayList<String> shortcutNames = new ArrayList<String>();
2402 shortcutNames.add(getString(R.string.group_applications));
2403 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2404
2405 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2406 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2407 R.drawable.ic_launcher_application));
2408 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2409
2410 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2411 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002412 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002413 pickIntent.putExtras(bundle);
2414
Joe Onoratodeb98af2010-02-19 14:59:39 -08002415 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002416 }
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 private class RenameFolder {
2419 private EditText mInput;
2420
2421 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2423 mInput = (EditText) layout.findViewById(R.id.folder_name);
2424
2425 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2426 builder.setIcon(0);
2427 builder.setTitle(getString(R.string.rename_folder_title));
2428 builder.setCancelable(true);
2429 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2430 public void onCancel(DialogInterface dialog) {
2431 cleanup();
2432 }
2433 });
2434 builder.setNegativeButton(getString(R.string.cancel_action),
2435 new Dialog.OnClickListener() {
2436 public void onClick(DialogInterface dialog, int which) {
2437 cleanup();
2438 }
2439 }
2440 );
2441 builder.setPositiveButton(getString(R.string.rename_action),
2442 new Dialog.OnClickListener() {
2443 public void onClick(DialogInterface dialog, int which) {
2444 changeFolderName();
2445 }
2446 }
2447 );
2448 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002449
2450 final AlertDialog dialog = builder.create();
2451 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2452 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002453 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002454 mInput.requestFocus();
2455 InputMethodManager inputManager = (InputMethodManager)
2456 getSystemService(Context.INPUT_METHOD_SERVICE);
2457 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002458 }
2459 });
2460
2461 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 }
2463
2464 private void changeFolderName() {
2465 final String name = mInput.getText().toString();
2466 if (!TextUtils.isEmpty(name)) {
2467 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002468 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 mFolderInfo.title = name;
2470 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2471
Joe Onorato9c1289c2009-08-17 11:03:03 -04002472 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002473 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002474 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 } else {
2476 final FolderIcon folderIcon = (FolderIcon)
2477 mWorkspace.getViewForTag(mFolderInfo);
2478 if (folderIcon != null) {
2479 folderIcon.setText(name);
2480 getWorkspace().requestLayout();
2481 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002482 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 mWorkspaceLoading = true;
2484 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 }
2486 }
2487 }
2488 cleanup();
2489 }
2490
2491 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 dismissDialog(DIALOG_RENAME_FOLDER);
2493 mWaitingForResult = false;
2494 mFolderInfo = null;
2495 }
2496 }
2497
Daniel Sandler843e8602010-06-07 14:59:01 -04002498 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2499 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002500 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002501 }
2502
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002503 // AllAppsView.Watcher
2504 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002505 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2506 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002507 mWorkspace.setVisibility(View.GONE);
2508 }
2509 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002511 private void showToolbarButton(View button) {
2512 button.setAlpha(1.0f);
2513 button.setVisibility(View.VISIBLE);
2514 button.setFocusable(true);
2515 button.setClickable(true);
2516 }
2517
2518 private void hideToolbarButton(View button) {
2519 button.setAlpha(0.0f);
2520 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2521 button.setVisibility(View.INVISIBLE);
2522 button.setFocusable(false);
2523 button.setClickable(false);
2524 }
2525
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002526 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002527 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002528 *
2529 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2530 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002531 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002533 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002534 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002535 final boolean showing = show;
2536 final boolean hiding = !show;
2537
2538 final int duration = show ?
2539 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2540 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2541
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002543 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2544 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002545 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002546 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002547 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002548 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002550 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002551 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002552 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002553 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 });
2555 seq.play(anim);
2556 } else {
2557 if (showing) {
2558 showToolbarButton(view);
2559 } else {
2560 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 }
2564
2565 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 * Show/hide the appropriate toolbar buttons for newState.
2567 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 *
2569 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002570 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2571 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002573 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002574 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002576 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 final View configureButton = findViewById(R.id.configure_button);
2578
2579 switch (newState) {
2580 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002581 hideOrShowToolbarButton(true, searchButton, showSeq);
2582 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002583 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002585 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 break;
2587 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002588 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002590 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002591 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002592 break;
2593 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002594 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002596 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002598 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 break;
2600 }
2601 }
2602
2603 /**
2604 * Helper method for the cameraZoomIn/cameraZoomOut animations
2605 * @param view The view being animated
2606 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2607 * @param scaleFactor The scale factor used for the zoom
2608 */
2609 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2610 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002611
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002613 // Set pivotY so that at the starting zoom factor, the view is partially
2614 // visible. Modifying initialHeightFactor changes how much of the view is
2615 // initially showing, and hence the perceived angle from which the view enters.
2616 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002618 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002620 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002621 }
2622 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002623
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002624 /**
2625 * Zoom the camera out from the workspace to reveal 'toView'.
2626 * Assumes that the view to show is anchored at either the very top or very bottom
2627 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002629 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002630 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002631 final Resources res = getResources();
2632 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2633 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 final boolean toAllApps = (toState == State.ALL_APPS);
2635 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002636
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 setPivotsForZoom(toView, toState, scale);
2638
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002639 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002640 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002641 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002642 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002644
2645 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002646 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2647 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2648 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2649 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002650
2651 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002652 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002653 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002654 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 // Prepare the position
2656 toView.setTranslationX(0.0f);
2657 toView.setTranslationY(0.0f);
2658 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002659 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002660 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002661 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002662 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002663 // If we don't set the final scale values here, if this animation is cancelled
2664 // it will have the wrong scale value and subsequent cameraPan animations will
2665 // not fix that
2666 toView.setScaleX(1.0f);
2667 toView.setScaleY(1.0f);
2668 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002669 });
2670
Chet Haaseb1254a62010-09-07 13:35:00 -07002671 AnimatorSet toolbarHideAnim = new AnimatorSet();
2672 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002673 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2674
2675 // toView should appear right at the end of the workspace shrink animation
2676 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2677
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002678 if (mStateAnimation != null) mStateAnimation.cancel();
2679 mStateAnimation = new AnimatorSet();
2680 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2681 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002682
2683 // Show the new toolbar buttons just as the main animation is ending
2684 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002685 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2686 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002687 } else {
2688 toView.setTranslationX(0.0f);
2689 toView.setTranslationY(0.0f);
2690 toView.setScaleX(1.0f);
2691 toView.setScaleY(1.0f);
2692 toView.setVisibility(View.VISIBLE);
2693 hideAndShowToolbarButtons(toState, null, null);
2694 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002695 }
2696
2697 /**
2698 * Zoom the camera back into the workspace, hiding 'fromView'.
2699 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2701 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002702 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002704 cameraZoomIn(fromState, animated, false);
2705 }
2706
2707 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002708 Resources res = getResources();
2709 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2710 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002711 final View fromView =
2712 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2713
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002714 mCustomizePagedView.endChoiceMode();
2715 mAllAppsPagedView.endChoiceMode();
2716
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002717 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002718
Michael Jurkad3ef3062010-11-23 16:23:58 -08002719 if (!springLoaded) {
2720 mWorkspace.unshrink(animated);
2721 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722
2723 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002724 if (mStateAnimation != null) mStateAnimation.cancel();
2725 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002726 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2727 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2728 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2729 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002730 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2731
2732 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2733 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2734 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002735 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002736 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002737 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002739 }
2740 });
2741
Chet Haaseb1254a62010-09-07 13:35:00 -07002742 AnimatorSet toolbarHideAnim = new AnimatorSet();
2743 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002744 if (!springLoaded) {
2745 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2746 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002747
Adam Cohen61033d32010-11-15 18:29:44 -08002748 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002749
2750 // Show the new toolbar buttons at the very end of the whole animation
2751 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2752 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002753 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2754 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755 } else {
2756 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002757 if (!springLoaded) {
2758 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2759 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002760 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002761 }
2762
2763 /**
2764 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2765 * This is the transition used on xlarge screens to go between all apps and
2766 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002767 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2768 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2769 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002770 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002771 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002772 final Resources res = getResources();
2773 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002774 final int workspaceHeight = mWorkspace.getHeight();
2775
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002776 final boolean fromAllApps = (fromState == State.ALL_APPS);
2777 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2778 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002779
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002780 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2781 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2782 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2783 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002784
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002785 mCustomizePagedView.endChoiceMode();
2786 mAllAppsPagedView.endChoiceMode();
2787
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002788 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002789 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002790 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002791 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002792 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002793
2794 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002795 if (mStateAnimation != null) mStateAnimation.cancel();
2796 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002797 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002798 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002799 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 toView.setVisibility(View.VISIBLE);
2801 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002802 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002804 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002805 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 fromView.setVisibility(View.GONE);
2807 }
2808 });
2809
Chet Haaseb1254a62010-09-07 13:35:00 -07002810 AnimatorSet toolbarHideAnim = new AnimatorSet();
2811 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002812 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2813
Chet Haase472b2812010-10-14 07:02:04 -07002814 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2815 fromViewStartY, fromViewEndY);
2816 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002817 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2818 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2819 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2820 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2821 );
Chet Haase472b2812010-10-14 07:02:04 -07002822 fromAnim.setDuration(duration);
2823 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824
2825 // Show the new toolbar buttons just as the main animation is ending
2826 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002827 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2828 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002829 } else {
2830 fromView.setY(fromViewEndY);
2831 fromView.setVisibility(View.GONE);
2832 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002833 toView.setScaleX(1.0f);
2834 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002835 toView.setVisibility(View.VISIBLE);
2836 hideAndShowToolbarButtons(toState, null, null);
2837 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002838 }
2839
2840 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002841 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002842 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002843 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002844
Michael Jurka79212d82010-07-30 16:36:20 -07002845 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002846 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002847 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002848 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002849 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002850 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002851 } else {
2852 mAllAppsGrid.zoom(1.0f, animated);
2853 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002854
Romain Guyc16fea72010-03-12 17:17:56 -08002855 ((View) mAllAppsGrid).setFocusable(true);
2856 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002857
Joe Onorato7c312c12009-08-13 21:36:53 -07002858 // TODO: fade these two too
2859 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002860
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002861 // Change the state *after* we've called all the transition code
2862 mState = State.ALL_APPS;
2863 }
2864
2865
2866 void showWorkspace(boolean animated) {
2867 showWorkspace(animated, null);
2868 }
2869
2870 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002871 if (layout != null) {
2872 // always animated, but that's ok since we never specify a layout and
2873 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002874 mWorkspace.unshrink(layout);
2875 } else {
2876 mWorkspace.unshrink(animated);
2877 }
2878 if (mState == State.ALL_APPS) {
2879 closeAllApps(animated);
2880 } else if (mState == State.CUSTOMIZE) {
2881 hideCustomizationDrawer(animated);
2882 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002883
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002884 // Change the state *after* we've called all the transition code
2885 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002886 }
2887
Michael Jurkad3ef3062010-11-23 16:23:58 -08002888 void enterSpringLoadedDragMode(CellLayout layout) {
2889 mWorkspace.enterSpringLoadedDragMode(layout);
2890 if (mState == State.ALL_APPS) {
2891 cameraZoomIn(State.ALL_APPS, true, true);
2892 mState = State.ALL_APPS_SPRING_LOADED;
2893 } else if (mState == State.CUSTOMIZE) {
2894 cameraZoomIn(State.CUSTOMIZE, true, true);
2895 mState = State.CUSTOMIZE_SPRING_LOADED;
2896 }/* else {
2897 // we're already in spring loaded mode; don't do anything
2898 }*/
2899 }
2900
2901 void exitSpringLoadedDragMode() {
2902 if (mState == State.ALL_APPS_SPRING_LOADED) {
2903 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2904 cameraZoomOut(State.ALL_APPS, true);
2905 mState = State.ALL_APPS;
2906 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2907 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2908 cameraZoomOut(State.CUSTOMIZE, true);
2909 mState = State.CUSTOMIZE;
2910 }/* else {
2911 // we're not in spring loaded mode; don't do anything
2912 }*/
2913 }
2914
Joe Onoratob2061212009-11-24 16:13:54 -05002915 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002916 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002917 * - Home from workspace
2918 * - from center screen
2919 * - from other screens
2920 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002921 * - from center screen
2922 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002923 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002924 * - from center screen
2925 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002926 * - Launch app from workspace and quit
2927 * - with back
2928 * - with home
2929 * - Launch app from all apps and quit
2930 * - with back
2931 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002932 * - Go to a screen that's not the default, then all
2933 * apps, and launch and app, and go back
2934 * - with back
2935 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002936 * - On workspace, long press power and go back
2937 * - with back
2938 * - with home
2939 * - On all apps, long press power and go back
2940 * - with back
2941 * - with home
2942 * - On workspace, power off
2943 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002944 * - Launch an app and turn off the screen while in that app
2945 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002946 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002947 * - From all apps
2948 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002949 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2950 * - From all apps
2951 * - From the center workspace
2952 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002953 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002954 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002955 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002956 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002958 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002959 } else {
2960 mAllAppsGrid.zoom(0.0f, animated);
2961 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002962 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002963 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002964 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002965 }
2966
Joe Onorato7c312c12009-08-13 21:36:53 -07002967 void lockAllApps() {
2968 // TODO
2969 }
2970
2971 void unlockAllApps() {
2972 // TODO
2973 }
2974
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002975 // Show the customization drawer (only exists in x-large configuration)
2976 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002977 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002978 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002979 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002980 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002981 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002982 // Change the state *after* we've called all the transition code
2983 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002984 }
2985
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002986 // Hide the customization drawer (only exists in x-large configuration)
2987 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002988 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002989 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002990 }
2991 }
2992
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002993 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2994 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2995 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002996 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002997 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002998
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002999 void onWorkspaceClick(CellLayout layout) {
3000 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003001 }
3002
Michael Jurka4ef207b2010-11-29 17:05:45 -08003003 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003004 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003005 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3006 ImageView button = (ImageView) findViewById(buttonId);
3007 Drawable toolbarIcon = null;
3008 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003009 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003010 // Look for the toolbar icon specified in the activity meta-data
3011 Bundle metaData = packageManager.getActivityInfo(
3012 activityName, PackageManager.GET_META_DATA).metaData;
3013 if (metaData != null) {
3014 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3015 if (iconResId != 0) {
3016 Resources res = packageManager.getResourcesForActivity(activityName);
3017 toolbarIcon = res.getDrawable(iconResId);
3018 }
3019 }
3020 } catch (NameNotFoundException e) {
3021 // Do nothing
3022 }
3023 // If we were unable to find the icon via the meta-data, use a generic one
3024 if (toolbarIcon == null) {
3025 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003026 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003027 } else {
3028 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003029 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003030 }
3031 }
3032
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003033 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003034 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003035 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003036 }
3037
Michael Jurka0423dcf2010-10-05 14:56:18 -07003038 private void updateGlobalSearchIcon() {
3039 if (LauncherApplication.isScreenXLarge()) {
3040 final SearchManager searchManager =
3041 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3042 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003043 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003044 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003045 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003046 } else {
3047 findViewById(R.id.search_button).setVisibility(View.GONE);
3048 }
3049 }
3050 }
3051
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003052 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003053 updateButtonWithDrawable(R.id.search_button, d);
3054 }
3055
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003056 private void updateVoiceSearchIcon() {
3057 if (LauncherApplication.isScreenXLarge()) {
3058 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3059 ComponentName activityName = intent.resolveActivity(getPackageManager());
3060 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003061 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003062 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3063 } else {
3064 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003065 }
3066 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003067 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003068
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003069 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003070 updateButtonWithDrawable(R.id.voice_button, d);
3071 }
3072
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003073 /**
3074 * Sets the app market icon (shown when all apps is visible on x-large screens)
3075 */
3076 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003077 if (LauncherApplication.isScreenXLarge()) {
3078 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3079 // Find the app market activity by resolving an intent.
3080 // (If multiple app markets are installed, it will return the ResolverActivity.)
3081 ComponentName activityName = intent.resolveActivity(getPackageManager());
3082 if (activityName != null) {
3083 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003084 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003085 R.id.market_button, activityName, R.drawable.app_market_generic);
3086 }
3087 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003088 }
3089
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003090 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003091 updateButtonWithDrawable(R.id.market_button, d);
3092 }
3093
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003094 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 * Displays the shortcut creation dialog and launches, if necessary, the
3096 * appropriate activity.
3097 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003098 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003099 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3100 DialogInterface.OnShowListener {
3101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3108 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003109 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111 builder.setInverseBackgroundForced(true);
3112
3113 AlertDialog dialog = builder.create();
3114 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003115 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003116 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118 return dialog;
3119 }
3120
3121 public void onCancel(DialogInterface dialog) {
3122 mWaitingForResult = false;
3123 cleanup();
3124 }
3125
Romain Guycbb89e42009-06-08 15:52:54 -07003126 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003127 }
3128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003130 try {
3131 dismissDialog(DIALOG_CREATE_SHORTCUT);
3132 } catch (Exception e) {
3133 // An exception is thrown if the dialog is not visible, which is fine
3134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135 }
3136
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003137 /**
3138 * Handle the action clicked in the "Add to home" dialog.
3139 */
3140 public void onClick(DialogInterface dialog, int which) {
3141 Resources res = getResources();
3142 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003143
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003144 switch (which) {
3145 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003146 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003147 break;
3148 }
Romain Guycbb89e42009-06-08 15:52:54 -07003149
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003150 case AddAdapter.ITEM_APPWIDGET: {
3151 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003152
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003153 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3154 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003155 // start the pick activity
3156 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3157 break;
3158 }
Romain Guycbb89e42009-06-08 15:52:54 -07003159
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003160 case AddAdapter.ITEM_LIVE_FOLDER: {
3161 // Insert extra item to handle inserting folder
3162 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003163
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003164 ArrayList<String> shortcutNames = new ArrayList<String>();
3165 shortcutNames.add(res.getString(R.string.group_folder));
3166 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003167
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003168 ArrayList<ShortcutIconResource> shortcutIcons =
3169 new ArrayList<ShortcutIconResource>();
3170 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3171 R.drawable.ic_launcher_folder));
3172 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3173
3174 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3175 pickIntent.putExtra(Intent.EXTRA_INTENT,
3176 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3177 pickIntent.putExtra(Intent.EXTRA_TITLE,
3178 getText(R.string.title_select_live_folder));
3179 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003180
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003181 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3182 break;
3183 }
3184
3185 case AddAdapter.ITEM_WALLPAPER: {
3186 startWallpaper();
3187 break;
3188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 }
3190 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003191
3192 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003193 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 }
3196
3197 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003198 * Receives notifications when applications are added/removed.
3199 */
3200 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3201 @Override
3202 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003203 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003204 String reason = intent.getStringExtra("reason");
3205 if (!"homekey".equals(reason)) {
3206 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003207 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003208 animate = false;
3209 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003210 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003211 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003212 }
3213 }
3214
3215 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003216 * Receives notifications whenever the appwidgets are reset.
3217 */
3218 private class AppWidgetResetObserver extends ContentObserver {
3219 public AppWidgetResetObserver() {
3220 super(new Handler());
3221 }
3222
3223 @Override
3224 public void onChange(boolean selfChange) {
3225 onAppWidgetReset();
3226 }
3227 }
3228
3229 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003230 * If the activity is currently paused, signal that we need to re-run the loader
3231 * in onResume.
3232 *
3233 * This needs to be called from incoming places where resources might have been loaded
3234 * while we are paused. That is becaues the Configuration might be wrong
3235 * when we're not running, and if it comes back to what it was when we
3236 * were paused, we are not restarted.
3237 *
3238 * Implementation of the method from LauncherModel.Callbacks.
3239 *
3240 * @return true if we are currently paused. The caller might be able to
3241 * skip some work in that case since we will come back again.
3242 */
3243 public boolean setLoadOnResume() {
3244 if (mPaused) {
3245 Log.i(TAG, "setLoadOnResume");
3246 mOnResumeNeedsLoad = true;
3247 return true;
3248 } else {
3249 return false;
3250 }
3251 }
3252
3253 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003254 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003257 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003258 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003259 } else {
3260 return SCREEN_COUNT / 2;
3261 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003263
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003264 void setAllAppsPagedView(PagedView view) {
3265 mAllAppsPagedView = view;
3266 }
3267
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 /**
3269 * Refreshes the shortcuts shown on the workspace.
3270 *
3271 * Implementation of the method from LauncherModel.Callbacks.
3272 */
3273 public void startBinding() {
3274 final Workspace workspace = mWorkspace;
3275 int count = workspace.getChildCount();
3276 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003277 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3279 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003280
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 if (DEBUG_USER_INTERFACE) {
3282 android.widget.Button finishButton = new android.widget.Button(this);
3283 finishButton.setText("Finish");
3284 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3285
3286 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3287 public void onClick(View v) {
3288 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003289 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003290 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003291 }
3292 }
3293
3294 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 * Bind the items start-end from the list.
3296 *
3297 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003299 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3300
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003301 setLoadOnResume();
3302
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 final Workspace workspace = mWorkspace;
3304
3305 for (int i=start; i<end; i++) {
3306 final ItemInfo item = shortcuts.get(i);
3307 mDesktopItems.add(item);
3308 switch (item.itemType) {
3309 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3310 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003311 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3313 false);
3314 break;
3315 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3316 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003317 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003318 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3320 false);
3321 break;
3322 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3323 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3324 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003325 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003326 (LiveFolderInfo) item);
3327 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3328 false);
3329 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003331 }
3332
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003334 }
3335
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 /**
3337 * Implementation of the method from LauncherModel.Callbacks.
3338 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003339 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003340 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003341 sFolders.clear();
3342 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003343 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003344
3345 /**
3346 * Add the views for a widget to the workspace.
3347 *
3348 * Implementation of the method from LauncherModel.Callbacks.
3349 */
3350 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003351 setLoadOnResume();
3352
Daniel Sandler843e8602010-06-07 14:59:01 -04003353 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3354 if (DEBUG_WIDGETS) {
3355 Log.d(TAG, "bindAppWidget: " + item);
3356 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 final Workspace workspace = mWorkspace;
3358
3359 final int appWidgetId = item.appWidgetId;
3360 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003361 if (DEBUG_WIDGETS) {
3362 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3363 }
3364
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3366
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3368 item.hostView.setTag(item);
3369
3370 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3371 item.cellY, item.spanX, item.spanY, false);
3372
Adam Cohended9f8d2010-11-03 13:25:16 -07003373 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 workspace.requestLayout();
3376
3377 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003378
3379 if (DEBUG_WIDGETS) {
3380 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3381 + (SystemClock.uptimeMillis()-start) + "ms");
3382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 }
3384
3385 /**
3386 * Callback saying that there aren't any more items to bind.
3387 *
3388 * Implementation of the method from LauncherModel.Callbacks.
3389 */
3390 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003391 setLoadOnResume();
3392
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 if (mSavedState != null) {
3394 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003395 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 }
3397
3398 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3399 if (userFolders != null) {
3400 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003401 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 if (info != null) {
3403 openFolder(info);
3404 }
3405 }
3406 final Folder openFolder = mWorkspace.getOpenFolder();
3407 if (openFolder != null) {
3408 openFolder.requestFocus();
3409 }
3410 }
3411
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 mSavedState = null;
3413 }
3414
3415 if (mSavedInstanceState != null) {
3416 super.onRestoreInstanceState(mSavedInstanceState);
3417 mSavedInstanceState = null;
3418 }
3419
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 mWorkspaceLoading = false;
3421 }
3422
3423 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003424 * Updates the icons on the launcher that are affected by changes to the package list
3425 * on the device.
3426 */
3427 private void updateIconsAffectedByPackageManagerChanges() {
3428 updateAppMarketIcon();
3429 updateGlobalSearchIcon();
3430 updateVoiceSearchIcon();
3431 }
3432
3433 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434 * Add the icons for all apps.
3435 *
3436 * Implementation of the method from LauncherModel.Callbacks.
3437 */
3438 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003439 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003440 if (mCustomizePagedView != null) {
3441 mCustomizePagedView.setApps(apps);
3442 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003443 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 }
3445
3446 /**
3447 * A package was installed.
3448 *
3449 * Implementation of the method from LauncherModel.Callbacks.
3450 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003451 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003452 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003454 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003455 if (mCustomizePagedView != null) {
3456 mCustomizePagedView.addApps(apps);
3457 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003458 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 }
3460
3461 /**
3462 * A package was updated.
3463 *
3464 * Implementation of the method from LauncherModel.Callbacks.
3465 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003466 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003467 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003469 mWorkspace.updateShortcuts(apps);
3470 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003471 if (mCustomizePagedView != null) {
3472 mCustomizePagedView.updateApps(apps);
3473 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003474 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 }
3476
3477 /**
3478 * A package was uninstalled.
3479 *
3480 * Implementation of the method from LauncherModel.Callbacks.
3481 */
Joe Onorato36115782010-06-17 13:28:48 -04003482 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003484 if (permanent) {
3485 mWorkspace.removeItems(apps);
3486 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003487 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003488 if (mCustomizePagedView != null) {
3489 mCustomizePagedView.removeApps(apps);
3490 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003491 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003492 }
Joe Onoratobe386092009-11-17 17:32:16 -08003493
3494 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003495 * A number of packages were updated.
3496 */
3497 public void bindPackagesUpdated() {
3498 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003499 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003500 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003501 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003502 }
3503
3504 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003505 * Prints out out state for debugging.
3506 */
3507 public void dumpState() {
3508 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003509 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003510 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3511 Log.d(TAG, "mRestoring=" + mRestoring);
3512 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3513 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3514 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003515 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003516 mModel.dumpState();
3517 mAllAppsGrid.dumpState();
3518 Log.d(TAG, "END launcher2 dump state");
3519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003520}