blob: 549303d034972bc147543755800451e7ad3dc6d8 [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
Winson Chung68846fd2010-10-29 11:00:27 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070030import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070031import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070033import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.Dialog;
36import android.app.SearchManager;
37import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070038import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070043import android.content.ClipData;
44import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040045import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070050import android.content.Intent.ShortcutIconResource;
Winson Chung68846fd2010-10-29 11:00:27 -070051import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070054import android.content.pm.PackageManager.NameNotFoundException;
Winson Chung68846fd2010-10-29 11:00:27 -070055import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040058import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080061import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080063import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040065import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020068import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080069import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040071import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080072import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070073import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070074import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
Winson Chung68846fd2010-10-29 11:00:27 -070088import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070091import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070095import android.widget.ImageView;
96import android.widget.LinearLayout;
97import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070098import android.widget.TabHost;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070099import android.widget.TabHost.OnTabChangeListener;
100import android.widget.TabHost.TabContentFactory;
Winson Chung68846fd2010-10-29 11:00:27 -0700101import android.widget.TextView;
102import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700103
Winson Chung68846fd2010-10-29 11:00:27 -0700104import com.android.common.Search;
105import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -0800106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800121 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700124 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
131 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_PICK_APPLICATION = 6;
134 private static final int REQUEST_PICK_SHORTCUT = 7;
135 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato7c312c12009-08-13 21:36:53 -0700144 static final int DIALOG_CREATE_SHORTCUT = 1;
145 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700151 // Type: int
152 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: long
154 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: boolean
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
163 // Type: long
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
165
Winson Chung80baf5a2010-08-09 16:03:15 -0700166 // tags for the customization tabs
167 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700168 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 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;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700221 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222
223 private Bundle mSavedInstanceState;
224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800226 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700228 private static LocaleConfiguration sLocaleConfiguration = null;
229
Romain Guy84f296c2009-11-04 15:00:44 -0800230 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700231 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Romain Guy1fbc1c82009-11-09 20:43:08 -0800233 private ImageView mPreviousView;
234 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500235
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400236 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400237 private String[] mHotseatConfig = null;
238 private Intent[] mHotseats = null;
239 private Drawable[] mHotseatIcons = null;
240 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400241
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700242 private Intent mAppMarketIntent = null;
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 @Override
245 protected void onCreate(Bundle savedInstanceState) {
246 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700247
Winson Chungaafa03c2010-06-11 17:34:16 -0700248 if (LauncherApplication.isInPlaceRotationEnabled()) {
249 // hide the status bar (temporary until we get the status bar design figured out)
250 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
251 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
252 }
253
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 LauncherApplication app = ((LauncherApplication)getApplication());
255 mModel = app.setLauncher(this);
256 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400257 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
262 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200265 android.os.Debug.startMethodTracing(
266 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 }
268
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400269 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 checkForLocaleChange();
271 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700273 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
274 if (mHomeCustomizationDrawer != null) {
275 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700276
Winson Chung80baf5a2010-08-09 16:03:15 -0700277 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700278 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
279 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700280 TabContentFactory contentFactory = new TabContentFactory() {
281 public View createTabContent(String tag) {
282 return mCustomizePagedView;
283 }
284 };
285
Michael Jurka946ad472010-07-09 18:05:18 -0700286 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700287 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
288 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700289 String applicationsLabel = getString(R.string.applications_tab_label);
290 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
291 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700292 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
294 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700295 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
296 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
297 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700298
Winson Chung80baf5a2010-08-09 16:03:15 -0700299 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
300 public void onTabChanged(String tabId) {
301 // animate the changing of the tab content by fading pages in and out
302 final int duration = 150;
303 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700304 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700306 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700307 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700308 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700309 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700310 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
311 if (tag == WIDGETS_TAG) {
312 mCustomizePagedView.setCustomizationFilter(
313 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700314 } else if (tag == APPLICATIONS_TAG) {
315 mCustomizePagedView.setCustomizationFilter(
316 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 } else if (tag == WALLPAPERS_TAG) {
318 mCustomizePagedView.setCustomizationFilter(
319 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700320 } else if (tag == SHORTCUTS_TAG) {
321 mCustomizePagedView.setCustomizationFilter(
322 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 }
324
325 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700326 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700327 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700328 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700329 alphaAnim.start();
330 }
331 });
332 alphaAnim.start();
333 }
334 });
Michael Jurka946ad472010-07-09 18:05:18 -0700335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 setupViews();
337
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800338 registerContentObservers();
339
Joe Onorato7c312c12009-08-13 21:36:53 -0700340 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 mSavedState = savedInstanceState;
343 restoreState(mSavedState);
344
345 if (PROFILE_STARTUP) {
346 android.os.Debug.stopMethodTracing();
347 }
348
349 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400350 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
353 // For handling default keys
354 mDefaultKeySsb = new SpannableStringBuilder();
355 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800356
357 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
358 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700362 if (sLocaleConfiguration == null) {
363 new AsyncTask<Void, Void, LocaleConfiguration>() {
364 @Override
365 protected LocaleConfiguration doInBackground(Void... unused) {
366 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
367 readConfiguration(Launcher.this, localeConfiguration);
368 return localeConfiguration;
369 }
370
371 @Override
372 protected void onPostExecute(LocaleConfiguration result) {
373 sLocaleConfiguration = result;
374 checkForLocaleChange(); // recursive, but now with a locale configuration
375 }
376 }.execute();
377 return;
378 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 final Configuration configuration = getResources().getConfiguration();
381
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 final String locale = configuration.locale.toString();
384
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700385 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 final int mcc = configuration.mcc;
387
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700388 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 final int mnc = configuration.mnc;
390
Romain Guy84f296c2009-11-04 15:00:44 -0800391 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392
Romain Guy84f296c2009-11-04 15:00:44 -0800393 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394 sLocaleConfiguration.locale = locale;
395 sLocaleConfiguration.mcc = mcc;
396 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700397
Joe Onorato0589f0f2010-02-08 13:44:00 -0800398 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400399 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400
401 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
402 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700403 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404 public void run() {
405 writeConfiguration(Launcher.this, localeConfiguration);
406 }
407 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700408 }
409 }
410
411 private static class LocaleConfiguration {
412 public String locale;
413 public int mcc = -1;
414 public int mnc = -1;
415 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700416
Romain Guy98d01652009-06-30 16:21:04 -0700417 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
418 DataInputStream in = null;
419 try {
420 in = new DataInputStream(context.openFileInput(PREFERENCES));
421 configuration.locale = in.readUTF();
422 configuration.mcc = in.readInt();
423 configuration.mnc = in.readInt();
424 } catch (FileNotFoundException e) {
425 // Ignore
426 } catch (IOException e) {
427 // Ignore
428 } finally {
429 if (in != null) {
430 try {
431 in.close();
432 } catch (IOException e) {
433 // Ignore
434 }
435 }
436 }
437 }
438
439 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
440 DataOutputStream out = null;
441 try {
442 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
443 out.writeUTF(configuration.locale);
444 out.writeInt(configuration.mcc);
445 out.writeInt(configuration.mnc);
446 out.flush();
447 } catch (FileNotFoundException e) {
448 // Ignore
449 } catch (IOException e) {
450 //noinspection ResultOfMethodCallIgnored
451 context.getFileStreamPath(PREFERENCES).delete();
452 } finally {
453 if (out != null) {
454 try {
455 out.close();
456 } catch (IOException e) {
457 // Ignore
458 }
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461 }
462
463 static int getScreen() {
464 synchronized (sLock) {
465 return sScreen;
466 }
467 }
468
469 static void setScreen(int screen) {
470 synchronized (sLock) {
471 sScreen = screen;
472 }
473 }
474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700476 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477
478 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700479 // TODO: Put back when we decide about scrolling the wallpaper
480 // boolean isPortrait = display.getWidth() < display.getHeight();
481 // final int width = isPortrait ? display.getWidth() : display.getHeight();
482 // final int height = isPortrait ? display.getHeight() : display.getWidth();
483 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
484 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 }
486
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400487 // Note: This doesn't do all the client-id magic that BrowserProvider does
488 // in Browser. (http://b/2425179)
489 private Uri getDefaultBrowserUri() {
490 String url = getString(R.string.default_browser_url);
491 if (url.indexOf("{CID}") != -1) {
492 url = url.replace("{CID}", "android-google");
493 }
494 return Uri.parse(url);
495 }
496
497 // Load the Intent templates from arrays.xml to populate the hotseats. For
498 // each Intent, if it resolves to a single app, use that as the launch
499 // intent & use that app's label as the contentDescription. Otherwise,
500 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400501 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400502 if (mHotseatConfig == null) {
503 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
504 if (mHotseatConfig.length > 0) {
505 mHotseats = new Intent[mHotseatConfig.length];
506 mHotseatLabels = new CharSequence[mHotseatConfig.length];
507 mHotseatIcons = new Drawable[mHotseatConfig.length];
508 } else {
509 mHotseats = null;
510 mHotseatIcons = null;
511 mHotseatLabels = null;
512 }
513
514 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
515 for (int i=0; i<mHotseatConfig.length; i++) {
516 // load icon for this slot; currently unrelated to the actual activity
517 try {
518 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
519 } catch (ArrayIndexOutOfBoundsException ex) {
520 Log.w(TAG, "Missing hotseat_icons array item #" + i);
521 mHotseatIcons[i] = null;
522 }
523 }
524 hotseatIconDrawables.recycle();
525 }
526
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400527 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400528 for (int i=0; i<mHotseatConfig.length; i++) {
529 Intent intent = null;
530 if (mHotseatConfig[i].equals("*BROWSER*")) {
531 // magic value meaning "launch user's default web browser"
532 // replace it with a generic web request so we can see if there is indeed a default
533 String defaultUri = getString(R.string.default_browser_url);
534 intent = new Intent(
535 Intent.ACTION_VIEW,
536 ((defaultUri != null)
537 ? Uri.parse(defaultUri)
538 : getDefaultBrowserUri())
539 ).addCategory(Intent.CATEGORY_BROWSABLE);
540 // note: if the user launches this without a default set, she
541 // will always be taken to the default URL above; this is
542 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700543 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 // URL is unavoidably sent to the chosen app.
545 } else {
546 try {
547 intent = Intent.parseUri(mHotseatConfig[i], 0);
548 } catch (java.net.URISyntaxException ex) {
549 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
550 // bogus; leave intent=null
551 }
552 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700553
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400554 if (intent == null) {
555 mHotseats[i] = null;
556 mHotseatLabels[i] = getText(R.string.activity_not_found);
557 continue;
558 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400559
560 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 Log.d(TAG, "loadHotseats: hotseat " + i
562 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400564 + "]");
565 }
566
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
568 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400570 Log.d(TAG, "Best match for intent: " + bestMatch);
571 Log.d(TAG, "All matches: ");
572 for (ResolveInfo ri : allMatches) {
573 Log.d(TAG, " --> " + ri);
574 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400575 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 // did this resolve to a single app, or the resolver?
577 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700578 // can't find any activity to handle this. let's leave the
579 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400580 // to launch.
581 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400582
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400583 // set accessibility text to "Not installed"
584 mHotseatLabels[i] = getText(R.string.activity_not_found);
585 } else {
586 boolean found = false;
587 for (ResolveInfo ri : allMatches) {
588 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
589 && bestMatch.activityInfo.applicationInfo.packageName
590 .equals(ri.activityInfo.applicationInfo.packageName)) {
591 found = true;
592 break;
593 }
594 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700595
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400596 if (!found) {
597 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
598 // the bestMatch is probably the ResolveActivity, meaning the
599 // user has not yet selected a default
600 // so: we'll keep the original intent for now
601 mHotseats[i] = intent;
602
603 // set the accessibility text to "Select shortcut"
604 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
605 } else {
606 // we have an app!
607 // now reconstruct the intent to launch it through the front
608 // door
609 ComponentName com = new ComponentName(
610 bestMatch.activityInfo.applicationInfo.packageName,
611 bestMatch.activityInfo.name);
612 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
613
614 // load the app label for accessibility
615 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
616 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400617 }
618
619 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700620 Log.d(TAG, "loadHotseats: hotseat " + i
621 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400622 + ((mHotseats[i] == null)
623 ? "null"
624 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625 + "] label=[" + mHotseatLabels[i]
626 + "]"
627 );
628 }
629 }
630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 @Override
633 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700634 mWaitingForResult = false;
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 // The pattern used here is that a user PICKs a specific application,
637 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
640 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700641
Michael Jurka0280c3b2010-09-17 15:00:07 -0700642 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 switch (requestCode) {
644 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700645 completeAddApplication(
646 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 break;
648 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800649 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 break;
651 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 break;
654 case REQUEST_PICK_LIVE_FOLDER:
655 addLiveFolder(data);
656 break;
657 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700658 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700660 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700661 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700663 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700664 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700667 case REQUEST_PICK_WALLPAPER:
668 // We just wanted the activity result here so we can clear mWaitingForResult
669 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Romain Guy18042c82009-11-06 11:44:55 -0800671 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
672 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
673 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 // Clean up the appWidgetId if we canceled
675 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
676 if (appWidgetId != -1) {
677 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681
682 @Override
683 protected void onResume() {
684 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800685 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700686 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 mWorkspaceLoading = true;
688 mModel.startLoader(this, true);
689 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700690 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700692 // When we resume Launcher, a different Activity might be responsible for the app
693 // market intent, so refresh the icon
694 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
696
697 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698 protected void onPause() {
699 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700700 // Some launcher layouts don't have a previous and next view
701 if (mPreviousView != null) {
702 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700703 }
704 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700705 dismissPreview(mNextView);
706 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700707 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800708 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 }
Romain Guycbb89e42009-06-08 15:52:54 -0700710
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700711 @Override
712 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400713 // Flag the loader to stop early before switching
714 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800715 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800716 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700717 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800719 // We can't hide the IME if it was forced open. So don't bother
720 /*
721 @Override
722 public void onWindowFocusChanged(boolean hasFocus) {
723 super.onWindowFocusChanged(hasFocus);
724
725 if (hasFocus) {
726 final InputMethodManager inputManager = (InputMethodManager)
727 getSystemService(Context.INPUT_METHOD_SERVICE);
728 WindowManager.LayoutParams lp = getWindow().getAttributes();
729 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
730 android.os.Handler()) {
731 protected void onReceiveResult(int resultCode, Bundle resultData) {
732 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
733 }
734 });
735 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
736 }
737 }
738 */
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 private boolean acceptFilter() {
741 final InputMethodManager inputManager = (InputMethodManager)
742 getSystemService(Context.INPUT_METHOD_SERVICE);
743 return !inputManager.isFullscreenMode();
744 }
745
746 @Override
747 public boolean onKeyDown(int keyCode, KeyEvent event) {
748 boolean handled = super.onKeyDown(keyCode, event);
749 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
750 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
751 keyCode, event);
752 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700753 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700754 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700755 // showSearchDialog()
756 // If there are multiple keystrokes before the search dialog takes focus,
757 // onSearchRequested() will be called for every keystroke,
758 // but it is idempotent, so it's fine.
759 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761 }
762
Joe Onorato8a9625e2010-01-28 15:55:35 -0800763 // Eat the long press event so the keyboard doesn't come up.
764 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
765 return true;
766 }
767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 return handled;
769 }
770
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 private String getTypedText() {
772 return mDefaultKeySsb.toString();
773 }
774
775 private void clearTypedText() {
776 mDefaultKeySsb.clear();
777 mDefaultKeySsb.clearSpans();
778 Selection.setSelection(mDefaultKeySsb, 0);
779 }
780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700782 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
783 * State
784 */
785 private static State intToState(int stateOrdinal) {
786 State state = State.WORKSPACE;
787 final State[] stateValues = State.values();
788 for (int i = 0; i < stateValues.length; i++) {
789 if (stateValues[i].ordinal() == stateOrdinal) {
790 state = stateValues[i];
791 break;
792 }
793 }
794 return state;
795 }
796
797 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 * Restores the previous state, if it exists.
799 *
800 * @param savedState The previous state.
801 */
802 private void restoreState(Bundle savedState) {
803 if (savedState == null) {
804 return;
805 }
806
Michael Jurka883f55b2010-10-21 15:47:14 -0700807 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
808
809 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800810 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700811 } else if (state == State.CUSTOMIZE) {
812 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800813 }
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
816 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700817 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
819
820 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700823 mAddScreen = addScreen;
824 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
825 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 mRestoring = true;
827 }
828
829 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
830 if (renameFolder) {
831 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700832 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 mRestoring = true;
834 }
835 }
836
837 /**
838 * Finds all the views we need and configure them properly.
839 */
840 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700841 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400842
Romain Guyb1b69f52009-08-10 15:10:15 -0700843 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400844 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845
Joe Onorato7c312c12009-08-13 21:36:53 -0700846 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700847 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700848 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800849 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700850 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700851 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700852
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700853 if (LauncherApplication.isScreenXLarge()) {
854 // They need to be INVISIBLE initially so that they will be measured in the layout.
855 // Otherwise the animations are messed up when we show them for the first time.
856 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
857 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
858 }
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
861 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500862 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700863
Joe Onorato7c312c12009-08-13 21:36:53 -0700864 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
865 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700867 View handleView = findViewById(R.id.all_apps_button);
868 if (handleView != null && handleView instanceof HandleView) {
869 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700870 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700871 mHandleView.setLauncher(this);
872 mHandleView.setOnClickListener(this);
873 mHandleView.setOnLongClickListener(this);
874 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800875
Winson Chung80baf5a2010-08-09 16:03:15 -0700876 if (mCustomizePagedView != null) {
877 mCustomizePagedView.setLauncher(this);
878 mCustomizePagedView.setDragController(dragController);
879 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700880 } else {
881 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
882 hotseatLeft.setContentDescription(mHotseatLabels[0]);
883 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
884 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
885 hotseatRight.setContentDescription(mHotseatLabels[1]);
886 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400887
Michael Jurkaaf442092010-06-10 17:01:57 -0700888 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
889 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800890
Michael Jurkaaf442092010-06-10 17:01:57 -0700891 Drawable previous = mPreviousView.getDrawable();
892 Drawable next = mNextView.getDrawable();
893 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894
Michael Jurkaaf442092010-06-10 17:01:57 -0700895 mPreviousView.setHapticFeedbackEnabled(false);
896 mPreviousView.setOnLongClickListener(this);
897 mNextView.setHapticFeedbackEnabled(false);
898 mNextView.setOnLongClickListener(this);
899 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400902 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904
905 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400906 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700907 int deleteZoneHandleId;
908 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700909 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700910 } else {
911 deleteZoneHandleId = R.id.all_apps_button_cluster;
912 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700913 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700914 dragController.addDragListener(deleteZone);
915
916 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
917 if (infoButton != null) {
918 infoButton.setLauncher(this);
919 infoButton.setHandle(findViewById(R.id.configure_button));
920 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
921 dragController.addDragListener(infoButton);
922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923
Joe Onorato00acb122009-08-04 16:04:30 -0400924 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400925 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800926 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700927
Joe Onorato00acb122009-08-04 16:04:30 -0400928 // The order here is bottom to top.
929 dragController.addDropTarget(workspace);
930 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700931 if (infoButton != null) {
932 dragController.addDropTarget(infoButton);
933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 }
935
Romain Guy8a73c512009-11-09 19:19:59 -0800936 @SuppressWarnings({"UnusedDeclaration"})
937 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700938 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800939 mWorkspace.scrollLeft();
940 }
Romain Guy8a73c512009-11-09 19:19:59 -0800941 }
942
943 @SuppressWarnings({"UnusedDeclaration"})
944 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700945 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800946 mWorkspace.scrollRight();
947 }
Romain Guy8a73c512009-11-09 19:19:59 -0800948 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400949
950 @SuppressWarnings({"UnusedDeclaration"})
951 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700952 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400953
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400954 int index = -1;
955 if (v.getId() == R.id.hotseat_left) {
956 index = 0;
957 } else if (v.getId() == R.id.hotseat_right) {
958 index = 1;
959 }
960
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400961 // reload these every tap; you never know when they might change
962 loadHotseats();
963 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
964 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400965 startActivitySafely(
966 mHotseats[index],
967 "hotseat"
968 );
969 }
970 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 /**
973 * Creates a view representing a shortcut.
974 *
975 * @param info The data structure describing the shortcut.
976 *
977 * @return A View inflated from R.layout.application.
978 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800979 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700981 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 /**
985 * Creates a view representing a shortcut inflated from the specified resource.
986 *
987 * @param layoutResId The id of the XML layout used to create the shortcut.
988 * @param parent The group the shortcut belongs to.
989 * @param info The data structure describing the shortcut.
990 *
991 * @return A View inflated from layoutResId.
992 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
995
Patrick Dubroy9e482382010-11-01 15:16:13 -0700996 Bitmap b = info.getIcon(mIconCache);
997
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -0700999 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 favorite.setText(info.title);
1002 favorite.setTag(info);
1003 favorite.setOnClickListener(this);
1004
1005 return favorite;
1006 }
1007
1008 /**
1009 * Add an application shortcut to the workspace.
1010 *
1011 * @param data The intent describing the application.
1012 * @param cellInfo The position on screen where to create the shortcut.
1013 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014 void completeAddApplication(Context context, Intent data, int screen,
1015 int intersectCellX, int intersectCellY) {
1016 final int[] cellXY = mTmpAddItemCellCoordinates;
1017 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1018
1019 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1020 showOutOfSpaceMessage();
1021 return;
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1025 data, context);
1026
Romain Guy73b979d2009-06-09 12:57:21 -07001027 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001028 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001030 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001031 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1032 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001033 } else {
1034 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 /**
1039 * Add a shortcut to the workspace.
1040 *
1041 * @param data The intent describing the shortcut.
1042 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001044 private void completeAddShortcut(Intent data, int screen,
1045 int intersectCellX, int intersectCellY) {
1046 final int[] cellXY = mTmpAddItemCellCoordinates;
1047 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1050 showOutOfSpaceMessage();
1051 return;
1052 }
1053
1054 final ShortcutInfo info = mModel.addShortcut(
1055 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
1057 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001059 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061 }
1062
Romain Guycbb89e42009-06-08 15:52:54 -07001063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001067 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001072
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1075 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001078 // We have saved the position to which the widget was dragged-- this really only matters
1079 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001081
1082 // For now, we don't save the coordinate where we dropped the icon because we're not
1083 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1084 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001085 int[] touchXY = null;
1086 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1087 touchXY = mAddDropPosition;
1088 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001089 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 boolean foundCellSpan = false;
1091 if (touchXY != null) {
1092 // when dragging and dropping, just find the closest free spot
1093 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1094 int[] result = screenLayout.findNearestVacantArea(
1095 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001096 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 foundCellSpan = !findNearestVacantAreaFailed;
1098 } else {
1099 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1100 // if we long pressed on an empty cell to bring up a menu,
1101 // make sure we intersect the empty cell
1102 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1103 mAddIntersectCellX, mAddIntersectCellY);
1104 } else {
1105 // if we went through the menu -> add, just find any spot
1106 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1107 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001108 }
1109
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001111 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001113 return;
1114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 // Build Launcher-specific widget info and save to database
1117 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001118 launcherInfo.spanX = spanXY[0];
1119 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 LauncherModel.addItemToDatabase(this, launcherInfo,
1122 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001123 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124
1125 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001131 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
1137 }
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Michael Jurka0280c3b2010-09-17 15:00:07 -07001139 void showOutOfSpaceMessage() {
1140 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1141 }
1142
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1144 mDesktopItems.remove(launcherInfo);
1145 launcherInfo.hostView = null;
1146 }
1147
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001148 public LauncherAppWidgetHost getAppWidgetHost() {
1149 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Winson Chunga9abd0e2010-10-27 17:18:37 -07001152 public LauncherModel getModel() {
1153 return mModel;
1154 }
1155
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001156 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001157 getWindow().closeAllPanels();
1158
1159 try {
1160 dismissDialog(DIALOG_CREATE_SHORTCUT);
1161 // Unlock the workspace if the dialog was showing
1162 } catch (Exception e) {
1163 // An exception is thrown if the dialog is not visible, which is fine
1164 }
1165
1166 try {
1167 dismissDialog(DIALOG_RENAME_FOLDER);
1168 // Unlock the workspace if the dialog was showing
1169 } catch (Exception e) {
1170 // An exception is thrown if the dialog is not visible, which is fine
1171 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001172
1173 // Whatever we were doing is hereby canceled.
1174 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001175 }
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 @Override
1178 protected void onNewIntent(Intent intent) {
1179 super.onNewIntent(intent);
1180
1181 // Close the menu
1182 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001183 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001184 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001185
Joe Onorato14f122b2009-11-19 14:06:36 -08001186 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1187 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001188
Michael Jurka4cb37242010-08-09 21:05:32 -07001189 // in all these cases, only animate if we're already on home
1190 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001191 mWorkspace.unshrink(alreadyOnHome);
1192 }
1193 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001194 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001195 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1196 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001197 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001198 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001199
Joe Onorato3a8820b2009-11-10 15:06:42 -08001200 final View v = getWindow().peekDecorView();
1201 if (v != null && v.getWindowToken() != null) {
1202 InputMethodManager imm = (InputMethodManager)getSystemService(
1203 INPUT_METHOD_SERVICE);
1204 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206 }
1207 }
1208
1209 @Override
1210 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1211 // Do not call super here
1212 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001213
1214 if (mHomeCustomizationDrawer != null) {
1215 String cur = savedInstanceState.getString("currentTab");
1216 if (cur != null) {
1217 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1218 }
1219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221
1222 @Override
1223 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001224 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
1226 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1227 if (folders.size() > 0) {
1228 final int count = folders.size();
1229 long[] ids = new long[count];
1230 for (int i = 0; i < count; i++) {
1231 final FolderInfo info = folders.get(i).getInfo();
1232 ids[i] = info.id;
1233 }
1234 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1235 } else {
1236 super.onSaveInstanceState(outState);
1237 }
1238
Michael Jurka883f55b2010-10-21 15:47:14 -07001239 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 if (mAddScreen > -1 && mWaitingForResult) {
1242 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1243 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1244 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246
1247 if (mFolderInfo != null && mWaitingForResult) {
1248 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1249 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1250 }
Michael Jurka946ad472010-07-09 18:05:18 -07001251
1252 if (mHomeCustomizationDrawer != null) {
1253 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1254 if (currentTabTag != null) {
1255 outState.putString("currentTab", currentTabTag);
1256 }
1257 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
1259
1260 @Override
1261 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001265 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001267 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269
1270 TextKeyListener.getInstance().release();
1271
Joe Onorato9c1289c2009-08-17 11:03:03 -04001272 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Joe Onorato9c1289c2009-08-17 11:03:03 -04001274 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001275
1276 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001277
Patrick Dubroyab962b72010-07-26 12:10:10 -07001278 // Some launcher layouts don't have a previous and next view
1279 if (mPreviousView != null) {
1280 dismissPreview(mPreviousView);
1281 }
1282 if (mNextView != null) {
1283 dismissPreview(mNextView);
1284 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001285
1286 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288
1289 @Override
1290 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001291 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 super.startActivityForResult(intent, requestCode);
1293 }
1294
1295 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001296 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001298
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001299 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001300
Karl Rosaen138a0412009-04-23 19:00:21 -07001301 if (initialQuery == null) {
1302 // Use any text typed in the launcher as the initial query
1303 initialQuery = getTypedText();
1304 clearTypedText();
1305 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 if (appSearchData == null) {
1307 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001308 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
Romain Guycbb89e42009-06-08 15:52:54 -07001310
Karl Rosaen138a0412009-04-23 19:00:21 -07001311 final SearchManager searchManager =
1312 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001313 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001314 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316
1317 @Override
1318 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001319 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 return false;
1321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
1323 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1326 .setIcon(android.R.drawable.ic_menu_add)
1327 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001328 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1329 .setIcon(android.R.drawable.ic_menu_manage)
1330 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001331 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 .setIcon(android.R.drawable.ic_menu_gallery)
1333 .setAlphabeticShortcut('W');
1334 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1335 .setIcon(android.R.drawable.ic_search_category_default)
1336 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1337 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1338 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1339 .setAlphabeticShortcut('N');
1340
1341 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001342 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1343 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344
1345 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1346 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1347 .setIntent(settings);
1348
1349 return true;
1350 }
1351
1352 @Override
1353 public boolean onPrepareOptionsMenu(Menu menu) {
1354 super.onPrepareOptionsMenu(menu);
1355
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001356 // If all apps is animating, don't show the menu, because we don't know
1357 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001358 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001359 return false;
1360 }
1361
1362 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001363 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001364 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1365 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1366
1367 // Disable add if the workspace is full.
1368 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001369 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1370 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372
1373 return true;
1374 }
1375
1376 @Override
1377 public boolean onOptionsItemSelected(MenuItem item) {
1378 switch (item.getItemId()) {
1379 case MENU_ADD:
1380 addItems();
1381 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001382 case MENU_MANAGE_APPS:
1383 manageApps();
1384 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 case MENU_WALLPAPER_SETTINGS:
1386 startWallpaper();
1387 return true;
1388 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001389 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 return true;
1391 case MENU_NOTIFICATIONS:
1392 showNotifications();
1393 return true;
1394 }
1395
1396 return super.onOptionsItemSelected(item);
1397 }
Romain Guycbb89e42009-06-08 15:52:54 -07001398
Karl Rosaen138a0412009-04-23 19:00:21 -07001399 /**
1400 * Indicates that we want global search for this activity by setting the globalSearch
1401 * argument for {@link #startSearch} to true.
1402 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001404 @Override
1405 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001406 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001407 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001408 }
1409
Joe Onorato9c1289c2009-08-17 11:03:03 -04001410 public boolean isWorkspaceLocked() {
1411 return mWorkspaceLoading || mWaitingForResult;
1412 }
1413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001415 if (LauncherApplication.isScreenXLarge()) {
1416 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001417 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001418 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001419 }
1420 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001421 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001422 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 }
1425
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 private void resetAddInfo() {
1427 mAddScreen = -1;
1428 mAddIntersectCellX = -1;
1429 mAddIntersectCellY = -1;
1430 mAddDropPosition = null;
1431 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001432
Winson Chung55cef262010-10-28 14:14:18 -07001433 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001434 resetAddInfo();
1435 mAddScreen = screen;
1436
1437 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1438 mAddDropPosition = position;
1439
Michael Jurkaaf442092010-06-10 17:01:57 -07001440 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001441 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1442 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001443 }
1444
Winson Chung7ad01412010-09-27 11:33:03 -07001445 private void manageApps() {
1446 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1447 }
1448
Michael Jurkaaf442092010-06-10 17:01:57 -07001449 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001450 // TODO: catch bad widget exception when sent
1451 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001452 // TODO: Is this log message meaningful?
1453 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001454 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001455 }
1456
Winson Chung55cef262010-10-28 14:14:18 -07001457 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001458 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
Bjorn Bringert7984c942009-12-09 15:38:25 +00001460 if (appWidget.configure != null) {
1461 // Launch over to configure widget, if needed
1462 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1463 intent.setComponent(appWidget.configure);
1464 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001465 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001466 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1467 intent.putExtra(
1468 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1469 info.mimeType);
1470
1471 final String mimeType = info.mimeType;
1472 final ClipData clipData = (ClipData) info.configurationData;
1473 final ClipDescription clipDesc = clipData.getDescription();
1474 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1475 if (clipDesc.getMimeType(i).equals(mimeType)) {
1476 final ClipData.Item item = clipData.getItem(i);
1477 final CharSequence stringData = item.getText();
1478 final Uri uriData = item.getUri();
1479 final Intent intentData = item.getIntent();
1480 final String key =
1481 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1482 if (uriData != null) {
1483 intent.putExtra(key, uriData);
1484 } else if (intentData != null) {
1485 intent.putExtra(key, intentData);
1486 } else if (stringData != null) {
1487 intent.putExtra(key, stringData);
1488 }
1489 break;
1490 }
1491 }
1492 }
Winson Chung55cef262010-10-28 14:14:18 -07001493 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001494
Romain Guy8e633c52010-03-04 12:51:36 -08001495 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001497 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001498 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500 }
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1503 resetAddInfo();
1504 mAddScreen = screen;
1505 mAddDropPosition = position;
1506
1507 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1508 createShortcutIntent.setComponent(componentName);
1509 processShortcut(createShortcutIntent);
1510 }
1511
Joe Onoratodeb98af2010-02-19 14:59:39 -08001512 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001513 // Handle case where user selected "Applications"
1514 String applicationName = getResources().getString(R.string.group_applications);
1515 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001517 if (applicationName != null && applicationName.equals(shortcutName)) {
1518 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1519 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001521 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1522 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001523 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001524 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001525 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001526 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
Winson Chung24ab2f12010-09-16 14:10:47 -07001530 void processWallpaper(Intent intent) {
1531 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1532 }
1533
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1535 resetAddInfo();
1536 mAddScreen = screen;
1537 mAddDropPosition = position;
1538
1539 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1540 createFolderIntent.setComponent(componentName);
1541
1542 addLiveFolder(createFolderIntent);
1543 }
1544
1545 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001546 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001547 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001548 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001550 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001552 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001553 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 UserFolderInfo folderInfo = new UserFolderInfo();
1559 folderInfo.title = getText(R.string.folder_name);
1560
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1562 final int[] cellXY = mTmpAddItemCellCoordinates;
1563 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1564 showOutOfSpaceMessage();
1565 return;
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
1568 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 LauncherModel.addItemToDatabase(this, folderInfo,
1570 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001571 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001572 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573
1574 // Create the view
1575 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001576 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1577 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001578 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Romain Guycbb89e42009-06-08 15:52:54 -07001580
Joe Onorato9c1289c2009-08-17 11:03:03 -04001581 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001582 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001583 }
1584
Michael Jurka28750fb2010-09-24 17:43:49 -07001585 private void completeAddLiveFolder(
1586 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001587 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1588 final int[] cellXY = mTmpAddItemCellCoordinates;
1589 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1590 showOutOfSpaceMessage();
1591 return;
1592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595
1596 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001598 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001599 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
1601 }
1602
1603 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001604 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605
1606 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1607 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1608
1609 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 Intent.ShortcutIconResource iconResource = null;
1611
1612 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1613 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1614 try {
1615 iconResource = (Intent.ShortcutIconResource) extra;
1616 final PackageManager packageManager = context.getPackageManager();
1617 Resources resources = packageManager.getResourcesForApplication(
1618 iconResource.packageName);
1619 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1620 icon = resources.getDrawable(id);
1621 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001622 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 }
1624 }
1625
1626 if (icon == null) {
1627 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1628 }
1629
1630 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001631 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 info.title = name;
1633 info.iconResource = iconResource;
1634 info.uri = data.getData();
1635 info.baseIntent = baseIntent;
1636 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1637 LiveFolders.DISPLAY_MODE_GRID);
1638
1639 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001640 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001641 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642
1643 return info;
1644 }
1645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 private void showNotifications() {
1647 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1648 if (statusBar != null) {
1649 statusBar.expand();
1650 }
1651 }
1652
1653 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001654 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001656 Intent chooser = Intent.createChooser(pickWallpaper,
1657 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001658 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1659 // Removed in Eclair MR1
1660// WallpaperManager wm = (WallpaperManager)
1661// getSystemService(Context.WALLPAPER_SERVICE);
1662// WallpaperInfo wi = wm.getWallpaperInfo();
1663// if (wi != null && wi.getSettingsActivity() != null) {
1664// LabeledIntent li = new LabeledIntent(getPackageName(),
1665// R.string.configure_wallpaper, 0);
1666// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1667// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1668// }
Mike Clerona0618e42009-10-22 13:55:21 -07001669 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
1671
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001672 /**
1673 * Registers various content observers. The current implementation registers
1674 * only a favorites observer to keep track of the favorites applications.
1675 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001676 private void registerContentObservers() {
1677 ContentResolver resolver = getContentResolver();
1678 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1679 true, mWidgetObserver);
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 @Override
1683 public boolean dispatchKeyEvent(KeyEvent event) {
1684 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1685 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001687 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001688 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001689 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001690 dumpState();
1691 return true;
1692 }
1693 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001694 }
1695 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1696 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001697 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 return true;
1699 }
1700 }
1701
1702 return super.dispatchKeyEvent(event);
1703 }
1704
Joe Onorato88ec0992009-11-19 13:16:06 -08001705 @Override
1706 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001707 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1708 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001709 } else {
1710 closeFolder();
1711 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001712 // Some launcher layouts don't have a previous and next view
1713 if (mPreviousView != null) {
1714 dismissPreview(mPreviousView);
1715 dismissPreview(mNextView);
1716 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001717 }
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 private void closeFolder() {
1720 Folder folder = mWorkspace.getOpenFolder();
1721 if (folder != null) {
1722 closeFolder(folder);
1723 }
1724 }
1725
1726 void closeFolder(Folder folder) {
1727 folder.getInfo().opened = false;
1728 ViewGroup parent = (ViewGroup) folder.getParent();
1729 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001730 CellLayout cl = (CellLayout) parent;
1731 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001732 if (folder instanceof DropTarget) {
1733 // Live folders aren't DropTargets.
1734 mDragController.removeDropTarget((DropTarget)folder);
1735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 }
1737 folder.onClose();
1738 }
1739
1740 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001741 * Re-listen when widgets are reset.
1742 */
1743 private void onAppWidgetReset() {
1744 mAppWidgetHost.startListening();
1745 }
1746
1747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1749 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751 private void unbindDesktopItems() {
1752 for (ItemInfo item: mDesktopItems) {
1753 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 /**
1758 * Launches the intent referred by the clicked shortcut.
1759 *
1760 * @param v The view representing the clicked shortcut.
1761 */
1762 public void onClick(View v) {
1763 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001764 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001766 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001767 int[] pos = new int[2];
1768 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001769 intent.setSourceBounds(new Rect(pos[0], pos[1],
1770 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001771 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 } else if (tag instanceof FolderInfo) {
1773 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001774 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001775 if (mState == State.ALL_APPS) {
1776 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001777 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001778 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781 }
1782
Michael Jurka0e260592010-06-30 17:07:39 -07001783 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001784 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001785 // clicking anywhere on the workspace causes the customization drawer to slide down
1786 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001787 return false;
1788 }
1789
Michael Jurkaaf442092010-06-10 17:01:57 -07001790 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001791 * Event handler for the search button
1792 *
1793 * @param v The view that was clicked.
1794 */
1795 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001796 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001797 }
1798
1799 /**
1800 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001801 * enters home screen customization mode.
1802 *
1803 * @param v The view that was clicked.
1804 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001805 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001806 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001807 }
1808
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001809 /**
1810 * Event handler for the "grid" button that appears on the home screen, which
1811 * enters all apps mode.
1812 *
1813 * @param v The view that was clicked.
1814 */
1815 public void onClickAllAppsButton(View v) {
1816 showAllApps(true);
1817 }
1818
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001819 public void onClickAppMarketButton(View v) {
1820 if (mAppMarketIntent != null) {
1821 startActivitySafely(mAppMarketIntent, "app market");
1822 }
1823 }
1824
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001825 void startApplicationDetailsActivity(ComponentName componentName) {
1826 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001827 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1828 Uri.fromParts("package", packageName, null));
1829 startActivity(intent);
1830 }
1831
Patrick Dubroy5539af72010-09-07 15:22:01 -07001832 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1833 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1834 // System applications cannot be installed. For now, show a toast explaining that.
1835 // We may give them the option of disabling apps this way.
1836 int messageId = R.string.uninstall_system_app_text;
1837 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1838 } else {
1839 String packageName = appInfo.componentName.getPackageName();
1840 String className = appInfo.componentName.getClassName();
1841 Intent intent = new Intent(
1842 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1843 startActivity(intent);
1844 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001845 }
1846
Joe Onoratof984e852010-03-25 09:47:45 -07001847 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1849 try {
1850 startActivity(intent);
1851 } catch (ActivityNotFoundException e) {
1852 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001853 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 } catch (SecurityException e) {
1855 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001856 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001858 "or use the exported attribute for this activity. "
1859 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
1861 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001862
Romain Guy8e633c52010-03-04 12:51:36 -08001863 void startActivityForResultSafely(Intent intent, int requestCode) {
1864 try {
1865 startActivityForResult(intent, requestCode);
1866 } catch (ActivityNotFoundException e) {
1867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1868 } catch (SecurityException e) {
1869 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1870 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1871 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1872 "or use the exported attribute for this activity.", e);
1873 }
1874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875
1876 private void handleFolderClick(FolderInfo folderInfo) {
1877 if (!folderInfo.opened) {
1878 // Close any open folder
1879 closeFolder();
1880 // Open the requested folder
1881 openFolder(folderInfo);
1882 } else {
1883 // Find the open folder...
1884 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1885 int folderScreen;
1886 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001887 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 // .. and close it
1889 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001890 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 // Close any folder open on the current screen
1892 closeFolder();
1893 // Pull the folder onto this screen
1894 openFolder(folderInfo);
1895 }
1896 }
1897 }
1898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001901 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 * is animated relative to the specified View. If the View is null, no animation
1903 * is played.
1904 *
1905 * @param folderInfo The FolderInfo describing the folder to open.
1906 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001907 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 Folder openFolder;
1909
1910 if (folderInfo instanceof UserFolderInfo) {
1911 openFolder = UserFolder.fromXml(this);
1912 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001913 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 } else {
1915 return;
1916 }
1917
Joe Onorato00acb122009-08-04 16:04:30 -04001918 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 openFolder.setLauncher(this);
1920
1921 openFolder.bind(folderInfo);
1922 folderInfo.opened = true;
1923
Winson Chungaafa03c2010-06-11 17:34:16 -07001924 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 openFolder.onOpen();
1927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001930 switch (v.getId()) {
1931 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001932 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001933 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1934 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001935 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001936 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001937 return true;
1938 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001939 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001940 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1941 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001942 showPreviews(v);
1943 }
1944 return true;
1945 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001946 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001947 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1948 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1949 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001950 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001951 return true;
1952 }
1953
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 return false;
1956 }
1957
1958 if (!(v instanceof CellLayout)) {
1959 v = (View) v.getParent();
1960 }
1961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Michael Jurka0280c3b2010-09-17 15:00:07 -07001963 resetAddInfo();
1964 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001966 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 return true;
1968 }
1969
Michael Jurka0280c3b2010-09-17 15:00:07 -07001970 final View itemUnderLongClick = longClickCellInfo.cell;
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001973 if (itemUnderLongClick == null) {
1974 // User long pressed on empty space
1975 mWorkspace.setAllowLongPress(false);
1976 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1977 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001978 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001979 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
1981 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001982 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001984 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1985 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001986 mAddIntersectCellX = longClickCellInfo.cellX;
1987 mAddIntersectCellY = longClickCellInfo.cellY;
1988 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990 }
1991 }
1992 return true;
1993 }
1994
Romain Guye6b8e2f2009-11-10 11:56:55 -08001995 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001996 private void dismissPreview(final View v) {
1997 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001998 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001999 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2000 public void onDismiss() {
2001 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2002 int count = group.getChildCount();
2003 for (int i = 0; i < count; i++) {
2004 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2005 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002006 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2007 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2008
2009 v.setTag(R.id.workspace, null);
2010 v.setTag(R.id.icon, null);
2011 window.setOnDismissListener(null);
2012 }
2013 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002014 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002015 }
2016 v.setTag(null);
2017 }
2018
Romain Guyf8e6a802009-12-07 17:48:02 -08002019 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002020 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002021 }
2022
Romain Guya6abce82009-11-10 02:54:41 -08002023 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002024 final Resources resources = getResources();
2025 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002026
Romain Guya6abce82009-11-10 02:54:41 -08002027 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002028
Romain Guy9d31e9f2009-11-11 18:54:28 -08002029 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002030
Romain Guyf8e6a802009-12-07 17:48:02 -08002031 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002032 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002033 int extraW = (int) ((r.left + r.right) * max);
2034 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002035
2036 int aW = cell.getWidth() - extraW;
2037 float w = aW / max;
2038
2039 int width = cell.getWidth();
2040 int height = cell.getHeight();
2041 int x = cell.getLeftPadding();
2042 int y = cell.getTopPadding();
2043 width -= (x + cell.getRightPadding());
2044 height -= (y + cell.getBottomPadding());
2045
2046 float scale = w / width;
2047
2048 int count = end - start;
2049
2050 final float sWidth = width * scale;
2051 float sHeight = height * scale;
2052
Romain Guye6b8e2f2009-11-10 11:56:55 -08002053 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002054
Romain Guye6b8e2f2009-11-10 11:56:55 -08002055 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2056 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002057
2058 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002059 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002060 cell = (CellLayout) workspace.getChildAt(i);
2061
Romain Guyf8e6a802009-12-07 17:48:02 -08002062 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002063 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002064
2065 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002066 c.scale(scale, scale);
2067 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002068 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002069
Romain Guy9d31e9f2009-11-11 18:54:28 -08002070 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002071 image.setImageBitmap(bitmap);
2072 image.setTag(i);
2073 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002074 image.setOnFocusChangeListener(handler);
2075 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002076 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002077
2078 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002079 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2080
Winson Chungaafa03c2010-06-11 17:34:16 -07002081 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002082 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002083
2084 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002085 p.setContentView(preview);
2086 p.setWidth((int) (sWidth * count + extraW));
2087 p.setHeight((int) (sHeight + extraH));
2088 p.setAnimationStyle(R.style.AnimationPreview);
2089 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002090 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002091 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002092 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002093
Romain Guye6b8e2f2009-11-10 11:56:55 -08002094 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2095 public void onDismiss() {
2096 dismissPreview(anchor);
2097 }
2098 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002099
2100 anchor.setTag(p);
2101 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002102 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002103 }
2104
Romain Guy9d31e9f2009-11-11 18:54:28 -08002105 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002106 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002107
Romain Guye6b8e2f2009-11-10 11:56:55 -08002108 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002109 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002110 }
2111
2112 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002113 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002114 v.post(this);
2115 }
2116
2117 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002118 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002119 }
2120
2121 public void onFocusChange(View v, boolean hasFocus) {
2122 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002123 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002124 }
Romain Guya6abce82009-11-10 02:54:41 -08002125 }
2126 }
2127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 Workspace getWorkspace() {
2129 return mWorkspace;
2130 }
2131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 @Override
2133 protected Dialog onCreateDialog(int id) {
2134 switch (id) {
2135 case DIALOG_CREATE_SHORTCUT:
2136 return new CreateShortcut().createDialog();
2137 case DIALOG_RENAME_FOLDER:
2138 return new RenameFolder().createDialog();
2139 }
2140
2141 return super.onCreateDialog(id);
2142 }
2143
2144 @Override
2145 protected void onPrepareDialog(int id, Dialog dialog) {
2146 switch (id) {
2147 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 break;
2149 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002150 if (mFolderInfo != null) {
2151 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2152 final CharSequence text = mFolderInfo.title;
2153 input.setText(text);
2154 input.setSelection(0, text.length());
2155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 break;
2157 }
2158 }
2159
2160 void showRenameDialog(FolderInfo info) {
2161 mFolderInfo = info;
2162 mWaitingForResult = true;
2163 showDialog(DIALOG_RENAME_FOLDER);
2164 }
2165
Michael Jurka0280c3b2010-09-17 15:00:07 -07002166 private void showAddDialog(int intersectX, int intersectY) {
2167 resetAddInfo();
2168 mAddIntersectCellX = intersectX;
2169 mAddIntersectCellY = intersectY;
2170 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 mWaitingForResult = true;
2172 showDialog(DIALOG_CREATE_SHORTCUT);
2173 }
2174
Joe Onoratodeb98af2010-02-19 14:59:39 -08002175 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002176 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002177 Bundle bundle = new Bundle();
2178
2179 ArrayList<String> shortcutNames = new ArrayList<String>();
2180 shortcutNames.add(getString(R.string.group_applications));
2181 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2182
2183 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2184 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2185 R.drawable.ic_launcher_application));
2186 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2187
2188 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2189 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002190 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002191 pickIntent.putExtras(bundle);
2192
Joe Onoratodeb98af2010-02-19 14:59:39 -08002193 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002194 }
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 private class RenameFolder {
2197 private EditText mInput;
2198
2199 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2201 mInput = (EditText) layout.findViewById(R.id.folder_name);
2202
2203 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2204 builder.setIcon(0);
2205 builder.setTitle(getString(R.string.rename_folder_title));
2206 builder.setCancelable(true);
2207 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2208 public void onCancel(DialogInterface dialog) {
2209 cleanup();
2210 }
2211 });
2212 builder.setNegativeButton(getString(R.string.cancel_action),
2213 new Dialog.OnClickListener() {
2214 public void onClick(DialogInterface dialog, int which) {
2215 cleanup();
2216 }
2217 }
2218 );
2219 builder.setPositiveButton(getString(R.string.rename_action),
2220 new Dialog.OnClickListener() {
2221 public void onClick(DialogInterface dialog, int which) {
2222 changeFolderName();
2223 }
2224 }
2225 );
2226 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002227
2228 final AlertDialog dialog = builder.create();
2229 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2230 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002231 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002232 mInput.requestFocus();
2233 InputMethodManager inputManager = (InputMethodManager)
2234 getSystemService(Context.INPUT_METHOD_SERVICE);
2235 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002236 }
2237 });
2238
2239 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241
2242 private void changeFolderName() {
2243 final String name = mInput.getText().toString();
2244 if (!TextUtils.isEmpty(name)) {
2245 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002246 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 mFolderInfo.title = name;
2248 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2249
Joe Onorato9c1289c2009-08-17 11:03:03 -04002250 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002251 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 } else {
2254 final FolderIcon folderIcon = (FolderIcon)
2255 mWorkspace.getViewForTag(mFolderInfo);
2256 if (folderIcon != null) {
2257 folderIcon.setText(name);
2258 getWorkspace().requestLayout();
2259 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002260 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 mWorkspaceLoading = true;
2262 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 }
2264 }
2265 }
2266 cleanup();
2267 }
2268
2269 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 dismissDialog(DIALOG_RENAME_FOLDER);
2271 mWaitingForResult = false;
2272 mFolderInfo = null;
2273 }
2274 }
2275
Daniel Sandler843e8602010-06-07 14:59:01 -04002276 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2277 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002278 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002279 }
2280
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002281 // AllAppsView.Watcher
2282 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002283 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2284 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002285 mWorkspace.setVisibility(View.GONE);
2286 }
2287 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002288
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002289 private void showToolbarButton(View button) {
2290 button.setAlpha(1.0f);
2291 button.setVisibility(View.VISIBLE);
2292 button.setFocusable(true);
2293 button.setClickable(true);
2294 }
2295
2296 private void hideToolbarButton(View button) {
2297 button.setAlpha(0.0f);
2298 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2299 button.setVisibility(View.INVISIBLE);
2300 button.setFocusable(false);
2301 button.setClickable(false);
2302 }
2303
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002304 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002305 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 *
2307 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2308 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002309 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002310 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002311 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002312 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313 final boolean showing = show;
2314 final boolean hiding = !show;
2315
2316 final int duration = show ?
2317 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2318 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2319
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002320 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002321 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2322 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002323 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002324 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002325 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002326 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002327 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002328 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002329 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002330 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002331 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002332 });
2333 seq.play(anim);
2334 } else {
2335 if (showing) {
2336 showToolbarButton(view);
2337 } else {
2338 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002339 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002340 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002341 }
2342
2343 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002344 * Show/hide the appropriate toolbar buttons for newState.
2345 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002346 *
2347 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002348 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2349 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002350 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002351 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002352 final View searchButton = findViewById(R.id.search_button);
2353 final View allAppsButton = findViewById(R.id.all_apps_button);
2354 final View marketButton = findViewById(R.id.market_button);
2355 final View configureButton = findViewById(R.id.configure_button);
2356
2357 switch (newState) {
2358 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002359 hideOrShowToolbarButton(true, searchButton, showSeq);
2360 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2361 hideOrShowToolbarButton(true, configureButton, showSeq);
2362 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002363 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002364 break;
2365 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002366 hideOrShowToolbarButton(true, configureButton, showSeq);
2367 hideOrShowToolbarButton(true, marketButton, showSeq);
2368 hideOrShowToolbarButton(false, searchButton, hideSeq);
2369 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002370 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002371 break;
2372 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002373 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2374 hideOrShowToolbarButton(false, searchButton, hideSeq);
2375 hideOrShowToolbarButton(false, marketButton, hideSeq);
2376 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002377 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002378 break;
2379 }
2380 }
2381
2382 /**
2383 * Helper method for the cameraZoomIn/cameraZoomOut animations
2384 * @param view The view being animated
2385 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2386 * @param scaleFactor The scale factor used for the zoom
2387 */
2388 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2389 final int height = view.getHeight();
2390 view.setPivotX(view.getWidth() / 2.0f);
2391 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2392 // Assumes that the view is normally anchored to either the top or bottom of the screen
2393 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2394 if (state == State.ALL_APPS) {
2395 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2396 } else {
2397 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2398 }
2399 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002400
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002401 /**
2402 * Zoom the camera out from the workspace to reveal 'toView'.
2403 * Assumes that the view to show is anchored at either the very top or very bottom
2404 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002405 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002406 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002407 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002408 final Resources res = getResources();
2409 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2410 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002411 final boolean toAllApps = (toState == State.ALL_APPS);
2412 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002413
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002414 setPivotsForZoom(toView, toState, scale);
2415
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002416 if (toAllApps) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002417 mWorkspace.shrinkToBottom(animated);
2418 } else {
2419 mWorkspace.shrinkToTop(animated);
2420 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002421
2422 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002423 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2424 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2425 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2426 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002427 scaleAnim.setInterpolator(new DecelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002428 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002429 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002430 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002431 // Prepare the position
2432 toView.setTranslationX(0.0f);
2433 toView.setTranslationY(0.0f);
2434 toView.setVisibility(View.VISIBLE);
2435 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002436 @Override
2437 public void onAnimationEndOrCancel(Animator animation) {
2438 // If we don't set the final scale values here, if this animation is cancelled
2439 // it will have the wrong scale value and subsequent cameraPan animations will
2440 // not fix that
2441 toView.setScaleX(1.0f);
2442 toView.setScaleY(1.0f);
2443 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002444 });
2445
Chet Haaseb1254a62010-09-07 13:35:00 -07002446 AnimatorSet toolbarHideAnim = new AnimatorSet();
2447 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002448 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2449
2450 // toView should appear right at the end of the workspace shrink animation
2451 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2452
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002453 if (mStateAnimation != null) mStateAnimation.cancel();
2454 mStateAnimation = new AnimatorSet();
2455 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2456 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002457
2458 // Show the new toolbar buttons just as the main animation is ending
2459 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002460 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2461 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002462 } else {
2463 toView.setTranslationX(0.0f);
2464 toView.setTranslationY(0.0f);
2465 toView.setScaleX(1.0f);
2466 toView.setScaleY(1.0f);
2467 toView.setVisibility(View.VISIBLE);
2468 hideAndShowToolbarButtons(toState, null, null);
2469 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002470 }
2471
2472 /**
2473 * Zoom the camera back into the workspace, hiding 'fromView'.
2474 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002475 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2476 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002477 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002478 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002479 Resources res = getResources();
2480 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2481 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002482 final View fromView =
2483 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2484
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002485 mCustomizePagedView.endChoiceMode();
2486 mAllAppsPagedView.endChoiceMode();
2487
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002488 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002489
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002491
2492 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002493 if (mStateAnimation != null) mStateAnimation.cancel();
2494 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002495 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2496 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2497 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2498 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002499 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002500 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002501 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002502 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002503 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002504 }
2505 });
2506
Chet Haaseb1254a62010-09-07 13:35:00 -07002507 AnimatorSet toolbarHideAnim = new AnimatorSet();
2508 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002509 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2510
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002511 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512
2513 // Show the new toolbar buttons at the very end of the whole animation
2514 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2515 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002516 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2517 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002518 } else {
2519 fromView.setVisibility(View.GONE);
2520 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2521 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002522 }
2523
2524 /**
2525 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2526 * This is the transition used on xlarge screens to go between all apps and
2527 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002528 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2529 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2530 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002531 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002533 final Resources res = getResources();
2534 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002535 final int workspaceHeight = mWorkspace.getHeight();
2536
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002537 final boolean fromAllApps = (fromState == State.ALL_APPS);
2538 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2539 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002540
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2542 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2543 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2544 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002545
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002546 mCustomizePagedView.endChoiceMode();
2547 mAllAppsPagedView.endChoiceMode();
2548
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 if (toState == State.ALL_APPS) {
2550 mWorkspace.shrinkToBottom(animated);
2551 } else {
2552 mWorkspace.shrinkToTop(animated);
2553 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554
2555 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002556 if (mStateAnimation != null) mStateAnimation.cancel();
2557 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002558 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002559 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002560 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002561 toView.setVisibility(View.VISIBLE);
2562 toView.setY(toViewStartY);
2563 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002564 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002565 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 fromView.setVisibility(View.GONE);
2567 }
2568 });
2569
Chet Haaseb1254a62010-09-07 13:35:00 -07002570 AnimatorSet toolbarHideAnim = new AnimatorSet();
2571 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2573
Chet Haase472b2812010-10-14 07:02:04 -07002574 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2575 fromViewStartY, fromViewEndY);
2576 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002577 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2578 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2579 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2580 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2581 );
Chet Haase472b2812010-10-14 07:02:04 -07002582 fromAnim.setDuration(duration);
2583 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584
2585 // Show the new toolbar buttons just as the main animation is ending
2586 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002587 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2588 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 } else {
2590 fromView.setY(fromViewEndY);
2591 fromView.setVisibility(View.GONE);
2592 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002593 toView.setScaleX(1.0f);
2594 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 toView.setVisibility(View.VISIBLE);
2596 hideAndShowToolbarButtons(toState, null, null);
2597 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 }
2599
2600 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002601 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002602 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002603 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002604
Michael Jurka79212d82010-07-30 16:36:20 -07002605 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002606 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002608 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002610 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002611 } else {
2612 mAllAppsGrid.zoom(1.0f, animated);
2613 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002614
Romain Guyc16fea72010-03-12 17:17:56 -08002615 ((View) mAllAppsGrid).setFocusable(true);
2616 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002617
Joe Onorato7c312c12009-08-13 21:36:53 -07002618 // TODO: fade these two too
2619 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002620 // Change the state *after* we've called all the transition code
2621 mState = State.ALL_APPS;
2622 }
2623
2624
2625 void showWorkspace(boolean animated) {
2626 showWorkspace(animated, null);
2627 }
2628
2629 void showWorkspace(boolean animated, CellLayout layout) {
2630 if (layout != null && animated) {
2631 mWorkspace.unshrink(layout);
2632 } else {
2633 mWorkspace.unshrink(animated);
2634 }
2635 if (mState == State.ALL_APPS) {
2636 closeAllApps(animated);
2637 } else if (mState == State.CUSTOMIZE) {
2638 hideCustomizationDrawer(animated);
2639 }
2640 // Change the state *after* we've called all the transition code
2641 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002642 }
2643
Joe Onoratob2061212009-11-24 16:13:54 -05002644 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002645 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002646 * - Home from workspace
2647 * - from center screen
2648 * - from other screens
2649 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002650 * - from center screen
2651 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002652 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002653 * - from center screen
2654 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002655 * - Launch app from workspace and quit
2656 * - with back
2657 * - with home
2658 * - Launch app from all apps and quit
2659 * - with back
2660 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002661 * - Go to a screen that's not the default, then all
2662 * apps, and launch and app, and go back
2663 * - with back
2664 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002665 * - On workspace, long press power and go back
2666 * - with back
2667 * - with home
2668 * - On all apps, long press power and go back
2669 * - with back
2670 * - with home
2671 * - On workspace, power off
2672 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002673 * - Launch an app and turn off the screen while in that app
2674 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002675 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002676 * - From all apps
2677 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002678 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2679 * - From all apps
2680 * - From the center workspace
2681 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002682 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002683 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002684 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002685 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002686 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002688 } else {
2689 mAllAppsGrid.zoom(0.0f, animated);
2690 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002691 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002692 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002693 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002694 }
2695
Joe Onorato7c312c12009-08-13 21:36:53 -07002696 void lockAllApps() {
2697 // TODO
2698 }
2699
2700 void unlockAllApps() {
2701 // TODO
2702 }
2703
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002704 // Show the customization drawer (only exists in x-large configuration)
2705 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002706 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002707 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002708 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002709 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002710 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002711 // Change the state *after* we've called all the transition code
2712 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002713 }
2714
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002715 // Hide the customization drawer (only exists in x-large configuration)
2716 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002717 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002718 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002719 }
2720 }
2721
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002722 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2723 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2724 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002725 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002726 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002727
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002728 void onWorkspaceClick(CellLayout layout) {
2729 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002730 }
2731
Michael Jurka0423dcf2010-10-05 14:56:18 -07002732 private void updateButtonWithIconFromExternalActivity(
2733 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2734 ImageView button = (ImageView) findViewById(buttonId);
2735 Drawable toolbarIcon = null;
2736 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002737 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002738 // Look for the toolbar icon specified in the activity meta-data
2739 Bundle metaData = packageManager.getActivityInfo(
2740 activityName, PackageManager.GET_META_DATA).metaData;
2741 if (metaData != null) {
2742 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2743 if (iconResId != 0) {
2744 Resources res = packageManager.getResourcesForActivity(activityName);
2745 toolbarIcon = res.getDrawable(iconResId);
2746 }
2747 }
2748 } catch (NameNotFoundException e) {
2749 // Do nothing
2750 }
2751 // If we were unable to find the icon via the meta-data, use a generic one
2752 if (toolbarIcon == null) {
2753 button.setImageResource(fallbackDrawableId);
2754 } else {
2755 button.setImageDrawable(toolbarIcon);
2756 }
2757 }
2758
2759 private void updateGlobalSearchIcon() {
2760 if (LauncherApplication.isScreenXLarge()) {
2761 final SearchManager searchManager =
2762 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2763 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002764 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002765 updateButtonWithIconFromExternalActivity(
2766 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002767 }
2768 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002769 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002770
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002771 /**
2772 * Sets the app market icon (shown when all apps is visible on x-large screens)
2773 */
2774 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002775 if (LauncherApplication.isScreenXLarge()) {
2776 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2777 // Find the app market activity by resolving an intent.
2778 // (If multiple app markets are installed, it will return the ResolverActivity.)
2779 ComponentName activityName = intent.resolveActivity(getPackageManager());
2780 if (activityName != null) {
2781 mAppMarketIntent = intent;
2782 updateButtonWithIconFromExternalActivity(
2783 R.id.market_button, activityName, R.drawable.app_market_generic);
2784 }
2785 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002786 }
2787
2788 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 * Displays the shortcut creation dialog and launches, if necessary, the
2790 * appropriate activity.
2791 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002792 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002793 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2794 DialogInterface.OnShowListener {
2795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002797
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2802 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002803 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 builder.setInverseBackgroundForced(true);
2806
2807 AlertDialog dialog = builder.create();
2808 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002809 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002810 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 return dialog;
2813 }
2814
2815 public void onCancel(DialogInterface dialog) {
2816 mWaitingForResult = false;
2817 cleanup();
2818 }
2819
Romain Guycbb89e42009-06-08 15:52:54 -07002820 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002821 }
2822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002824 try {
2825 dismissDialog(DIALOG_CREATE_SHORTCUT);
2826 } catch (Exception e) {
2827 // An exception is thrown if the dialog is not visible, which is fine
2828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 }
2830
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002831 /**
2832 * Handle the action clicked in the "Add to home" dialog.
2833 */
2834 public void onClick(DialogInterface dialog, int which) {
2835 Resources res = getResources();
2836 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002837
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002838 switch (which) {
2839 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002840 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002841 break;
2842 }
Romain Guycbb89e42009-06-08 15:52:54 -07002843
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002844 case AddAdapter.ITEM_APPWIDGET: {
2845 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002846
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002847 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2848 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002849 // start the pick activity
2850 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2851 break;
2852 }
Romain Guycbb89e42009-06-08 15:52:54 -07002853
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002854 case AddAdapter.ITEM_LIVE_FOLDER: {
2855 // Insert extra item to handle inserting folder
2856 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002857
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002858 ArrayList<String> shortcutNames = new ArrayList<String>();
2859 shortcutNames.add(res.getString(R.string.group_folder));
2860 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002861
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002862 ArrayList<ShortcutIconResource> shortcutIcons =
2863 new ArrayList<ShortcutIconResource>();
2864 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2865 R.drawable.ic_launcher_folder));
2866 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2867
2868 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2869 pickIntent.putExtra(Intent.EXTRA_INTENT,
2870 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2871 pickIntent.putExtra(Intent.EXTRA_TITLE,
2872 getText(R.string.title_select_live_folder));
2873 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002874
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002875 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2876 break;
2877 }
2878
2879 case AddAdapter.ITEM_WALLPAPER: {
2880 startWallpaper();
2881 break;
2882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 }
2884 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002885
2886 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002887 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002889 }
2890
2891 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002892 * Receives notifications when applications are added/removed.
2893 */
2894 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2895 @Override
2896 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002897 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002898 String reason = intent.getStringExtra("reason");
2899 if (!"homekey".equals(reason)) {
2900 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002901 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002902 animate = false;
2903 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002904 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002905 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002906 }
2907 }
2908
2909 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002910 * Receives notifications whenever the appwidgets are reset.
2911 */
2912 private class AppWidgetResetObserver extends ContentObserver {
2913 public AppWidgetResetObserver() {
2914 super(new Handler());
2915 }
2916
2917 @Override
2918 public void onChange(boolean selfChange) {
2919 onAppWidgetReset();
2920 }
2921 }
2922
2923 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002924 * If the activity is currently paused, signal that we need to re-run the loader
2925 * in onResume.
2926 *
2927 * This needs to be called from incoming places where resources might have been loaded
2928 * while we are paused. That is becaues the Configuration might be wrong
2929 * when we're not running, and if it comes back to what it was when we
2930 * were paused, we are not restarted.
2931 *
2932 * Implementation of the method from LauncherModel.Callbacks.
2933 *
2934 * @return true if we are currently paused. The caller might be able to
2935 * skip some work in that case since we will come back again.
2936 */
2937 public boolean setLoadOnResume() {
2938 if (mPaused) {
2939 Log.i(TAG, "setLoadOnResume");
2940 mOnResumeNeedsLoad = true;
2941 return true;
2942 } else {
2943 return false;
2944 }
2945 }
2946
2947 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002951 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002952 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002953 } else {
2954 return SCREEN_COUNT / 2;
2955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002957
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002958 void setAllAppsPagedView(PagedView view) {
2959 mAllAppsPagedView = view;
2960 }
2961
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 /**
2963 * Refreshes the shortcuts shown on the workspace.
2964 *
2965 * Implementation of the method from LauncherModel.Callbacks.
2966 */
2967 public void startBinding() {
2968 final Workspace workspace = mWorkspace;
2969 int count = workspace.getChildCount();
2970 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002971 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2973 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002974
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 if (DEBUG_USER_INTERFACE) {
2976 android.widget.Button finishButton = new android.widget.Button(this);
2977 finishButton.setText("Finish");
2978 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2979
2980 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2981 public void onClick(View v) {
2982 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 }
2986 }
2987
2988 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002989 * Bind the items start-end from the list.
2990 *
2991 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002993 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2994
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002995 setLoadOnResume();
2996
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 final Workspace workspace = mWorkspace;
2998
2999 for (int i=start; i<end; i++) {
3000 final ItemInfo item = shortcuts.get(i);
3001 mDesktopItems.add(item);
3002 switch (item.itemType) {
3003 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3004 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003005 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003006 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3007 false);
3008 break;
3009 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3010 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003011 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003012 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3014 false);
3015 break;
3016 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3017 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3018 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003019 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 (LiveFolderInfo) item);
3021 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3022 false);
3023 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 }
3026
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 /**
3031 * Implementation of the method from LauncherModel.Callbacks.
3032 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003033 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003034 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003035 sFolders.clear();
3036 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038
3039 /**
3040 * Add the views for a widget to the workspace.
3041 *
3042 * Implementation of the method from LauncherModel.Callbacks.
3043 */
3044 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003045 setLoadOnResume();
3046
Daniel Sandler843e8602010-06-07 14:59:01 -04003047 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3048 if (DEBUG_WIDGETS) {
3049 Log.d(TAG, "bindAppWidget: " + item);
3050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 final Workspace workspace = mWorkspace;
3052
3053 final int appWidgetId = item.appWidgetId;
3054 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003055 if (DEBUG_WIDGETS) {
3056 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3057 }
3058
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3060
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3062 item.hostView.setTag(item);
3063
3064 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3065 item.cellY, item.spanX, item.spanY, false);
3066
3067 workspace.requestLayout();
3068
3069 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003070
3071 if (DEBUG_WIDGETS) {
3072 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3073 + (SystemClock.uptimeMillis()-start) + "ms");
3074 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 }
3076
3077 /**
3078 * Callback saying that there aren't any more items to bind.
3079 *
3080 * Implementation of the method from LauncherModel.Callbacks.
3081 */
3082 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003083 setLoadOnResume();
3084
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 if (mSavedState != null) {
3086 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003087 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 }
3089
3090 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3091 if (userFolders != null) {
3092 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003093 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 if (info != null) {
3095 openFolder(info);
3096 }
3097 }
3098 final Folder openFolder = mWorkspace.getOpenFolder();
3099 if (openFolder != null) {
3100 openFolder.requestFocus();
3101 }
3102 }
3103
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 mSavedState = null;
3105 }
3106
3107 if (mSavedInstanceState != null) {
3108 super.onRestoreInstanceState(mSavedInstanceState);
3109 mSavedInstanceState = null;
3110 }
3111
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 mWorkspaceLoading = false;
3113 }
3114
3115 /**
3116 * Add the icons for all apps.
3117 *
3118 * Implementation of the method from LauncherModel.Callbacks.
3119 */
3120 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003121 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003122 if (mCustomizePagedView != null) {
3123 mCustomizePagedView.setApps(apps);
3124 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003125 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003126 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 }
3128
3129 /**
3130 * A package was installed.
3131 *
3132 * Implementation of the method from LauncherModel.Callbacks.
3133 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003134 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003135 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003137 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003138 if (mCustomizePagedView != null) {
3139 mCustomizePagedView.addApps(apps);
3140 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003141 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003142 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 }
3144
3145 /**
3146 * A package was updated.
3147 *
3148 * Implementation of the method from LauncherModel.Callbacks.
3149 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003150 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003151 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003153 mWorkspace.updateShortcuts(apps);
3154 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003155 if (mCustomizePagedView != null) {
3156 mCustomizePagedView.updateApps(apps);
3157 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003158 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003159 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003160 }
3161
3162 /**
3163 * A package was uninstalled.
3164 *
3165 * Implementation of the method from LauncherModel.Callbacks.
3166 */
Joe Onorato36115782010-06-17 13:28:48 -04003167 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003169 if (permanent) {
3170 mWorkspace.removeItems(apps);
3171 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003172 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003173 if (mCustomizePagedView != null) {
3174 mCustomizePagedView.removeApps(apps);
3175 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003176 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003177 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 }
Joe Onoratobe386092009-11-17 17:32:16 -08003179
3180 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003181 * A number of packages were updated.
3182 */
3183 public void bindPackagesUpdated() {
3184 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003185 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003186 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003187 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003188 }
3189
3190 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003191 * Prints out out state for debugging.
3192 */
3193 public void dumpState() {
3194 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003195 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003196 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3197 Log.d(TAG, "mRestoring=" + mRestoring);
3198 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3199 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3200 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003201 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003202 mModel.dumpState();
3203 mAllAppsGrid.dumpState();
3204 Log.d(TAG, "END launcher2 dump state");
3205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206}