blob: 6b1290de898b296a3b923b823ac36acafb5a9fd4 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import com.android.common.Search;
21import com.android.launcher.R;
22
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070025import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070026import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
32import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070033import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Romain Guy5bbc91b2010-08-18 11:38:46 -070043import android.content.IntentFilter;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070044import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040047import android.content.pm.ResolveInfo;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040051import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080054import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080065import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070066import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
70import android.text.TextUtils;
71import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080074import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
81import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070082import android.view.WindowManager;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070083import android.view.View.OnLongClickListener;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070084import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
89import android.widget.LinearLayout;
90import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070091import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.widget.TextView;
93import android.widget.Toast;
Michael Jurka3c4c20f2010-10-28 15:36:06 -070094import android.widget.TabHost.OnTabChangeListener;
95import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
97import java.io.DataInputStream;
98import java.io.DataOutputStream;
99import java.io.FileNotFoundException;
100import java.io.IOException;
101import java.util.ArrayList;
102import java.util.HashMap;
103import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105/**
106 * Default launcher application.
107 */
Michael Jurka946ad472010-07-09 18:05:18 -0700108public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700109 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
110 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800111 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700112 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400115 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800119 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700122 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
123 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
125 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700126 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 private static final int REQUEST_CREATE_SHORTCUT = 1;
129 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int REQUEST_PICK_APPLICATION = 6;
132 private static final int REQUEST_PICK_SHORTCUT = 7;
133 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato7c312c12009-08-13 21:36:53 -0700142 static final int DIALOG_CREATE_SHORTCUT = 1;
143 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: long
152 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 // tags for the customization tabs
165 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700166 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700167 private static final String SHORTCUTS_TAG = "shortcuts";
168 private static final String WALLPAPERS_TAG = "wallpapers";
169
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700170 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
171
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700172 /** The different states that Launcher can be in. */
173 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700174 private State mState = State.WORKSPACE;
175 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800180 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800182 private final BroadcastReceiver mCloseSystemDialogsReceiver
183 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800184 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private LayoutInflater mInflater;
187
Joe Onorato00acb122009-08-04 16:04:30 -0400188 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700190
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private AppWidgetManager mAppWidgetManager;
192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 private int mAddScreen = -1;
195 private int mAddIntersectCellX = -1;
196 private int mAddIntersectCellY = -1;
197 private int[] mAddDropPosition;
198 private int[] mTmpAddItemCellCoordinates = new int[2];
199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private FolderInfo mFolderInfo;
201
Joe Onorato7c312c12009-08-13 21:36:53 -0700202 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700204 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700205 private TabHost mHomeCustomizationDrawer;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700206
207 private PagedView mAllAppsPagedView = null;
208 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private Bundle mSavedState;
211
212 private SpannableStringBuilder mDefaultKeySsb = null;
213
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 private boolean mWorkspaceLoading = true;
215
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800216 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private boolean mRestoring;
218 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700219 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
221 private Bundle mSavedInstanceState;
222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800224 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700226 private static LocaleConfiguration sLocaleConfiguration = null;
227
Romain Guy84f296c2009-11-04 15:00:44 -0800228 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700229 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Romain Guy1fbc1c82009-11-09 20:43:08 -0800231 private ImageView mPreviousView;
232 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500233
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400234 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400235 private String[] mHotseatConfig = null;
236 private Intent[] mHotseats = null;
237 private Drawable[] mHotseatIcons = null;
238 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400239
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700240 private Intent mAppMarketIntent = null;
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 @Override
243 protected void onCreate(Bundle savedInstanceState) {
244 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700245
Winson Chungaafa03c2010-06-11 17:34:16 -0700246 if (LauncherApplication.isInPlaceRotationEnabled()) {
247 // hide the status bar (temporary until we get the status bar design figured out)
248 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
249 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
250 }
251
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 LauncherApplication app = ((LauncherApplication)getApplication());
253 mModel = app.setLauncher(this);
254 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400255 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700257
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700258 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
260 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200263 android.os.Debug.startMethodTracing(
264 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
266
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400267 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 checkForLocaleChange();
269 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700271 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
272 if (mHomeCustomizationDrawer != null) {
273 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700274
Winson Chung80baf5a2010-08-09 16:03:15 -0700275 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700276 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
277 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700278 TabContentFactory contentFactory = new TabContentFactory() {
279 public View createTabContent(String tag) {
280 return mCustomizePagedView;
281 }
282 };
283
Michael Jurka946ad472010-07-09 18:05:18 -0700284 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700285 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
286 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700287 String applicationsLabel = getString(R.string.applications_tab_label);
288 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
289 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700290 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700291 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
292 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700293 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
294 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
295 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700296
Winson Chung80baf5a2010-08-09 16:03:15 -0700297 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
298 public void onTabChanged(String tabId) {
299 // animate the changing of the tab content by fading pages in and out
300 final int duration = 150;
301 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700302 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700303 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700304 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700305 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700306 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700307 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700308 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
309 if (tag == WIDGETS_TAG) {
310 mCustomizePagedView.setCustomizationFilter(
311 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700312 } else if (tag == APPLICATIONS_TAG) {
313 mCustomizePagedView.setCustomizationFilter(
314 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 } else if (tag == WALLPAPERS_TAG) {
316 mCustomizePagedView.setCustomizationFilter(
317 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700318 } else if (tag == SHORTCUTS_TAG) {
319 mCustomizePagedView.setCustomizationFilter(
320 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 }
322
323 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700324 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700325 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700326 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700327 alphaAnim.start();
328 }
329 });
330 alphaAnim.start();
331 }
332 });
Michael Jurka946ad472010-07-09 18:05:18 -0700333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 setupViews();
335
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800336 registerContentObservers();
337
Joe Onorato7c312c12009-08-13 21:36:53 -0700338 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 mSavedState = savedInstanceState;
341 restoreState(mSavedState);
342
343 if (PROFILE_STARTUP) {
344 android.os.Debug.stopMethodTracing();
345 }
346
347 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400348 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 }
350
351 // For handling default keys
352 mDefaultKeySsb = new SpannableStringBuilder();
353 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800354
355 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
356 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
Romain Guycbb89e42009-06-08 15:52:54 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 if (sLocaleConfiguration == null) {
361 new AsyncTask<Void, Void, LocaleConfiguration>() {
362 @Override
363 protected LocaleConfiguration doInBackground(Void... unused) {
364 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
365 readConfiguration(Launcher.this, localeConfiguration);
366 return localeConfiguration;
367 }
368
369 @Override
370 protected void onPostExecute(LocaleConfiguration result) {
371 sLocaleConfiguration = result;
372 checkForLocaleChange(); // recursive, but now with a locale configuration
373 }
374 }.execute();
375 return;
376 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 final Configuration configuration = getResources().getConfiguration();
379
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 final String locale = configuration.locale.toString();
382
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700383 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 final int mcc = configuration.mcc;
385
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700386 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 final int mnc = configuration.mnc;
388
Romain Guy84f296c2009-11-04 15:00:44 -0800389 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390
Romain Guy84f296c2009-11-04 15:00:44 -0800391 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700392 sLocaleConfiguration.locale = locale;
393 sLocaleConfiguration.mcc = mcc;
394 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700395
Joe Onorato0589f0f2010-02-08 13:44:00 -0800396 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400397 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700398
399 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
400 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700401 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 public void run() {
403 writeConfiguration(Launcher.this, localeConfiguration);
404 }
405 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700406 }
407 }
408
409 private static class LocaleConfiguration {
410 public String locale;
411 public int mcc = -1;
412 public int mnc = -1;
413 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700414
Romain Guy98d01652009-06-30 16:21:04 -0700415 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
416 DataInputStream in = null;
417 try {
418 in = new DataInputStream(context.openFileInput(PREFERENCES));
419 configuration.locale = in.readUTF();
420 configuration.mcc = in.readInt();
421 configuration.mnc = in.readInt();
422 } catch (FileNotFoundException e) {
423 // Ignore
424 } catch (IOException e) {
425 // Ignore
426 } finally {
427 if (in != null) {
428 try {
429 in.close();
430 } catch (IOException e) {
431 // Ignore
432 }
433 }
434 }
435 }
436
437 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
438 DataOutputStream out = null;
439 try {
440 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
441 out.writeUTF(configuration.locale);
442 out.writeInt(configuration.mcc);
443 out.writeInt(configuration.mnc);
444 out.flush();
445 } catch (FileNotFoundException e) {
446 // Ignore
447 } catch (IOException e) {
448 //noinspection ResultOfMethodCallIgnored
449 context.getFileStreamPath(PREFERENCES).delete();
450 } finally {
451 if (out != null) {
452 try {
453 out.close();
454 } catch (IOException e) {
455 // Ignore
456 }
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
459 }
460
461 static int getScreen() {
462 synchronized (sLock) {
463 return sScreen;
464 }
465 }
466
467 static void setScreen(int screen) {
468 synchronized (sLock) {
469 sScreen = screen;
470 }
471 }
472
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700474 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475
476 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700477 // TODO: Put back when we decide about scrolling the wallpaper
478 // boolean isPortrait = display.getWidth() < display.getHeight();
479 // final int width = isPortrait ? display.getWidth() : display.getHeight();
480 // final int height = isPortrait ? display.getHeight() : display.getWidth();
481 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
482 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400485 // Note: This doesn't do all the client-id magic that BrowserProvider does
486 // in Browser. (http://b/2425179)
487 private Uri getDefaultBrowserUri() {
488 String url = getString(R.string.default_browser_url);
489 if (url.indexOf("{CID}") != -1) {
490 url = url.replace("{CID}", "android-google");
491 }
492 return Uri.parse(url);
493 }
494
495 // Load the Intent templates from arrays.xml to populate the hotseats. For
496 // each Intent, if it resolves to a single app, use that as the launch
497 // intent & use that app's label as the contentDescription. Otherwise,
498 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400499 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400500 if (mHotseatConfig == null) {
501 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
502 if (mHotseatConfig.length > 0) {
503 mHotseats = new Intent[mHotseatConfig.length];
504 mHotseatLabels = new CharSequence[mHotseatConfig.length];
505 mHotseatIcons = new Drawable[mHotseatConfig.length];
506 } else {
507 mHotseats = null;
508 mHotseatIcons = null;
509 mHotseatLabels = null;
510 }
511
512 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
513 for (int i=0; i<mHotseatConfig.length; i++) {
514 // load icon for this slot; currently unrelated to the actual activity
515 try {
516 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
517 } catch (ArrayIndexOutOfBoundsException ex) {
518 Log.w(TAG, "Missing hotseat_icons array item #" + i);
519 mHotseatIcons[i] = null;
520 }
521 }
522 hotseatIconDrawables.recycle();
523 }
524
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400525 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400526 for (int i=0; i<mHotseatConfig.length; i++) {
527 Intent intent = null;
528 if (mHotseatConfig[i].equals("*BROWSER*")) {
529 // magic value meaning "launch user's default web browser"
530 // replace it with a generic web request so we can see if there is indeed a default
531 String defaultUri = getString(R.string.default_browser_url);
532 intent = new Intent(
533 Intent.ACTION_VIEW,
534 ((defaultUri != null)
535 ? Uri.parse(defaultUri)
536 : getDefaultBrowserUri())
537 ).addCategory(Intent.CATEGORY_BROWSABLE);
538 // note: if the user launches this without a default set, she
539 // will always be taken to the default URL above; this is
540 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400542 // URL is unavoidably sent to the chosen app.
543 } else {
544 try {
545 intent = Intent.parseUri(mHotseatConfig[i], 0);
546 } catch (java.net.URISyntaxException ex) {
547 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
548 // bogus; leave intent=null
549 }
550 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700551
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400552 if (intent == null) {
553 mHotseats[i] = null;
554 mHotseatLabels[i] = getText(R.string.activity_not_found);
555 continue;
556 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400557
558 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700559 Log.d(TAG, "loadHotseats: hotseat " + i
560 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400562 + "]");
563 }
564
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400565 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
566 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700567 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 Log.d(TAG, "Best match for intent: " + bestMatch);
569 Log.d(TAG, "All matches: ");
570 for (ResolveInfo ri : allMatches) {
571 Log.d(TAG, " --> " + ri);
572 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 // did this resolve to a single app, or the resolver?
575 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700576 // can't find any activity to handle this. let's leave the
577 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400578 // to launch.
579 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400581 // set accessibility text to "Not installed"
582 mHotseatLabels[i] = getText(R.string.activity_not_found);
583 } else {
584 boolean found = false;
585 for (ResolveInfo ri : allMatches) {
586 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
587 && bestMatch.activityInfo.applicationInfo.packageName
588 .equals(ri.activityInfo.applicationInfo.packageName)) {
589 found = true;
590 break;
591 }
592 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700593
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400594 if (!found) {
595 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
596 // the bestMatch is probably the ResolveActivity, meaning the
597 // user has not yet selected a default
598 // so: we'll keep the original intent for now
599 mHotseats[i] = intent;
600
601 // set the accessibility text to "Select shortcut"
602 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
603 } else {
604 // we have an app!
605 // now reconstruct the intent to launch it through the front
606 // door
607 ComponentName com = new ComponentName(
608 bestMatch.activityInfo.applicationInfo.packageName,
609 bestMatch.activityInfo.name);
610 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
611
612 // load the app label for accessibility
613 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
614 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400615 }
616
617 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700618 Log.d(TAG, "loadHotseats: hotseat " + i
619 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400620 + ((mHotseats[i] == null)
621 ? "null"
622 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400623 + "] label=[" + mHotseatLabels[i]
624 + "]"
625 );
626 }
627 }
628 }
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 @Override
631 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700632 mWaitingForResult = false;
633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // The pattern used here is that a user PICKs a specific application,
635 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
638 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700639
Michael Jurka0280c3b2010-09-17 15:00:07 -0700640 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 switch (requestCode) {
642 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700643 completeAddApplication(
644 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 break;
646 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800647 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 break;
649 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700650 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 break;
652 case REQUEST_PICK_LIVE_FOLDER:
653 addLiveFolder(data);
654 break;
655 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700658 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700659 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700661 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700662 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700665 case REQUEST_PICK_WALLPAPER:
666 // We just wanted the activity result here so we can clear mWaitingForResult
667 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Romain Guy18042c82009-11-06 11:44:55 -0800669 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
670 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
671 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700672 // Clean up the appWidgetId if we canceled
673 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
674 if (appWidgetId != -1) {
675 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
679
680 @Override
681 protected void onResume() {
682 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800683 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700684 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 mWorkspaceLoading = true;
686 mModel.startLoader(this, true);
687 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700688 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700690 // When we resume Launcher, a different Activity might be responsible for the app
691 // market intent, so refresh the icon
692 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
694
695 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 protected void onPause() {
697 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700698 // Some launcher layouts don't have a previous and next view
699 if (mPreviousView != null) {
700 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700701 }
702 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700703 dismissPreview(mNextView);
704 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700705 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800706 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700707 }
Romain Guycbb89e42009-06-08 15:52:54 -0700708
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700709 @Override
710 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 // Flag the loader to stop early before switching
712 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800713 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800714 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700715 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800717 // We can't hide the IME if it was forced open. So don't bother
718 /*
719 @Override
720 public void onWindowFocusChanged(boolean hasFocus) {
721 super.onWindowFocusChanged(hasFocus);
722
723 if (hasFocus) {
724 final InputMethodManager inputManager = (InputMethodManager)
725 getSystemService(Context.INPUT_METHOD_SERVICE);
726 WindowManager.LayoutParams lp = getWindow().getAttributes();
727 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
728 android.os.Handler()) {
729 protected void onReceiveResult(int resultCode, Bundle resultData) {
730 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
731 }
732 });
733 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
734 }
735 }
736 */
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 private boolean acceptFilter() {
739 final InputMethodManager inputManager = (InputMethodManager)
740 getSystemService(Context.INPUT_METHOD_SERVICE);
741 return !inputManager.isFullscreenMode();
742 }
743
744 @Override
745 public boolean onKeyDown(int keyCode, KeyEvent event) {
746 boolean handled = super.onKeyDown(keyCode, event);
747 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
748 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
749 keyCode, event);
750 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700751 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700752 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700753 // showSearchDialog()
754 // If there are multiple keystrokes before the search dialog takes focus,
755 // onSearchRequested() will be called for every keystroke,
756 // but it is idempotent, so it's fine.
757 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759 }
760
Joe Onorato8a9625e2010-01-28 15:55:35 -0800761 // Eat the long press event so the keyboard doesn't come up.
762 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
763 return true;
764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 return handled;
767 }
768
Karl Rosaen138a0412009-04-23 19:00:21 -0700769 private String getTypedText() {
770 return mDefaultKeySsb.toString();
771 }
772
773 private void clearTypedText() {
774 mDefaultKeySsb.clear();
775 mDefaultKeySsb.clearSpans();
776 Selection.setSelection(mDefaultKeySsb, 0);
777 }
778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700780 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
781 * State
782 */
783 private static State intToState(int stateOrdinal) {
784 State state = State.WORKSPACE;
785 final State[] stateValues = State.values();
786 for (int i = 0; i < stateValues.length; i++) {
787 if (stateValues[i].ordinal() == stateOrdinal) {
788 state = stateValues[i];
789 break;
790 }
791 }
792 return state;
793 }
794
795 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 * Restores the previous state, if it exists.
797 *
798 * @param savedState The previous state.
799 */
800 private void restoreState(Bundle savedState) {
801 if (savedState == null) {
802 return;
803 }
804
Michael Jurka883f55b2010-10-21 15:47:14 -0700805 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
806
807 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800808 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700809 } else if (state == State.CUSTOMIZE) {
810 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800811 }
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
814 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700815 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 }
817
818 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700819
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821 mAddScreen = addScreen;
822 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
823 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 mRestoring = true;
825 }
826
827 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
828 if (renameFolder) {
829 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700830 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 mRestoring = true;
832 }
833 }
834
835 /**
836 * Finds all the views we need and configure them properly.
837 */
838 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700839 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400840
Romain Guyb1b69f52009-08-10 15:10:15 -0700841 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400842 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
Joe Onorato7c312c12009-08-13 21:36:53 -0700844 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700845 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700846 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800847 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700848 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700849 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700850
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700851 if (LauncherApplication.isScreenXLarge()) {
852 // They need to be INVISIBLE initially so that they will be measured in the layout.
853 // Otherwise the animations are messed up when we show them for the first time.
854 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
855 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
859 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500860 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700861
Joe Onorato7c312c12009-08-13 21:36:53 -0700862 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
863 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700865 View handleView = findViewById(R.id.all_apps_button);
866 if (handleView != null && handleView instanceof HandleView) {
867 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700868 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700869 mHandleView.setLauncher(this);
870 mHandleView.setOnClickListener(this);
871 mHandleView.setOnLongClickListener(this);
872 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800873
Winson Chung80baf5a2010-08-09 16:03:15 -0700874 if (mCustomizePagedView != null) {
875 mCustomizePagedView.setLauncher(this);
876 mCustomizePagedView.setDragController(dragController);
877 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700878 } else {
879 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
880 hotseatLeft.setContentDescription(mHotseatLabels[0]);
881 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
882 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
883 hotseatRight.setContentDescription(mHotseatLabels[1]);
884 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400885
Michael Jurkaaf442092010-06-10 17:01:57 -0700886 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
887 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800888
Michael Jurkaaf442092010-06-10 17:01:57 -0700889 Drawable previous = mPreviousView.getDrawable();
890 Drawable next = mNextView.getDrawable();
891 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892
Michael Jurkaaf442092010-06-10 17:01:57 -0700893 mPreviousView.setHapticFeedbackEnabled(false);
894 mPreviousView.setOnLongClickListener(this);
895 mNextView.setHapticFeedbackEnabled(false);
896 mNextView.setOnLongClickListener(this);
897 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400900 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
903 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400904 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700905 int deleteZoneHandleId;
906 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700907 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700908 } else {
909 deleteZoneHandleId = R.id.all_apps_button_cluster;
910 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700911 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700912 dragController.addDragListener(deleteZone);
913
914 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
915 if (infoButton != null) {
916 infoButton.setLauncher(this);
917 infoButton.setHandle(findViewById(R.id.configure_button));
918 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
919 dragController.addDragListener(infoButton);
920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921
Joe Onorato00acb122009-08-04 16:04:30 -0400922 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400923 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800924 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700925
Joe Onorato00acb122009-08-04 16:04:30 -0400926 // The order here is bottom to top.
927 dragController.addDropTarget(workspace);
928 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700929 if (infoButton != null) {
930 dragController.addDropTarget(infoButton);
931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
Romain Guy8a73c512009-11-09 19:19:59 -0800934 @SuppressWarnings({"UnusedDeclaration"})
935 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700936 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800937 mWorkspace.scrollLeft();
938 }
Romain Guy8a73c512009-11-09 19:19:59 -0800939 }
940
941 @SuppressWarnings({"UnusedDeclaration"})
942 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700943 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800944 mWorkspace.scrollRight();
945 }
Romain Guy8a73c512009-11-09 19:19:59 -0800946 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400947
948 @SuppressWarnings({"UnusedDeclaration"})
949 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700950 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400951
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400952 int index = -1;
953 if (v.getId() == R.id.hotseat_left) {
954 index = 0;
955 } else if (v.getId() == R.id.hotseat_right) {
956 index = 1;
957 }
958
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400959 // reload these every tap; you never know when they might change
960 loadHotseats();
961 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
962 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400963 startActivitySafely(
964 mHotseats[index],
965 "hotseat"
966 );
967 }
968 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700969
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 /**
971 * Creates a view representing a shortcut.
972 *
973 * @param info The data structure describing the shortcut.
974 *
975 * @return A View inflated from R.layout.application.
976 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800977 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700979 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981
982 /**
983 * Creates a view representing a shortcut inflated from the specified resource.
984 *
985 * @param layoutResId The id of the XML layout used to create the shortcut.
986 * @param parent The group the shortcut belongs to.
987 * @param info The data structure describing the shortcut.
988 *
989 * @return A View inflated from layoutResId.
990 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800991 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
993
Patrick Dubroy9e482382010-11-01 15:16:13 -0700994 Bitmap b = info.getIcon(mIconCache);
995
996 if (LauncherApplication.isScreenXLarge()) {
997 // Temporarily, we are scaling up all shortcuts on the workspace
998 int scaledSize = getResources().getDimensionPixelSize(R.dimen.temp_scaled_icon_size);
999 b = Bitmap.createScaledBitmap(b, scaledSize, scaledSize, true);
1000 }
Winson Chung0e41ae62010-10-27 18:10:32 -07001001
Joe Onorato0589f0f2010-02-08 13:44:00 -08001002 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -07001003 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001004 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 favorite.setText(info.title);
1006 favorite.setTag(info);
1007 favorite.setOnClickListener(this);
1008
1009 return favorite;
1010 }
1011
1012 /**
1013 * Add an application shortcut to the workspace.
1014 *
1015 * @param data The intent describing the application.
1016 * @param cellInfo The position on screen where to create the shortcut.
1017 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001018 void completeAddApplication(Context context, Intent data, int screen,
1019 int intersectCellX, int intersectCellY) {
1020 final int[] cellXY = mTmpAddItemCellCoordinates;
1021 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1022
1023 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1024 showOutOfSpaceMessage();
1025 return;
1026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027
Joe Onorato0589f0f2010-02-08 13:44:00 -08001028 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1029 data, context);
1030
Romain Guy73b979d2009-06-09 12:57:21 -07001031 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001035 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1036 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001037 } else {
1038 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 /**
1043 * Add a shortcut to the workspace.
1044 *
1045 * @param data The intent describing the shortcut.
1046 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001048 private void completeAddShortcut(Intent data, int screen,
1049 int intersectCellX, int intersectCellY) {
1050 final int[] cellXY = mTmpAddItemCellCoordinates;
1051 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Michael Jurka0280c3b2010-09-17 15:00:07 -07001053 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1054 showOutOfSpaceMessage();
1055 return;
1056 }
1057
1058 final ShortcutInfo info = mModel.addShortcut(
1059 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060
1061 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065 }
1066
Romain Guycbb89e42009-06-08 15:52:54 -07001067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001071 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1079 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001082 // We have saved the position to which the widget was dragged-- this really only matters
1083 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001085
1086 // For now, we don't save the coordinate where we dropped the icon because we're not
1087 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1088 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001089 int[] touchXY = null;
1090 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1091 touchXY = mAddDropPosition;
1092 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001093 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094 boolean foundCellSpan = false;
1095 if (touchXY != null) {
1096 // when dragging and dropping, just find the closest free spot
1097 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1098 int[] result = screenLayout.findNearestVacantArea(
1099 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001100 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 foundCellSpan = !findNearestVacantAreaFailed;
1102 } else {
1103 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1104 // if we long pressed on an empty cell to bring up a menu,
1105 // make sure we intersect the empty cell
1106 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1107 mAddIntersectCellX, mAddIntersectCellY);
1108 } else {
1109 // if we went through the menu -> add, just find any spot
1110 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1111 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001112 }
1113
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001115 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001117 return;
1118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001120 // Build Launcher-specific widget info and save to database
1121 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001122 launcherInfo.spanX = spanXY[0];
1123 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 LauncherModel.addItemToDatabase(this, launcherInfo,
1126 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128
1129 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001133 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001135 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001137
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141 }
Romain Guycbb89e42009-06-08 15:52:54 -07001142
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 void showOutOfSpaceMessage() {
1144 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1145 }
1146
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1148 mDesktopItems.remove(launcherInfo);
1149 launcherInfo.hostView = null;
1150 }
1151
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001152 public LauncherAppWidgetHost getAppWidgetHost() {
1153 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
Romain Guycbb89e42009-06-08 15:52:54 -07001155
Winson Chunga9abd0e2010-10-27 17:18:37 -07001156 public LauncherModel getModel() {
1157 return mModel;
1158 }
1159
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001160 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001161 getWindow().closeAllPanels();
1162
1163 try {
1164 dismissDialog(DIALOG_CREATE_SHORTCUT);
1165 // Unlock the workspace if the dialog was showing
1166 } catch (Exception e) {
1167 // An exception is thrown if the dialog is not visible, which is fine
1168 }
1169
1170 try {
1171 dismissDialog(DIALOG_RENAME_FOLDER);
1172 // Unlock the workspace if the dialog was showing
1173 } catch (Exception e) {
1174 // An exception is thrown if the dialog is not visible, which is fine
1175 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001176
1177 // Whatever we were doing is hereby canceled.
1178 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001179 }
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 @Override
1182 protected void onNewIntent(Intent intent) {
1183 super.onNewIntent(intent);
1184
1185 // Close the menu
1186 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001187 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001188 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001189
Joe Onorato14f122b2009-11-19 14:06:36 -08001190 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1191 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001192
Michael Jurka4cb37242010-08-09 21:05:32 -07001193 // in all these cases, only animate if we're already on home
1194 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001195 mWorkspace.unshrink(alreadyOnHome);
1196 }
1197 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001198 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001199 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1200 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001201 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001202 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001203
Joe Onorato3a8820b2009-11-10 15:06:42 -08001204 final View v = getWindow().peekDecorView();
1205 if (v != null && v.getWindowToken() != null) {
1206 InputMethodManager imm = (InputMethodManager)getSystemService(
1207 INPUT_METHOD_SERVICE);
1208 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210 }
1211 }
1212
1213 @Override
1214 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1215 // Do not call super here
1216 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001217
1218 if (mHomeCustomizationDrawer != null) {
1219 String cur = savedInstanceState.getString("currentTab");
1220 if (cur != null) {
1221 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1222 }
1223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
1226 @Override
1227 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001228 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
1230 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1231 if (folders.size() > 0) {
1232 final int count = folders.size();
1233 long[] ids = new long[count];
1234 for (int i = 0; i < count; i++) {
1235 final FolderInfo info = folders.get(i).getInfo();
1236 ids[i] = info.id;
1237 }
1238 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1239 } else {
1240 super.onSaveInstanceState(outState);
1241 }
1242
Michael Jurka883f55b2010-10-21 15:47:14 -07001243 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 if (mAddScreen > -1 && mWaitingForResult) {
1246 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1247 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1248 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
1251 if (mFolderInfo != null && mWaitingForResult) {
1252 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1253 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1254 }
Michael Jurka946ad472010-07-09 18:05:18 -07001255
1256 if (mHomeCustomizationDrawer != null) {
1257 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1258 if (currentTabTag != null) {
1259 outState.putString("currentTab", currentTabTag);
1260 }
1261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263
1264 @Override
1265 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001269 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001271 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
1273
1274 TextKeyListener.getInstance().release();
1275
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001279
1280 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001281
Patrick Dubroyab962b72010-07-26 12:10:10 -07001282 // Some launcher layouts don't have a previous and next view
1283 if (mPreviousView != null) {
1284 dismissPreview(mPreviousView);
1285 }
1286 if (mNextView != null) {
1287 dismissPreview(mNextView);
1288 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001289
1290 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292
1293 @Override
1294 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001295 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 super.startActivityForResult(intent, requestCode);
1297 }
1298
1299 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001300 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001302
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001303 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001304
Karl Rosaen138a0412009-04-23 19:00:21 -07001305 if (initialQuery == null) {
1306 // Use any text typed in the launcher as the initial query
1307 initialQuery = getTypedText();
1308 clearTypedText();
1309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 if (appSearchData == null) {
1311 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001312 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Karl Rosaen138a0412009-04-23 19:00:21 -07001315 final SearchManager searchManager =
1316 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001317 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001318 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 @Override
1322 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001323 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 return false;
1325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
1327 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1330 .setIcon(android.R.drawable.ic_menu_add)
1331 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001332 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1333 .setIcon(android.R.drawable.ic_menu_manage)
1334 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001335 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 .setIcon(android.R.drawable.ic_menu_gallery)
1337 .setAlphabeticShortcut('W');
1338 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1339 .setIcon(android.R.drawable.ic_search_category_default)
1340 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1341 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1342 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1343 .setAlphabeticShortcut('N');
1344
1345 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001346 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1347 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348
1349 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1350 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1351 .setIntent(settings);
1352
1353 return true;
1354 }
1355
1356 @Override
1357 public boolean onPrepareOptionsMenu(Menu menu) {
1358 super.onPrepareOptionsMenu(menu);
1359
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001360 // If all apps is animating, don't show the menu, because we don't know
1361 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001362 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001363 return false;
1364 }
1365
1366 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001367 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001368 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1369 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1370
1371 // Disable add if the workspace is full.
1372 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001373 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1374 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376
1377 return true;
1378 }
1379
1380 @Override
1381 public boolean onOptionsItemSelected(MenuItem item) {
1382 switch (item.getItemId()) {
1383 case MENU_ADD:
1384 addItems();
1385 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001386 case MENU_MANAGE_APPS:
1387 manageApps();
1388 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 case MENU_WALLPAPER_SETTINGS:
1390 startWallpaper();
1391 return true;
1392 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001393 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 return true;
1395 case MENU_NOTIFICATIONS:
1396 showNotifications();
1397 return true;
1398 }
1399
1400 return super.onOptionsItemSelected(item);
1401 }
Romain Guycbb89e42009-06-08 15:52:54 -07001402
Karl Rosaen138a0412009-04-23 19:00:21 -07001403 /**
1404 * Indicates that we want global search for this activity by setting the globalSearch
1405 * argument for {@link #startSearch} to true.
1406 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001408 @Override
1409 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001410 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001411 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001412 }
1413
Joe Onorato9c1289c2009-08-17 11:03:03 -04001414 public boolean isWorkspaceLocked() {
1415 return mWorkspaceLoading || mWaitingForResult;
1416 }
1417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001419 if (LauncherApplication.isScreenXLarge()) {
1420 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001421 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001422 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001423 }
1424 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001425 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001426 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 private void resetAddInfo() {
1431 mAddScreen = -1;
1432 mAddIntersectCellX = -1;
1433 mAddIntersectCellY = -1;
1434 mAddDropPosition = null;
1435 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001436
Winson Chung55cef262010-10-28 14:14:18 -07001437 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001438 resetAddInfo();
1439 mAddScreen = screen;
1440
1441 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1442 mAddDropPosition = position;
1443
Michael Jurkaaf442092010-06-10 17:01:57 -07001444 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001445 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1446 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001447 }
1448
Winson Chung7ad01412010-09-27 11:33:03 -07001449 private void manageApps() {
1450 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1451 }
1452
Michael Jurkaaf442092010-06-10 17:01:57 -07001453 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001454 // TODO: catch bad widget exception when sent
1455 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001456 // TODO: Is this log message meaningful?
1457 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001458 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001459 }
1460
Winson Chung55cef262010-10-28 14:14:18 -07001461 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001462 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463
Bjorn Bringert7984c942009-12-09 15:38:25 +00001464 if (appWidget.configure != null) {
1465 // Launch over to configure widget, if needed
1466 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1467 intent.setComponent(appWidget.configure);
1468 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001469 if (info != null) {
1470 intent.putExtra(InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA,
1471 info.configurationData);
1472 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001473
Romain Guy8e633c52010-03-04 12:51:36 -08001474 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001476 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001477 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479 }
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Michael Jurka0280c3b2010-09-17 15:00:07 -07001481 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1482 resetAddInfo();
1483 mAddScreen = screen;
1484 mAddDropPosition = position;
1485
1486 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1487 createShortcutIntent.setComponent(componentName);
1488 processShortcut(createShortcutIntent);
1489 }
1490
Joe Onoratodeb98af2010-02-19 14:59:39 -08001491 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001492 // Handle case where user selected "Applications"
1493 String applicationName = getResources().getString(R.string.group_applications);
1494 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001495
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001496 if (applicationName != null && applicationName.equals(shortcutName)) {
1497 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1498 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001499
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001500 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1501 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001502 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001503 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001504 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001505 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
Winson Chung24ab2f12010-09-16 14:10:47 -07001509 void processWallpaper(Intent intent) {
1510 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1511 }
1512
Michael Jurka0280c3b2010-09-17 15:00:07 -07001513 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1514 resetAddInfo();
1515 mAddScreen = screen;
1516 mAddDropPosition = position;
1517
1518 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1519 createFolderIntent.setComponent(componentName);
1520
1521 addLiveFolder(createFolderIntent);
1522 }
1523
1524 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001525 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001526 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001527 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001528
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001529 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001530 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001531 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001532 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
Michael Jurka0280c3b2010-09-17 15:00:07 -07001536 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 UserFolderInfo folderInfo = new UserFolderInfo();
1538 folderInfo.title = getText(R.string.folder_name);
1539
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1541 final int[] cellXY = mTmpAddItemCellCoordinates;
1542 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1543 showOutOfSpaceMessage();
1544 return;
1545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
1547 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 LauncherModel.addItemToDatabase(this, folderInfo,
1549 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001550 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001551 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
1553 // Create the view
1554 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001555 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Joe Onorato9c1289c2009-08-17 11:03:03 -04001559 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001560 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 }
1562
Michael Jurka28750fb2010-09-24 17:43:49 -07001563 private void completeAddLiveFolder(
1564 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001565 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1566 final int[] cellXY = mTmpAddItemCellCoordinates;
1567 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1568 showOutOfSpaceMessage();
1569 return;
1570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573
1574 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001576 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001577 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
1579 }
1580
1581 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001582 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583
1584 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1585 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1586
1587 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 Intent.ShortcutIconResource iconResource = null;
1589
1590 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1591 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1592 try {
1593 iconResource = (Intent.ShortcutIconResource) extra;
1594 final PackageManager packageManager = context.getPackageManager();
1595 Resources resources = packageManager.getResourcesForApplication(
1596 iconResource.packageName);
1597 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1598 icon = resources.getDrawable(id);
1599 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001600 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602 }
1603
1604 if (icon == null) {
1605 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1606 }
1607
1608 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001609 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 info.title = name;
1611 info.iconResource = iconResource;
1612 info.uri = data.getData();
1613 info.baseIntent = baseIntent;
1614 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1615 LiveFolders.DISPLAY_MODE_GRID);
1616
1617 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001618 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001619 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620
1621 return info;
1622 }
1623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 private void showNotifications() {
1625 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1626 if (statusBar != null) {
1627 statusBar.expand();
1628 }
1629 }
1630
1631 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001632 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001634 Intent chooser = Intent.createChooser(pickWallpaper,
1635 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001636 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1637 // Removed in Eclair MR1
1638// WallpaperManager wm = (WallpaperManager)
1639// getSystemService(Context.WALLPAPER_SERVICE);
1640// WallpaperInfo wi = wm.getWallpaperInfo();
1641// if (wi != null && wi.getSettingsActivity() != null) {
1642// LabeledIntent li = new LabeledIntent(getPackageName(),
1643// R.string.configure_wallpaper, 0);
1644// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1645// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1646// }
Mike Clerona0618e42009-10-22 13:55:21 -07001647 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
1649
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001650 /**
1651 * Registers various content observers. The current implementation registers
1652 * only a favorites observer to keep track of the favorites applications.
1653 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001654 private void registerContentObservers() {
1655 ContentResolver resolver = getContentResolver();
1656 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1657 true, mWidgetObserver);
1658 }
1659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 @Override
1661 public boolean dispatchKeyEvent(KeyEvent event) {
1662 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1663 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001665 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001666 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001667 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001668 dumpState();
1669 return true;
1670 }
1671 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001672 }
1673 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1674 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001675 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 return true;
1677 }
1678 }
1679
1680 return super.dispatchKeyEvent(event);
1681 }
1682
Joe Onorato88ec0992009-11-19 13:16:06 -08001683 @Override
1684 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001685 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1686 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001687 } else {
1688 closeFolder();
1689 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001690 // Some launcher layouts don't have a previous and next view
1691 if (mPreviousView != null) {
1692 dismissPreview(mPreviousView);
1693 dismissPreview(mNextView);
1694 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001695 }
1696
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 private void closeFolder() {
1698 Folder folder = mWorkspace.getOpenFolder();
1699 if (folder != null) {
1700 closeFolder(folder);
1701 }
1702 }
1703
1704 void closeFolder(Folder folder) {
1705 folder.getInfo().opened = false;
1706 ViewGroup parent = (ViewGroup) folder.getParent();
1707 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001708 CellLayout cl = (CellLayout) parent;
1709 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001710 if (folder instanceof DropTarget) {
1711 // Live folders aren't DropTargets.
1712 mDragController.removeDropTarget((DropTarget)folder);
1713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
1715 folder.onClose();
1716 }
1717
1718 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001719 * Re-listen when widgets are reset.
1720 */
1721 private void onAppWidgetReset() {
1722 mAppWidgetHost.startListening();
1723 }
1724
1725 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001726 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1727 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001729 private void unbindDesktopItems() {
1730 for (ItemInfo item: mDesktopItems) {
1731 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 }
1733 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001734
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 /**
1736 * Launches the intent referred by the clicked shortcut.
1737 *
1738 * @param v The view representing the clicked shortcut.
1739 */
1740 public void onClick(View v) {
1741 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001742 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001744 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001745 int[] pos = new int[2];
1746 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001747 intent.setSourceBounds(new Rect(pos[0], pos[1],
1748 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001749 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 } else if (tag instanceof FolderInfo) {
1751 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001752 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001753 if (mState == State.ALL_APPS) {
1754 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001755 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001756 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
1759 }
1760
Michael Jurka0e260592010-06-30 17:07:39 -07001761 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001762 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001763 // clicking anywhere on the workspace causes the customization drawer to slide down
1764 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001765 return false;
1766 }
1767
Michael Jurkaaf442092010-06-10 17:01:57 -07001768 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001769 * Event handler for the search button
1770 *
1771 * @param v The view that was clicked.
1772 */
1773 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001774 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001775 }
1776
1777 /**
1778 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001779 * enters home screen customization mode.
1780 *
1781 * @param v The view that was clicked.
1782 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001783 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001784 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001785 }
1786
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001787 /**
1788 * Event handler for the "grid" button that appears on the home screen, which
1789 * enters all apps mode.
1790 *
1791 * @param v The view that was clicked.
1792 */
1793 public void onClickAllAppsButton(View v) {
1794 showAllApps(true);
1795 }
1796
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001797 public void onClickAppMarketButton(View v) {
1798 if (mAppMarketIntent != null) {
1799 startActivitySafely(mAppMarketIntent, "app market");
1800 }
1801 }
1802
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001803 void startApplicationDetailsActivity(ComponentName componentName) {
1804 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001805 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1806 Uri.fromParts("package", packageName, null));
1807 startActivity(intent);
1808 }
1809
Patrick Dubroy5539af72010-09-07 15:22:01 -07001810 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1811 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1812 // System applications cannot be installed. For now, show a toast explaining that.
1813 // We may give them the option of disabling apps this way.
1814 int messageId = R.string.uninstall_system_app_text;
1815 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1816 } else {
1817 String packageName = appInfo.componentName.getPackageName();
1818 String className = appInfo.componentName.getClassName();
1819 Intent intent = new Intent(
1820 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1821 startActivity(intent);
1822 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001823 }
1824
Joe Onoratof984e852010-03-25 09:47:45 -07001825 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1827 try {
1828 startActivity(intent);
1829 } catch (ActivityNotFoundException e) {
1830 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001831 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 } catch (SecurityException e) {
1833 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001834 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001836 "or use the exported attribute for this activity. "
1837 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
1839 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001840
Romain Guy8e633c52010-03-04 12:51:36 -08001841 void startActivityForResultSafely(Intent intent, int requestCode) {
1842 try {
1843 startActivityForResult(intent, requestCode);
1844 } catch (ActivityNotFoundException e) {
1845 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1846 } catch (SecurityException e) {
1847 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1848 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1849 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1850 "or use the exported attribute for this activity.", e);
1851 }
1852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853
1854 private void handleFolderClick(FolderInfo folderInfo) {
1855 if (!folderInfo.opened) {
1856 // Close any open folder
1857 closeFolder();
1858 // Open the requested folder
1859 openFolder(folderInfo);
1860 } else {
1861 // Find the open folder...
1862 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1863 int folderScreen;
1864 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001865 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 // .. and close it
1867 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001868 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 // Close any folder open on the current screen
1870 closeFolder();
1871 // Pull the folder onto this screen
1872 openFolder(folderInfo);
1873 }
1874 }
1875 }
1876 }
1877
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001879 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 * is animated relative to the specified View. If the View is null, no animation
1881 * is played.
1882 *
1883 * @param folderInfo The FolderInfo describing the folder to open.
1884 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001885 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 Folder openFolder;
1887
1888 if (folderInfo instanceof UserFolderInfo) {
1889 openFolder = UserFolder.fromXml(this);
1890 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001891 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 } else {
1893 return;
1894 }
1895
Joe Onorato00acb122009-08-04 16:04:30 -04001896 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 openFolder.setLauncher(this);
1898
1899 openFolder.bind(folderInfo);
1900 folderInfo.opened = true;
1901
Winson Chungaafa03c2010-06-11 17:34:16 -07001902 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 openFolder.onOpen();
1905 }
1906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001908 switch (v.getId()) {
1909 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001910 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001911 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1912 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001913 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001914 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001915 return true;
1916 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001917 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001918 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1919 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001920 showPreviews(v);
1921 }
1922 return true;
1923 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001924 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001925 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1926 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1927 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001928 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001929 return true;
1930 }
1931
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 return false;
1934 }
1935
1936 if (!(v instanceof CellLayout)) {
1937 v = (View) v.getParent();
1938 }
1939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
Michael Jurka0280c3b2010-09-17 15:00:07 -07001941 resetAddInfo();
1942 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001944 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 return true;
1946 }
1947
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 final View itemUnderLongClick = longClickCellInfo.cell;
1949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 if (itemUnderLongClick == null) {
1952 // User long pressed on empty space
1953 mWorkspace.setAllowLongPress(false);
1954 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1955 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001956 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001957 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001960 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001962 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1963 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001964 mAddIntersectCellX = longClickCellInfo.cellX;
1965 mAddIntersectCellY = longClickCellInfo.cellY;
1966 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968 }
1969 }
1970 return true;
1971 }
1972
Romain Guye6b8e2f2009-11-10 11:56:55 -08001973 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001974 private void dismissPreview(final View v) {
1975 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001976 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001977 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1978 public void onDismiss() {
1979 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1980 int count = group.getChildCount();
1981 for (int i = 0; i < count; i++) {
1982 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1983 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001984 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1985 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1986
1987 v.setTag(R.id.workspace, null);
1988 v.setTag(R.id.icon, null);
1989 window.setOnDismissListener(null);
1990 }
1991 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001992 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001993 }
1994 v.setTag(null);
1995 }
1996
Romain Guyf8e6a802009-12-07 17:48:02 -08001997 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001998 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001999 }
2000
Romain Guya6abce82009-11-10 02:54:41 -08002001 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002002 final Resources resources = getResources();
2003 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002004
Romain Guya6abce82009-11-10 02:54:41 -08002005 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002006
Romain Guy9d31e9f2009-11-11 18:54:28 -08002007 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002008
Romain Guyf8e6a802009-12-07 17:48:02 -08002009 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002010 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002011 int extraW = (int) ((r.left + r.right) * max);
2012 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002013
2014 int aW = cell.getWidth() - extraW;
2015 float w = aW / max;
2016
2017 int width = cell.getWidth();
2018 int height = cell.getHeight();
2019 int x = cell.getLeftPadding();
2020 int y = cell.getTopPadding();
2021 width -= (x + cell.getRightPadding());
2022 height -= (y + cell.getBottomPadding());
2023
2024 float scale = w / width;
2025
2026 int count = end - start;
2027
2028 final float sWidth = width * scale;
2029 float sHeight = height * scale;
2030
Romain Guye6b8e2f2009-11-10 11:56:55 -08002031 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002032
Romain Guye6b8e2f2009-11-10 11:56:55 -08002033 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2034 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002035
2036 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002037 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002038 cell = (CellLayout) workspace.getChildAt(i);
2039
Romain Guyf8e6a802009-12-07 17:48:02 -08002040 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002041 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002042
2043 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002044 c.scale(scale, scale);
2045 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002046 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002047
Romain Guy9d31e9f2009-11-11 18:54:28 -08002048 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002049 image.setImageBitmap(bitmap);
2050 image.setTag(i);
2051 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002052 image.setOnFocusChangeListener(handler);
2053 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002054 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002055
2056 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002057 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2058
Winson Chungaafa03c2010-06-11 17:34:16 -07002059 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002060 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002061
2062 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002063 p.setContentView(preview);
2064 p.setWidth((int) (sWidth * count + extraW));
2065 p.setHeight((int) (sHeight + extraH));
2066 p.setAnimationStyle(R.style.AnimationPreview);
2067 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002068 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002069 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002070 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002071
Romain Guye6b8e2f2009-11-10 11:56:55 -08002072 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2073 public void onDismiss() {
2074 dismissPreview(anchor);
2075 }
2076 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002077
2078 anchor.setTag(p);
2079 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002080 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002081 }
2082
Romain Guy9d31e9f2009-11-11 18:54:28 -08002083 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002084 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002085
Romain Guye6b8e2f2009-11-10 11:56:55 -08002086 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002087 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002088 }
2089
2090 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002091 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002092 v.post(this);
2093 }
2094
2095 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002096 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002097 }
2098
2099 public void onFocusChange(View v, boolean hasFocus) {
2100 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002101 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002102 }
Romain Guya6abce82009-11-10 02:54:41 -08002103 }
2104 }
2105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 Workspace getWorkspace() {
2107 return mWorkspace;
2108 }
2109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 @Override
2111 protected Dialog onCreateDialog(int id) {
2112 switch (id) {
2113 case DIALOG_CREATE_SHORTCUT:
2114 return new CreateShortcut().createDialog();
2115 case DIALOG_RENAME_FOLDER:
2116 return new RenameFolder().createDialog();
2117 }
2118
2119 return super.onCreateDialog(id);
2120 }
2121
2122 @Override
2123 protected void onPrepareDialog(int id, Dialog dialog) {
2124 switch (id) {
2125 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 break;
2127 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002128 if (mFolderInfo != null) {
2129 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2130 final CharSequence text = mFolderInfo.title;
2131 input.setText(text);
2132 input.setSelection(0, text.length());
2133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 break;
2135 }
2136 }
2137
2138 void showRenameDialog(FolderInfo info) {
2139 mFolderInfo = info;
2140 mWaitingForResult = true;
2141 showDialog(DIALOG_RENAME_FOLDER);
2142 }
2143
Michael Jurka0280c3b2010-09-17 15:00:07 -07002144 private void showAddDialog(int intersectX, int intersectY) {
2145 resetAddInfo();
2146 mAddIntersectCellX = intersectX;
2147 mAddIntersectCellY = intersectY;
2148 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 mWaitingForResult = true;
2150 showDialog(DIALOG_CREATE_SHORTCUT);
2151 }
2152
Joe Onoratodeb98af2010-02-19 14:59:39 -08002153 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002154 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002155 Bundle bundle = new Bundle();
2156
2157 ArrayList<String> shortcutNames = new ArrayList<String>();
2158 shortcutNames.add(getString(R.string.group_applications));
2159 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2160
2161 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2162 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2163 R.drawable.ic_launcher_application));
2164 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2165
2166 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2167 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002168 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002169 pickIntent.putExtras(bundle);
2170
Joe Onoratodeb98af2010-02-19 14:59:39 -08002171 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002172 }
2173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 private class RenameFolder {
2175 private EditText mInput;
2176
2177 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2179 mInput = (EditText) layout.findViewById(R.id.folder_name);
2180
2181 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2182 builder.setIcon(0);
2183 builder.setTitle(getString(R.string.rename_folder_title));
2184 builder.setCancelable(true);
2185 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2186 public void onCancel(DialogInterface dialog) {
2187 cleanup();
2188 }
2189 });
2190 builder.setNegativeButton(getString(R.string.cancel_action),
2191 new Dialog.OnClickListener() {
2192 public void onClick(DialogInterface dialog, int which) {
2193 cleanup();
2194 }
2195 }
2196 );
2197 builder.setPositiveButton(getString(R.string.rename_action),
2198 new Dialog.OnClickListener() {
2199 public void onClick(DialogInterface dialog, int which) {
2200 changeFolderName();
2201 }
2202 }
2203 );
2204 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002205
2206 final AlertDialog dialog = builder.create();
2207 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2208 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002209 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002210 mInput.requestFocus();
2211 InputMethodManager inputManager = (InputMethodManager)
2212 getSystemService(Context.INPUT_METHOD_SERVICE);
2213 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002214 }
2215 });
2216
2217 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219
2220 private void changeFolderName() {
2221 final String name = mInput.getText().toString();
2222 if (!TextUtils.isEmpty(name)) {
2223 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002224 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 mFolderInfo.title = name;
2226 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2227
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002229 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 } else {
2232 final FolderIcon folderIcon = (FolderIcon)
2233 mWorkspace.getViewForTag(mFolderInfo);
2234 if (folderIcon != null) {
2235 folderIcon.setText(name);
2236 getWorkspace().requestLayout();
2237 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002238 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 mWorkspaceLoading = true;
2240 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 }
2242 }
2243 }
2244 cleanup();
2245 }
2246
2247 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 dismissDialog(DIALOG_RENAME_FOLDER);
2249 mWaitingForResult = false;
2250 mFolderInfo = null;
2251 }
2252 }
2253
Daniel Sandler843e8602010-06-07 14:59:01 -04002254 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2255 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002256 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002257 }
2258
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002259 // AllAppsView.Watcher
2260 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002261 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2262 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002263 mWorkspace.setVisibility(View.GONE);
2264 }
2265 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002266
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002267 private void showToolbarButton(View button) {
2268 button.setAlpha(1.0f);
2269 button.setVisibility(View.VISIBLE);
2270 button.setFocusable(true);
2271 button.setClickable(true);
2272 }
2273
2274 private void hideToolbarButton(View button) {
2275 button.setAlpha(0.0f);
2276 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2277 button.setVisibility(View.INVISIBLE);
2278 button.setFocusable(false);
2279 button.setClickable(false);
2280 }
2281
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002282 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002283 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002284 *
2285 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2286 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002287 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002288 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002289 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002290 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002291 final boolean showing = show;
2292 final boolean hiding = !show;
2293
2294 final int duration = show ?
2295 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2296 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2297
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002298 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002299 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2300 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002301 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002302 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002303 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002304 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002305 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002306 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002307 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002308 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002309 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002310 });
2311 seq.play(anim);
2312 } else {
2313 if (showing) {
2314 showToolbarButton(view);
2315 } else {
2316 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002317 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002318 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002319 }
2320
2321 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002322 * Show/hide the appropriate toolbar buttons for newState.
2323 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002324 *
2325 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002326 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2327 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002328 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002329 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002330 final View searchButton = findViewById(R.id.search_button);
2331 final View allAppsButton = findViewById(R.id.all_apps_button);
2332 final View marketButton = findViewById(R.id.market_button);
2333 final View configureButton = findViewById(R.id.configure_button);
2334
2335 switch (newState) {
2336 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002337 hideOrShowToolbarButton(true, searchButton, showSeq);
2338 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2339 hideOrShowToolbarButton(true, configureButton, showSeq);
2340 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002341 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 break;
2343 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002344 hideOrShowToolbarButton(true, configureButton, showSeq);
2345 hideOrShowToolbarButton(true, marketButton, showSeq);
2346 hideOrShowToolbarButton(false, searchButton, hideSeq);
2347 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002348 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002349 break;
2350 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002351 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2352 hideOrShowToolbarButton(false, searchButton, hideSeq);
2353 hideOrShowToolbarButton(false, marketButton, hideSeq);
2354 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002355 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002356 break;
2357 }
2358 }
2359
2360 /**
2361 * Helper method for the cameraZoomIn/cameraZoomOut animations
2362 * @param view The view being animated
2363 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2364 * @param scaleFactor The scale factor used for the zoom
2365 */
2366 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2367 final int height = view.getHeight();
2368 view.setPivotX(view.getWidth() / 2.0f);
2369 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2370 // Assumes that the view is normally anchored to either the top or bottom of the screen
2371 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2372 if (state == State.ALL_APPS) {
2373 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2374 } else {
2375 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2376 }
2377 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002378
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002379 /**
2380 * Zoom the camera out from the workspace to reveal 'toView'.
2381 * Assumes that the view to show is anchored at either the very top or very bottom
2382 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002383 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002384 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002385 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002386 final Resources res = getResources();
2387 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2388 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002389 final boolean toAllApps = (toState == State.ALL_APPS);
2390 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002391
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002392 setPivotsForZoom(toView, toState, scale);
2393
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002394 if (toAllApps) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002395 mWorkspace.shrinkToBottom(animated);
2396 } else {
2397 mWorkspace.shrinkToTop(animated);
2398 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002399
2400 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002401 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2402 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2403 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2404 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002405 scaleAnim.setInterpolator(new DecelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002406 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002407 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002408 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002409 // Prepare the position
2410 toView.setTranslationX(0.0f);
2411 toView.setTranslationY(0.0f);
2412 toView.setVisibility(View.VISIBLE);
2413 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002414 @Override
2415 public void onAnimationEndOrCancel(Animator animation) {
2416 // If we don't set the final scale values here, if this animation is cancelled
2417 // it will have the wrong scale value and subsequent cameraPan animations will
2418 // not fix that
2419 toView.setScaleX(1.0f);
2420 toView.setScaleY(1.0f);
2421 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002422 });
2423
Chet Haaseb1254a62010-09-07 13:35:00 -07002424 AnimatorSet toolbarHideAnim = new AnimatorSet();
2425 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002426 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2427
2428 // toView should appear right at the end of the workspace shrink animation
2429 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2430
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002431 if (mStateAnimation != null) mStateAnimation.cancel();
2432 mStateAnimation = new AnimatorSet();
2433 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2434 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435
2436 // Show the new toolbar buttons just as the main animation is ending
2437 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002438 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2439 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002440 } else {
2441 toView.setTranslationX(0.0f);
2442 toView.setTranslationY(0.0f);
2443 toView.setScaleX(1.0f);
2444 toView.setScaleY(1.0f);
2445 toView.setVisibility(View.VISIBLE);
2446 hideAndShowToolbarButtons(toState, null, null);
2447 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002448 }
2449
2450 /**
2451 * Zoom the camera back into the workspace, hiding 'fromView'.
2452 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002453 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2454 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002455 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002456 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002457 Resources res = getResources();
2458 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2459 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002460 final View fromView =
2461 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2462
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002463 mCustomizePagedView.endChoiceMode();
2464 mAllAppsPagedView.endChoiceMode();
2465
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002466 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002467
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002468 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469
2470 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002471 if (mStateAnimation != null) mStateAnimation.cancel();
2472 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002473 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2474 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2475 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2476 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002478 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002479 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002480 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002481 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002482 }
2483 });
2484
Chet Haaseb1254a62010-09-07 13:35:00 -07002485 AnimatorSet toolbarHideAnim = new AnimatorSet();
2486 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002487 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2488
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002489 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002490
2491 // Show the new toolbar buttons at the very end of the whole animation
2492 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2493 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002494 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2495 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002496 } else {
2497 fromView.setVisibility(View.GONE);
2498 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2499 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002500 }
2501
2502 /**
2503 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2504 * This is the transition used on xlarge screens to go between all apps and
2505 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002506 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2507 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2508 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002509 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002510 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002511 final Resources res = getResources();
2512 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002513 final int workspaceHeight = mWorkspace.getHeight();
2514
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002515 final boolean fromAllApps = (fromState == State.ALL_APPS);
2516 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2517 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002518
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002519 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2520 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2521 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2522 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002523
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002524 mCustomizePagedView.endChoiceMode();
2525 mAllAppsPagedView.endChoiceMode();
2526
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002527 if (toState == State.ALL_APPS) {
2528 mWorkspace.shrinkToBottom(animated);
2529 } else {
2530 mWorkspace.shrinkToTop(animated);
2531 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532
2533 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002534 if (mStateAnimation != null) mStateAnimation.cancel();
2535 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002536 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002537 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002538 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002539 toView.setVisibility(View.VISIBLE);
2540 toView.setY(toViewStartY);
2541 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002542 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002543 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002544 fromView.setVisibility(View.GONE);
2545 }
2546 });
2547
Chet Haaseb1254a62010-09-07 13:35:00 -07002548 AnimatorSet toolbarHideAnim = new AnimatorSet();
2549 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2551
Chet Haase472b2812010-10-14 07:02:04 -07002552 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2553 fromViewStartY, fromViewEndY);
2554 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002555 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2556 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2557 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2558 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2559 );
Chet Haase472b2812010-10-14 07:02:04 -07002560 fromAnim.setDuration(duration);
2561 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562
2563 // Show the new toolbar buttons just as the main animation is ending
2564 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002565 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2566 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 } else {
2568 fromView.setY(fromViewEndY);
2569 fromView.setVisibility(View.GONE);
2570 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002571 toView.setScaleX(1.0f);
2572 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002573 toView.setVisibility(View.VISIBLE);
2574 hideAndShowToolbarButtons(toState, null, null);
2575 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002576 }
2577
2578 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002579 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002580 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002581 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002582
Michael Jurka79212d82010-07-30 16:36:20 -07002583 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002584 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002585 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002586 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002587 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002588 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002589 } else {
2590 mAllAppsGrid.zoom(1.0f, animated);
2591 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002592
Romain Guyc16fea72010-03-12 17:17:56 -08002593 ((View) mAllAppsGrid).setFocusable(true);
2594 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002595
Joe Onorato7c312c12009-08-13 21:36:53 -07002596 // TODO: fade these two too
2597 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002598 // Change the state *after* we've called all the transition code
2599 mState = State.ALL_APPS;
2600 }
2601
2602
2603 void showWorkspace(boolean animated) {
2604 showWorkspace(animated, null);
2605 }
2606
2607 void showWorkspace(boolean animated, CellLayout layout) {
2608 if (layout != null && animated) {
2609 mWorkspace.unshrink(layout);
2610 } else {
2611 mWorkspace.unshrink(animated);
2612 }
2613 if (mState == State.ALL_APPS) {
2614 closeAllApps(animated);
2615 } else if (mState == State.CUSTOMIZE) {
2616 hideCustomizationDrawer(animated);
2617 }
2618 // Change the state *after* we've called all the transition code
2619 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002620 }
2621
Joe Onoratob2061212009-11-24 16:13:54 -05002622 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002623 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002624 * - Home from workspace
2625 * - from center screen
2626 * - from other screens
2627 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002628 * - from center screen
2629 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002630 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002631 * - from center screen
2632 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002633 * - Launch app from workspace and quit
2634 * - with back
2635 * - with home
2636 * - Launch app from all apps and quit
2637 * - with back
2638 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002639 * - Go to a screen that's not the default, then all
2640 * apps, and launch and app, and go back
2641 * - with back
2642 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002643 * - On workspace, long press power and go back
2644 * - with back
2645 * - with home
2646 * - On all apps, long press power and go back
2647 * - with back
2648 * - with home
2649 * - On workspace, power off
2650 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002651 * - Launch an app and turn off the screen while in that app
2652 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002653 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002654 * - From all apps
2655 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002656 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2657 * - From all apps
2658 * - From the center workspace
2659 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002660 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002661 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002662 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002663 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002665 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002666 } else {
2667 mAllAppsGrid.zoom(0.0f, animated);
2668 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002669 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002670 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002671 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002672 }
2673
Joe Onorato7c312c12009-08-13 21:36:53 -07002674 void lockAllApps() {
2675 // TODO
2676 }
2677
2678 void unlockAllApps() {
2679 // TODO
2680 }
2681
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 // Show the customization drawer (only exists in x-large configuration)
2683 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002684 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002686 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002688 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002689 // Change the state *after* we've called all the transition code
2690 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002691 }
2692
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002693 // Hide the customization drawer (only exists in x-large configuration)
2694 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002695 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002697 }
2698 }
2699
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002700 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2701 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2702 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002703 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002704 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002705
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002706 void onWorkspaceClick(CellLayout layout) {
2707 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002708 }
2709
Michael Jurka0423dcf2010-10-05 14:56:18 -07002710 private void updateButtonWithIconFromExternalActivity(
2711 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2712 ImageView button = (ImageView) findViewById(buttonId);
2713 Drawable toolbarIcon = null;
2714 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002715 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002716 // Look for the toolbar icon specified in the activity meta-data
2717 Bundle metaData = packageManager.getActivityInfo(
2718 activityName, PackageManager.GET_META_DATA).metaData;
2719 if (metaData != null) {
2720 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2721 if (iconResId != 0) {
2722 Resources res = packageManager.getResourcesForActivity(activityName);
2723 toolbarIcon = res.getDrawable(iconResId);
2724 }
2725 }
2726 } catch (NameNotFoundException e) {
2727 // Do nothing
2728 }
2729 // If we were unable to find the icon via the meta-data, use a generic one
2730 if (toolbarIcon == null) {
2731 button.setImageResource(fallbackDrawableId);
2732 } else {
2733 button.setImageDrawable(toolbarIcon);
2734 }
2735 }
2736
2737 private void updateGlobalSearchIcon() {
2738 if (LauncherApplication.isScreenXLarge()) {
2739 final SearchManager searchManager =
2740 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2741 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002742 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002743 updateButtonWithIconFromExternalActivity(
2744 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002745 }
2746 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002747 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002748
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002749 /**
2750 * Sets the app market icon (shown when all apps is visible on x-large screens)
2751 */
2752 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002753 if (LauncherApplication.isScreenXLarge()) {
2754 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2755 // Find the app market activity by resolving an intent.
2756 // (If multiple app markets are installed, it will return the ResolverActivity.)
2757 ComponentName activityName = intent.resolveActivity(getPackageManager());
2758 if (activityName != null) {
2759 mAppMarketIntent = intent;
2760 updateButtonWithIconFromExternalActivity(
2761 R.id.market_button, activityName, R.drawable.app_market_generic);
2762 }
2763 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002764 }
2765
2766 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 * Displays the shortcut creation dialog and launches, if necessary, the
2768 * appropriate activity.
2769 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002770 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002771 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2772 DialogInterface.OnShowListener {
2773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002775
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2780 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002781 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002782
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783 builder.setInverseBackgroundForced(true);
2784
2785 AlertDialog dialog = builder.create();
2786 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002787 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002788 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 return dialog;
2791 }
2792
2793 public void onCancel(DialogInterface dialog) {
2794 mWaitingForResult = false;
2795 cleanup();
2796 }
2797
Romain Guycbb89e42009-06-08 15:52:54 -07002798 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002799 }
2800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002802 try {
2803 dismissDialog(DIALOG_CREATE_SHORTCUT);
2804 } catch (Exception e) {
2805 // An exception is thrown if the dialog is not visible, which is fine
2806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 }
2808
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002809 /**
2810 * Handle the action clicked in the "Add to home" dialog.
2811 */
2812 public void onClick(DialogInterface dialog, int which) {
2813 Resources res = getResources();
2814 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002815
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002816 switch (which) {
2817 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002818 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002819 break;
2820 }
Romain Guycbb89e42009-06-08 15:52:54 -07002821
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002822 case AddAdapter.ITEM_APPWIDGET: {
2823 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002824
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002825 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2826 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002827 // start the pick activity
2828 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2829 break;
2830 }
Romain Guycbb89e42009-06-08 15:52:54 -07002831
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002832 case AddAdapter.ITEM_LIVE_FOLDER: {
2833 // Insert extra item to handle inserting folder
2834 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002835
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002836 ArrayList<String> shortcutNames = new ArrayList<String>();
2837 shortcutNames.add(res.getString(R.string.group_folder));
2838 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002839
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002840 ArrayList<ShortcutIconResource> shortcutIcons =
2841 new ArrayList<ShortcutIconResource>();
2842 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2843 R.drawable.ic_launcher_folder));
2844 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2845
2846 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2847 pickIntent.putExtra(Intent.EXTRA_INTENT,
2848 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2849 pickIntent.putExtra(Intent.EXTRA_TITLE,
2850 getText(R.string.title_select_live_folder));
2851 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002852
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002853 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2854 break;
2855 }
2856
2857 case AddAdapter.ITEM_WALLPAPER: {
2858 startWallpaper();
2859 break;
2860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 }
2862 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002863
2864 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002865 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002866 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 }
2868
2869 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002870 * Receives notifications when applications are added/removed.
2871 */
2872 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2873 @Override
2874 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002875 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002876 String reason = intent.getStringExtra("reason");
2877 if (!"homekey".equals(reason)) {
2878 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002879 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002880 animate = false;
2881 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002882 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002883 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002884 }
2885 }
2886
2887 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002888 * Receives notifications whenever the appwidgets are reset.
2889 */
2890 private class AppWidgetResetObserver extends ContentObserver {
2891 public AppWidgetResetObserver() {
2892 super(new Handler());
2893 }
2894
2895 @Override
2896 public void onChange(boolean selfChange) {
2897 onAppWidgetReset();
2898 }
2899 }
2900
2901 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002902 * If the activity is currently paused, signal that we need to re-run the loader
2903 * in onResume.
2904 *
2905 * This needs to be called from incoming places where resources might have been loaded
2906 * while we are paused. That is becaues the Configuration might be wrong
2907 * when we're not running, and if it comes back to what it was when we
2908 * were paused, we are not restarted.
2909 *
2910 * Implementation of the method from LauncherModel.Callbacks.
2911 *
2912 * @return true if we are currently paused. The caller might be able to
2913 * skip some work in that case since we will come back again.
2914 */
2915 public boolean setLoadOnResume() {
2916 if (mPaused) {
2917 Log.i(TAG, "setLoadOnResume");
2918 mOnResumeNeedsLoad = true;
2919 return true;
2920 } else {
2921 return false;
2922 }
2923 }
2924
2925 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002926 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002929 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002930 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002931 } else {
2932 return SCREEN_COUNT / 2;
2933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002934 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002935
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002936 void setAllAppsPagedView(PagedView view) {
2937 mAllAppsPagedView = view;
2938 }
2939
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 /**
2941 * Refreshes the shortcuts shown on the workspace.
2942 *
2943 * Implementation of the method from LauncherModel.Callbacks.
2944 */
2945 public void startBinding() {
2946 final Workspace workspace = mWorkspace;
2947 int count = workspace.getChildCount();
2948 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002949 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2951 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002952
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 if (DEBUG_USER_INTERFACE) {
2954 android.widget.Button finishButton = new android.widget.Button(this);
2955 finishButton.setText("Finish");
2956 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2957
2958 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2959 public void onClick(View v) {
2960 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 }
2964 }
2965
2966 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967 * Bind the items start-end from the list.
2968 *
2969 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002971 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2972
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002973 setLoadOnResume();
2974
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 final Workspace workspace = mWorkspace;
2976
2977 for (int i=start; i<end; i++) {
2978 final ItemInfo item = shortcuts.get(i);
2979 mDesktopItems.add(item);
2980 switch (item.itemType) {
2981 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2982 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002983 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2985 false);
2986 break;
2987 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2988 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002989 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002990 (UserFolderInfo) item);
2991 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2992 false);
2993 break;
2994 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2995 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2996 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002997 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 (LiveFolderInfo) item);
2999 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3000 false);
3001 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 }
3004
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 }
3007
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 /**
3009 * Implementation of the method from LauncherModel.Callbacks.
3010 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003011 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003012 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003013 sFolders.clear();
3014 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003016
3017 /**
3018 * Add the views for a widget to the workspace.
3019 *
3020 * Implementation of the method from LauncherModel.Callbacks.
3021 */
3022 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003023 setLoadOnResume();
3024
Daniel Sandler843e8602010-06-07 14:59:01 -04003025 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3026 if (DEBUG_WIDGETS) {
3027 Log.d(TAG, "bindAppWidget: " + item);
3028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 final Workspace workspace = mWorkspace;
3030
3031 final int appWidgetId = item.appWidgetId;
3032 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003033 if (DEBUG_WIDGETS) {
3034 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3035 }
3036
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3038
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3040 item.hostView.setTag(item);
3041
3042 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3043 item.cellY, item.spanX, item.spanY, false);
3044
3045 workspace.requestLayout();
3046
3047 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003048
3049 if (DEBUG_WIDGETS) {
3050 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3051 + (SystemClock.uptimeMillis()-start) + "ms");
3052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053 }
3054
3055 /**
3056 * Callback saying that there aren't any more items to bind.
3057 *
3058 * Implementation of the method from LauncherModel.Callbacks.
3059 */
3060 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003061 setLoadOnResume();
3062
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 if (mSavedState != null) {
3064 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003065 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 }
3067
3068 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3069 if (userFolders != null) {
3070 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003071 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 if (info != null) {
3073 openFolder(info);
3074 }
3075 }
3076 final Folder openFolder = mWorkspace.getOpenFolder();
3077 if (openFolder != null) {
3078 openFolder.requestFocus();
3079 }
3080 }
3081
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 mSavedState = null;
3083 }
3084
3085 if (mSavedInstanceState != null) {
3086 super.onRestoreInstanceState(mSavedInstanceState);
3087 mSavedInstanceState = null;
3088 }
3089
Joe Onorato9c1289c2009-08-17 11:03:03 -04003090 mWorkspaceLoading = false;
3091 }
3092
3093 /**
3094 * Add the icons for all apps.
3095 *
3096 * Implementation of the method from LauncherModel.Callbacks.
3097 */
3098 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003099 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003100 if (mCustomizePagedView != null) {
3101 mCustomizePagedView.setApps(apps);
3102 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003103 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003104 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003105 }
3106
3107 /**
3108 * A package was installed.
3109 *
3110 * Implementation of the method from LauncherModel.Callbacks.
3111 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003112 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003113 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003115 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003116 if (mCustomizePagedView != null) {
3117 mCustomizePagedView.addApps(apps);
3118 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003119 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003120 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 }
3122
3123 /**
3124 * A package was updated.
3125 *
3126 * Implementation of the method from LauncherModel.Callbacks.
3127 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003128 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003129 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003131 mWorkspace.updateShortcuts(apps);
3132 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003133 if (mCustomizePagedView != null) {
3134 mCustomizePagedView.updateApps(apps);
3135 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003136 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003137 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 }
3139
3140 /**
3141 * A package was uninstalled.
3142 *
3143 * Implementation of the method from LauncherModel.Callbacks.
3144 */
Joe Onorato36115782010-06-17 13:28:48 -04003145 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003147 if (permanent) {
3148 mWorkspace.removeItems(apps);
3149 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003150 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003151 if (mCustomizePagedView != null) {
3152 mCustomizePagedView.removeApps(apps);
3153 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003154 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003155 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 }
Joe Onoratobe386092009-11-17 17:32:16 -08003157
3158 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003159 * A number of packages were updated.
3160 */
3161 public void bindPackagesUpdated() {
3162 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003163 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003164 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003165 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003166 }
3167
3168 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003169 * Prints out out state for debugging.
3170 */
3171 public void dumpState() {
3172 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003173 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003174 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3175 Log.d(TAG, "mRestoring=" + mRestoring);
3176 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3177 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3178 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003179 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003180 mModel.dumpState();
3181 mAllAppsGrid.dumpState();
3182 Log.d(TAG, "END launcher2 dump state");
3183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184}