blob: d5f20c548f2d34176a6189024c60499205b68259 [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070026import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070034import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070035import android.appwidget.AppWidgetManager;
36import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070039import android.content.ClipData;
40import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040041import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Context;
44import android.content.DialogInterface;
45import android.content.Intent;
Winson Chung68846fd2010-10-29 11:00:27 -070046import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070047import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
Winson Chung68846fd2010-10-29 11:00:27 -070050import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070051import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040054import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080057import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080059import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040061import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080069import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070070import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070071import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080072import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
75import android.text.TextUtils;
76import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080079import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.KeyEvent;
81import android.view.LayoutInflater;
82import android.view.Menu;
83import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070084import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.View;
86import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070087import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070088import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
93import android.widget.LinearLayout;
94import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070095import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080096import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -070097import android.widget.TextView;
98import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -070099import android.widget.TabHost.OnTabChangeListener;
100import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700101
Michael Jurkaaf91de02010-11-23 16:23:58 -0800102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800109
Adam Cohended9f8d2010-11-03 13:25:16 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800125 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700128 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
129 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
131 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700132 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
135 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
139 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Joe Onorato7c312c12009-08-13 21:36:53 -0700148 static final int DIALOG_CREATE_SHORTCUT = 1;
149 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Romain Guy98d01652009-06-30 16:21:04 -0700151 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
153 // Type: int
154 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700155 // Type: int
156 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: long
158 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Winson Chung80baf5a2010-08-09 16:03:15 -0700170 // tags for the customization tabs
171 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700172 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700173 private static final String SHORTCUTS_TAG = "shortcuts";
174 private static final String WALLPAPERS_TAG = "wallpapers";
175
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700176 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
177
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178 /** The different states that Launcher can be in. */
179 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700180 private State mState = State.WORKSPACE;
181 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800186 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800188 private final BroadcastReceiver mCloseSystemDialogsReceiver
189 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800190 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private LayoutInflater mInflater;
193
Joe Onorato00acb122009-08-04 16:04:30 -0400194 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700197 private AppWidgetManager mAppWidgetManager;
198 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 private int mAddScreen = -1;
201 private int mAddIntersectCellX = -1;
202 private int mAddIntersectCellY = -1;
203 private int[] mAddDropPosition;
204 private int[] mTmpAddItemCellCoordinates = new int[2];
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private FolderInfo mFolderInfo;
207
Joe Onorato7c312c12009-08-13 21:36:53 -0700208 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700210 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700211 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700212 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700213
214 private PagedView mAllAppsPagedView = null;
215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Michael Jurka4ef207b2010-11-29 17:05:45 -0800262 // External icons saved in case of resource changes, orientation, etc.
263 private static Drawable sGlobalSearchIcon;
264 private static Drawable sVoiceSearchIcon;
265 private static Drawable sAppMarketIcon;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 @Override
268 protected void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700270
Winson Chungaafa03c2010-06-11 17:34:16 -0700271 if (LauncherApplication.isInPlaceRotationEnabled()) {
272 // hide the status bar (temporary until we get the status bar design figured out)
273 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
274 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
275 }
276
Joe Onorato0589f0f2010-02-08 13:44:00 -0800277 LauncherApplication app = ((LauncherApplication)getApplication());
278 mModel = app.setLauncher(this);
279 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400280 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700282
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700283 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700284 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
285 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200288 android.os.Debug.startMethodTracing(
289 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
291
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400292 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 checkForLocaleChange();
294 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700296 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
297 if (mHomeCustomizationDrawer != null) {
298 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700299
Winson Chung80baf5a2010-08-09 16:03:15 -0700300 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700301 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
302 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700303 TabContentFactory contentFactory = new TabContentFactory() {
304 public View createTabContent(String tag) {
305 return mCustomizePagedView;
306 }
307 };
308
Winson Chung49767ae2010-11-29 14:48:30 -0800309
310 TextView tabView;
311 TabWidget tabWidget = (TabWidget)
312 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
313
314 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
315 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800317 .setIndicator(tabView).setContent(contentFactory));
318 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
319 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700320 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800321 .setIndicator(tabView).setContent(contentFactory));
322 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
323 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800325 .setIndicator(tabView).setContent(contentFactory));
326 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
327 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700328 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800329 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
331 public void onTabChanged(String tabId) {
332 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800333 final Resources res = getResources();
334 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700336 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700338 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700339 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700340 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700341 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700342 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
343 if (tag == WIDGETS_TAG) {
344 mCustomizePagedView.setCustomizationFilter(
345 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700346 } else if (tag == APPLICATIONS_TAG) {
347 mCustomizePagedView.setCustomizationFilter(
348 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700349 } else if (tag == WALLPAPERS_TAG) {
350 mCustomizePagedView.setCustomizationFilter(
351 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700352 } else if (tag == SHORTCUTS_TAG) {
353 mCustomizePagedView.setCustomizationFilter(
354 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700355 }
356
357 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700358 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700359 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700360 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700361 alphaAnim.start();
362 }
363 });
364 alphaAnim.start();
365 }
366 });
Michael Jurka946ad472010-07-09 18:05:18 -0700367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 setupViews();
369
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800370 registerContentObservers();
371
Joe Onorato7c312c12009-08-13 21:36:53 -0700372 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 mSavedState = savedInstanceState;
375 restoreState(mSavedState);
376
377 if (PROFILE_STARTUP) {
378 android.os.Debug.stopMethodTracing();
379 }
380
381 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384
385 // For handling default keys
386 mDefaultKeySsb = new SpannableStringBuilder();
387 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800388
389 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
390 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800391
392 // If we have a saved version of these external icons, we load them up immediately
393 if (LauncherApplication.isScreenXLarge()) {
394 if (sGlobalSearchIcon != null) {
395 updateGlobalSearchIcon(sGlobalSearchIcon);
396 }
397 if (sVoiceSearchIcon != null) {
398 updateVoiceSearchIcon(sVoiceSearchIcon);
399 }
400 if (sAppMarketIcon != null) {
401 updateAppMarketIcon(sAppMarketIcon);
402 }
403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 }
Romain Guycbb89e42009-06-08 15:52:54 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700407 if (sLocaleConfiguration == null) {
408 new AsyncTask<Void, Void, LocaleConfiguration>() {
409 @Override
410 protected LocaleConfiguration doInBackground(Void... unused) {
411 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
412 readConfiguration(Launcher.this, localeConfiguration);
413 return localeConfiguration;
414 }
415
416 @Override
417 protected void onPostExecute(LocaleConfiguration result) {
418 sLocaleConfiguration = result;
419 checkForLocaleChange(); // recursive, but now with a locale configuration
420 }
421 }.execute();
422 return;
423 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 final Configuration configuration = getResources().getConfiguration();
426
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 final String locale = configuration.locale.toString();
429
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 final int mcc = configuration.mcc;
432
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700433 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 final int mnc = configuration.mnc;
435
Romain Guy84f296c2009-11-04 15:00:44 -0800436 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437
Romain Guy84f296c2009-11-04 15:00:44 -0800438 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700439 sLocaleConfiguration.locale = locale;
440 sLocaleConfiguration.mcc = mcc;
441 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700442
Joe Onorato0589f0f2010-02-08 13:44:00 -0800443 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400444 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700445
446 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
447 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700448 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700449 public void run() {
450 writeConfiguration(Launcher.this, localeConfiguration);
451 }
452 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700453 }
454 }
455
456 private static class LocaleConfiguration {
457 public String locale;
458 public int mcc = -1;
459 public int mnc = -1;
460 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700461
Romain Guy98d01652009-06-30 16:21:04 -0700462 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
463 DataInputStream in = null;
464 try {
465 in = new DataInputStream(context.openFileInput(PREFERENCES));
466 configuration.locale = in.readUTF();
467 configuration.mcc = in.readInt();
468 configuration.mnc = in.readInt();
469 } catch (FileNotFoundException e) {
470 // Ignore
471 } catch (IOException e) {
472 // Ignore
473 } finally {
474 if (in != null) {
475 try {
476 in.close();
477 } catch (IOException e) {
478 // Ignore
479 }
480 }
481 }
482 }
483
484 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
485 DataOutputStream out = null;
486 try {
487 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
488 out.writeUTF(configuration.locale);
489 out.writeInt(configuration.mcc);
490 out.writeInt(configuration.mnc);
491 out.flush();
492 } catch (FileNotFoundException e) {
493 // Ignore
494 } catch (IOException e) {
495 //noinspection ResultOfMethodCallIgnored
496 context.getFileStreamPath(PREFERENCES).delete();
497 } finally {
498 if (out != null) {
499 try {
500 out.close();
501 } catch (IOException e) {
502 // Ignore
503 }
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 }
506 }
507
508 static int getScreen() {
509 synchronized (sLock) {
510 return sScreen;
511 }
512 }
513
514 static void setScreen(int screen) {
515 synchronized (sLock) {
516 sScreen = screen;
517 }
518 }
519
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700521 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522
523 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700524 // TODO: Put back when we decide about scrolling the wallpaper
525 // boolean isPortrait = display.getWidth() < display.getHeight();
526 // final int width = isPortrait ? display.getWidth() : display.getHeight();
527 // final int height = isPortrait ? display.getHeight() : display.getWidth();
528 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
529 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 }
531
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400532 // Note: This doesn't do all the client-id magic that BrowserProvider does
533 // in Browser. (http://b/2425179)
534 private Uri getDefaultBrowserUri() {
535 String url = getString(R.string.default_browser_url);
536 if (url.indexOf("{CID}") != -1) {
537 url = url.replace("{CID}", "android-google");
538 }
539 return Uri.parse(url);
540 }
541
542 // Load the Intent templates from arrays.xml to populate the hotseats. For
543 // each Intent, if it resolves to a single app, use that as the launch
544 // intent & use that app's label as the contentDescription. Otherwise,
545 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 if (mHotseatConfig == null) {
548 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
549 if (mHotseatConfig.length > 0) {
550 mHotseats = new Intent[mHotseatConfig.length];
551 mHotseatLabels = new CharSequence[mHotseatConfig.length];
552 mHotseatIcons = new Drawable[mHotseatConfig.length];
553 } else {
554 mHotseats = null;
555 mHotseatIcons = null;
556 mHotseatLabels = null;
557 }
558
559 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
560 for (int i=0; i<mHotseatConfig.length; i++) {
561 // load icon for this slot; currently unrelated to the actual activity
562 try {
563 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
564 } catch (ArrayIndexOutOfBoundsException ex) {
565 Log.w(TAG, "Missing hotseat_icons array item #" + i);
566 mHotseatIcons[i] = null;
567 }
568 }
569 hotseatIconDrawables.recycle();
570 }
571
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 for (int i=0; i<mHotseatConfig.length; i++) {
574 Intent intent = null;
575 if (mHotseatConfig[i].equals("*BROWSER*")) {
576 // magic value meaning "launch user's default web browser"
577 // replace it with a generic web request so we can see if there is indeed a default
578 String defaultUri = getString(R.string.default_browser_url);
579 intent = new Intent(
580 Intent.ACTION_VIEW,
581 ((defaultUri != null)
582 ? Uri.parse(defaultUri)
583 : getDefaultBrowserUri())
584 ).addCategory(Intent.CATEGORY_BROWSABLE);
585 // note: if the user launches this without a default set, she
586 // will always be taken to the default URL above; this is
587 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700588 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 // URL is unavoidably sent to the chosen app.
590 } else {
591 try {
592 intent = Intent.parseUri(mHotseatConfig[i], 0);
593 } catch (java.net.URISyntaxException ex) {
594 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
595 // bogus; leave intent=null
596 }
597 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700598
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 if (intent == null) {
600 mHotseats[i] = null;
601 mHotseatLabels[i] = getText(R.string.activity_not_found);
602 continue;
603 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400604
605 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700606 Log.d(TAG, "loadHotseats: hotseat " + i
607 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400608 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400609 + "]");
610 }
611
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400612 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
613 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700614 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400615 Log.d(TAG, "Best match for intent: " + bestMatch);
616 Log.d(TAG, "All matches: ");
617 for (ResolveInfo ri : allMatches) {
618 Log.d(TAG, " --> " + ri);
619 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400620 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400621 // did this resolve to a single app, or the resolver?
622 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 // can't find any activity to handle this. let's leave the
624 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400625 // to launch.
626 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400627
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 // set accessibility text to "Not installed"
629 mHotseatLabels[i] = getText(R.string.activity_not_found);
630 } else {
631 boolean found = false;
632 for (ResolveInfo ri : allMatches) {
633 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
634 && bestMatch.activityInfo.applicationInfo.packageName
635 .equals(ri.activityInfo.applicationInfo.packageName)) {
636 found = true;
637 break;
638 }
639 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700640
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 if (!found) {
642 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
643 // the bestMatch is probably the ResolveActivity, meaning the
644 // user has not yet selected a default
645 // so: we'll keep the original intent for now
646 mHotseats[i] = intent;
647
648 // set the accessibility text to "Select shortcut"
649 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
650 } else {
651 // we have an app!
652 // now reconstruct the intent to launch it through the front
653 // door
654 ComponentName com = new ComponentName(
655 bestMatch.activityInfo.applicationInfo.packageName,
656 bestMatch.activityInfo.name);
657 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
658
659 // load the app label for accessibility
660 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
661 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400662 }
663
664 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700665 Log.d(TAG, "loadHotseats: hotseat " + i
666 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400667 + ((mHotseats[i] == null)
668 ? "null"
669 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400670 + "] label=[" + mHotseatLabels[i]
671 + "]"
672 );
673 }
674 }
675 }
676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 @Override
678 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700679 mWaitingForResult = false;
680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 // The pattern used here is that a user PICKs a specific application,
682 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
685 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700686
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 switch (requestCode) {
689 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700690 completeAddApplication(
691 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 break;
693 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800694 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 break;
696 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 break;
699 case REQUEST_PICK_LIVE_FOLDER:
700 addLiveFolder(data);
701 break;
702 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700710 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700712 case REQUEST_PICK_WALLPAPER:
713 // We just wanted the activity result here so we can clear mWaitingForResult
714 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
Romain Guy18042c82009-11-06 11:44:55 -0800716 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
717 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
718 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 // Clean up the appWidgetId if we canceled
720 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
721 if (appWidgetId != -1) {
722 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 }
724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726
727 @Override
728 protected void onResume() {
729 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800730 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700731 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 mWorkspaceLoading = true;
733 mModel.startLoader(this, true);
734 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700735 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700737 // When we resume Launcher, a different Activity might be responsible for the app
738 // market intent, so refresh the icon
739 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 protected void onPause() {
744 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 // Some launcher layouts don't have a previous and next view
746 if (mPreviousView != null) {
747 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700748 }
749 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 dismissPreview(mNextView);
751 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700752 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800753 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 }
Romain Guycbb89e42009-06-08 15:52:54 -0700755
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700756 @Override
757 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 // Flag the loader to stop early before switching
759 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800760 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800761 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700762 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700763
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800764 // We can't hide the IME if it was forced open. So don't bother
765 /*
766 @Override
767 public void onWindowFocusChanged(boolean hasFocus) {
768 super.onWindowFocusChanged(hasFocus);
769
770 if (hasFocus) {
771 final InputMethodManager inputManager = (InputMethodManager)
772 getSystemService(Context.INPUT_METHOD_SERVICE);
773 WindowManager.LayoutParams lp = getWindow().getAttributes();
774 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
775 android.os.Handler()) {
776 protected void onReceiveResult(int resultCode, Bundle resultData) {
777 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
778 }
779 });
780 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
781 }
782 }
783 */
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 private boolean acceptFilter() {
786 final InputMethodManager inputManager = (InputMethodManager)
787 getSystemService(Context.INPUT_METHOD_SERVICE);
788 return !inputManager.isFullscreenMode();
789 }
790
791 @Override
792 public boolean onKeyDown(int keyCode, KeyEvent event) {
793 boolean handled = super.onKeyDown(keyCode, event);
794 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
795 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
796 keyCode, event);
797 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700798 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700799 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700800 // showSearchDialog()
801 // If there are multiple keystrokes before the search dialog takes focus,
802 // onSearchRequested() will be called for every keystroke,
803 // but it is idempotent, so it's fine.
804 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806 }
807
Joe Onorato8a9625e2010-01-28 15:55:35 -0800808 // Eat the long press event so the keyboard doesn't come up.
809 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
810 return true;
811 }
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 return handled;
814 }
815
Karl Rosaen138a0412009-04-23 19:00:21 -0700816 private String getTypedText() {
817 return mDefaultKeySsb.toString();
818 }
819
820 private void clearTypedText() {
821 mDefaultKeySsb.clear();
822 mDefaultKeySsb.clearSpans();
823 Selection.setSelection(mDefaultKeySsb, 0);
824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700827 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
828 * State
829 */
830 private static State intToState(int stateOrdinal) {
831 State state = State.WORKSPACE;
832 final State[] stateValues = State.values();
833 for (int i = 0; i < stateValues.length; i++) {
834 if (stateValues[i].ordinal() == stateOrdinal) {
835 state = stateValues[i];
836 break;
837 }
838 }
839 return state;
840 }
841
842 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 * Restores the previous state, if it exists.
844 *
845 * @param savedState The previous state.
846 */
847 private void restoreState(Bundle savedState) {
848 if (savedState == null) {
849 return;
850 }
851
Michael Jurka883f55b2010-10-21 15:47:14 -0700852 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
853
854 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800855 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700856 } else if (state == State.CUSTOMIZE) {
857 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800858 }
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
861 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700862 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864
865 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700868 mAddScreen = addScreen;
869 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
870 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 mRestoring = true;
872 }
873
874 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
875 if (renameFolder) {
876 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700877 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 mRestoring = true;
879 }
880 }
881
882 /**
883 * Finds all the views we need and configure them properly.
884 */
885 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700886 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400887
Romain Guyb1b69f52009-08-10 15:10:15 -0700888 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400889 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890
Joe Onorato7c312c12009-08-13 21:36:53 -0700891 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700892 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700893 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800894 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700895 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700896 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700897
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700898 if (LauncherApplication.isScreenXLarge()) {
899 // They need to be INVISIBLE initially so that they will be measured in the layout.
900 // Otherwise the animations are messed up when we show them for the first time.
901 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
902 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
903 }
904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700906
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500908 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700909
Joe Onorato7c312c12009-08-13 21:36:53 -0700910 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
911 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700913 View handleView = findViewById(R.id.all_apps_button);
914 if (handleView != null && handleView instanceof HandleView) {
915 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700916 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700917 mHandleView.setLauncher(this);
918 mHandleView.setOnClickListener(this);
919 mHandleView.setOnLongClickListener(this);
920 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800921
Winson Chung80baf5a2010-08-09 16:03:15 -0700922 if (mCustomizePagedView != null) {
923 mCustomizePagedView.setLauncher(this);
924 mCustomizePagedView.setDragController(dragController);
925 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700926 } else {
927 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
928 hotseatLeft.setContentDescription(mHotseatLabels[0]);
929 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
930 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
931 hotseatRight.setContentDescription(mHotseatLabels[1]);
932 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400933
Michael Jurkaaf442092010-06-10 17:01:57 -0700934 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
935 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800936
Michael Jurkaaf442092010-06-10 17:01:57 -0700937 Drawable previous = mPreviousView.getDrawable();
938 Drawable next = mNextView.getDrawable();
939 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940
Michael Jurkaaf442092010-06-10 17:01:57 -0700941 mPreviousView.setHapticFeedbackEnabled(false);
942 mPreviousView.setOnLongClickListener(this);
943 mNextView.setHapticFeedbackEnabled(false);
944 mNextView.setOnLongClickListener(this);
945 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400948 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950
951 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400952 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700953 int deleteZoneHandleId;
954 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700955 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700956 } else {
957 deleteZoneHandleId = R.id.all_apps_button_cluster;
958 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700959 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700960 dragController.addDragListener(deleteZone);
961
Adam Cohencdc30d52010-12-01 15:09:47 -0800962 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
963 if (allAppsDeleteZone != null) {
964 allAppsDeleteZone.setLauncher(this);
965 allAppsDeleteZone.setDragController(dragController);
966 allAppsDeleteZone.setDragAndDropEnabled(false);
967 dragController.addDragListener(allAppsDeleteZone);
968 dragController.addDropTarget(allAppsDeleteZone);
969 }
970
971 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
972 findViewById(R.id.all_apps_info_target);
973 if (allAppsInfoTarget != null) {
974 allAppsInfoTarget.setLauncher(this);
975 dragController.addDragListener(allAppsInfoTarget);
976 allAppsInfoTarget.setDragColor(getResources().getColor(R.color.app_info_filter));
977 allAppsInfoTarget.setDragAndDropEnabled(false);
978 View marketButton = findViewById(R.id.market_button);
979 if (marketButton != null) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800980 allAppsInfoTarget.setHandle(marketButton);
981 }
982 }
983
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700984 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
985 if (infoButton != null) {
986 infoButton.setLauncher(this);
987 infoButton.setHandle(findViewById(R.id.configure_button));
988 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
989 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 Jurka0280c3b2010-09-17 15:00:07 -07001115 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1116 showOutOfSpaceMessage();
1117 return;
1118 }
1119
1120 final ShortcutInfo info = mModel.addShortcut(
1121 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122
1123 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001125 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127 }
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001130 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001132 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001133 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001136 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001138 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001139 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1140 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001143 // We have saved the position to which the widget was dragged-- this really only matters
1144 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001146
1147 // For now, we don't save the coordinate where we dropped the icon because we're not
1148 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1149 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001150 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001151 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001152 touchXY = mAddDropPosition;
1153 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001154 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001155 boolean foundCellSpan = false;
1156 if (touchXY != null) {
1157 // when dragging and dropping, just find the closest free spot
1158 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1159 int[] result = screenLayout.findNearestVacantArea(
1160 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001161 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001162 foundCellSpan = !findNearestVacantAreaFailed;
1163 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001164 // if we long pressed on an empty cell to bring up a menu,
1165 // make sure we intersect the empty cell
1166 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1167 // findCellForSpanThatIntersects will just ignore them
1168 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1169 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001170 }
1171
Michael Jurka0280c3b2010-09-17 15:00:07 -07001172 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001173 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001174 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001175 return;
1176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001178 // Build Launcher-specific widget info and save to database
1179 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 launcherInfo.spanX = spanXY[0];
1181 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 LauncherModel.addItemToDatabase(this, launcherInfo,
1184 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186
1187 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001192
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Michael Jurka0280c3b2010-09-17 15:00:07 -07001196 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001198
1199 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201 }
Romain Guycbb89e42009-06-08 15:52:54 -07001202
Adam Cohended9f8d2010-11-03 13:25:16 -07001203 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1204 @Override
1205 public void onReceive(Context context, Intent intent) {
1206 final String action = intent.getAction();
1207 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1208 mUserPresent = false;
1209 updateRunning();
1210 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1211 mUserPresent = true;
1212 updateRunning();
1213 }
1214 }
1215 };
1216
1217 @Override
1218 public void onAttachedToWindow() {
1219 super.onAttachedToWindow();
1220
1221 // Listen for broadcasts related to user-presence
1222 final IntentFilter filter = new IntentFilter();
1223 filter.addAction(Intent.ACTION_SCREEN_OFF);
1224 filter.addAction(Intent.ACTION_USER_PRESENT);
1225 registerReceiver(mReceiver, filter);
1226
Adam Cohend113e0c2010-11-11 10:48:05 -08001227 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001228 mVisible = true;
1229 }
1230
1231 @Override
1232 public void onDetachedFromWindow() {
1233 super.onDetachedFromWindow();
1234 mVisible = false;
1235
Adam Cohend113e0c2010-11-11 10:48:05 -08001236 if (mAttached) {
1237 unregisterReceiver(mReceiver);
1238 mAttached = false;
1239 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001240 updateRunning();
1241 }
1242
1243 public void onWindowVisibilityChanged(int visibility) {
1244 mVisible = visibility == View.VISIBLE;
1245 updateRunning();
1246 }
1247
1248 private void sendAdvanceMessage(long delay) {
1249 mHandler.removeMessages(ADVANCE_MSG);
1250 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1251 mHandler.sendMessageDelayed(msg, delay);
1252 mAutoAdvanceSentTime = System.currentTimeMillis();
1253 }
1254
1255 private void updateRunning() {
1256 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1257 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1258 mAutoAdvanceRunning = autoAdvanceRunning;
1259 if (autoAdvanceRunning) {
1260 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1261 sendAdvanceMessage(delay);
1262 } else {
1263 if (!mWidgetsToAdvance.isEmpty()) {
1264 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1265 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1266 }
1267 mHandler.removeMessages(ADVANCE_MSG);
1268 }
1269 }
1270 }
1271
1272 private final Handler mHandler = new Handler() {
1273 @Override
1274 public void handleMessage(Message msg) {
1275 if (msg.what == ADVANCE_MSG) {
1276 int i = 0;
1277 for (View key: mWidgetsToAdvance.keySet()) {
1278 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1279 final int delay = mAdvanceStagger * i;
1280 if (v instanceof Advanceable) {
1281 postDelayed(new Runnable() {
1282 public void run() {
1283 ((Advanceable) v).advance();
1284 }
1285 }, delay);
1286 }
1287 i++;
1288 }
1289 sendAdvanceMessage(mAdvanceInterval);
1290 }
1291 }
1292 };
1293
1294 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1295 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1296 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1297 if (v instanceof Advanceable) {
1298 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1299 ((Advanceable) v).willBeAdvancedByHost();
1300 updateRunning();
1301 }
1302 }
1303
1304 void removeWidgetToAutoAdvance(View hostView) {
1305 if (mWidgetsToAdvance.containsKey(hostView)) {
1306 mWidgetsToAdvance.remove(hostView);
1307 updateRunning();
1308 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001309 }
1310
Joe Onorato9c1289c2009-08-17 11:03:03 -04001311 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1312 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001313 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001314 launcherInfo.hostView = null;
1315 }
1316
Adam Cohended9f8d2010-11-03 13:25:16 -07001317 void showOutOfSpaceMessage() {
1318 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1319 }
1320
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001321 public LauncherAppWidgetHost getAppWidgetHost() {
1322 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
Romain Guycbb89e42009-06-08 15:52:54 -07001324
Winson Chunga9abd0e2010-10-27 17:18:37 -07001325 public LauncherModel getModel() {
1326 return mModel;
1327 }
1328
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001329 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001330 getWindow().closeAllPanels();
1331
1332 try {
1333 dismissDialog(DIALOG_CREATE_SHORTCUT);
1334 // Unlock the workspace if the dialog was showing
1335 } catch (Exception e) {
1336 // An exception is thrown if the dialog is not visible, which is fine
1337 }
1338
1339 try {
1340 dismissDialog(DIALOG_RENAME_FOLDER);
1341 // Unlock the workspace if the dialog was showing
1342 } catch (Exception e) {
1343 // An exception is thrown if the dialog is not visible, which is fine
1344 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001345
1346 // Whatever we were doing is hereby canceled.
1347 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001348 }
1349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 @Override
1351 protected void onNewIntent(Intent intent) {
1352 super.onNewIntent(intent);
1353
1354 // Close the menu
1355 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001356 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001357 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001358
Joe Onorato14f122b2009-11-19 14:06:36 -08001359 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1360 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001361
Michael Jurka4cb37242010-08-09 21:05:32 -07001362 // in all these cases, only animate if we're already on home
1363 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001364 mWorkspace.unshrink(alreadyOnHome);
1365 }
1366 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001367 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001368 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1369 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001370 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001371 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001372
Joe Onorato3a8820b2009-11-10 15:06:42 -08001373 final View v = getWindow().peekDecorView();
1374 if (v != null && v.getWindowToken() != null) {
1375 InputMethodManager imm = (InputMethodManager)getSystemService(
1376 INPUT_METHOD_SERVICE);
1377 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379 }
1380 }
1381
1382 @Override
1383 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1384 // Do not call super here
1385 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001386
Winson Chung43b119d2010-12-06 16:45:05 -08001387 // Restore the current AllApps drawer tab
1388 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1389 String cur = savedInstanceState.getString("allapps_currentTab");
1390 if (cur != null) {
1391 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1392 tabhost.setCurrentTabByTag(cur);
1393 }
1394 }
1395
1396 // Restore the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001397 if (mHomeCustomizationDrawer != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001398 String cur = savedInstanceState.getString("customize_currentTab");
Michael Jurka946ad472010-07-09 18:05:18 -07001399 if (cur != null) {
1400 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1401 }
1402 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404
1405 @Override
1406 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001407 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408
1409 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1410 if (folders.size() > 0) {
1411 final int count = folders.size();
1412 long[] ids = new long[count];
1413 for (int i = 0; i < count; i++) {
1414 final FolderInfo info = folders.get(i).getInfo();
1415 ids[i] = info.id;
1416 }
1417 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1418 } else {
1419 super.onSaveInstanceState(outState);
1420 }
1421
Michael Jurka883f55b2010-10-21 15:47:14 -07001422 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423
Michael Jurka0280c3b2010-09-17 15:00:07 -07001424 if (mAddScreen > -1 && mWaitingForResult) {
1425 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1426 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1427 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
1430 if (mFolderInfo != null && mWaitingForResult) {
1431 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1432 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1433 }
Michael Jurka946ad472010-07-09 18:05:18 -07001434
Winson Chung43b119d2010-12-06 16:45:05 -08001435 // Save the current AllApps drawer tab
1436 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1437 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1438 String currentTabTag = tabhost.getCurrentTabTag();
1439 if (currentTabTag != null) {
1440 outState.putString("allapps_currentTab", currentTabTag);
1441 }
1442 }
1443
1444 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001445 if (mHomeCustomizationDrawer != null) {
1446 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1447 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001448 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001449 }
1450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
1453 @Override
1454 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001458 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001460 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462
1463 TextKeyListener.getInstance().release();
1464
Joe Onorato9c1289c2009-08-17 11:03:03 -04001465 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001468
1469 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001470
Patrick Dubroyab962b72010-07-26 12:10:10 -07001471 // Some launcher layouts don't have a previous and next view
1472 if (mPreviousView != null) {
1473 dismissPreview(mPreviousView);
1474 }
1475 if (mNextView != null) {
1476 dismissPreview(mNextView);
1477 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001478
1479 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
1482 @Override
1483 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001484 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 super.startActivityForResult(intent, requestCode);
1486 }
1487
1488 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001489 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001491
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001492 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001493
Karl Rosaen138a0412009-04-23 19:00:21 -07001494 if (initialQuery == null) {
1495 // Use any text typed in the launcher as the initial query
1496 initialQuery = getTypedText();
1497 clearTypedText();
1498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 if (appSearchData == null) {
1500 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001501 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
Romain Guycbb89e42009-06-08 15:52:54 -07001503
Karl Rosaen138a0412009-04-23 19:00:21 -07001504 final SearchManager searchManager =
1505 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001506 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001507 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
1510 @Override
1511 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001512 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001513 return false;
1514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515
1516 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1519 .setIcon(android.R.drawable.ic_menu_add)
1520 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001521 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1522 .setIcon(android.R.drawable.ic_menu_manage)
1523 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001524 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 .setIcon(android.R.drawable.ic_menu_gallery)
1526 .setAlphabeticShortcut('W');
1527 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1528 .setIcon(android.R.drawable.ic_search_category_default)
1529 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1530 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1531 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1532 .setAlphabeticShortcut('N');
1533
1534 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001535 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1536 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537
1538 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1539 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1540 .setIntent(settings);
1541
1542 return true;
1543 }
1544
1545 @Override
1546 public boolean onPrepareOptionsMenu(Menu menu) {
1547 super.onPrepareOptionsMenu(menu);
1548
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001549 // If all apps is animating, don't show the menu, because we don't know
1550 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001551 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001552 return false;
1553 }
1554
1555 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001556 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001557 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1558 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1559
1560 // Disable add if the workspace is full.
1561 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1563 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 return true;
1567 }
1568
1569 @Override
1570 public boolean onOptionsItemSelected(MenuItem item) {
1571 switch (item.getItemId()) {
1572 case MENU_ADD:
1573 addItems();
1574 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001575 case MENU_MANAGE_APPS:
1576 manageApps();
1577 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 case MENU_WALLPAPER_SETTINGS:
1579 startWallpaper();
1580 return true;
1581 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001582 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 return true;
1584 case MENU_NOTIFICATIONS:
1585 showNotifications();
1586 return true;
1587 }
1588
1589 return super.onOptionsItemSelected(item);
1590 }
Romain Guycbb89e42009-06-08 15:52:54 -07001591
Karl Rosaen138a0412009-04-23 19:00:21 -07001592 /**
1593 * Indicates that we want global search for this activity by setting the globalSearch
1594 * argument for {@link #startSearch} to true.
1595 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001597 @Override
1598 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001599 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001600 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001601 }
1602
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 public boolean isWorkspaceLocked() {
1604 return mWorkspaceLoading || mWaitingForResult;
1605 }
1606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001608 if (LauncherApplication.isScreenXLarge()) {
1609 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001610 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001611 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001612 }
1613 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001614 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
1618
Michael Jurka0280c3b2010-09-17 15:00:07 -07001619 private void resetAddInfo() {
1620 mAddScreen = -1;
1621 mAddIntersectCellX = -1;
1622 mAddIntersectCellY = -1;
1623 mAddDropPosition = null;
1624 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001625
Winson Chung55cef262010-10-28 14:14:18 -07001626 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001627 resetAddInfo();
1628 mAddScreen = screen;
1629
1630 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1631 mAddDropPosition = position;
1632
Michael Jurkaaf442092010-06-10 17:01:57 -07001633 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001634 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1635 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001636 }
1637
Winson Chung7ad01412010-09-27 11:33:03 -07001638 private void manageApps() {
1639 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1640 }
1641
Michael Jurkaaf442092010-06-10 17:01:57 -07001642 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001643 // TODO: catch bad widget exception when sent
1644 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001645 // TODO: Is this log message meaningful?
1646 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001647 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001648 }
1649
Winson Chung55cef262010-10-28 14:14:18 -07001650 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001651 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
Bjorn Bringert7984c942009-12-09 15:38:25 +00001653 if (appWidget.configure != null) {
1654 // Launch over to configure widget, if needed
1655 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1656 intent.setComponent(appWidget.configure);
1657 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001658 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001659 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1660 intent.putExtra(
1661 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1662 info.mimeType);
1663
1664 final String mimeType = info.mimeType;
1665 final ClipData clipData = (ClipData) info.configurationData;
1666 final ClipDescription clipDesc = clipData.getDescription();
1667 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1668 if (clipDesc.getMimeType(i).equals(mimeType)) {
1669 final ClipData.Item item = clipData.getItem(i);
1670 final CharSequence stringData = item.getText();
1671 final Uri uriData = item.getUri();
1672 final Intent intentData = item.getIntent();
1673 final String key =
1674 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1675 if (uriData != null) {
1676 intent.putExtra(key, uriData);
1677 } else if (intentData != null) {
1678 intent.putExtra(key, intentData);
1679 } else if (stringData != null) {
1680 intent.putExtra(key, stringData);
1681 }
1682 break;
1683 }
1684 }
1685 }
Winson Chung55cef262010-10-28 14:14:18 -07001686 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001687
Romain Guy8e633c52010-03-04 12:51:36 -08001688 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001690 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001691 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 }
1693 }
Romain Guycbb89e42009-06-08 15:52:54 -07001694
Michael Jurka0280c3b2010-09-17 15:00:07 -07001695 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1696 resetAddInfo();
1697 mAddScreen = screen;
1698 mAddDropPosition = position;
1699
1700 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1701 createShortcutIntent.setComponent(componentName);
1702 processShortcut(createShortcutIntent);
1703 }
1704
Joe Onoratodeb98af2010-02-19 14:59:39 -08001705 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001706 // Handle case where user selected "Applications"
1707 String applicationName = getResources().getString(R.string.group_applications);
1708 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001710 if (applicationName != null && applicationName.equals(shortcutName)) {
1711 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1712 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001713
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001714 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1715 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001716 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001717 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001718 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001719 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001720 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722
Winson Chung24ab2f12010-09-16 14:10:47 -07001723 void processWallpaper(Intent intent) {
1724 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1725 }
1726
Michael Jurka0280c3b2010-09-17 15:00:07 -07001727 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1728 resetAddInfo();
1729 mAddScreen = screen;
1730 mAddDropPosition = position;
1731
1732 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1733 createFolderIntent.setComponent(componentName);
1734
1735 addLiveFolder(createFolderIntent);
1736 }
1737
1738 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001739 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001740 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001741 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001742
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001743 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001744 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001745 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001746 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749
Michael Jurka0280c3b2010-09-17 15:00:07 -07001750 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 UserFolderInfo folderInfo = new UserFolderInfo();
1752 folderInfo.title = getText(R.string.folder_name);
1753
Michael Jurka0280c3b2010-09-17 15:00:07 -07001754 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1755 final int[] cellXY = mTmpAddItemCellCoordinates;
1756 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1757 showOutOfSpaceMessage();
1758 return;
1759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760
1761 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 LauncherModel.addItemToDatabase(this, folderInfo,
1763 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001765 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766
1767 // Create the view
1768 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001769 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1770 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001771 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
Romain Guycbb89e42009-06-08 15:52:54 -07001773
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001775 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776 }
1777
Michael Jurka28750fb2010-09-24 17:43:49 -07001778 private void completeAddLiveFolder(
1779 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1781 final int[] cellXY = mTmpAddItemCellCoordinates;
1782 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1783 showOutOfSpaceMessage();
1784 return;
1785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786
Michael Jurka0280c3b2010-09-17 15:00:07 -07001787 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788
1789 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001791 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001792 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
1794 }
1795
1796 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001797 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798
1799 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1800 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1801
1802 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 Intent.ShortcutIconResource iconResource = null;
1804
1805 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1806 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1807 try {
1808 iconResource = (Intent.ShortcutIconResource) extra;
1809 final PackageManager packageManager = context.getPackageManager();
1810 Resources resources = packageManager.getResourcesForApplication(
1811 iconResource.packageName);
1812 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1813 icon = resources.getDrawable(id);
1814 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001815 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
1817 }
1818
1819 if (icon == null) {
1820 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1821 }
1822
1823 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001824 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 info.title = name;
1826 info.iconResource = iconResource;
1827 info.uri = data.getData();
1828 info.baseIntent = baseIntent;
1829 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1830 LiveFolders.DISPLAY_MODE_GRID);
1831
1832 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001834 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835
1836 return info;
1837 }
1838
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 private void showNotifications() {
1840 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1841 if (statusBar != null) {
1842 statusBar.expand();
1843 }
1844 }
1845
1846 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001847 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001849 Intent chooser = Intent.createChooser(pickWallpaper,
1850 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001851 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1852 // Removed in Eclair MR1
1853// WallpaperManager wm = (WallpaperManager)
1854// getSystemService(Context.WALLPAPER_SERVICE);
1855// WallpaperInfo wi = wm.getWallpaperInfo();
1856// if (wi != null && wi.getSettingsActivity() != null) {
1857// LabeledIntent li = new LabeledIntent(getPackageName(),
1858// R.string.configure_wallpaper, 0);
1859// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1860// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1861// }
Mike Clerona0618e42009-10-22 13:55:21 -07001862 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001865 /**
1866 * Registers various content observers. The current implementation registers
1867 * only a favorites observer to keep track of the favorites applications.
1868 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001869 private void registerContentObservers() {
1870 ContentResolver resolver = getContentResolver();
1871 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1872 true, mWidgetObserver);
1873 }
1874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 @Override
1876 public boolean dispatchKeyEvent(KeyEvent event) {
1877 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1878 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001880 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001881 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001882 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001883 dumpState();
1884 return true;
1885 }
1886 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001887 }
1888 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1889 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001890 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 return true;
1892 }
1893 }
1894
1895 return super.dispatchKeyEvent(event);
1896 }
1897
Joe Onorato88ec0992009-11-19 13:16:06 -08001898 @Override
1899 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001900 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1901 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001902 } else {
1903 closeFolder();
1904 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001905 // Some launcher layouts don't have a previous and next view
1906 if (mPreviousView != null) {
1907 dismissPreview(mPreviousView);
1908 dismissPreview(mNextView);
1909 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 private void closeFolder() {
1913 Folder folder = mWorkspace.getOpenFolder();
1914 if (folder != null) {
1915 closeFolder(folder);
1916 }
1917 }
1918
1919 void closeFolder(Folder folder) {
1920 folder.getInfo().opened = false;
1921 ViewGroup parent = (ViewGroup) folder.getParent();
1922 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001923 CellLayout cl = (CellLayout) parent;
1924 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001925 if (folder instanceof DropTarget) {
1926 // Live folders aren't DropTargets.
1927 mDragController.removeDropTarget((DropTarget)folder);
1928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 }
1930 folder.onClose();
1931 }
1932
1933 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001934 * Re-listen when widgets are reset.
1935 */
1936 private void onAppWidgetReset() {
1937 mAppWidgetHost.startListening();
1938 }
1939
1940 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1942 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001944 private void unbindDesktopItems() {
1945 for (ItemInfo item: mDesktopItems) {
1946 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 /**
1951 * Launches the intent referred by the clicked shortcut.
1952 *
1953 * @param v The view representing the clicked shortcut.
1954 */
1955 public void onClick(View v) {
1956 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001957 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001959 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001960 int[] pos = new int[2];
1961 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001962 intent.setSourceBounds(new Rect(pos[0], pos[1],
1963 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001964 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 } else if (tag instanceof FolderInfo) {
1966 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001967 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001968 if (mState == State.ALL_APPS) {
1969 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001970 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001971 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974 }
1975
Michael Jurka0e260592010-06-30 17:07:39 -07001976 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001977 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001978 // clicking anywhere on the workspace causes the customization drawer to slide down
1979 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001980 return false;
1981 }
1982
Michael Jurkaaf442092010-06-10 17:01:57 -07001983 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001984 * Event handler for the search button
1985 *
1986 * @param v The view that was clicked.
1987 */
1988 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001989 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001990 // Use a custom animation for launching search
1991 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001992 }
1993
1994 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001995 * Event handler for the voice button
1996 *
1997 * @param v The view that was clicked.
1998 */
1999 public void onClickVoiceButton(View v) {
2000 startVoiceSearch();
2001 }
2002
2003 private void startVoiceSearch() {
2004 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2005 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2006 startActivity(intent);
2007 }
2008
2009 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002010 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002011 * enters home screen customization mode.
2012 *
2013 * @param v The view that was clicked.
2014 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002015 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002016 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002017 }
2018
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002019 /**
2020 * Event handler for the "grid" button that appears on the home screen, which
2021 * enters all apps mode.
2022 *
2023 * @param v The view that was clicked.
2024 */
2025 public void onClickAllAppsButton(View v) {
2026 showAllApps(true);
2027 }
2028
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002029 public void onClickAppMarketButton(View v) {
2030 if (mAppMarketIntent != null) {
2031 startActivitySafely(mAppMarketIntent, "app market");
2032 }
2033 }
2034
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002035 void startApplicationDetailsActivity(ComponentName componentName) {
2036 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002037 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2038 Uri.fromParts("package", packageName, null));
2039 startActivity(intent);
2040 }
2041
Patrick Dubroy5539af72010-09-07 15:22:01 -07002042 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2043 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2044 // System applications cannot be installed. For now, show a toast explaining that.
2045 // We may give them the option of disabling apps this way.
2046 int messageId = R.string.uninstall_system_app_text;
2047 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2048 } else {
2049 String packageName = appInfo.componentName.getPackageName();
2050 String className = appInfo.componentName.getClassName();
2051 Intent intent = new Intent(
2052 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2053 startActivity(intent);
2054 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002055 }
2056
Joe Onoratof984e852010-03-25 09:47:45 -07002057 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2059 try {
2060 startActivity(intent);
2061 } catch (ActivityNotFoundException e) {
2062 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002063 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 } catch (SecurityException e) {
2065 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002066 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002068 "or use the exported attribute for this activity. "
2069 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002072
Romain Guy8e633c52010-03-04 12:51:36 -08002073 void startActivityForResultSafely(Intent intent, int requestCode) {
2074 try {
2075 startActivityForResult(intent, requestCode);
2076 } catch (ActivityNotFoundException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2078 } catch (SecurityException e) {
2079 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2080 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2081 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2082 "or use the exported attribute for this activity.", e);
2083 }
2084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085
2086 private void handleFolderClick(FolderInfo folderInfo) {
2087 if (!folderInfo.opened) {
2088 // Close any open folder
2089 closeFolder();
2090 // Open the requested folder
2091 openFolder(folderInfo);
2092 } else {
2093 // Find the open folder...
2094 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2095 int folderScreen;
2096 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002097 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 // .. and close it
2099 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002100 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 // Close any folder open on the current screen
2102 closeFolder();
2103 // Pull the folder onto this screen
2104 openFolder(folderInfo);
2105 }
2106 }
2107 }
2108 }
2109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002111 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 * is animated relative to the specified View. If the View is null, no animation
2113 * is played.
2114 *
2115 * @param folderInfo The FolderInfo describing the folder to open.
2116 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002117 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 Folder openFolder;
2119
2120 if (folderInfo instanceof UserFolderInfo) {
2121 openFolder = UserFolder.fromXml(this);
2122 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002123 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 } else {
2125 return;
2126 }
2127
Joe Onorato00acb122009-08-04 16:04:30 -04002128 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 openFolder.setLauncher(this);
2130
2131 openFolder.bind(folderInfo);
2132 folderInfo.opened = true;
2133
Winson Chungaafa03c2010-06-11 17:34:16 -07002134 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 openFolder.onOpen();
2137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002140 switch (v.getId()) {
2141 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002142 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002143 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2144 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002145 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002146 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002147 return true;
2148 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002149 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002150 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2151 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002152 showPreviews(v);
2153 }
2154 return true;
2155 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002156 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002157 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2158 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2159 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002160 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002161 return true;
2162 }
2163
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 return false;
2166 }
2167
2168 if (!(v instanceof CellLayout)) {
2169 v = (View) v.getParent();
2170 }
2171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172
Michael Jurka0280c3b2010-09-17 15:00:07 -07002173 resetAddInfo();
2174 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002176 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 return true;
2178 }
2179
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 final View itemUnderLongClick = longClickCellInfo.cell;
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002183 if (itemUnderLongClick == null) {
2184 // User long pressed on empty space
2185 mWorkspace.setAllowLongPress(false);
2186 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2187 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002188 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002189 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
2191 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002192 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002194 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2195 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 mAddIntersectCellX = longClickCellInfo.cellX;
2197 mAddIntersectCellY = longClickCellInfo.cellY;
2198 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200 }
2201 }
2202 return true;
2203 }
2204
Romain Guye6b8e2f2009-11-10 11:56:55 -08002205 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002206 private void dismissPreview(final View v) {
2207 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002208 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002209 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2210 public void onDismiss() {
2211 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2212 int count = group.getChildCount();
2213 for (int i = 0; i < count; i++) {
2214 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2215 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002216 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2217 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2218
2219 v.setTag(R.id.workspace, null);
2220 v.setTag(R.id.icon, null);
2221 window.setOnDismissListener(null);
2222 }
2223 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002224 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002225 }
2226 v.setTag(null);
2227 }
2228
Romain Guyf8e6a802009-12-07 17:48:02 -08002229 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002230 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002231 }
2232
Romain Guya6abce82009-11-10 02:54:41 -08002233 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002234 final Resources resources = getResources();
2235 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002236
Romain Guya6abce82009-11-10 02:54:41 -08002237 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002238
Romain Guy9d31e9f2009-11-11 18:54:28 -08002239 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002240
Romain Guyf8e6a802009-12-07 17:48:02 -08002241 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002242 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002243 int extraW = (int) ((r.left + r.right) * max);
2244 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002245
2246 int aW = cell.getWidth() - extraW;
2247 float w = aW / max;
2248
2249 int width = cell.getWidth();
2250 int height = cell.getHeight();
2251 int x = cell.getLeftPadding();
2252 int y = cell.getTopPadding();
2253 width -= (x + cell.getRightPadding());
2254 height -= (y + cell.getBottomPadding());
2255
2256 float scale = w / width;
2257
2258 int count = end - start;
2259
2260 final float sWidth = width * scale;
2261 float sHeight = height * scale;
2262
Romain Guye6b8e2f2009-11-10 11:56:55 -08002263 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002264
Romain Guye6b8e2f2009-11-10 11:56:55 -08002265 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2266 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002267
2268 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002269 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002270 cell = (CellLayout) workspace.getChildAt(i);
2271
Romain Guyf8e6a802009-12-07 17:48:02 -08002272 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002273 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002274
2275 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002276 c.scale(scale, scale);
2277 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002278 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002279
Romain Guy9d31e9f2009-11-11 18:54:28 -08002280 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002281 image.setImageBitmap(bitmap);
2282 image.setTag(i);
2283 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002284 image.setOnFocusChangeListener(handler);
2285 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002286 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002287
2288 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002289 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2290
Winson Chungaafa03c2010-06-11 17:34:16 -07002291 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002292 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002293
2294 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002295 p.setContentView(preview);
2296 p.setWidth((int) (sWidth * count + extraW));
2297 p.setHeight((int) (sHeight + extraH));
2298 p.setAnimationStyle(R.style.AnimationPreview);
2299 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002300 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002301 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002302 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002303
Romain Guye6b8e2f2009-11-10 11:56:55 -08002304 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2305 public void onDismiss() {
2306 dismissPreview(anchor);
2307 }
2308 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002309
2310 anchor.setTag(p);
2311 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002312 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002313 }
2314
Romain Guy9d31e9f2009-11-11 18:54:28 -08002315 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002316 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002317
Romain Guye6b8e2f2009-11-10 11:56:55 -08002318 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002319 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002320 }
2321
2322 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002323 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002324 v.post(this);
2325 }
2326
2327 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002328 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002329 }
2330
2331 public void onFocusChange(View v, boolean hasFocus) {
2332 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002333 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002334 }
Romain Guya6abce82009-11-10 02:54:41 -08002335 }
2336 }
2337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 Workspace getWorkspace() {
2339 return mWorkspace;
2340 }
2341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 @Override
2343 protected Dialog onCreateDialog(int id) {
2344 switch (id) {
2345 case DIALOG_CREATE_SHORTCUT:
2346 return new CreateShortcut().createDialog();
2347 case DIALOG_RENAME_FOLDER:
2348 return new RenameFolder().createDialog();
2349 }
2350
2351 return super.onCreateDialog(id);
2352 }
2353
2354 @Override
2355 protected void onPrepareDialog(int id, Dialog dialog) {
2356 switch (id) {
2357 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 break;
2359 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002360 if (mFolderInfo != null) {
2361 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2362 final CharSequence text = mFolderInfo.title;
2363 input.setText(text);
2364 input.setSelection(0, text.length());
2365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 break;
2367 }
2368 }
2369
2370 void showRenameDialog(FolderInfo info) {
2371 mFolderInfo = info;
2372 mWaitingForResult = true;
2373 showDialog(DIALOG_RENAME_FOLDER);
2374 }
2375
Michael Jurka0280c3b2010-09-17 15:00:07 -07002376 private void showAddDialog(int intersectX, int intersectY) {
2377 resetAddInfo();
2378 mAddIntersectCellX = intersectX;
2379 mAddIntersectCellY = intersectY;
2380 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 mWaitingForResult = true;
2382 showDialog(DIALOG_CREATE_SHORTCUT);
2383 }
2384
Joe Onoratodeb98af2010-02-19 14:59:39 -08002385 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002386 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002387 Bundle bundle = new Bundle();
2388
2389 ArrayList<String> shortcutNames = new ArrayList<String>();
2390 shortcutNames.add(getString(R.string.group_applications));
2391 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2392
2393 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2394 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2395 R.drawable.ic_launcher_application));
2396 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2397
2398 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2399 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002400 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002401 pickIntent.putExtras(bundle);
2402
Joe Onoratodeb98af2010-02-19 14:59:39 -08002403 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002404 }
2405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 private class RenameFolder {
2407 private EditText mInput;
2408
2409 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2411 mInput = (EditText) layout.findViewById(R.id.folder_name);
2412
2413 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2414 builder.setIcon(0);
2415 builder.setTitle(getString(R.string.rename_folder_title));
2416 builder.setCancelable(true);
2417 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2418 public void onCancel(DialogInterface dialog) {
2419 cleanup();
2420 }
2421 });
2422 builder.setNegativeButton(getString(R.string.cancel_action),
2423 new Dialog.OnClickListener() {
2424 public void onClick(DialogInterface dialog, int which) {
2425 cleanup();
2426 }
2427 }
2428 );
2429 builder.setPositiveButton(getString(R.string.rename_action),
2430 new Dialog.OnClickListener() {
2431 public void onClick(DialogInterface dialog, int which) {
2432 changeFolderName();
2433 }
2434 }
2435 );
2436 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002437
2438 final AlertDialog dialog = builder.create();
2439 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2440 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002441 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002442 mInput.requestFocus();
2443 InputMethodManager inputManager = (InputMethodManager)
2444 getSystemService(Context.INPUT_METHOD_SERVICE);
2445 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002446 }
2447 });
2448
2449 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 }
2451
2452 private void changeFolderName() {
2453 final String name = mInput.getText().toString();
2454 if (!TextUtils.isEmpty(name)) {
2455 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002456 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 mFolderInfo.title = name;
2458 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2459
Joe Onorato9c1289c2009-08-17 11:03:03 -04002460 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002461 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002462 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 } else {
2464 final FolderIcon folderIcon = (FolderIcon)
2465 mWorkspace.getViewForTag(mFolderInfo);
2466 if (folderIcon != null) {
2467 folderIcon.setText(name);
2468 getWorkspace().requestLayout();
2469 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002470 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002471 mWorkspaceLoading = true;
2472 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 }
2474 }
2475 }
2476 cleanup();
2477 }
2478
2479 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 dismissDialog(DIALOG_RENAME_FOLDER);
2481 mWaitingForResult = false;
2482 mFolderInfo = null;
2483 }
2484 }
2485
Daniel Sandler843e8602010-06-07 14:59:01 -04002486 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2487 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002488 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002489 }
2490
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002491 // AllAppsView.Watcher
2492 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002493 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2494 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002495 mWorkspace.setVisibility(View.GONE);
2496 }
2497 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002498
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002499 private void showToolbarButton(View button) {
2500 button.setAlpha(1.0f);
2501 button.setVisibility(View.VISIBLE);
2502 button.setFocusable(true);
2503 button.setClickable(true);
2504 }
2505
2506 private void hideToolbarButton(View button) {
2507 button.setAlpha(0.0f);
2508 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2509 button.setVisibility(View.INVISIBLE);
2510 button.setFocusable(false);
2511 button.setClickable(false);
2512 }
2513
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002514 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002516 *
2517 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2518 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002519 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002520 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002521 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002522 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002523 final boolean showing = show;
2524 final boolean hiding = !show;
2525
2526 final int duration = show ?
2527 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2528 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2529
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002531 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2532 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002533 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002534 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002535 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002536 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002537 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002538 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002539 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002540 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 });
2543 seq.play(anim);
2544 } else {
2545 if (showing) {
2546 showToolbarButton(view);
2547 } else {
2548 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551 }
2552
2553 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 * Show/hide the appropriate toolbar buttons for newState.
2555 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002556 *
2557 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002558 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2559 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002560 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002561 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002562 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 final View allAppsButton = findViewById(R.id.all_apps_button);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002564 final View configureButton = findViewById(R.id.configure_button);
2565
2566 switch (newState) {
2567 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 hideOrShowToolbarButton(true, searchButton, showSeq);
2569 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2570 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002571 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 break;
2573 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002574 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 hideOrShowToolbarButton(false, searchButton, hideSeq);
2576 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 break;
2578 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002579 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 hideOrShowToolbarButton(false, searchButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002581 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002582 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 break;
2584 }
2585 }
2586
2587 /**
2588 * Helper method for the cameraZoomIn/cameraZoomOut animations
2589 * @param view The view being animated
2590 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2591 * @param scaleFactor The scale factor used for the zoom
2592 */
2593 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2594 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002595
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002597 // Set pivotY so that at the starting zoom factor, the view is partially
2598 // visible. Modifying initialHeightFactor changes how much of the view is
2599 // initially showing, and hence the perceived angle from which the view enters.
2600 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002602 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002604 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 }
2606 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002607
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002608 /**
2609 * Zoom the camera out from the workspace to reveal 'toView'.
2610 * Assumes that the view to show is anchored at either the very top or very bottom
2611 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002613 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002615 final Resources res = getResources();
2616 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2617 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 final boolean toAllApps = (toState == State.ALL_APPS);
2619 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002620
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002621 setPivotsForZoom(toView, toState, scale);
2622
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002623 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002624 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002625 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002626 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628
2629 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002630 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2631 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2632 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2633 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002634
2635 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002636 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002637 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002638 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002639 // Prepare the position
2640 toView.setTranslationX(0.0f);
2641 toView.setTranslationY(0.0f);
2642 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002643 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002644 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002645 @Override
2646 public void onAnimationEndOrCancel(Animator animation) {
2647 // If we don't set the final scale values here, if this animation is cancelled
2648 // it will have the wrong scale value and subsequent cameraPan animations will
2649 // not fix that
2650 toView.setScaleX(1.0f);
2651 toView.setScaleY(1.0f);
2652 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002653 });
2654
Chet Haaseb1254a62010-09-07 13:35:00 -07002655 AnimatorSet toolbarHideAnim = new AnimatorSet();
2656 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2658
2659 // toView should appear right at the end of the workspace shrink animation
2660 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2661
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002662 if (mStateAnimation != null) mStateAnimation.cancel();
2663 mStateAnimation = new AnimatorSet();
2664 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2665 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002666
2667 // Show the new toolbar buttons just as the main animation is ending
2668 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002669 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2670 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002671 } else {
2672 toView.setTranslationX(0.0f);
2673 toView.setTranslationY(0.0f);
2674 toView.setScaleX(1.0f);
2675 toView.setScaleY(1.0f);
2676 toView.setVisibility(View.VISIBLE);
2677 hideAndShowToolbarButtons(toState, null, null);
2678 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002679 }
2680
2681 /**
2682 * Zoom the camera back into the workspace, hiding 'fromView'.
2683 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002684 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2685 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002686 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002687 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002688 Resources res = getResources();
2689 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2690 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002691 final View fromView =
2692 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2693
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002694 mCustomizePagedView.endChoiceMode();
2695 mAllAppsPagedView.endChoiceMode();
2696
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002698
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002699 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700
2701 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002702 if (mStateAnimation != null) mStateAnimation.cancel();
2703 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002704 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2705 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2706 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2707 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002708 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2709
2710 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2711 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2712 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2713 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002714 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002715 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 }
2718 });
2719
Chet Haaseb1254a62010-09-07 13:35:00 -07002720 AnimatorSet toolbarHideAnim = new AnimatorSet();
2721 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2723
Adam Cohen61033d32010-11-15 18:29:44 -08002724 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002725
2726 // Show the new toolbar buttons at the very end of the whole animation
2727 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2728 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002729 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2730 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002731 } else {
2732 fromView.setVisibility(View.GONE);
2733 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2734 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002735 }
2736
2737 /**
2738 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2739 * This is the transition used on xlarge screens to go between all apps and
2740 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002741 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2742 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2743 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002744 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002745 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 final Resources res = getResources();
2747 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002748 final int workspaceHeight = mWorkspace.getHeight();
2749
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002750 final boolean fromAllApps = (fromState == State.ALL_APPS);
2751 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2752 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002753
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002754 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2755 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2756 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2757 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002758
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002759 mCustomizePagedView.endChoiceMode();
2760 mAllAppsPagedView.endChoiceMode();
2761
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002762 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002763 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002764 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002765 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002766 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002767
2768 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002769 if (mStateAnimation != null) mStateAnimation.cancel();
2770 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002771 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002772 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002773 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002774 toView.setVisibility(View.VISIBLE);
2775 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002776 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002777 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002778 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002779 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002780 fromView.setVisibility(View.GONE);
2781 }
2782 });
2783
Chet Haaseb1254a62010-09-07 13:35:00 -07002784 AnimatorSet toolbarHideAnim = new AnimatorSet();
2785 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002786 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2787
Chet Haase472b2812010-10-14 07:02:04 -07002788 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2789 fromViewStartY, fromViewEndY);
2790 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002791 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2792 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2793 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2794 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2795 );
Chet Haase472b2812010-10-14 07:02:04 -07002796 fromAnim.setDuration(duration);
2797 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002798
2799 // Show the new toolbar buttons just as the main animation is ending
2800 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002801 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2802 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 } else {
2804 fromView.setY(fromViewEndY);
2805 fromView.setVisibility(View.GONE);
2806 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002807 toView.setScaleX(1.0f);
2808 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 toView.setVisibility(View.VISIBLE);
2810 hideAndShowToolbarButtons(toState, null, null);
2811 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002812 }
2813
2814 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002815 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002816 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002817 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002818
Michael Jurka79212d82010-07-30 16:36:20 -07002819 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002820 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002821 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002822 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002823 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002824 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002825 } else {
2826 mAllAppsGrid.zoom(1.0f, animated);
2827 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002828
Romain Guyc16fea72010-03-12 17:17:56 -08002829 ((View) mAllAppsGrid).setFocusable(true);
2830 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002831
Joe Onorato7c312c12009-08-13 21:36:53 -07002832 // TODO: fade these two too
2833 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002834
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002835 // Change the state *after* we've called all the transition code
2836 mState = State.ALL_APPS;
2837 }
2838
2839
2840 void showWorkspace(boolean animated) {
2841 showWorkspace(animated, null);
2842 }
2843
2844 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002845 if (layout != null) {
2846 // always animated, but that's ok since we never specify a layout and
2847 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002848 mWorkspace.unshrink(layout);
2849 } else {
2850 mWorkspace.unshrink(animated);
2851 }
2852 if (mState == State.ALL_APPS) {
2853 closeAllApps(animated);
2854 } else if (mState == State.CUSTOMIZE) {
2855 hideCustomizationDrawer(animated);
2856 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002857
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002858 // Change the state *after* we've called all the transition code
2859 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002860 }
2861
Joe Onoratob2061212009-11-24 16:13:54 -05002862 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002863 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002864 * - Home from workspace
2865 * - from center screen
2866 * - from other screens
2867 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002868 * - from center screen
2869 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002870 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002871 * - from center screen
2872 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002873 * - Launch app from workspace and quit
2874 * - with back
2875 * - with home
2876 * - Launch app from all apps and quit
2877 * - with back
2878 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002879 * - Go to a screen that's not the default, then all
2880 * apps, and launch and app, and go back
2881 * - with back
2882 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002883 * - On workspace, long press power and go back
2884 * - with back
2885 * - with home
2886 * - On all apps, long press power and go back
2887 * - with back
2888 * - with home
2889 * - On workspace, power off
2890 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002891 * - Launch an app and turn off the screen while in that app
2892 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002893 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002894 * - From all apps
2895 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002896 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2897 * - From all apps
2898 * - From the center workspace
2899 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002900 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002901 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002902 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002903 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002905 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002906 } else {
2907 mAllAppsGrid.zoom(0.0f, animated);
2908 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002909 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002910 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002911 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002912 }
2913
Joe Onorato7c312c12009-08-13 21:36:53 -07002914 void lockAllApps() {
2915 // TODO
2916 }
2917
2918 void unlockAllApps() {
2919 // TODO
2920 }
2921
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002922 // Show the customization drawer (only exists in x-large configuration)
2923 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002924 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002925 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002926 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002927 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002928 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002929 // Change the state *after* we've called all the transition code
2930 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002931 }
2932
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002933 // Hide the customization drawer (only exists in x-large configuration)
2934 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002936 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002937 }
2938 }
2939
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002940 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2941 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2942 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002943 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002944 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002945
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002946 void onWorkspaceClick(CellLayout layout) {
2947 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002948 }
2949
Michael Jurka4ef207b2010-11-29 17:05:45 -08002950 // if successful in getting icon, return it; otherwise, set button to use default drawable
2951 private Drawable updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07002952 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2953 ImageView button = (ImageView) findViewById(buttonId);
2954 Drawable toolbarIcon = null;
2955 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002956 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002957 // Look for the toolbar icon specified in the activity meta-data
2958 Bundle metaData = packageManager.getActivityInfo(
2959 activityName, PackageManager.GET_META_DATA).metaData;
2960 if (metaData != null) {
2961 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2962 if (iconResId != 0) {
2963 Resources res = packageManager.getResourcesForActivity(activityName);
2964 toolbarIcon = res.getDrawable(iconResId);
2965 }
2966 }
2967 } catch (NameNotFoundException e) {
2968 // Do nothing
2969 }
2970 // If we were unable to find the icon via the meta-data, use a generic one
2971 if (toolbarIcon == null) {
2972 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002973 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002974 } else {
2975 button.setImageDrawable(toolbarIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002976 return toolbarIcon;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002977 }
2978 }
2979
Michael Jurka4ef207b2010-11-29 17:05:45 -08002980 private void updateButtonWithDrawable(int buttonId, Drawable d) {
2981 ImageView button = (ImageView) findViewById(buttonId);
2982 button.setImageDrawable(d);
2983 }
2984
Michael Jurka0423dcf2010-10-05 14:56:18 -07002985 private void updateGlobalSearchIcon() {
2986 if (LauncherApplication.isScreenXLarge()) {
2987 final SearchManager searchManager =
2988 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2989 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002990 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002991 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07002992 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002993 } else {
2994 findViewById(R.id.search_button).setVisibility(View.GONE);
2995 }
2996 }
2997 }
2998
Michael Jurka4ef207b2010-11-29 17:05:45 -08002999 private void updateGlobalSearchIcon(Drawable d) {
3000 updateButtonWithDrawable(R.id.search_button, d);
3001 }
3002
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003003 private void updateVoiceSearchIcon() {
3004 if (LauncherApplication.isScreenXLarge()) {
3005 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3006 ComponentName activityName = intent.resolveActivity(getPackageManager());
3007 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003008 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003009 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3010 } else {
3011 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003012 }
3013 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003014 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003015
Michael Jurka4ef207b2010-11-29 17:05:45 -08003016 private void updateVoiceSearchIcon(Drawable d) {
3017 updateButtonWithDrawable(R.id.voice_button, d);
3018 }
3019
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003020 /**
3021 * Sets the app market icon (shown when all apps is visible on x-large screens)
3022 */
3023 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003024 if (LauncherApplication.isScreenXLarge()) {
3025 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3026 // Find the app market activity by resolving an intent.
3027 // (If multiple app markets are installed, it will return the ResolverActivity.)
3028 ComponentName activityName = intent.resolveActivity(getPackageManager());
3029 if (activityName != null) {
3030 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003031 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003032 R.id.market_button, activityName, R.drawable.app_market_generic);
3033 }
3034 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003035 }
3036
Michael Jurka4ef207b2010-11-29 17:05:45 -08003037 private void updateAppMarketIcon(Drawable d) {
3038 updateButtonWithDrawable(R.id.market_button, d);
3039 }
3040
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003041 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 * Displays the shortcut creation dialog and launches, if necessary, the
3043 * appropriate activity.
3044 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003045 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003046 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3047 DialogInterface.OnShowListener {
3048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3055 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003056 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 builder.setInverseBackgroundForced(true);
3059
3060 AlertDialog dialog = builder.create();
3061 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003062 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003063 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 return dialog;
3066 }
3067
3068 public void onCancel(DialogInterface dialog) {
3069 mWaitingForResult = false;
3070 cleanup();
3071 }
3072
Romain Guycbb89e42009-06-08 15:52:54 -07003073 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003074 }
3075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003077 try {
3078 dismissDialog(DIALOG_CREATE_SHORTCUT);
3079 } catch (Exception e) {
3080 // An exception is thrown if the dialog is not visible, which is fine
3081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
3083
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003084 /**
3085 * Handle the action clicked in the "Add to home" dialog.
3086 */
3087 public void onClick(DialogInterface dialog, int which) {
3088 Resources res = getResources();
3089 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003090
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003091 switch (which) {
3092 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003093 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003094 break;
3095 }
Romain Guycbb89e42009-06-08 15:52:54 -07003096
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003097 case AddAdapter.ITEM_APPWIDGET: {
3098 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003099
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003100 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3101 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003102 // start the pick activity
3103 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3104 break;
3105 }
Romain Guycbb89e42009-06-08 15:52:54 -07003106
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003107 case AddAdapter.ITEM_LIVE_FOLDER: {
3108 // Insert extra item to handle inserting folder
3109 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003110
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003111 ArrayList<String> shortcutNames = new ArrayList<String>();
3112 shortcutNames.add(res.getString(R.string.group_folder));
3113 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003114
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003115 ArrayList<ShortcutIconResource> shortcutIcons =
3116 new ArrayList<ShortcutIconResource>();
3117 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3118 R.drawable.ic_launcher_folder));
3119 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3120
3121 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3122 pickIntent.putExtra(Intent.EXTRA_INTENT,
3123 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3124 pickIntent.putExtra(Intent.EXTRA_TITLE,
3125 getText(R.string.title_select_live_folder));
3126 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003127
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003128 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3129 break;
3130 }
3131
3132 case AddAdapter.ITEM_WALLPAPER: {
3133 startWallpaper();
3134 break;
3135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 }
3137 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003138
3139 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003140 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 }
3143
3144 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003145 * Receives notifications when applications are added/removed.
3146 */
3147 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3148 @Override
3149 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003150 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003151 String reason = intent.getStringExtra("reason");
3152 if (!"homekey".equals(reason)) {
3153 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003154 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003155 animate = false;
3156 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003157 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003158 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003159 }
3160 }
3161
3162 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003163 * Receives notifications whenever the appwidgets are reset.
3164 */
3165 private class AppWidgetResetObserver extends ContentObserver {
3166 public AppWidgetResetObserver() {
3167 super(new Handler());
3168 }
3169
3170 @Override
3171 public void onChange(boolean selfChange) {
3172 onAppWidgetReset();
3173 }
3174 }
3175
3176 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003177 * If the activity is currently paused, signal that we need to re-run the loader
3178 * in onResume.
3179 *
3180 * This needs to be called from incoming places where resources might have been loaded
3181 * while we are paused. That is becaues the Configuration might be wrong
3182 * when we're not running, and if it comes back to what it was when we
3183 * were paused, we are not restarted.
3184 *
3185 * Implementation of the method from LauncherModel.Callbacks.
3186 *
3187 * @return true if we are currently paused. The caller might be able to
3188 * skip some work in that case since we will come back again.
3189 */
3190 public boolean setLoadOnResume() {
3191 if (mPaused) {
3192 Log.i(TAG, "setLoadOnResume");
3193 mOnResumeNeedsLoad = true;
3194 return true;
3195 } else {
3196 return false;
3197 }
3198 }
3199
3200 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003201 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003203 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003204 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003205 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003206 } else {
3207 return SCREEN_COUNT / 2;
3208 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003210
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003211 void setAllAppsPagedView(PagedView view) {
3212 mAllAppsPagedView = view;
3213 }
3214
Joe Onorato9c1289c2009-08-17 11:03:03 -04003215 /**
3216 * Refreshes the shortcuts shown on the workspace.
3217 *
3218 * Implementation of the method from LauncherModel.Callbacks.
3219 */
3220 public void startBinding() {
3221 final Workspace workspace = mWorkspace;
3222 int count = workspace.getChildCount();
3223 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003224 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003225 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3226 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003227
Joe Onorato9c1289c2009-08-17 11:03:03 -04003228 if (DEBUG_USER_INTERFACE) {
3229 android.widget.Button finishButton = new android.widget.Button(this);
3230 finishButton.setText("Finish");
3231 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3232
3233 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3234 public void onClick(View v) {
3235 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 }
3239 }
3240
3241 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003242 * Bind the items start-end from the list.
3243 *
3244 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003246 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3247
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003248 setLoadOnResume();
3249
Joe Onorato9c1289c2009-08-17 11:03:03 -04003250 final Workspace workspace = mWorkspace;
3251
3252 for (int i=start; i<end; i++) {
3253 final ItemInfo item = shortcuts.get(i);
3254 mDesktopItems.add(item);
3255 switch (item.itemType) {
3256 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3257 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003258 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003259 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3260 false);
3261 break;
3262 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3263 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003264 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003265 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003266 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3267 false);
3268 break;
3269 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3270 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3271 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003272 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 (LiveFolderInfo) item);
3274 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3275 false);
3276 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003278 }
3279
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003281 }
3282
Joe Onorato9c1289c2009-08-17 11:03:03 -04003283 /**
3284 * Implementation of the method from LauncherModel.Callbacks.
3285 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003286 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003287 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003288 sFolders.clear();
3289 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291
3292 /**
3293 * Add the views for a widget to the workspace.
3294 *
3295 * Implementation of the method from LauncherModel.Callbacks.
3296 */
3297 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003298 setLoadOnResume();
3299
Daniel Sandler843e8602010-06-07 14:59:01 -04003300 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3301 if (DEBUG_WIDGETS) {
3302 Log.d(TAG, "bindAppWidget: " + item);
3303 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 final Workspace workspace = mWorkspace;
3305
3306 final int appWidgetId = item.appWidgetId;
3307 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003308 if (DEBUG_WIDGETS) {
3309 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3310 }
3311
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3313
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3315 item.hostView.setTag(item);
3316
3317 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3318 item.cellY, item.spanX, item.spanY, false);
3319
Adam Cohended9f8d2010-11-03 13:25:16 -07003320 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3321
Joe Onorato9c1289c2009-08-17 11:03:03 -04003322 workspace.requestLayout();
3323
3324 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003325
3326 if (DEBUG_WIDGETS) {
3327 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3328 + (SystemClock.uptimeMillis()-start) + "ms");
3329 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 }
3331
3332 /**
3333 * Callback saying that there aren't any more items to bind.
3334 *
3335 * Implementation of the method from LauncherModel.Callbacks.
3336 */
3337 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003338 setLoadOnResume();
3339
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 if (mSavedState != null) {
3341 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003342 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 }
3344
3345 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3346 if (userFolders != null) {
3347 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003348 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 if (info != null) {
3350 openFolder(info);
3351 }
3352 }
3353 final Folder openFolder = mWorkspace.getOpenFolder();
3354 if (openFolder != null) {
3355 openFolder.requestFocus();
3356 }
3357 }
3358
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 mSavedState = null;
3360 }
3361
3362 if (mSavedInstanceState != null) {
3363 super.onRestoreInstanceState(mSavedInstanceState);
3364 mSavedInstanceState = null;
3365 }
3366
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 mWorkspaceLoading = false;
3368 }
3369
3370 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003371 * Updates the icons on the launcher that are affected by changes to the package list
3372 * on the device.
3373 */
3374 private void updateIconsAffectedByPackageManagerChanges() {
3375 updateAppMarketIcon();
3376 updateGlobalSearchIcon();
3377 updateVoiceSearchIcon();
3378 }
3379
3380 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 * Add the icons for all apps.
3382 *
3383 * Implementation of the method from LauncherModel.Callbacks.
3384 */
3385 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003386 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003387 if (mCustomizePagedView != null) {
3388 mCustomizePagedView.setApps(apps);
3389 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003390 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 }
3392
3393 /**
3394 * A package was installed.
3395 *
3396 * Implementation of the method from LauncherModel.Callbacks.
3397 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003398 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003399 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003401 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003402 if (mCustomizePagedView != null) {
3403 mCustomizePagedView.addApps(apps);
3404 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003405 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 }
3407
3408 /**
3409 * A package was updated.
3410 *
3411 * Implementation of the method from LauncherModel.Callbacks.
3412 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003413 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003414 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003416 mWorkspace.updateShortcuts(apps);
3417 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003418 if (mCustomizePagedView != null) {
3419 mCustomizePagedView.updateApps(apps);
3420 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003421 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
3423
3424 /**
3425 * A package was uninstalled.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
Joe Onorato36115782010-06-17 13:28:48 -04003429 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003431 if (permanent) {
3432 mWorkspace.removeItems(apps);
3433 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003434 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003435 if (mCustomizePagedView != null) {
3436 mCustomizePagedView.removeApps(apps);
3437 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003438 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 }
Joe Onoratobe386092009-11-17 17:32:16 -08003440
3441 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003442 * A number of packages were updated.
3443 */
3444 public void bindPackagesUpdated() {
3445 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003446 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003447 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003448 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003449 }
3450
3451 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003452 * Prints out out state for debugging.
3453 */
3454 public void dumpState() {
3455 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003456 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003457 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3458 Log.d(TAG, "mRestoring=" + mRestoring);
3459 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3460 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3461 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003462 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003463 mModel.dumpState();
3464 mAllAppsGrid.dumpState();
3465 Log.d(TAG, "END launcher2 dump state");
3466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003467}