blob: 9eddbf2067d2d8f332f36a4950d2af61f3d113dc [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
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import com.android.common.Search;
21import com.android.launcher.R;
22
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.Animator;
24import android.animation.AnimatorListenerAdapter;
25import 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;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070045import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Patrick Dubroyceae05d2010-08-30 10:40:53 -070048import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040052import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080055import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080066import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070067import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
71import android.text.TextUtils;
72import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070082import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070084import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070085import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070086import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070092import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070093import android.widget.TabHost.OnTabChangeListener;
94import android.widget.TabHost.TabContentFactory;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
98import java.io.DataInputStream;
99import java.io.DataOutputStream;
100import java.io.FileNotFoundException;
101import java.io.IOException;
102import java.util.ArrayList;
103import java.util.HashMap;
104import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106/**
107 * Default launcher application.
108 */
Michael Jurka946ad472010-07-09 18:05:18 -0700109public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700110 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
111 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800112 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700113 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400116 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800120 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700123 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
124 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
126 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700127 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 private static final int REQUEST_CREATE_SHORTCUT = 1;
130 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
134 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700136 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
139
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800140 static final int SCREEN_COUNT = 5;
141 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato7c312c12009-08-13 21:36:53 -0700143 static final int DIALOG_CREATE_SHORTCUT = 1;
144 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Romain Guy98d01652009-06-30 16:21:04 -0700146 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 // Type: int
149 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700150 // Type: int
151 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: long
153 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
154 // Type: int
155 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: boolean
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
162 // Type: long
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
164
Winson Chung80baf5a2010-08-09 16:03:15 -0700165 // tags for the customization tabs
166 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700167 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700168 private static final String FOLDERS_TAG = "folders";
169 private static final String SHORTCUTS_TAG = "shortcuts";
170 private static final String WALLPAPERS_TAG = "wallpapers";
171
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700172 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
173
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700174 /** The different states that Launcher can be in. */
175 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700176 private State mState = State.WORKSPACE;
177 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800182 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800184 private final BroadcastReceiver mCloseSystemDialogsReceiver
185 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800186 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private LayoutInflater mInflater;
189
Joe Onorato00acb122009-08-04 16:04:30 -0400190 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private AppWidgetManager mAppWidgetManager;
194 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
Michael Jurka0280c3b2010-09-17 15:00:07 -0700196 private int mAddScreen = -1;
197 private int mAddIntersectCellX = -1;
198 private int mAddIntersectCellY = -1;
199 private int[] mAddDropPosition;
200 private int[] mTmpAddItemCellCoordinates = new int[2];
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private FolderInfo mFolderInfo;
203
Joe Onorato7c312c12009-08-13 21:36:53 -0700204 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700205 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700206 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700207 private TabHost mHomeCustomizationDrawer;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700208
209 private PagedView mAllAppsPagedView = null;
210 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private Bundle mSavedState;
213
214 private SpannableStringBuilder mDefaultKeySsb = null;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private boolean mWorkspaceLoading = true;
217
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800218 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private boolean mRestoring;
220 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
222 private Bundle mSavedInstanceState;
223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800225 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700227 private static LocaleConfiguration sLocaleConfiguration = null;
228
Romain Guy84f296c2009-11-04 15:00:44 -0800229 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Romain Guy1fbc1c82009-11-09 20:43:08 -0800232 private ImageView mPreviousView;
233 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500234
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400235 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400236 private String[] mHotseatConfig = null;
237 private Intent[] mHotseats = null;
238 private Drawable[] mHotseatIcons = null;
239 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400240
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700241 private Intent mAppMarketIntent = null;
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 @Override
244 protected void onCreate(Bundle savedInstanceState) {
245 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700246
Winson Chungaafa03c2010-06-11 17:34:16 -0700247 if (LauncherApplication.isInPlaceRotationEnabled()) {
248 // hide the status bar (temporary until we get the status bar design figured out)
249 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
250 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
251 }
252
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 LauncherApplication app = ((LauncherApplication)getApplication());
254 mModel = app.setLauncher(this);
255 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400256 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
261 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200264 android.os.Debug.startMethodTracing(
265 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400268 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 checkForLocaleChange();
270 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700272 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
273 if (mHomeCustomizationDrawer != null) {
274 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700275
Winson Chung80baf5a2010-08-09 16:03:15 -0700276 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700277 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
278 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700279 TabContentFactory contentFactory = new TabContentFactory() {
280 public View createTabContent(String tag) {
281 return mCustomizePagedView;
282 }
283 };
284
Michael Jurka946ad472010-07-09 18:05:18 -0700285 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
287 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700288 String applicationsLabel = getString(R.string.applications_tab_label);
289 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
290 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700291 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
293 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700294 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
296 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700297 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
298 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
299 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700300
Winson Chung80baf5a2010-08-09 16:03:15 -0700301 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
302 public void onTabChanged(String tabId) {
303 // animate the changing of the tab content by fading pages in and out
304 final int duration = 150;
305 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700306 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700307 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700308 alphaAnim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -0700309 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700310 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -0700311 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700312 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
313 if (tag == WIDGETS_TAG) {
314 mCustomizePagedView.setCustomizationFilter(
315 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700316 } else if (tag == APPLICATIONS_TAG) {
317 mCustomizePagedView.setCustomizationFilter(
318 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700319 } else if (tag == FOLDERS_TAG) {
320 mCustomizePagedView.setCustomizationFilter(
321 CustomizePagedView.CustomizationType.FolderCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 } else if (tag == WALLPAPERS_TAG) {
323 mCustomizePagedView.setCustomizationFilter(
324 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700325 } else if (tag == SHORTCUTS_TAG) {
326 mCustomizePagedView.setCustomizationFilter(
327 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 }
329
330 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700331 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700332 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700333 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 alphaAnim.start();
335 }
336 });
337 alphaAnim.start();
338 }
339 });
Michael Jurka946ad472010-07-09 18:05:18 -0700340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 setupViews();
342
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800343 registerContentObservers();
344
Joe Onorato7c312c12009-08-13 21:36:53 -0700345 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 mSavedState = savedInstanceState;
348 restoreState(mSavedState);
349
350 if (PROFILE_STARTUP) {
351 android.os.Debug.stopMethodTracing();
352 }
353
354 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 }
357
358 // For handling default keys
359 mDefaultKeySsb = new SpannableStringBuilder();
360 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800361
362 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
363 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
Romain Guycbb89e42009-06-08 15:52:54 -0700365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700367 if (sLocaleConfiguration == null) {
368 new AsyncTask<Void, Void, LocaleConfiguration>() {
369 @Override
370 protected LocaleConfiguration doInBackground(Void... unused) {
371 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
372 readConfiguration(Launcher.this, localeConfiguration);
373 return localeConfiguration;
374 }
375
376 @Override
377 protected void onPostExecute(LocaleConfiguration result) {
378 sLocaleConfiguration = result;
379 checkForLocaleChange(); // recursive, but now with a locale configuration
380 }
381 }.execute();
382 return;
383 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 final Configuration configuration = getResources().getConfiguration();
386
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700387 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final String locale = configuration.locale.toString();
389
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700390 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 final int mcc = configuration.mcc;
392
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 final int mnc = configuration.mnc;
395
Romain Guy84f296c2009-11-04 15:00:44 -0800396 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397
Romain Guy84f296c2009-11-04 15:00:44 -0800398 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399 sLocaleConfiguration.locale = locale;
400 sLocaleConfiguration.mcc = mcc;
401 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700402
Joe Onorato0589f0f2010-02-08 13:44:00 -0800403 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400404 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405
406 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
407 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700408 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409 public void run() {
410 writeConfiguration(Launcher.this, localeConfiguration);
411 }
412 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700413 }
414 }
415
416 private static class LocaleConfiguration {
417 public String locale;
418 public int mcc = -1;
419 public int mnc = -1;
420 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700421
Romain Guy98d01652009-06-30 16:21:04 -0700422 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
423 DataInputStream in = null;
424 try {
425 in = new DataInputStream(context.openFileInput(PREFERENCES));
426 configuration.locale = in.readUTF();
427 configuration.mcc = in.readInt();
428 configuration.mnc = in.readInt();
429 } catch (FileNotFoundException e) {
430 // Ignore
431 } catch (IOException e) {
432 // Ignore
433 } finally {
434 if (in != null) {
435 try {
436 in.close();
437 } catch (IOException e) {
438 // Ignore
439 }
440 }
441 }
442 }
443
444 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
445 DataOutputStream out = null;
446 try {
447 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
448 out.writeUTF(configuration.locale);
449 out.writeInt(configuration.mcc);
450 out.writeInt(configuration.mnc);
451 out.flush();
452 } catch (FileNotFoundException e) {
453 // Ignore
454 } catch (IOException e) {
455 //noinspection ResultOfMethodCallIgnored
456 context.getFileStreamPath(PREFERENCES).delete();
457 } finally {
458 if (out != null) {
459 try {
460 out.close();
461 } catch (IOException e) {
462 // Ignore
463 }
464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466 }
467
468 static int getScreen() {
469 synchronized (sLock) {
470 return sScreen;
471 }
472 }
473
474 static void setScreen(int screen) {
475 synchronized (sLock) {
476 sScreen = screen;
477 }
478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700481 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482
483 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700484 // TODO: Put back when we decide about scrolling the wallpaper
485 // boolean isPortrait = display.getWidth() < display.getHeight();
486 // final int width = isPortrait ? display.getWidth() : display.getHeight();
487 // final int height = isPortrait ? display.getHeight() : display.getWidth();
488 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
489 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400492 // Note: This doesn't do all the client-id magic that BrowserProvider does
493 // in Browser. (http://b/2425179)
494 private Uri getDefaultBrowserUri() {
495 String url = getString(R.string.default_browser_url);
496 if (url.indexOf("{CID}") != -1) {
497 url = url.replace("{CID}", "android-google");
498 }
499 return Uri.parse(url);
500 }
501
502 // Load the Intent templates from arrays.xml to populate the hotseats. For
503 // each Intent, if it resolves to a single app, use that as the launch
504 // intent & use that app's label as the contentDescription. Otherwise,
505 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400506 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400507 if (mHotseatConfig == null) {
508 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
509 if (mHotseatConfig.length > 0) {
510 mHotseats = new Intent[mHotseatConfig.length];
511 mHotseatLabels = new CharSequence[mHotseatConfig.length];
512 mHotseatIcons = new Drawable[mHotseatConfig.length];
513 } else {
514 mHotseats = null;
515 mHotseatIcons = null;
516 mHotseatLabels = null;
517 }
518
519 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
520 for (int i=0; i<mHotseatConfig.length; i++) {
521 // load icon for this slot; currently unrelated to the actual activity
522 try {
523 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
524 } catch (ArrayIndexOutOfBoundsException ex) {
525 Log.w(TAG, "Missing hotseat_icons array item #" + i);
526 mHotseatIcons[i] = null;
527 }
528 }
529 hotseatIconDrawables.recycle();
530 }
531
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400532 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400533 for (int i=0; i<mHotseatConfig.length; i++) {
534 Intent intent = null;
535 if (mHotseatConfig[i].equals("*BROWSER*")) {
536 // magic value meaning "launch user's default web browser"
537 // replace it with a generic web request so we can see if there is indeed a default
538 String defaultUri = getString(R.string.default_browser_url);
539 intent = new Intent(
540 Intent.ACTION_VIEW,
541 ((defaultUri != null)
542 ? Uri.parse(defaultUri)
543 : getDefaultBrowserUri())
544 ).addCategory(Intent.CATEGORY_BROWSABLE);
545 // note: if the user launches this without a default set, she
546 // will always be taken to the default URL above; this is
547 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700548 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 // URL is unavoidably sent to the chosen app.
550 } else {
551 try {
552 intent = Intent.parseUri(mHotseatConfig[i], 0);
553 } catch (java.net.URISyntaxException ex) {
554 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
555 // bogus; leave intent=null
556 }
557 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 if (intent == null) {
560 mHotseats[i] = null;
561 mHotseatLabels[i] = getText(R.string.activity_not_found);
562 continue;
563 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400564
565 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700566 Log.d(TAG, "loadHotseats: hotseat " + i
567 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400569 + "]");
570 }
571
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
573 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700574 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 Log.d(TAG, "Best match for intent: " + bestMatch);
576 Log.d(TAG, "All matches: ");
577 for (ResolveInfo ri : allMatches) {
578 Log.d(TAG, " --> " + ri);
579 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400581 // did this resolve to a single app, or the resolver?
582 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 // can't find any activity to handle this. let's leave the
584 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 // to launch.
586 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400587
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 // set accessibility text to "Not installed"
589 mHotseatLabels[i] = getText(R.string.activity_not_found);
590 } else {
591 boolean found = false;
592 for (ResolveInfo ri : allMatches) {
593 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
594 && bestMatch.activityInfo.applicationInfo.packageName
595 .equals(ri.activityInfo.applicationInfo.packageName)) {
596 found = true;
597 break;
598 }
599 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700600
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400601 if (!found) {
602 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
603 // the bestMatch is probably the ResolveActivity, meaning the
604 // user has not yet selected a default
605 // so: we'll keep the original intent for now
606 mHotseats[i] = intent;
607
608 // set the accessibility text to "Select shortcut"
609 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
610 } else {
611 // we have an app!
612 // now reconstruct the intent to launch it through the front
613 // door
614 ComponentName com = new ComponentName(
615 bestMatch.activityInfo.applicationInfo.packageName,
616 bestMatch.activityInfo.name);
617 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
618
619 // load the app label for accessibility
620 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
621 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400622 }
623
624 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 Log.d(TAG, "loadHotseats: hotseat " + i
626 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400627 + ((mHotseats[i] == null)
628 ? "null"
629 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400630 + "] label=[" + mHotseatLabels[i]
631 + "]"
632 );
633 }
634 }
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 @Override
638 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700639 mWaitingForResult = false;
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 // The pattern used here is that a user PICKs a specific application,
642 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
645 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700646
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 switch (requestCode) {
649 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700650 completeAddApplication(
651 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 break;
653 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800654 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 break;
656 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 break;
659 case REQUEST_PICK_LIVE_FOLDER:
660 addLiveFolder(data);
661 break;
662 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700666 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700669 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700672 case REQUEST_PICK_WALLPAPER:
673 // We just wanted the activity result here so we can clear mWaitingForResult
674 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Romain Guy18042c82009-11-06 11:44:55 -0800676 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
677 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
678 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 // Clean up the appWidgetId if we canceled
680 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
681 if (appWidgetId != -1) {
682 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
686
687 @Override
688 protected void onResume() {
689 super.onResume();
690
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800691 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 mWorkspaceLoading = true;
695 mModel.startLoader(this, true);
696 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700698 // When we resume Launcher, a different Activity might be responsible for the app
699 // market intent, so refresh the icon
700 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702
703 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 protected void onPause() {
705 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 // Some launcher layouts don't have a previous and next view
707 if (mPreviousView != null) {
708 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700709 }
710 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 dismissPreview(mNextView);
712 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800713 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 }
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700716 @Override
717 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 // Flag the loader to stop early before switching
719 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800720 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800721 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700722 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800724 // We can't hide the IME if it was forced open. So don't bother
725 /*
726 @Override
727 public void onWindowFocusChanged(boolean hasFocus) {
728 super.onWindowFocusChanged(hasFocus);
729
730 if (hasFocus) {
731 final InputMethodManager inputManager = (InputMethodManager)
732 getSystemService(Context.INPUT_METHOD_SERVICE);
733 WindowManager.LayoutParams lp = getWindow().getAttributes();
734 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
735 android.os.Handler()) {
736 protected void onReceiveResult(int resultCode, Bundle resultData) {
737 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
738 }
739 });
740 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
741 }
742 }
743 */
744
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 private boolean acceptFilter() {
746 final InputMethodManager inputManager = (InputMethodManager)
747 getSystemService(Context.INPUT_METHOD_SERVICE);
748 return !inputManager.isFullscreenMode();
749 }
750
751 @Override
752 public boolean onKeyDown(int keyCode, KeyEvent event) {
753 boolean handled = super.onKeyDown(keyCode, event);
754 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
755 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
756 keyCode, event);
757 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700758 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700759 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700760 // showSearchDialog()
761 // If there are multiple keystrokes before the search dialog takes focus,
762 // onSearchRequested() will be called for every keystroke,
763 // but it is idempotent, so it's fine.
764 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
766 }
767
Joe Onorato8a9625e2010-01-28 15:55:35 -0800768 // Eat the long press event so the keyboard doesn't come up.
769 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
770 return true;
771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 return handled;
774 }
775
Karl Rosaen138a0412009-04-23 19:00:21 -0700776 private String getTypedText() {
777 return mDefaultKeySsb.toString();
778 }
779
780 private void clearTypedText() {
781 mDefaultKeySsb.clear();
782 mDefaultKeySsb.clearSpans();
783 Selection.setSelection(mDefaultKeySsb, 0);
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700787 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
788 * State
789 */
790 private static State intToState(int stateOrdinal) {
791 State state = State.WORKSPACE;
792 final State[] stateValues = State.values();
793 for (int i = 0; i < stateValues.length; i++) {
794 if (stateValues[i].ordinal() == stateOrdinal) {
795 state = stateValues[i];
796 break;
797 }
798 }
799 return state;
800 }
801
802 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 * Restores the previous state, if it exists.
804 *
805 * @param savedState The previous state.
806 */
807 private void restoreState(Bundle savedState) {
808 if (savedState == null) {
809 return;
810 }
811
Michael Jurka883f55b2010-10-21 15:47:14 -0700812 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
813
814 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800815 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700816 } else if (state == State.CUSTOMIZE) {
817 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800818 }
819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
821 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700822 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 }
824
825 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700828 mAddScreen = addScreen;
829 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
830 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 mRestoring = true;
832 }
833
834 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
835 if (renameFolder) {
836 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700837 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 mRestoring = true;
839 }
840 }
841
842 /**
843 * Finds all the views we need and configure them properly.
844 */
845 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700846 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400847
Romain Guyb1b69f52009-08-10 15:10:15 -0700848 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400849 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850
Joe Onorato7c312c12009-08-13 21:36:53 -0700851 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700852 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700853 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800854 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700855 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700856 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700857
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700858 if (LauncherApplication.isScreenXLarge()) {
859 // They need to be INVISIBLE initially so that they will be measured in the layout.
860 // Otherwise the animations are messed up when we show them for the first time.
861 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
862 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
866 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500867 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700868
Joe Onorato7c312c12009-08-13 21:36:53 -0700869 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
870 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700872 View handleView = findViewById(R.id.all_apps_button);
873 if (handleView != null && handleView instanceof HandleView) {
874 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700875 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700876 mHandleView.setLauncher(this);
877 mHandleView.setOnClickListener(this);
878 mHandleView.setOnLongClickListener(this);
879 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800880
Winson Chung80baf5a2010-08-09 16:03:15 -0700881 if (mCustomizePagedView != null) {
882 mCustomizePagedView.setLauncher(this);
883 mCustomizePagedView.setDragController(dragController);
884 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700885 } else {
886 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
887 hotseatLeft.setContentDescription(mHotseatLabels[0]);
888 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
889 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
890 hotseatRight.setContentDescription(mHotseatLabels[1]);
891 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400892
Michael Jurkaaf442092010-06-10 17:01:57 -0700893 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
894 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800895
Michael Jurkaaf442092010-06-10 17:01:57 -0700896 Drawable previous = mPreviousView.getDrawable();
897 Drawable next = mNextView.getDrawable();
898 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
Michael Jurkaaf442092010-06-10 17:01:57 -0700900 mPreviousView.setHapticFeedbackEnabled(false);
901 mPreviousView.setOnLongClickListener(this);
902 mNextView.setHapticFeedbackEnabled(false);
903 mNextView.setOnLongClickListener(this);
904 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400907 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909
910 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400911 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700912 int deleteZoneHandleId;
913 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700914 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700915 } else {
916 deleteZoneHandleId = R.id.all_apps_button_cluster;
917 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700918 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700919 dragController.addDragListener(deleteZone);
920
921 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
922 if (infoButton != null) {
923 infoButton.setLauncher(this);
924 infoButton.setHandle(findViewById(R.id.configure_button));
925 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
926 dragController.addDragListener(infoButton);
927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Joe Onorato00acb122009-08-04 16:04:30 -0400929 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400930 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800931 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700932
Joe Onorato00acb122009-08-04 16:04:30 -0400933 // The order here is bottom to top.
934 dragController.addDropTarget(workspace);
935 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700936 if (infoButton != null) {
937 dragController.addDropTarget(infoButton);
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
Romain Guy8a73c512009-11-09 19:19:59 -0800941 @SuppressWarnings({"UnusedDeclaration"})
942 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700943 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800944 mWorkspace.scrollLeft();
945 }
Romain Guy8a73c512009-11-09 19:19:59 -0800946 }
947
948 @SuppressWarnings({"UnusedDeclaration"})
949 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700950 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800951 mWorkspace.scrollRight();
952 }
Romain Guy8a73c512009-11-09 19:19:59 -0800953 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400954
955 @SuppressWarnings({"UnusedDeclaration"})
956 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700957 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400958
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400959 int index = -1;
960 if (v.getId() == R.id.hotseat_left) {
961 index = 0;
962 } else if (v.getId() == R.id.hotseat_right) {
963 index = 1;
964 }
965
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400966 // reload these every tap; you never know when they might change
967 loadHotseats();
968 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
969 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400970 startActivitySafely(
971 mHotseats[index],
972 "hotseat"
973 );
974 }
975 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 /**
978 * Creates a view representing a shortcut.
979 *
980 * @param info The data structure describing the shortcut.
981 *
982 * @return A View inflated from R.layout.application.
983 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700986 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 /**
990 * Creates a view representing a shortcut inflated from the specified resource.
991 *
992 * @param layoutResId The id of the XML layout used to create the shortcut.
993 * @param parent The group the shortcut belongs to.
994 * @param info The data structure describing the shortcut.
995 *
996 * @return A View inflated from layoutResId.
997 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1000
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1002 new FastBitmapDrawable(info.getIcon(mIconCache)),
1003 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 favorite.setText(info.title);
1005 favorite.setTag(info);
1006 favorite.setOnClickListener(this);
1007
1008 return favorite;
1009 }
1010
1011 /**
1012 * Add an application shortcut to the workspace.
1013 *
1014 * @param data The intent describing the application.
1015 * @param cellInfo The position on screen where to create the shortcut.
1016 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 void completeAddApplication(Context context, Intent data, int screen,
1018 int intersectCellX, int intersectCellY) {
1019 final int[] cellXY = mTmpAddItemCellCoordinates;
1020 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1021
1022 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1023 showOutOfSpaceMessage();
1024 return;
1025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1028 data, context);
1029
Romain Guy73b979d2009-06-09 12:57:21 -07001030 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001031 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001033 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1035 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001036 } else {
1037 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039 }
Romain Guycbb89e42009-06-08 15:52:54 -07001040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 /**
1042 * Add a shortcut to the workspace.
1043 *
1044 * @param data The intent describing the shortcut.
1045 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001047 private void completeAddShortcut(Intent data, int screen,
1048 int intersectCellX, int intersectCellY) {
1049 final int[] cellXY = mTmpAddItemCellCoordinates;
1050 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001051
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1053 showOutOfSpaceMessage();
1054 return;
1055 }
1056
1057 final ShortcutInfo info = mModel.addShortcut(
1058 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059
1060 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 }
1064 }
1065
Romain Guycbb89e42009-06-08 15:52:54 -07001066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001070 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001074 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001075
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1078 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001081 // We have saved the position to which the widget was dragged-- this really only matters
1082 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001084
1085 // For now, we don't save the coordinate where we dropped the icon because we're not
1086 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1087 // a widget on the home screen in case we want to add it in the future
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 final int[] touchXY = null;
1089 //final int[] touchXY = mAddDropPosition;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001090 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 boolean foundCellSpan = false;
1092 if (touchXY != null) {
1093 // when dragging and dropping, just find the closest free spot
1094 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1095 int[] result = screenLayout.findNearestVacantArea(
1096 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001097 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 foundCellSpan = !findNearestVacantAreaFailed;
1099 } else {
1100 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1101 // if we long pressed on an empty cell to bring up a menu,
1102 // make sure we intersect the empty cell
1103 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1104 mAddIntersectCellX, mAddIntersectCellY);
1105 } else {
1106 // if we went through the menu -> add, just find any spot
1107 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1108 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001109 }
1110
Michael Jurka0280c3b2010-09-17 15:00:07 -07001111 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001112 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001113 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001114 return;
1115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001117 // Build Launcher-specific widget info and save to database
1118 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 launcherInfo.spanX = spanXY[0];
1120 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 LauncherModel.addItemToDatabase(this, launcherInfo,
1123 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001124 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125
1126 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001130 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001132 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 void showOutOfSpaceMessage() {
1141 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1142 }
1143
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1145 mDesktopItems.remove(launcherInfo);
1146 launcherInfo.hostView = null;
1147 }
1148
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001149 public LauncherAppWidgetHost getAppWidgetHost() {
1150 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001153 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001154 getWindow().closeAllPanels();
1155
1156 try {
1157 dismissDialog(DIALOG_CREATE_SHORTCUT);
1158 // Unlock the workspace if the dialog was showing
1159 } catch (Exception e) {
1160 // An exception is thrown if the dialog is not visible, which is fine
1161 }
1162
1163 try {
1164 dismissDialog(DIALOG_RENAME_FOLDER);
1165 // Unlock the workspace if the dialog was showing
1166 } catch (Exception e) {
1167 // An exception is thrown if the dialog is not visible, which is fine
1168 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001169
1170 // Whatever we were doing is hereby canceled.
1171 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001172 }
1173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 @Override
1175 protected void onNewIntent(Intent intent) {
1176 super.onNewIntent(intent);
1177
1178 // Close the menu
1179 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001180 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001181 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001182
Joe Onorato14f122b2009-11-19 14:06:36 -08001183 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1184 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001185
Michael Jurka4cb37242010-08-09 21:05:32 -07001186 // in all these cases, only animate if we're already on home
1187 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001188 mWorkspace.unshrink(alreadyOnHome);
1189 }
1190 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001191 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001192 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1193 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001194 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001195 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001196
Joe Onorato3a8820b2009-11-10 15:06:42 -08001197 final View v = getWindow().peekDecorView();
1198 if (v != null && v.getWindowToken() != null) {
1199 InputMethodManager imm = (InputMethodManager)getSystemService(
1200 INPUT_METHOD_SERVICE);
1201 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
1203 }
1204 }
1205
1206 @Override
1207 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1208 // Do not call super here
1209 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001210
1211 if (mHomeCustomizationDrawer != null) {
1212 String cur = savedInstanceState.getString("currentTab");
1213 if (cur != null) {
1214 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1215 }
1216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
1218
1219 @Override
1220 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001221 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222
1223 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1224 if (folders.size() > 0) {
1225 final int count = folders.size();
1226 long[] ids = new long[count];
1227 for (int i = 0; i < count; i++) {
1228 final FolderInfo info = folders.get(i).getInfo();
1229 ids[i] = info.id;
1230 }
1231 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1232 } else {
1233 super.onSaveInstanceState(outState);
1234 }
1235
Michael Jurka883f55b2010-10-21 15:47:14 -07001236 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 if (mAddScreen > -1 && mWaitingForResult) {
1239 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1240 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1241 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243
1244 if (mFolderInfo != null && mWaitingForResult) {
1245 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1246 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1247 }
Michael Jurka946ad472010-07-09 18:05:18 -07001248
1249 if (mHomeCustomizationDrawer != null) {
1250 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1251 if (currentTabTag != null) {
1252 outState.putString("currentTab", currentTabTag);
1253 }
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256
1257 @Override
1258 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001264 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
1266
1267 TextKeyListener.getInstance().release();
1268
Joe Onorato9c1289c2009-08-17 11:03:03 -04001269 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001272
1273 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001274
Patrick Dubroyab962b72010-07-26 12:10:10 -07001275 // Some launcher layouts don't have a previous and next view
1276 if (mPreviousView != null) {
1277 dismissPreview(mPreviousView);
1278 }
1279 if (mNextView != null) {
1280 dismissPreview(mNextView);
1281 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001282
1283 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285
1286 @Override
1287 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001288 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 super.startActivityForResult(intent, requestCode);
1290 }
1291
1292 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001293 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001295
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001296 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001297
Karl Rosaen138a0412009-04-23 19:00:21 -07001298 if (initialQuery == null) {
1299 // Use any text typed in the launcher as the initial query
1300 initialQuery = getTypedText();
1301 clearTypedText();
1302 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 if (appSearchData == null) {
1304 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001305 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
Romain Guycbb89e42009-06-08 15:52:54 -07001307
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 final SearchManager searchManager =
1309 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001310 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001311 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313
1314 @Override
1315 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001316 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 return false;
1318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
1320 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1323 .setIcon(android.R.drawable.ic_menu_add)
1324 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001325 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1326 .setIcon(android.R.drawable.ic_menu_manage)
1327 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001328 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 .setIcon(android.R.drawable.ic_menu_gallery)
1330 .setAlphabeticShortcut('W');
1331 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1332 .setIcon(android.R.drawable.ic_search_category_default)
1333 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1334 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1335 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1336 .setAlphabeticShortcut('N');
1337
1338 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001339 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1340 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
1342 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1343 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1344 .setIntent(settings);
1345
1346 return true;
1347 }
1348
1349 @Override
1350 public boolean onPrepareOptionsMenu(Menu menu) {
1351 super.onPrepareOptionsMenu(menu);
1352
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001353 // If all apps is animating, don't show the menu, because we don't know
1354 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001355 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001356 return false;
1357 }
1358
1359 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001360 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001361 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1362 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1363
1364 // Disable add if the workspace is full.
1365 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001366 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1367 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
1370 return true;
1371 }
1372
1373 @Override
1374 public boolean onOptionsItemSelected(MenuItem item) {
1375 switch (item.getItemId()) {
1376 case MENU_ADD:
1377 addItems();
1378 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001379 case MENU_MANAGE_APPS:
1380 manageApps();
1381 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 case MENU_WALLPAPER_SETTINGS:
1383 startWallpaper();
1384 return true;
1385 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001386 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 return true;
1388 case MENU_NOTIFICATIONS:
1389 showNotifications();
1390 return true;
1391 }
1392
1393 return super.onOptionsItemSelected(item);
1394 }
Romain Guycbb89e42009-06-08 15:52:54 -07001395
Karl Rosaen138a0412009-04-23 19:00:21 -07001396 /**
1397 * Indicates that we want global search for this activity by setting the globalSearch
1398 * argument for {@link #startSearch} to true.
1399 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001401 @Override
1402 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001403 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001404 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001405 }
1406
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 public boolean isWorkspaceLocked() {
1408 return mWorkspaceLoading || mWaitingForResult;
1409 }
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001412 if (LauncherApplication.isScreenXLarge()) {
1413 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001414 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001415 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001416 }
1417 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001418 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001419 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
Michael Jurka0280c3b2010-09-17 15:00:07 -07001423 private void resetAddInfo() {
1424 mAddScreen = -1;
1425 mAddIntersectCellX = -1;
1426 mAddIntersectCellY = -1;
1427 mAddDropPosition = null;
1428 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001429
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 void addAppWidgetFromDrop(ComponentName appWidgetProvider, int screen, int[] position) {
1431 resetAddInfo();
1432 mAddScreen = screen;
1433
1434 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1435 mAddDropPosition = position;
1436
Michael Jurkaaf442092010-06-10 17:01:57 -07001437 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1438 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1439 addAppWidgetImpl(appWidgetId);
1440 }
1441
Winson Chung7ad01412010-09-27 11:33:03 -07001442 private void manageApps() {
1443 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1444 }
1445
Michael Jurkaaf442092010-06-10 17:01:57 -07001446 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 // TODO: catch bad widget exception when sent
1448 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001449 // TODO: Is this log message meaningful?
1450 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1451 addAppWidgetImpl(appWidgetId);
1452 }
1453
1454 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001455 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
Bjorn Bringert7984c942009-12-09 15:38:25 +00001457 if (appWidget.configure != null) {
1458 // Launch over to configure widget, if needed
1459 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1460 intent.setComponent(appWidget.configure);
1461 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1462
Romain Guy8e633c52010-03-04 12:51:36 -08001463 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001465 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001466 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
Romain Guycbb89e42009-06-08 15:52:54 -07001469
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1471 resetAddInfo();
1472 mAddScreen = screen;
1473 mAddDropPosition = position;
1474
1475 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1476 createShortcutIntent.setComponent(componentName);
1477 processShortcut(createShortcutIntent);
1478 }
1479
Joe Onoratodeb98af2010-02-19 14:59:39 -08001480 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001481 // Handle case where user selected "Applications"
1482 String applicationName = getResources().getString(R.string.group_applications);
1483 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001484
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001485 if (applicationName != null && applicationName.equals(shortcutName)) {
1486 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1487 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001488
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001489 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1490 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001491 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001492 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001493 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001494 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
Winson Chung24ab2f12010-09-16 14:10:47 -07001498 void processWallpaper(Intent intent) {
1499 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1500 }
1501
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1503 resetAddInfo();
1504 mAddScreen = screen;
1505 mAddDropPosition = position;
1506
1507 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1508 createFolderIntent.setComponent(componentName);
1509
1510 addLiveFolder(createFolderIntent);
1511 }
1512
1513 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001514 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001515 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001516 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001518 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001520 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001521 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
Michael Jurka0280c3b2010-09-17 15:00:07 -07001525 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 UserFolderInfo folderInfo = new UserFolderInfo();
1527 folderInfo.title = getText(R.string.folder_name);
1528
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1530 final int[] cellXY = mTmpAddItemCellCoordinates;
1531 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1532 showOutOfSpaceMessage();
1533 return;
1534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 LauncherModel.addItemToDatabase(this, folderInfo,
1538 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001540 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541
1542 // Create the view
1543 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001544 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Romain Guycbb89e42009-06-08 15:52:54 -07001547
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001549 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 }
1551
Michael Jurka28750fb2010-09-24 17:43:49 -07001552 private void completeAddLiveFolder(
1553 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1555 final int[] cellXY = mTmpAddItemCellCoordinates;
1556 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1557 showOutOfSpaceMessage();
1558 return;
1559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562
1563 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001565 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568 }
1569
1570 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001571 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572
1573 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1574 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1575
1576 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 Intent.ShortcutIconResource iconResource = null;
1578
1579 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1580 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1581 try {
1582 iconResource = (Intent.ShortcutIconResource) extra;
1583 final PackageManager packageManager = context.getPackageManager();
1584 Resources resources = packageManager.getResourcesForApplication(
1585 iconResource.packageName);
1586 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1587 icon = resources.getDrawable(id);
1588 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001589 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591 }
1592
1593 if (icon == null) {
1594 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1595 }
1596
1597 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001598 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 info.title = name;
1600 info.iconResource = iconResource;
1601 info.uri = data.getData();
1602 info.baseIntent = baseIntent;
1603 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1604 LiveFolders.DISPLAY_MODE_GRID);
1605
1606 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001607 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001608 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
1610 return info;
1611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 private void showNotifications() {
1614 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1615 if (statusBar != null) {
1616 statusBar.expand();
1617 }
1618 }
1619
1620 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001621 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001623 Intent chooser = Intent.createChooser(pickWallpaper,
1624 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001625 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1626 // Removed in Eclair MR1
1627// WallpaperManager wm = (WallpaperManager)
1628// getSystemService(Context.WALLPAPER_SERVICE);
1629// WallpaperInfo wi = wm.getWallpaperInfo();
1630// if (wi != null && wi.getSettingsActivity() != null) {
1631// LabeledIntent li = new LabeledIntent(getPackageName(),
1632// R.string.configure_wallpaper, 0);
1633// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1634// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1635// }
Mike Clerona0618e42009-10-22 13:55:21 -07001636 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
1638
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001639 /**
1640 * Registers various content observers. The current implementation registers
1641 * only a favorites observer to keep track of the favorites applications.
1642 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001643 private void registerContentObservers() {
1644 ContentResolver resolver = getContentResolver();
1645 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1646 true, mWidgetObserver);
1647 }
1648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 @Override
1650 public boolean dispatchKeyEvent(KeyEvent event) {
1651 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1652 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001654 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001655 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001656 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001657 dumpState();
1658 return true;
1659 }
1660 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001661 }
1662 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1663 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001664 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 return true;
1666 }
1667 }
1668
1669 return super.dispatchKeyEvent(event);
1670 }
1671
Joe Onorato88ec0992009-11-19 13:16:06 -08001672 @Override
1673 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001674 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1675 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001676 } else {
1677 closeFolder();
1678 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 // Some launcher layouts don't have a previous and next view
1680 if (mPreviousView != null) {
1681 dismissPreview(mPreviousView);
1682 dismissPreview(mNextView);
1683 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001684 }
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 private void closeFolder() {
1687 Folder folder = mWorkspace.getOpenFolder();
1688 if (folder != null) {
1689 closeFolder(folder);
1690 }
1691 }
1692
1693 void closeFolder(Folder folder) {
1694 folder.getInfo().opened = false;
1695 ViewGroup parent = (ViewGroup) folder.getParent();
1696 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001697 CellLayout cl = (CellLayout) parent;
1698 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001699 if (folder instanceof DropTarget) {
1700 // Live folders aren't DropTargets.
1701 mDragController.removeDropTarget((DropTarget)folder);
1702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 }
1704 folder.onClose();
1705 }
1706
1707 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001708 * Re-listen when widgets are reset.
1709 */
1710 private void onAppWidgetReset() {
1711 mAppWidgetHost.startListening();
1712 }
1713
1714 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001715 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1716 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718 private void unbindDesktopItems() {
1719 for (ItemInfo item: mDesktopItems) {
1720 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001723
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 /**
1725 * Launches the intent referred by the clicked shortcut.
1726 *
1727 * @param v The view representing the clicked shortcut.
1728 */
1729 public void onClick(View v) {
1730 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001731 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001733 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001734 int[] pos = new int[2];
1735 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001736 intent.setSourceBounds(new Rect(pos[0], pos[1],
1737 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001738 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 } else if (tag instanceof FolderInfo) {
1740 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001741 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001742 if (mState == State.ALL_APPS) {
1743 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001744 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001745 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 }
1748 }
1749
Michael Jurka0e260592010-06-30 17:07:39 -07001750 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001751 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001752 // clicking anywhere on the workspace causes the customization drawer to slide down
1753 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001754 return false;
1755 }
1756
Michael Jurkaaf442092010-06-10 17:01:57 -07001757 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001758 * Event handler for the search button
1759 *
1760 * @param v The view that was clicked.
1761 */
1762 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001763 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001764 }
1765
1766 /**
1767 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001768 * enters home screen customization mode.
1769 *
1770 * @param v The view that was clicked.
1771 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001772 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001773 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001774 }
1775
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001776 /**
1777 * Event handler for the "grid" button that appears on the home screen, which
1778 * enters all apps mode.
1779 *
1780 * @param v The view that was clicked.
1781 */
1782 public void onClickAllAppsButton(View v) {
1783 showAllApps(true);
1784 }
1785
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001786 public void onClickAppMarketButton(View v) {
1787 if (mAppMarketIntent != null) {
1788 startActivitySafely(mAppMarketIntent, "app market");
1789 }
1790 }
1791
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001792 void startApplicationDetailsActivity(ComponentName componentName) {
1793 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001794 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1795 Uri.fromParts("package", packageName, null));
1796 startActivity(intent);
1797 }
1798
Patrick Dubroy5539af72010-09-07 15:22:01 -07001799 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1800 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1801 // System applications cannot be installed. For now, show a toast explaining that.
1802 // We may give them the option of disabling apps this way.
1803 int messageId = R.string.uninstall_system_app_text;
1804 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1805 } else {
1806 String packageName = appInfo.componentName.getPackageName();
1807 String className = appInfo.componentName.getClassName();
1808 Intent intent = new Intent(
1809 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1810 startActivity(intent);
1811 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001812 }
1813
Joe Onoratof984e852010-03-25 09:47:45 -07001814 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1816 try {
1817 startActivity(intent);
1818 } catch (ActivityNotFoundException e) {
1819 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001820 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 } catch (SecurityException e) {
1822 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001823 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001825 "or use the exported attribute for this activity. "
1826 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
1828 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001829
Romain Guy8e633c52010-03-04 12:51:36 -08001830 void startActivityForResultSafely(Intent intent, int requestCode) {
1831 try {
1832 startActivityForResult(intent, requestCode);
1833 } catch (ActivityNotFoundException e) {
1834 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1835 } catch (SecurityException e) {
1836 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1837 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1838 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1839 "or use the exported attribute for this activity.", e);
1840 }
1841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842
1843 private void handleFolderClick(FolderInfo folderInfo) {
1844 if (!folderInfo.opened) {
1845 // Close any open folder
1846 closeFolder();
1847 // Open the requested folder
1848 openFolder(folderInfo);
1849 } else {
1850 // Find the open folder...
1851 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1852 int folderScreen;
1853 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001854 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 // .. and close it
1856 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001857 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 // Close any folder open on the current screen
1859 closeFolder();
1860 // Pull the folder onto this screen
1861 openFolder(folderInfo);
1862 }
1863 }
1864 }
1865 }
1866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001868 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 * is animated relative to the specified View. If the View is null, no animation
1870 * is played.
1871 *
1872 * @param folderInfo The FolderInfo describing the folder to open.
1873 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001874 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 Folder openFolder;
1876
1877 if (folderInfo instanceof UserFolderInfo) {
1878 openFolder = UserFolder.fromXml(this);
1879 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001880 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 } else {
1882 return;
1883 }
1884
Joe Onorato00acb122009-08-04 16:04:30 -04001885 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 openFolder.setLauncher(this);
1887
1888 openFolder.bind(folderInfo);
1889 folderInfo.opened = true;
1890
Winson Chungaafa03c2010-06-11 17:34:16 -07001891 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1892
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 openFolder.onOpen();
1894 }
1895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001897 switch (v.getId()) {
1898 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001899 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001900 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1901 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001902 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001903 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001904 return true;
1905 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001906 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001907 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1908 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001909 showPreviews(v);
1910 }
1911 return true;
1912 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001913 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001914 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1915 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1916 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001917 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001918 return true;
1919 }
1920
Joe Onorato9c1289c2009-08-17 11:03:03 -04001921 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 return false;
1923 }
1924
1925 if (!(v instanceof CellLayout)) {
1926 v = (View) v.getParent();
1927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929
Michael Jurka0280c3b2010-09-17 15:00:07 -07001930 resetAddInfo();
1931 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001933 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 return true;
1935 }
1936
Michael Jurka0280c3b2010-09-17 15:00:07 -07001937 final View itemUnderLongClick = longClickCellInfo.cell;
1938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001940 if (itemUnderLongClick == null) {
1941 // User long pressed on empty space
1942 mWorkspace.setAllowLongPress(false);
1943 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1944 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001945 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001946 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001949 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001951 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1952 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001953 mAddIntersectCellX = longClickCellInfo.cellX;
1954 mAddIntersectCellY = longClickCellInfo.cellY;
1955 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
1957 }
1958 }
1959 return true;
1960 }
1961
Romain Guye6b8e2f2009-11-10 11:56:55 -08001962 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001963 private void dismissPreview(final View v) {
1964 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001965 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001966 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1967 public void onDismiss() {
1968 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1969 int count = group.getChildCount();
1970 for (int i = 0; i < count; i++) {
1971 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1972 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001973 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1974 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1975
1976 v.setTag(R.id.workspace, null);
1977 v.setTag(R.id.icon, null);
1978 window.setOnDismissListener(null);
1979 }
1980 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001981 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001982 }
1983 v.setTag(null);
1984 }
1985
Romain Guyf8e6a802009-12-07 17:48:02 -08001986 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001987 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001988 }
1989
Romain Guya6abce82009-11-10 02:54:41 -08001990 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001991 final Resources resources = getResources();
1992 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001993
Romain Guya6abce82009-11-10 02:54:41 -08001994 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001995
Romain Guy9d31e9f2009-11-11 18:54:28 -08001996 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001997
Romain Guyf8e6a802009-12-07 17:48:02 -08001998 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001999 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002000 int extraW = (int) ((r.left + r.right) * max);
2001 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002002
2003 int aW = cell.getWidth() - extraW;
2004 float w = aW / max;
2005
2006 int width = cell.getWidth();
2007 int height = cell.getHeight();
2008 int x = cell.getLeftPadding();
2009 int y = cell.getTopPadding();
2010 width -= (x + cell.getRightPadding());
2011 height -= (y + cell.getBottomPadding());
2012
2013 float scale = w / width;
2014
2015 int count = end - start;
2016
2017 final float sWidth = width * scale;
2018 float sHeight = height * scale;
2019
Romain Guye6b8e2f2009-11-10 11:56:55 -08002020 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002021
Romain Guye6b8e2f2009-11-10 11:56:55 -08002022 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2023 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002024
2025 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002026 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002027 cell = (CellLayout) workspace.getChildAt(i);
2028
Romain Guyf8e6a802009-12-07 17:48:02 -08002029 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002030 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002031
2032 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002033 c.scale(scale, scale);
2034 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002035 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002036
Romain Guy9d31e9f2009-11-11 18:54:28 -08002037 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002038 image.setImageBitmap(bitmap);
2039 image.setTag(i);
2040 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002041 image.setOnFocusChangeListener(handler);
2042 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002043 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002044
2045 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002046 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2047
Winson Chungaafa03c2010-06-11 17:34:16 -07002048 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002049 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002050
2051 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002052 p.setContentView(preview);
2053 p.setWidth((int) (sWidth * count + extraW));
2054 p.setHeight((int) (sHeight + extraH));
2055 p.setAnimationStyle(R.style.AnimationPreview);
2056 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002057 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002058 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002059 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002060
Romain Guye6b8e2f2009-11-10 11:56:55 -08002061 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2062 public void onDismiss() {
2063 dismissPreview(anchor);
2064 }
2065 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002066
2067 anchor.setTag(p);
2068 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002069 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002070 }
2071
Romain Guy9d31e9f2009-11-11 18:54:28 -08002072 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002073 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002074
Romain Guye6b8e2f2009-11-10 11:56:55 -08002075 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002076 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002077 }
2078
2079 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002080 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002081 v.post(this);
2082 }
2083
2084 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002085 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002086 }
2087
2088 public void onFocusChange(View v, boolean hasFocus) {
2089 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002090 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002091 }
Romain Guya6abce82009-11-10 02:54:41 -08002092 }
2093 }
2094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 Workspace getWorkspace() {
2096 return mWorkspace;
2097 }
2098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 @Override
2100 protected Dialog onCreateDialog(int id) {
2101 switch (id) {
2102 case DIALOG_CREATE_SHORTCUT:
2103 return new CreateShortcut().createDialog();
2104 case DIALOG_RENAME_FOLDER:
2105 return new RenameFolder().createDialog();
2106 }
2107
2108 return super.onCreateDialog(id);
2109 }
2110
2111 @Override
2112 protected void onPrepareDialog(int id, Dialog dialog) {
2113 switch (id) {
2114 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 break;
2116 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002117 if (mFolderInfo != null) {
2118 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2119 final CharSequence text = mFolderInfo.title;
2120 input.setText(text);
2121 input.setSelection(0, text.length());
2122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 break;
2124 }
2125 }
2126
2127 void showRenameDialog(FolderInfo info) {
2128 mFolderInfo = info;
2129 mWaitingForResult = true;
2130 showDialog(DIALOG_RENAME_FOLDER);
2131 }
2132
Michael Jurka0280c3b2010-09-17 15:00:07 -07002133 private void showAddDialog(int intersectX, int intersectY) {
2134 resetAddInfo();
2135 mAddIntersectCellX = intersectX;
2136 mAddIntersectCellY = intersectY;
2137 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 mWaitingForResult = true;
2139 showDialog(DIALOG_CREATE_SHORTCUT);
2140 }
2141
Joe Onoratodeb98af2010-02-19 14:59:39 -08002142 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002143 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002144 Bundle bundle = new Bundle();
2145
2146 ArrayList<String> shortcutNames = new ArrayList<String>();
2147 shortcutNames.add(getString(R.string.group_applications));
2148 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2149
2150 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2151 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2152 R.drawable.ic_launcher_application));
2153 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2154
2155 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2156 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002157 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002158 pickIntent.putExtras(bundle);
2159
Joe Onoratodeb98af2010-02-19 14:59:39 -08002160 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002161 }
2162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 private class RenameFolder {
2164 private EditText mInput;
2165
2166 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2168 mInput = (EditText) layout.findViewById(R.id.folder_name);
2169
2170 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2171 builder.setIcon(0);
2172 builder.setTitle(getString(R.string.rename_folder_title));
2173 builder.setCancelable(true);
2174 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2175 public void onCancel(DialogInterface dialog) {
2176 cleanup();
2177 }
2178 });
2179 builder.setNegativeButton(getString(R.string.cancel_action),
2180 new Dialog.OnClickListener() {
2181 public void onClick(DialogInterface dialog, int which) {
2182 cleanup();
2183 }
2184 }
2185 );
2186 builder.setPositiveButton(getString(R.string.rename_action),
2187 new Dialog.OnClickListener() {
2188 public void onClick(DialogInterface dialog, int which) {
2189 changeFolderName();
2190 }
2191 }
2192 );
2193 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002194
2195 final AlertDialog dialog = builder.create();
2196 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2197 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002198 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002199 mInput.requestFocus();
2200 InputMethodManager inputManager = (InputMethodManager)
2201 getSystemService(Context.INPUT_METHOD_SERVICE);
2202 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002203 }
2204 });
2205
2206 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
2208
2209 private void changeFolderName() {
2210 final String name = mInput.getText().toString();
2211 if (!TextUtils.isEmpty(name)) {
2212 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002213 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 mFolderInfo.title = name;
2215 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2216
Joe Onorato9c1289c2009-08-17 11:03:03 -04002217 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002218 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002219 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 } else {
2221 final FolderIcon folderIcon = (FolderIcon)
2222 mWorkspace.getViewForTag(mFolderInfo);
2223 if (folderIcon != null) {
2224 folderIcon.setText(name);
2225 getWorkspace().requestLayout();
2226 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002227 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228 mWorkspaceLoading = true;
2229 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 }
2231 }
2232 }
2233 cleanup();
2234 }
2235
2236 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 dismissDialog(DIALOG_RENAME_FOLDER);
2238 mWaitingForResult = false;
2239 mFolderInfo = null;
2240 }
2241 }
2242
Daniel Sandler843e8602010-06-07 14:59:01 -04002243 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2244 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002245 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002246 }
2247
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002248 // AllAppsView.Watcher
2249 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002250 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2251 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002252 mWorkspace.setVisibility(View.GONE);
2253 }
2254 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002255
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002256 private void showToolbarButton(View button) {
2257 button.setAlpha(1.0f);
2258 button.setVisibility(View.VISIBLE);
2259 button.setFocusable(true);
2260 button.setClickable(true);
2261 }
2262
2263 private void hideToolbarButton(View button) {
2264 button.setAlpha(0.0f);
2265 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2266 button.setVisibility(View.INVISIBLE);
2267 button.setFocusable(false);
2268 button.setClickable(false);
2269 }
2270
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002271 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002272 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273 *
2274 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2275 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002276 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002277 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002278 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002279 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002280 final boolean showing = show;
2281 final boolean hiding = !show;
2282
2283 final int duration = show ?
2284 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2285 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2286
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002287 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002288 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2289 anim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -07002290 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002291 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002292 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002293 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002294 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002295 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002296 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002297 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002298 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002299 });
2300 seq.play(anim);
2301 } else {
2302 if (showing) {
2303 showToolbarButton(view);
2304 } else {
2305 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002308 }
2309
2310 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002311 * Show/hide the appropriate toolbar buttons for newState.
2312 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313 *
2314 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002315 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2316 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002317 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002318 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002319 final View searchButton = findViewById(R.id.search_button);
2320 final View allAppsButton = findViewById(R.id.all_apps_button);
2321 final View marketButton = findViewById(R.id.market_button);
2322 final View configureButton = findViewById(R.id.configure_button);
2323
2324 switch (newState) {
2325 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002326 hideOrShowToolbarButton(true, searchButton, showSeq);
2327 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2328 hideOrShowToolbarButton(true, configureButton, showSeq);
2329 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002330 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002331 break;
2332 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002333 hideOrShowToolbarButton(true, configureButton, showSeq);
2334 hideOrShowToolbarButton(true, marketButton, showSeq);
2335 hideOrShowToolbarButton(false, searchButton, hideSeq);
2336 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002337 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002338 break;
2339 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002340 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2341 hideOrShowToolbarButton(false, searchButton, hideSeq);
2342 hideOrShowToolbarButton(false, marketButton, hideSeq);
2343 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002344 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002345 break;
2346 }
2347 }
2348
2349 /**
2350 * Helper method for the cameraZoomIn/cameraZoomOut animations
2351 * @param view The view being animated
2352 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2353 * @param scaleFactor The scale factor used for the zoom
2354 */
2355 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2356 final int height = view.getHeight();
2357 view.setPivotX(view.getWidth() / 2.0f);
2358 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2359 // Assumes that the view is normally anchored to either the top or bottom of the screen
2360 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2361 if (state == State.ALL_APPS) {
2362 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2363 } else {
2364 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2365 }
2366 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002367
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002368 /**
2369 * Zoom the camera out from the workspace to reveal 'toView'.
2370 * Assumes that the view to show is anchored at either the very top or very bottom
2371 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002372 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002373 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002374 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002375 final Resources res = getResources();
2376 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2377 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002378 final boolean toAllApps = (toState == State.ALL_APPS);
2379 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002380
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002381 setPivotsForZoom(toView, toState, scale);
2382
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002383 if (toState == State.ALL_APPS) {
2384 mWorkspace.shrinkToBottom(animated);
2385 } else {
2386 mWorkspace.shrinkToTop(animated);
2387 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002388
2389 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002390 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2391 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2392 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2393 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002394 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002395 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002396 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002397 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002398 // Prepare the position
2399 toView.setTranslationX(0.0f);
2400 toView.setTranslationY(0.0f);
2401 toView.setVisibility(View.VISIBLE);
2402 }
2403 });
2404
Chet Haaseb1254a62010-09-07 13:35:00 -07002405 AnimatorSet toolbarHideAnim = new AnimatorSet();
2406 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002407 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2408
2409 // toView should appear right at the end of the workspace shrink animation
2410 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2411
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002412 if (mStateAnimation != null) mStateAnimation.cancel();
2413 mStateAnimation = new AnimatorSet();
2414 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2415 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002416
2417 // Show the new toolbar buttons just as the main animation is ending
2418 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002419 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2420 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002421 } else {
2422 toView.setTranslationX(0.0f);
2423 toView.setTranslationY(0.0f);
2424 toView.setScaleX(1.0f);
2425 toView.setScaleY(1.0f);
2426 toView.setVisibility(View.VISIBLE);
2427 hideAndShowToolbarButtons(toState, null, null);
2428 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002429 }
2430
2431 /**
2432 * Zoom the camera back into the workspace, hiding 'fromView'.
2433 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002434 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2435 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002436 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002437 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002438 Resources res = getResources();
2439 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2440 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002441 final View fromView =
2442 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2443
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002444 mCustomizePagedView.endChoiceMode();
2445 mAllAppsPagedView.endChoiceMode();
2446
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002447 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002448
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002449 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002450
2451 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002452 if (mStateAnimation != null) mStateAnimation.cancel();
2453 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002454 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2455 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2456 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2457 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002458 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002459 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002460 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002461 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002462 fromView.setVisibility(View.GONE);
2463 fromView.setScaleX(1.0f);
2464 fromView.setScaleY(1.0f);
2465 }
2466 });
2467
Chet Haaseb1254a62010-09-07 13:35:00 -07002468 AnimatorSet toolbarHideAnim = new AnimatorSet();
2469 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002470 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2471
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002472 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473
2474 // Show the new toolbar buttons at the very end of the whole animation
2475 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2476 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002477 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2478 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002479 } else {
2480 fromView.setVisibility(View.GONE);
2481 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2482 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002483 }
2484
2485 /**
2486 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2487 * This is the transition used on xlarge screens to go between all apps and
2488 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002489 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2490 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2491 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002492 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002494 final Resources res = getResources();
2495 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002496 final int workspaceHeight = mWorkspace.getHeight();
2497
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002498 final boolean fromAllApps = (fromState == State.ALL_APPS);
2499 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2500 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002501
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002502 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2503 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2504 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2505 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002506
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002507 mCustomizePagedView.endChoiceMode();
2508 mAllAppsPagedView.endChoiceMode();
2509
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 if (toState == State.ALL_APPS) {
2511 mWorkspace.shrinkToBottom(animated);
2512 } else {
2513 mWorkspace.shrinkToTop(animated);
2514 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515
2516 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002517 if (mStateAnimation != null) mStateAnimation.cancel();
2518 mStateAnimation = new AnimatorSet();
2519 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002520 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002521 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002522 toView.setVisibility(View.VISIBLE);
2523 toView.setY(toViewStartY);
2524 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002525 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002526 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002527 fromView.setVisibility(View.GONE);
2528 }
2529 });
2530
Chet Haaseb1254a62010-09-07 13:35:00 -07002531 AnimatorSet toolbarHideAnim = new AnimatorSet();
2532 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002533 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2534
Chet Haase472b2812010-10-14 07:02:04 -07002535 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2536 fromViewStartY, fromViewEndY);
2537 fromAnim.setDuration(duration);
2538 ObjectAnimator toAnim = ObjectAnimator.ofFloat(toView, "y",
2539 toViewStartY, toViewEndY);
2540 fromAnim.setDuration(duration);
2541 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542
2543 // Show the new toolbar buttons just as the main animation is ending
2544 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002545 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2546 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 } else {
2548 fromView.setY(fromViewEndY);
2549 fromView.setVisibility(View.GONE);
2550 toView.setY(toViewEndY);
2551 toView.setVisibility(View.VISIBLE);
2552 hideAndShowToolbarButtons(toState, null, null);
2553 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002554 }
2555
2556 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002557 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002558 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002559 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002560
Michael Jurka79212d82010-07-30 16:36:20 -07002561 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002562 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002564 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002566 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002567 } else {
2568 mAllAppsGrid.zoom(1.0f, animated);
2569 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002570
Romain Guyc16fea72010-03-12 17:17:56 -08002571 ((View) mAllAppsGrid).setFocusable(true);
2572 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002573
Joe Onorato7c312c12009-08-13 21:36:53 -07002574 // TODO: fade these two too
2575 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002576 // Change the state *after* we've called all the transition code
2577 mState = State.ALL_APPS;
2578 }
2579
2580
2581 void showWorkspace(boolean animated) {
2582 showWorkspace(animated, null);
2583 }
2584
2585 void showWorkspace(boolean animated, CellLayout layout) {
2586 if (layout != null && animated) {
2587 mWorkspace.unshrink(layout);
2588 } else {
2589 mWorkspace.unshrink(animated);
2590 }
2591 if (mState == State.ALL_APPS) {
2592 closeAllApps(animated);
2593 } else if (mState == State.CUSTOMIZE) {
2594 hideCustomizationDrawer(animated);
2595 }
2596 // Change the state *after* we've called all the transition code
2597 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002598 }
2599
Joe Onoratob2061212009-11-24 16:13:54 -05002600 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002601 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002602 * - Home from workspace
2603 * - from center screen
2604 * - from other screens
2605 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002606 * - from center screen
2607 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002608 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002609 * - from center screen
2610 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002611 * - Launch app from workspace and quit
2612 * - with back
2613 * - with home
2614 * - Launch app from all apps and quit
2615 * - with back
2616 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002617 * - Go to a screen that's not the default, then all
2618 * apps, and launch and app, and go back
2619 * - with back
2620 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002621 * - On workspace, long press power and go back
2622 * - with back
2623 * - with home
2624 * - On all apps, long press power and go back
2625 * - with back
2626 * - with home
2627 * - On workspace, power off
2628 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002629 * - Launch an app and turn off the screen while in that app
2630 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002631 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002632 * - From all apps
2633 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002634 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2635 * - From all apps
2636 * - From the center workspace
2637 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002638 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002639 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002640 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002641 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002644 } else {
2645 mAllAppsGrid.zoom(0.0f, animated);
2646 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002647 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002648 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002649 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002650 }
2651
Joe Onorato7c312c12009-08-13 21:36:53 -07002652 void lockAllApps() {
2653 // TODO
2654 }
2655
2656 void unlockAllApps() {
2657 // TODO
2658 }
2659
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 // Show the customization drawer (only exists in x-large configuration)
2661 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002662 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002664 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002665 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002666 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002667 // Change the state *after* we've called all the transition code
2668 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002669 }
2670
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 // Hide the customization drawer (only exists in x-large configuration)
2672 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002673 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002675 }
2676 }
2677
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002678 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2679 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2680 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002681 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002682 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002683
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002684 void onWorkspaceClick(CellLayout layout) {
2685 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002686 }
2687
Michael Jurka0423dcf2010-10-05 14:56:18 -07002688 private void updateButtonWithIconFromExternalActivity(
2689 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2690 ImageView button = (ImageView) findViewById(buttonId);
2691 Drawable toolbarIcon = null;
2692 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002693 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002694 // Look for the toolbar icon specified in the activity meta-data
2695 Bundle metaData = packageManager.getActivityInfo(
2696 activityName, PackageManager.GET_META_DATA).metaData;
2697 if (metaData != null) {
2698 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2699 if (iconResId != 0) {
2700 Resources res = packageManager.getResourcesForActivity(activityName);
2701 toolbarIcon = res.getDrawable(iconResId);
2702 }
2703 }
2704 } catch (NameNotFoundException e) {
2705 // Do nothing
2706 }
2707 // If we were unable to find the icon via the meta-data, use a generic one
2708 if (toolbarIcon == null) {
2709 button.setImageResource(fallbackDrawableId);
2710 } else {
2711 button.setImageDrawable(toolbarIcon);
2712 }
2713 }
2714
2715 private void updateGlobalSearchIcon() {
2716 if (LauncherApplication.isScreenXLarge()) {
2717 final SearchManager searchManager =
2718 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2719 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002720 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002721 updateButtonWithIconFromExternalActivity(
2722 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002723 }
2724 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002725 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002726
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002727 /**
2728 * Sets the app market icon (shown when all apps is visible on x-large screens)
2729 */
2730 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002731 if (LauncherApplication.isScreenXLarge()) {
2732 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2733 // Find the app market activity by resolving an intent.
2734 // (If multiple app markets are installed, it will return the ResolverActivity.)
2735 ComponentName activityName = intent.resolveActivity(getPackageManager());
2736 if (activityName != null) {
2737 mAppMarketIntent = intent;
2738 updateButtonWithIconFromExternalActivity(
2739 R.id.market_button, activityName, R.drawable.app_market_generic);
2740 }
2741 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002742 }
2743
2744 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 * Displays the shortcut creation dialog and launches, if necessary, the
2746 * appropriate activity.
2747 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002748 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002749 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2750 DialogInterface.OnShowListener {
2751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002755 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2758 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002759 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002760
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002761 builder.setInverseBackgroundForced(true);
2762
2763 AlertDialog dialog = builder.create();
2764 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002765 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002766 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002768 return dialog;
2769 }
2770
2771 public void onCancel(DialogInterface dialog) {
2772 mWaitingForResult = false;
2773 cleanup();
2774 }
2775
Romain Guycbb89e42009-06-08 15:52:54 -07002776 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002777 }
2778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002780 try {
2781 dismissDialog(DIALOG_CREATE_SHORTCUT);
2782 } catch (Exception e) {
2783 // An exception is thrown if the dialog is not visible, which is fine
2784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 }
2786
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002787 /**
2788 * Handle the action clicked in the "Add to home" dialog.
2789 */
2790 public void onClick(DialogInterface dialog, int which) {
2791 Resources res = getResources();
2792 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002793
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002794 switch (which) {
2795 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002796 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002797 break;
2798 }
Romain Guycbb89e42009-06-08 15:52:54 -07002799
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002800 case AddAdapter.ITEM_APPWIDGET: {
2801 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002802
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002803 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2804 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002805 // start the pick activity
2806 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2807 break;
2808 }
Romain Guycbb89e42009-06-08 15:52:54 -07002809
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002810 case AddAdapter.ITEM_LIVE_FOLDER: {
2811 // Insert extra item to handle inserting folder
2812 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002813
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002814 ArrayList<String> shortcutNames = new ArrayList<String>();
2815 shortcutNames.add(res.getString(R.string.group_folder));
2816 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002817
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002818 ArrayList<ShortcutIconResource> shortcutIcons =
2819 new ArrayList<ShortcutIconResource>();
2820 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2821 R.drawable.ic_launcher_folder));
2822 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2823
2824 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2825 pickIntent.putExtra(Intent.EXTRA_INTENT,
2826 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2827 pickIntent.putExtra(Intent.EXTRA_TITLE,
2828 getText(R.string.title_select_live_folder));
2829 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002830
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002831 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2832 break;
2833 }
2834
2835 case AddAdapter.ITEM_WALLPAPER: {
2836 startWallpaper();
2837 break;
2838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002839 }
2840 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002841
2842 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002843 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
2846
2847 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002848 * Receives notifications when applications are added/removed.
2849 */
2850 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2851 @Override
2852 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002853 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002854 String reason = intent.getStringExtra("reason");
2855 if (!"homekey".equals(reason)) {
2856 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002857 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002858 animate = false;
2859 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002860 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002861 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002862 }
2863 }
2864
2865 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002866 * Receives notifications whenever the appwidgets are reset.
2867 */
2868 private class AppWidgetResetObserver extends ContentObserver {
2869 public AppWidgetResetObserver() {
2870 super(new Handler());
2871 }
2872
2873 @Override
2874 public void onChange(boolean selfChange) {
2875 onAppWidgetReset();
2876 }
2877 }
2878
2879 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002883 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002884 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002885 } else {
2886 return SCREEN_COUNT / 2;
2887 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002889
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002890 void setAllAppsPagedView(PagedView view) {
2891 mAllAppsPagedView = view;
2892 }
2893
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 /**
2895 * Refreshes the shortcuts shown on the workspace.
2896 *
2897 * Implementation of the method from LauncherModel.Callbacks.
2898 */
2899 public void startBinding() {
2900 final Workspace workspace = mWorkspace;
2901 int count = workspace.getChildCount();
2902 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002903 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2905 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002906
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 if (DEBUG_USER_INTERFACE) {
2908 android.widget.Button finishButton = new android.widget.Button(this);
2909 finishButton.setText("Finish");
2910 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2911
2912 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2913 public void onClick(View v) {
2914 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002915 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 }
2918 }
2919
2920 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 * Bind the items start-end from the list.
2922 *
2923 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2926
2927 final Workspace workspace = mWorkspace;
2928
2929 for (int i=start; i<end; i++) {
2930 final ItemInfo item = shortcuts.get(i);
2931 mDesktopItems.add(item);
2932 switch (item.itemType) {
2933 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2934 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002935 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002936 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2937 false);
2938 break;
2939 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2940 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002941 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002942 (UserFolderInfo) item);
2943 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2944 false);
2945 break;
2946 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2947 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2948 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002949 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 (LiveFolderInfo) item);
2951 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2952 false);
2953 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 }
2956
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 }
2959
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960 /**
2961 * Implementation of the method from LauncherModel.Callbacks.
2962 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002963 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002964 sFolders.clear();
2965 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967
2968 /**
2969 * Add the views for a widget to the workspace.
2970 *
2971 * Implementation of the method from LauncherModel.Callbacks.
2972 */
2973 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002974 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2975 if (DEBUG_WIDGETS) {
2976 Log.d(TAG, "bindAppWidget: " + item);
2977 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 final Workspace workspace = mWorkspace;
2979
2980 final int appWidgetId = item.appWidgetId;
2981 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002982 if (DEBUG_WIDGETS) {
2983 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2984 }
2985
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2987
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2989 item.hostView.setTag(item);
2990
2991 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2992 item.cellY, item.spanX, item.spanY, false);
2993
2994 workspace.requestLayout();
2995
2996 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002997
2998 if (DEBUG_WIDGETS) {
2999 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3000 + (SystemClock.uptimeMillis()-start) + "ms");
3001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 }
3003
3004 /**
3005 * Callback saying that there aren't any more items to bind.
3006 *
3007 * Implementation of the method from LauncherModel.Callbacks.
3008 */
3009 public void finishBindingItems() {
3010 if (mSavedState != null) {
3011 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003012 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 }
3014
3015 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3016 if (userFolders != null) {
3017 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003018 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 if (info != null) {
3020 openFolder(info);
3021 }
3022 }
3023 final Folder openFolder = mWorkspace.getOpenFolder();
3024 if (openFolder != null) {
3025 openFolder.requestFocus();
3026 }
3027 }
3028
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 mSavedState = null;
3030 }
3031
3032 if (mSavedInstanceState != null) {
3033 super.onRestoreInstanceState(mSavedInstanceState);
3034 mSavedInstanceState = null;
3035 }
3036
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 mWorkspaceLoading = false;
3038 }
3039
3040 /**
3041 * Add the icons for all apps.
3042 *
3043 * Implementation of the method from LauncherModel.Callbacks.
3044 */
3045 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003046 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003047 if (mCustomizePagedView != null) {
3048 mCustomizePagedView.setApps(apps);
3049 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003050 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003051 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 }
3053
3054 /**
3055 * A package was installed.
3056 *
3057 * Implementation of the method from LauncherModel.Callbacks.
3058 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003059 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003061 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003062 if (mCustomizePagedView != null) {
3063 mCustomizePagedView.addApps(apps);
3064 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003065 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003066 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 }
3068
3069 /**
3070 * A package was updated.
3071 *
3072 * Implementation of the method from LauncherModel.Callbacks.
3073 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003074 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003076 mWorkspace.updateShortcuts(apps);
3077 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003078 if (mCustomizePagedView != null) {
3079 mCustomizePagedView.updateApps(apps);
3080 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003081 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003082 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 }
3084
3085 /**
3086 * A package was uninstalled.
3087 *
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
Joe Onorato36115782010-06-17 13:28:48 -04003090 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003092 if (permanent) {
3093 mWorkspace.removeItems(apps);
3094 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003095 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003096 if (mCustomizePagedView != null) {
3097 mCustomizePagedView.removeApps(apps);
3098 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003099 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003100 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 }
Joe Onoratobe386092009-11-17 17:32:16 -08003102
3103 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003104 * A number of packages were updated.
3105 */
3106 public void bindPackagesUpdated() {
3107 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003108 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003109 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003110 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003111 }
3112
3113 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003114 * Prints out out state for debugging.
3115 */
3116 public void dumpState() {
3117 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003118 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003119 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3120 Log.d(TAG, "mRestoring=" + mRestoring);
3121 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3122 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3123 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003124 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003125 mModel.dumpState();
3126 mAllAppsGrid.dumpState();
3127 Log.d(TAG, "END launcher2 dump state");
3128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129}