blob: 71978fa73446bdca51c585d02c5f10b7cad1fd32 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import com.android.common.Search;
21import com.android.launcher.R;
22
Gilles Debunnedd6c9922010-10-25 11:23:41 -070023import android.animation.Animator;
24import android.animation.AnimatorListenerAdapter;
25import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070026import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070034import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070035import android.appwidget.AppWidgetManager;
36import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070045import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Patrick Dubroyceae05d2010-08-30 10:40:53 -070048import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040052import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080055import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080066import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070067import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
71import android.text.TextUtils;
72import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070082import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070084import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070085import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070086import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070092import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070093import android.widget.TabHost.OnTabChangeListener;
94import android.widget.TabHost.TabContentFactory;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
98import java.io.DataInputStream;
99import java.io.DataOutputStream;
100import java.io.FileNotFoundException;
101import java.io.IOException;
102import java.util.ArrayList;
103import java.util.HashMap;
104import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106/**
107 * Default launcher application.
108 */
Michael Jurka946ad472010-07-09 18:05:18 -0700109public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700110 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
111 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800112 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700113 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400116 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800120 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700123 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
124 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
126 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700127 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 private static final int REQUEST_CREATE_SHORTCUT = 1;
130 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
134 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700136 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
139
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800140 static final int SCREEN_COUNT = 5;
141 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Joe Onorato7c312c12009-08-13 21:36:53 -0700143 static final int DIALOG_CREATE_SHORTCUT = 1;
144 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Romain Guy98d01652009-06-30 16:21:04 -0700146 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
148 // Type: int
149 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700150 // Type: int
151 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: long
153 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
154 // Type: int
155 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: boolean
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
162 // Type: long
163 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
164
Winson Chung80baf5a2010-08-09 16:03:15 -0700165 // tags for the customization tabs
166 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700167 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700168 private static final String SHORTCUTS_TAG = "shortcuts";
169 private static final String WALLPAPERS_TAG = "wallpapers";
170
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700171 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
172
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700173 /** The different states that Launcher can be in. */
174 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700175 private State mState = State.WORKSPACE;
176 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800181 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800183 private final BroadcastReceiver mCloseSystemDialogsReceiver
184 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800185 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private LayoutInflater mInflater;
188
Joe Onorato00acb122009-08-04 16:04:30 -0400189 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private AppWidgetManager mAppWidgetManager;
193 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195 private int mAddScreen = -1;
196 private int mAddIntersectCellX = -1;
197 private int mAddIntersectCellY = -1;
198 private int[] mAddDropPosition;
199 private int[] mTmpAddItemCellCoordinates = new int[2];
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private FolderInfo mFolderInfo;
202
Joe Onorato7c312c12009-08-13 21:36:53 -0700203 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700205 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700206 private TabHost mHomeCustomizationDrawer;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700207
208 private PagedView mAllAppsPagedView = null;
209 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private Bundle mSavedState;
212
213 private SpannableStringBuilder mDefaultKeySsb = null;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 private boolean mWorkspaceLoading = true;
216
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800217 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private boolean mRestoring;
219 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700220 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221
222 private Bundle mSavedInstanceState;
223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800225 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700227 private static LocaleConfiguration sLocaleConfiguration = null;
228
Romain Guy84f296c2009-11-04 15:00:44 -0800229 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Romain Guy1fbc1c82009-11-09 20:43:08 -0800232 private ImageView mPreviousView;
233 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500234
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400235 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400236 private String[] mHotseatConfig = null;
237 private Intent[] mHotseats = null;
238 private Drawable[] mHotseatIcons = null;
239 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400240
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700241 private Intent mAppMarketIntent = null;
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 @Override
244 protected void onCreate(Bundle savedInstanceState) {
245 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700246
Winson Chungaafa03c2010-06-11 17:34:16 -0700247 if (LauncherApplication.isInPlaceRotationEnabled()) {
248 // hide the status bar (temporary until we get the status bar design figured out)
249 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
250 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
251 }
252
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 LauncherApplication app = ((LauncherApplication)getApplication());
254 mModel = app.setLauncher(this);
255 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400256 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
261 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200264 android.os.Debug.startMethodTracing(
265 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400268 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 checkForLocaleChange();
270 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700272 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
273 if (mHomeCustomizationDrawer != null) {
274 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700275
Winson Chung80baf5a2010-08-09 16:03:15 -0700276 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700277 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
278 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700279 TabContentFactory contentFactory = new TabContentFactory() {
280 public View createTabContent(String tag) {
281 return mCustomizePagedView;
282 }
283 };
284
Michael Jurka946ad472010-07-09 18:05:18 -0700285 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
287 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700288 String applicationsLabel = getString(R.string.applications_tab_label);
289 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
290 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700291 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
293 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700294 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
295 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
296 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700297
Winson Chung80baf5a2010-08-09 16:03:15 -0700298 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
299 public void onTabChanged(String tabId) {
300 // animate the changing of the tab content by fading pages in and out
301 final int duration = 150;
302 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700303 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700305 alphaAnim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -0700306 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700307 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -0700308 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700309 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
310 if (tag == WIDGETS_TAG) {
311 mCustomizePagedView.setCustomizationFilter(
312 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700313 } else if (tag == APPLICATIONS_TAG) {
314 mCustomizePagedView.setCustomizationFilter(
315 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 } else if (tag == WALLPAPERS_TAG) {
317 mCustomizePagedView.setCustomizationFilter(
318 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700319 } else if (tag == SHORTCUTS_TAG) {
320 mCustomizePagedView.setCustomizationFilter(
321 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 }
323
324 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700325 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700326 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700327 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 alphaAnim.start();
329 }
330 });
331 alphaAnim.start();
332 }
333 });
Michael Jurka946ad472010-07-09 18:05:18 -0700334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 setupViews();
336
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800337 registerContentObservers();
338
Joe Onorato7c312c12009-08-13 21:36:53 -0700339 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 mSavedState = savedInstanceState;
342 restoreState(mSavedState);
343
344 if (PROFILE_STARTUP) {
345 android.os.Debug.stopMethodTracing();
346 }
347
348 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400349 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
351
352 // For handling default keys
353 mDefaultKeySsb = new SpannableStringBuilder();
354 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800355
356 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
357 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
Romain Guycbb89e42009-06-08 15:52:54 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700361 if (sLocaleConfiguration == null) {
362 new AsyncTask<Void, Void, LocaleConfiguration>() {
363 @Override
364 protected LocaleConfiguration doInBackground(Void... unused) {
365 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
366 readConfiguration(Launcher.this, localeConfiguration);
367 return localeConfiguration;
368 }
369
370 @Override
371 protected void onPostExecute(LocaleConfiguration result) {
372 sLocaleConfiguration = result;
373 checkForLocaleChange(); // recursive, but now with a locale configuration
374 }
375 }.execute();
376 return;
377 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 final Configuration configuration = getResources().getConfiguration();
380
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700381 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 final String locale = configuration.locale.toString();
383
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700384 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 final int mcc = configuration.mcc;
386
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700387 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final int mnc = configuration.mnc;
389
Romain Guy84f296c2009-11-04 15:00:44 -0800390 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391
Romain Guy84f296c2009-11-04 15:00:44 -0800392 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 sLocaleConfiguration.locale = locale;
394 sLocaleConfiguration.mcc = mcc;
395 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700396
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400398 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399
400 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
401 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700402 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 public void run() {
404 writeConfiguration(Launcher.this, localeConfiguration);
405 }
406 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700407 }
408 }
409
410 private static class LocaleConfiguration {
411 public String locale;
412 public int mcc = -1;
413 public int mnc = -1;
414 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700415
Romain Guy98d01652009-06-30 16:21:04 -0700416 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
417 DataInputStream in = null;
418 try {
419 in = new DataInputStream(context.openFileInput(PREFERENCES));
420 configuration.locale = in.readUTF();
421 configuration.mcc = in.readInt();
422 configuration.mnc = in.readInt();
423 } catch (FileNotFoundException e) {
424 // Ignore
425 } catch (IOException e) {
426 // Ignore
427 } finally {
428 if (in != null) {
429 try {
430 in.close();
431 } catch (IOException e) {
432 // Ignore
433 }
434 }
435 }
436 }
437
438 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
439 DataOutputStream out = null;
440 try {
441 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
442 out.writeUTF(configuration.locale);
443 out.writeInt(configuration.mcc);
444 out.writeInt(configuration.mnc);
445 out.flush();
446 } catch (FileNotFoundException e) {
447 // Ignore
448 } catch (IOException e) {
449 //noinspection ResultOfMethodCallIgnored
450 context.getFileStreamPath(PREFERENCES).delete();
451 } finally {
452 if (out != null) {
453 try {
454 out.close();
455 } catch (IOException e) {
456 // Ignore
457 }
458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 }
460 }
461
462 static int getScreen() {
463 synchronized (sLock) {
464 return sScreen;
465 }
466 }
467
468 static void setScreen(int screen) {
469 synchronized (sLock) {
470 sScreen = screen;
471 }
472 }
473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700475 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
477 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700478 // TODO: Put back when we decide about scrolling the wallpaper
479 // boolean isPortrait = display.getWidth() < display.getHeight();
480 // final int width = isPortrait ? display.getWidth() : display.getHeight();
481 // final int height = isPortrait ? display.getHeight() : display.getWidth();
482 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
483 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 }
485
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400486 // Note: This doesn't do all the client-id magic that BrowserProvider does
487 // in Browser. (http://b/2425179)
488 private Uri getDefaultBrowserUri() {
489 String url = getString(R.string.default_browser_url);
490 if (url.indexOf("{CID}") != -1) {
491 url = url.replace("{CID}", "android-google");
492 }
493 return Uri.parse(url);
494 }
495
496 // Load the Intent templates from arrays.xml to populate the hotseats. For
497 // each Intent, if it resolves to a single app, use that as the launch
498 // intent & use that app's label as the contentDescription. Otherwise,
499 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400500 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400501 if (mHotseatConfig == null) {
502 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
503 if (mHotseatConfig.length > 0) {
504 mHotseats = new Intent[mHotseatConfig.length];
505 mHotseatLabels = new CharSequence[mHotseatConfig.length];
506 mHotseatIcons = new Drawable[mHotseatConfig.length];
507 } else {
508 mHotseats = null;
509 mHotseatIcons = null;
510 mHotseatLabels = null;
511 }
512
513 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
514 for (int i=0; i<mHotseatConfig.length; i++) {
515 // load icon for this slot; currently unrelated to the actual activity
516 try {
517 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
518 } catch (ArrayIndexOutOfBoundsException ex) {
519 Log.w(TAG, "Missing hotseat_icons array item #" + i);
520 mHotseatIcons[i] = null;
521 }
522 }
523 hotseatIconDrawables.recycle();
524 }
525
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400526 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400527 for (int i=0; i<mHotseatConfig.length; i++) {
528 Intent intent = null;
529 if (mHotseatConfig[i].equals("*BROWSER*")) {
530 // magic value meaning "launch user's default web browser"
531 // replace it with a generic web request so we can see if there is indeed a default
532 String defaultUri = getString(R.string.default_browser_url);
533 intent = new Intent(
534 Intent.ACTION_VIEW,
535 ((defaultUri != null)
536 ? Uri.parse(defaultUri)
537 : getDefaultBrowserUri())
538 ).addCategory(Intent.CATEGORY_BROWSABLE);
539 // note: if the user launches this without a default set, she
540 // will always be taken to the default URL above; this is
541 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700542 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 // URL is unavoidably sent to the chosen app.
544 } else {
545 try {
546 intent = Intent.parseUri(mHotseatConfig[i], 0);
547 } catch (java.net.URISyntaxException ex) {
548 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
549 // bogus; leave intent=null
550 }
551 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700552
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400553 if (intent == null) {
554 mHotseats[i] = null;
555 mHotseatLabels[i] = getText(R.string.activity_not_found);
556 continue;
557 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400558
559 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 Log.d(TAG, "loadHotseats: hotseat " + i
561 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400563 + "]");
564 }
565
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400566 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
567 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700568 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400569 Log.d(TAG, "Best match for intent: " + bestMatch);
570 Log.d(TAG, "All matches: ");
571 for (ResolveInfo ri : allMatches) {
572 Log.d(TAG, " --> " + ri);
573 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400574 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 // did this resolve to a single app, or the resolver?
576 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700577 // can't find any activity to handle this. let's leave the
578 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400579 // to launch.
580 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 // set accessibility text to "Not installed"
583 mHotseatLabels[i] = getText(R.string.activity_not_found);
584 } else {
585 boolean found = false;
586 for (ResolveInfo ri : allMatches) {
587 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
588 && bestMatch.activityInfo.applicationInfo.packageName
589 .equals(ri.activityInfo.applicationInfo.packageName)) {
590 found = true;
591 break;
592 }
593 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700594
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400595 if (!found) {
596 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
597 // the bestMatch is probably the ResolveActivity, meaning the
598 // user has not yet selected a default
599 // so: we'll keep the original intent for now
600 mHotseats[i] = intent;
601
602 // set the accessibility text to "Select shortcut"
603 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
604 } else {
605 // we have an app!
606 // now reconstruct the intent to launch it through the front
607 // door
608 ComponentName com = new ComponentName(
609 bestMatch.activityInfo.applicationInfo.packageName,
610 bestMatch.activityInfo.name);
611 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
612
613 // load the app label for accessibility
614 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
615 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400616 }
617
618 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700619 Log.d(TAG, "loadHotseats: hotseat " + i
620 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400621 + ((mHotseats[i] == null)
622 ? "null"
623 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400624 + "] label=[" + mHotseatLabels[i]
625 + "]"
626 );
627 }
628 }
629 }
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 @Override
632 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700633 mWaitingForResult = false;
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 // The pattern used here is that a user PICKs a specific application,
636 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
639 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700640
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 switch (requestCode) {
643 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700644 completeAddApplication(
645 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 break;
647 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800648 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 break;
650 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700651 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 break;
653 case REQUEST_PICK_LIVE_FOLDER:
654 addLiveFolder(data);
655 break;
656 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700659 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700660 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700662 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700666 case REQUEST_PICK_WALLPAPER:
667 // We just wanted the activity result here so we can clear mWaitingForResult
668 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
Romain Guy18042c82009-11-06 11:44:55 -0800670 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
671 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
672 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 // Clean up the appWidgetId if we canceled
674 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
675 if (appWidgetId != -1) {
676 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680
681 @Override
682 protected void onResume() {
683 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800684 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700685 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400686 mWorkspaceLoading = true;
687 mModel.startLoader(this, true);
688 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700689 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700691 // When we resume Launcher, a different Activity might be responsible for the app
692 // market intent, so refresh the icon
693 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695
696 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 protected void onPause() {
698 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700699 // Some launcher layouts don't have a previous and next view
700 if (mPreviousView != null) {
701 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700702 }
703 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700704 dismissPreview(mNextView);
705 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700706 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800707 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 }
Romain Guycbb89e42009-06-08 15:52:54 -0700709
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700710 @Override
711 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712 // Flag the loader to stop early before switching
713 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800714 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800715 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700716 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800718 // We can't hide the IME if it was forced open. So don't bother
719 /*
720 @Override
721 public void onWindowFocusChanged(boolean hasFocus) {
722 super.onWindowFocusChanged(hasFocus);
723
724 if (hasFocus) {
725 final InputMethodManager inputManager = (InputMethodManager)
726 getSystemService(Context.INPUT_METHOD_SERVICE);
727 WindowManager.LayoutParams lp = getWindow().getAttributes();
728 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
729 android.os.Handler()) {
730 protected void onReceiveResult(int resultCode, Bundle resultData) {
731 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
732 }
733 });
734 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
735 }
736 }
737 */
738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 private boolean acceptFilter() {
740 final InputMethodManager inputManager = (InputMethodManager)
741 getSystemService(Context.INPUT_METHOD_SERVICE);
742 return !inputManager.isFullscreenMode();
743 }
744
745 @Override
746 public boolean onKeyDown(int keyCode, KeyEvent event) {
747 boolean handled = super.onKeyDown(keyCode, event);
748 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
749 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
750 keyCode, event);
751 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700752 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700753 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700754 // showSearchDialog()
755 // If there are multiple keystrokes before the search dialog takes focus,
756 // onSearchRequested() will be called for every keystroke,
757 // but it is idempotent, so it's fine.
758 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760 }
761
Joe Onorato8a9625e2010-01-28 15:55:35 -0800762 // Eat the long press event so the keyboard doesn't come up.
763 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
764 return true;
765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 return handled;
768 }
769
Karl Rosaen138a0412009-04-23 19:00:21 -0700770 private String getTypedText() {
771 return mDefaultKeySsb.toString();
772 }
773
774 private void clearTypedText() {
775 mDefaultKeySsb.clear();
776 mDefaultKeySsb.clearSpans();
777 Selection.setSelection(mDefaultKeySsb, 0);
778 }
779
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700781 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
782 * State
783 */
784 private static State intToState(int stateOrdinal) {
785 State state = State.WORKSPACE;
786 final State[] stateValues = State.values();
787 for (int i = 0; i < stateValues.length; i++) {
788 if (stateValues[i].ordinal() == stateOrdinal) {
789 state = stateValues[i];
790 break;
791 }
792 }
793 return state;
794 }
795
796 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 * Restores the previous state, if it exists.
798 *
799 * @param savedState The previous state.
800 */
801 private void restoreState(Bundle savedState) {
802 if (savedState == null) {
803 return;
804 }
805
Michael Jurka883f55b2010-10-21 15:47:14 -0700806 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
807
808 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800809 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700810 } else if (state == State.CUSTOMIZE) {
811 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
815 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700816 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
818
819 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700822 mAddScreen = addScreen;
823 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
824 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 mRestoring = true;
826 }
827
828 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
829 if (renameFolder) {
830 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700831 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 mRestoring = true;
833 }
834 }
835
836 /**
837 * Finds all the views we need and configure them properly.
838 */
839 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700840 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400841
Romain Guyb1b69f52009-08-10 15:10:15 -0700842 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400843 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
Joe Onorato7c312c12009-08-13 21:36:53 -0700845 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700846 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700847 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800848 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700849 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700850 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700851
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700852 if (LauncherApplication.isScreenXLarge()) {
853 // They need to be INVISIBLE initially so that they will be measured in the layout.
854 // Otherwise the animations are messed up when we show them for the first time.
855 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
856 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
860 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500861 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700862
Joe Onorato7c312c12009-08-13 21:36:53 -0700863 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
864 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700866 View handleView = findViewById(R.id.all_apps_button);
867 if (handleView != null && handleView instanceof HandleView) {
868 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700869 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700870 mHandleView.setLauncher(this);
871 mHandleView.setOnClickListener(this);
872 mHandleView.setOnLongClickListener(this);
873 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800874
Winson Chung80baf5a2010-08-09 16:03:15 -0700875 if (mCustomizePagedView != null) {
876 mCustomizePagedView.setLauncher(this);
877 mCustomizePagedView.setDragController(dragController);
878 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700879 } else {
880 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
881 hotseatLeft.setContentDescription(mHotseatLabels[0]);
882 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
883 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
884 hotseatRight.setContentDescription(mHotseatLabels[1]);
885 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400886
Michael Jurkaaf442092010-06-10 17:01:57 -0700887 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
888 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800889
Michael Jurkaaf442092010-06-10 17:01:57 -0700890 Drawable previous = mPreviousView.getDrawable();
891 Drawable next = mNextView.getDrawable();
892 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893
Michael Jurkaaf442092010-06-10 17:01:57 -0700894 mPreviousView.setHapticFeedbackEnabled(false);
895 mPreviousView.setOnLongClickListener(this);
896 mNextView.setHapticFeedbackEnabled(false);
897 mNextView.setOnLongClickListener(this);
898 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800899
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400901 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
904 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400905 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700906 int deleteZoneHandleId;
907 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700908 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700909 } else {
910 deleteZoneHandleId = R.id.all_apps_button_cluster;
911 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700912 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700913 dragController.addDragListener(deleteZone);
914
915 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
916 if (infoButton != null) {
917 infoButton.setLauncher(this);
918 infoButton.setHandle(findViewById(R.id.configure_button));
919 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
920 dragController.addDragListener(infoButton);
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
Joe Onorato00acb122009-08-04 16:04:30 -0400923 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400924 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800925 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700926
Joe Onorato00acb122009-08-04 16:04:30 -0400927 // The order here is bottom to top.
928 dragController.addDropTarget(workspace);
929 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700930 if (infoButton != null) {
931 dragController.addDropTarget(infoButton);
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
Romain Guy8a73c512009-11-09 19:19:59 -0800935 @SuppressWarnings({"UnusedDeclaration"})
936 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700937 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800938 mWorkspace.scrollLeft();
939 }
Romain Guy8a73c512009-11-09 19:19:59 -0800940 }
941
942 @SuppressWarnings({"UnusedDeclaration"})
943 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700944 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800945 mWorkspace.scrollRight();
946 }
Romain Guy8a73c512009-11-09 19:19:59 -0800947 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400948
949 @SuppressWarnings({"UnusedDeclaration"})
950 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700951 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400952
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400953 int index = -1;
954 if (v.getId() == R.id.hotseat_left) {
955 index = 0;
956 } else if (v.getId() == R.id.hotseat_right) {
957 index = 1;
958 }
959
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400960 // reload these every tap; you never know when they might change
961 loadHotseats();
962 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
963 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400964 startActivitySafely(
965 mHotseats[index],
966 "hotseat"
967 );
968 }
969 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 /**
972 * Creates a view representing a shortcut.
973 *
974 * @param info The data structure describing the shortcut.
975 *
976 * @return A View inflated from R.layout.application.
977 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700980 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982
983 /**
984 * Creates a view representing a shortcut inflated from the specified resource.
985 *
986 * @param layoutResId The id of the XML layout used to create the shortcut.
987 * @param parent The group the shortcut belongs to.
988 * @param info The data structure describing the shortcut.
989 *
990 * @return A View inflated from layoutResId.
991 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800992 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
994
Winson Chung0e41ae62010-10-27 18:10:32 -0700995 // Temporarily, we are scaling up all shortcuts on the workspace
996 int scaledSize = parent.getResources().getDimensionPixelSize(R.dimen.temp_scaled_icon_size);
997 Bitmap b = Bitmap.createScaledBitmap(info.getIcon(mIconCache), scaledSize, scaledSize, true);
998
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -07001000 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 favorite.setText(info.title);
1003 favorite.setTag(info);
1004 favorite.setOnClickListener(this);
1005
1006 return favorite;
1007 }
1008
1009 /**
1010 * Add an application shortcut to the workspace.
1011 *
1012 * @param data The intent describing the application.
1013 * @param cellInfo The position on screen where to create the shortcut.
1014 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001015 void completeAddApplication(Context context, Intent data, int screen,
1016 int intersectCellX, int intersectCellY) {
1017 final int[] cellXY = mTmpAddItemCellCoordinates;
1018 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1019
1020 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1021 showOutOfSpaceMessage();
1022 return;
1023 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024
Joe Onorato0589f0f2010-02-08 13:44:00 -08001025 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1026 data, context);
1027
Romain Guy73b979d2009-06-09 12:57:21 -07001028 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001029 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001031 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1033 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 } else {
1035 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037 }
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 /**
1040 * Add a shortcut to the workspace.
1041 *
1042 * @param data The intent describing the shortcut.
1043 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001045 private void completeAddShortcut(Intent data, int screen,
1046 int intersectCellX, int intersectCellY) {
1047 final int[] cellXY = mTmpAddItemCellCoordinates;
1048 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001049
Michael Jurka0280c3b2010-09-17 15:00:07 -07001050 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1051 showOutOfSpaceMessage();
1052 return;
1053 }
1054
1055 final ShortcutInfo info = mModel.addShortcut(
1056 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057
1058 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001060 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 }
1062 }
1063
Romain Guycbb89e42009-06-08 15:52:54 -07001064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001068 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001073
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001074 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1076 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001079 // We have saved the position to which the widget was dragged-- this really only matters
1080 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001081 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001082
1083 // For now, we don't save the coordinate where we dropped the icon because we're not
1084 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1085 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001086 int[] touchXY = null;
1087 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1088 touchXY = mAddDropPosition;
1089 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001090 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 boolean foundCellSpan = false;
1092 if (touchXY != null) {
1093 // when dragging and dropping, just find the closest free spot
1094 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1095 int[] result = screenLayout.findNearestVacantArea(
1096 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001097 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 foundCellSpan = !findNearestVacantAreaFailed;
1099 } else {
1100 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1101 // if we long pressed on an empty cell to bring up a menu,
1102 // make sure we intersect the empty cell
1103 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1104 mAddIntersectCellX, mAddIntersectCellY);
1105 } else {
1106 // if we went through the menu -> add, just find any spot
1107 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1108 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001109 }
1110
Michael Jurka0280c3b2010-09-17 15:00:07 -07001111 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001112 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001113 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001114 return;
1115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001117 // Build Launcher-specific widget info and save to database
1118 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 launcherInfo.spanX = spanXY[0];
1120 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 LauncherModel.addItemToDatabase(this, launcherInfo,
1123 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001124 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125
1126 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001130 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001132 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 void showOutOfSpaceMessage() {
1141 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1142 }
1143
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1145 mDesktopItems.remove(launcherInfo);
1146 launcherInfo.hostView = null;
1147 }
1148
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001149 public LauncherAppWidgetHost getAppWidgetHost() {
1150 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Winson Chunga9abd0e2010-10-27 17:18:37 -07001153 public LauncherModel getModel() {
1154 return mModel;
1155 }
1156
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001157 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001158 getWindow().closeAllPanels();
1159
1160 try {
1161 dismissDialog(DIALOG_CREATE_SHORTCUT);
1162 // Unlock the workspace if the dialog was showing
1163 } catch (Exception e) {
1164 // An exception is thrown if the dialog is not visible, which is fine
1165 }
1166
1167 try {
1168 dismissDialog(DIALOG_RENAME_FOLDER);
1169 // Unlock the workspace if the dialog was showing
1170 } catch (Exception e) {
1171 // An exception is thrown if the dialog is not visible, which is fine
1172 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001173
1174 // Whatever we were doing is hereby canceled.
1175 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001176 }
1177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 @Override
1179 protected void onNewIntent(Intent intent) {
1180 super.onNewIntent(intent);
1181
1182 // Close the menu
1183 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001184 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001185 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001186
Joe Onorato14f122b2009-11-19 14:06:36 -08001187 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1188 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001189
Michael Jurka4cb37242010-08-09 21:05:32 -07001190 // in all these cases, only animate if we're already on home
1191 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001192 mWorkspace.unshrink(alreadyOnHome);
1193 }
1194 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001195 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001196 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1197 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001198 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001199 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001200
Joe Onorato3a8820b2009-11-10 15:06:42 -08001201 final View v = getWindow().peekDecorView();
1202 if (v != null && v.getWindowToken() != null) {
1203 InputMethodManager imm = (InputMethodManager)getSystemService(
1204 INPUT_METHOD_SERVICE);
1205 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
1207 }
1208 }
1209
1210 @Override
1211 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1212 // Do not call super here
1213 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001214
1215 if (mHomeCustomizationDrawer != null) {
1216 String cur = savedInstanceState.getString("currentTab");
1217 if (cur != null) {
1218 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1219 }
1220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
1222
1223 @Override
1224 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001225 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226
1227 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1228 if (folders.size() > 0) {
1229 final int count = folders.size();
1230 long[] ids = new long[count];
1231 for (int i = 0; i < count; i++) {
1232 final FolderInfo info = folders.get(i).getInfo();
1233 ids[i] = info.id;
1234 }
1235 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1236 } else {
1237 super.onSaveInstanceState(outState);
1238 }
1239
Michael Jurka883f55b2010-10-21 15:47:14 -07001240 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 if (mAddScreen > -1 && mWaitingForResult) {
1243 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1244 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1245 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 }
1247
1248 if (mFolderInfo != null && mWaitingForResult) {
1249 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1250 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1251 }
Michael Jurka946ad472010-07-09 18:05:18 -07001252
1253 if (mHomeCustomizationDrawer != null) {
1254 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1255 if (currentTabTag != null) {
1256 outState.putString("currentTab", currentTabTag);
1257 }
1258 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
1261 @Override
1262 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001266 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001268 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 TextKeyListener.getInstance().release();
1272
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274
Joe Onorato9c1289c2009-08-17 11:03:03 -04001275 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001276
1277 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001278
Patrick Dubroyab962b72010-07-26 12:10:10 -07001279 // Some launcher layouts don't have a previous and next view
1280 if (mPreviousView != null) {
1281 dismissPreview(mPreviousView);
1282 }
1283 if (mNextView != null) {
1284 dismissPreview(mNextView);
1285 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001286
1287 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
1289
1290 @Override
1291 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001292 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 super.startActivityForResult(intent, requestCode);
1294 }
1295
1296 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001297 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001299
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001300 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001301
Karl Rosaen138a0412009-04-23 19:00:21 -07001302 if (initialQuery == null) {
1303 // Use any text typed in the launcher as the initial query
1304 initialQuery = getTypedText();
1305 clearTypedText();
1306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 if (appSearchData == null) {
1308 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001309 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
Romain Guycbb89e42009-06-08 15:52:54 -07001311
Karl Rosaen138a0412009-04-23 19:00:21 -07001312 final SearchManager searchManager =
1313 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001314 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001315 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
1317
1318 @Override
1319 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001320 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 return false;
1322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323
1324 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1327 .setIcon(android.R.drawable.ic_menu_add)
1328 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001329 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1330 .setIcon(android.R.drawable.ic_menu_manage)
1331 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001332 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 .setIcon(android.R.drawable.ic_menu_gallery)
1334 .setAlphabeticShortcut('W');
1335 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1336 .setIcon(android.R.drawable.ic_search_category_default)
1337 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1338 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1339 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1340 .setAlphabeticShortcut('N');
1341
1342 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001343 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1344 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345
1346 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1347 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1348 .setIntent(settings);
1349
1350 return true;
1351 }
1352
1353 @Override
1354 public boolean onPrepareOptionsMenu(Menu menu) {
1355 super.onPrepareOptionsMenu(menu);
1356
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001357 // If all apps is animating, don't show the menu, because we don't know
1358 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001359 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001360 return false;
1361 }
1362
1363 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001364 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001365 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1366 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1367
1368 // Disable add if the workspace is full.
1369 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1371 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001372 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373
1374 return true;
1375 }
1376
1377 @Override
1378 public boolean onOptionsItemSelected(MenuItem item) {
1379 switch (item.getItemId()) {
1380 case MENU_ADD:
1381 addItems();
1382 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001383 case MENU_MANAGE_APPS:
1384 manageApps();
1385 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 case MENU_WALLPAPER_SETTINGS:
1387 startWallpaper();
1388 return true;
1389 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001390 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 return true;
1392 case MENU_NOTIFICATIONS:
1393 showNotifications();
1394 return true;
1395 }
1396
1397 return super.onOptionsItemSelected(item);
1398 }
Romain Guycbb89e42009-06-08 15:52:54 -07001399
Karl Rosaen138a0412009-04-23 19:00:21 -07001400 /**
1401 * Indicates that we want global search for this activity by setting the globalSearch
1402 * argument for {@link #startSearch} to true.
1403 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001405 @Override
1406 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001407 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001408 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001409 }
1410
Joe Onorato9c1289c2009-08-17 11:03:03 -04001411 public boolean isWorkspaceLocked() {
1412 return mWorkspaceLoading || mWaitingForResult;
1413 }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001416 if (LauncherApplication.isScreenXLarge()) {
1417 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001418 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001419 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001420 }
1421 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001422 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001423 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
Michael Jurka0280c3b2010-09-17 15:00:07 -07001427 private void resetAddInfo() {
1428 mAddScreen = -1;
1429 mAddIntersectCellX = -1;
1430 mAddIntersectCellY = -1;
1431 mAddDropPosition = null;
1432 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001433
Winson Chung55cef262010-10-28 14:14:18 -07001434 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001435 resetAddInfo();
1436 mAddScreen = screen;
1437
1438 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1439 mAddDropPosition = position;
1440
Michael Jurkaaf442092010-06-10 17:01:57 -07001441 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001442 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1443 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001444 }
1445
Winson Chung7ad01412010-09-27 11:33:03 -07001446 private void manageApps() {
1447 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1448 }
1449
Michael Jurkaaf442092010-06-10 17:01:57 -07001450 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001451 // TODO: catch bad widget exception when sent
1452 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001453 // TODO: Is this log message meaningful?
1454 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001455 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001456 }
1457
Winson Chung55cef262010-10-28 14:14:18 -07001458 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001459 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460
Bjorn Bringert7984c942009-12-09 15:38:25 +00001461 if (appWidget.configure != null) {
1462 // Launch over to configure widget, if needed
1463 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1464 intent.setComponent(appWidget.configure);
1465 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001466 if (info != null) {
1467 intent.putExtra(InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA,
1468 info.configurationData);
1469 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001470
Romain Guy8e633c52010-03-04 12:51:36 -08001471 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001473 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001474 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476 }
Romain Guycbb89e42009-06-08 15:52:54 -07001477
Michael Jurka0280c3b2010-09-17 15:00:07 -07001478 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1479 resetAddInfo();
1480 mAddScreen = screen;
1481 mAddDropPosition = position;
1482
1483 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1484 createShortcutIntent.setComponent(componentName);
1485 processShortcut(createShortcutIntent);
1486 }
1487
Joe Onoratodeb98af2010-02-19 14:59:39 -08001488 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001489 // Handle case where user selected "Applications"
1490 String applicationName = getResources().getString(R.string.group_applications);
1491 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001492
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001493 if (applicationName != null && applicationName.equals(shortcutName)) {
1494 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1495 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001496
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001497 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1498 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001499 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001500 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001501 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001502 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505
Winson Chung24ab2f12010-09-16 14:10:47 -07001506 void processWallpaper(Intent intent) {
1507 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1508 }
1509
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1511 resetAddInfo();
1512 mAddScreen = screen;
1513 mAddDropPosition = position;
1514
1515 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1516 createFolderIntent.setComponent(componentName);
1517
1518 addLiveFolder(createFolderIntent);
1519 }
1520
1521 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001522 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001523 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001524 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001526 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001528 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001529 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
Michael Jurka0280c3b2010-09-17 15:00:07 -07001533 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 UserFolderInfo folderInfo = new UserFolderInfo();
1535 folderInfo.title = getText(R.string.folder_name);
1536
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1538 final int[] cellXY = mTmpAddItemCellCoordinates;
1539 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1540 showOutOfSpaceMessage();
1541 return;
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001545 LauncherModel.addItemToDatabase(this, folderInfo,
1546 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001548 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 // Create the view
1551 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001552 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001557 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001558 }
1559
Michael Jurka28750fb2010-09-24 17:43:49 -07001560 private void completeAddLiveFolder(
1561 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1563 final int[] cellXY = mTmpAddItemCellCoordinates;
1564 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1565 showOutOfSpaceMessage();
1566 return;
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568
Michael Jurka0280c3b2010-09-17 15:00:07 -07001569 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001573 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
1576 }
1577
1578 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001579 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1582 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1583
1584 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 Intent.ShortcutIconResource iconResource = null;
1586
1587 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1588 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1589 try {
1590 iconResource = (Intent.ShortcutIconResource) extra;
1591 final PackageManager packageManager = context.getPackageManager();
1592 Resources resources = packageManager.getResourcesForApplication(
1593 iconResource.packageName);
1594 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1595 icon = resources.getDrawable(id);
1596 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001597 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599 }
1600
1601 if (icon == null) {
1602 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1603 }
1604
1605 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001606 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 info.title = name;
1608 info.iconResource = iconResource;
1609 info.uri = data.getData();
1610 info.baseIntent = baseIntent;
1611 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1612 LiveFolders.DISPLAY_MODE_GRID);
1613
1614 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001616 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
1618 return info;
1619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 private void showNotifications() {
1622 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1623 if (statusBar != null) {
1624 statusBar.expand();
1625 }
1626 }
1627
1628 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001629 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001631 Intent chooser = Intent.createChooser(pickWallpaper,
1632 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001633 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1634 // Removed in Eclair MR1
1635// WallpaperManager wm = (WallpaperManager)
1636// getSystemService(Context.WALLPAPER_SERVICE);
1637// WallpaperInfo wi = wm.getWallpaperInfo();
1638// if (wi != null && wi.getSettingsActivity() != null) {
1639// LabeledIntent li = new LabeledIntent(getPackageName(),
1640// R.string.configure_wallpaper, 0);
1641// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1642// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1643// }
Mike Clerona0618e42009-10-22 13:55:21 -07001644 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001647 /**
1648 * Registers various content observers. The current implementation registers
1649 * only a favorites observer to keep track of the favorites applications.
1650 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001651 private void registerContentObservers() {
1652 ContentResolver resolver = getContentResolver();
1653 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1654 true, mWidgetObserver);
1655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 @Override
1658 public boolean dispatchKeyEvent(KeyEvent event) {
1659 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1660 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001662 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001663 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001664 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001665 dumpState();
1666 return true;
1667 }
1668 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001669 }
1670 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1671 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001672 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 return true;
1674 }
1675 }
1676
1677 return super.dispatchKeyEvent(event);
1678 }
1679
Joe Onorato88ec0992009-11-19 13:16:06 -08001680 @Override
1681 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001682 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1683 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001684 } else {
1685 closeFolder();
1686 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001687 // Some launcher layouts don't have a previous and next view
1688 if (mPreviousView != null) {
1689 dismissPreview(mPreviousView);
1690 dismissPreview(mNextView);
1691 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001692 }
1693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 private void closeFolder() {
1695 Folder folder = mWorkspace.getOpenFolder();
1696 if (folder != null) {
1697 closeFolder(folder);
1698 }
1699 }
1700
1701 void closeFolder(Folder folder) {
1702 folder.getInfo().opened = false;
1703 ViewGroup parent = (ViewGroup) folder.getParent();
1704 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001705 CellLayout cl = (CellLayout) parent;
1706 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001707 if (folder instanceof DropTarget) {
1708 // Live folders aren't DropTargets.
1709 mDragController.removeDropTarget((DropTarget)folder);
1710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 }
1712 folder.onClose();
1713 }
1714
1715 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001716 * Re-listen when widgets are reset.
1717 */
1718 private void onAppWidgetReset() {
1719 mAppWidgetHost.startListening();
1720 }
1721
1722 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001723 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1724 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001726 private void unbindDesktopItems() {
1727 for (ItemInfo item: mDesktopItems) {
1728 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001731
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 /**
1733 * Launches the intent referred by the clicked shortcut.
1734 *
1735 * @param v The view representing the clicked shortcut.
1736 */
1737 public void onClick(View v) {
1738 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001739 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001741 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001742 int[] pos = new int[2];
1743 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001744 intent.setSourceBounds(new Rect(pos[0], pos[1],
1745 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001746 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 } else if (tag instanceof FolderInfo) {
1748 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001749 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001750 if (mState == State.ALL_APPS) {
1751 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001752 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001753 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 }
1756 }
1757
Michael Jurka0e260592010-06-30 17:07:39 -07001758 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001759 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001760 // clicking anywhere on the workspace causes the customization drawer to slide down
1761 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001762 return false;
1763 }
1764
Michael Jurkaaf442092010-06-10 17:01:57 -07001765 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001766 * Event handler for the search button
1767 *
1768 * @param v The view that was clicked.
1769 */
1770 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001771 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001772 }
1773
1774 /**
1775 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001776 * enters home screen customization mode.
1777 *
1778 * @param v The view that was clicked.
1779 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001780 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001781 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001782 }
1783
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001784 /**
1785 * Event handler for the "grid" button that appears on the home screen, which
1786 * enters all apps mode.
1787 *
1788 * @param v The view that was clicked.
1789 */
1790 public void onClickAllAppsButton(View v) {
1791 showAllApps(true);
1792 }
1793
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001794 public void onClickAppMarketButton(View v) {
1795 if (mAppMarketIntent != null) {
1796 startActivitySafely(mAppMarketIntent, "app market");
1797 }
1798 }
1799
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001800 void startApplicationDetailsActivity(ComponentName componentName) {
1801 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001802 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1803 Uri.fromParts("package", packageName, null));
1804 startActivity(intent);
1805 }
1806
Patrick Dubroy5539af72010-09-07 15:22:01 -07001807 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1808 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1809 // System applications cannot be installed. For now, show a toast explaining that.
1810 // We may give them the option of disabling apps this way.
1811 int messageId = R.string.uninstall_system_app_text;
1812 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1813 } else {
1814 String packageName = appInfo.componentName.getPackageName();
1815 String className = appInfo.componentName.getClassName();
1816 Intent intent = new Intent(
1817 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1818 startActivity(intent);
1819 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001820 }
1821
Joe Onoratof984e852010-03-25 09:47:45 -07001822 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1824 try {
1825 startActivity(intent);
1826 } catch (ActivityNotFoundException e) {
1827 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001828 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 } catch (SecurityException e) {
1830 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001831 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001833 "or use the exported attribute for this activity. "
1834 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001837
Romain Guy8e633c52010-03-04 12:51:36 -08001838 void startActivityForResultSafely(Intent intent, int requestCode) {
1839 try {
1840 startActivityForResult(intent, requestCode);
1841 } catch (ActivityNotFoundException e) {
1842 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1843 } catch (SecurityException e) {
1844 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1845 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1846 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1847 "or use the exported attribute for this activity.", e);
1848 }
1849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850
1851 private void handleFolderClick(FolderInfo folderInfo) {
1852 if (!folderInfo.opened) {
1853 // Close any open folder
1854 closeFolder();
1855 // Open the requested folder
1856 openFolder(folderInfo);
1857 } else {
1858 // Find the open folder...
1859 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1860 int folderScreen;
1861 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001862 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 // .. and close it
1864 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001865 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 // Close any folder open on the current screen
1867 closeFolder();
1868 // Pull the folder onto this screen
1869 openFolder(folderInfo);
1870 }
1871 }
1872 }
1873 }
1874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001876 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 * is animated relative to the specified View. If the View is null, no animation
1878 * is played.
1879 *
1880 * @param folderInfo The FolderInfo describing the folder to open.
1881 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001882 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 Folder openFolder;
1884
1885 if (folderInfo instanceof UserFolderInfo) {
1886 openFolder = UserFolder.fromXml(this);
1887 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001888 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 } else {
1890 return;
1891 }
1892
Joe Onorato00acb122009-08-04 16:04:30 -04001893 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 openFolder.setLauncher(this);
1895
1896 openFolder.bind(folderInfo);
1897 folderInfo.opened = true;
1898
Winson Chungaafa03c2010-06-11 17:34:16 -07001899 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 openFolder.onOpen();
1902 }
1903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001905 switch (v.getId()) {
1906 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001907 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001908 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1909 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001910 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001911 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001912 return true;
1913 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001914 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001915 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1916 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001917 showPreviews(v);
1918 }
1919 return true;
1920 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001921 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001922 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1923 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1924 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001925 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001926 return true;
1927 }
1928
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 return false;
1931 }
1932
1933 if (!(v instanceof CellLayout)) {
1934 v = (View) v.getParent();
1935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937
Michael Jurka0280c3b2010-09-17 15:00:07 -07001938 resetAddInfo();
1939 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001941 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 return true;
1943 }
1944
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945 final View itemUnderLongClick = longClickCellInfo.cell;
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 if (itemUnderLongClick == null) {
1949 // User long pressed on empty space
1950 mWorkspace.setAllowLongPress(false);
1951 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1952 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001953 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001954 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001957 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001959 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1960 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001961 mAddIntersectCellX = longClickCellInfo.cellX;
1962 mAddIntersectCellY = longClickCellInfo.cellY;
1963 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965 }
1966 }
1967 return true;
1968 }
1969
Romain Guye6b8e2f2009-11-10 11:56:55 -08001970 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001971 private void dismissPreview(final View v) {
1972 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001973 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001974 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1975 public void onDismiss() {
1976 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1977 int count = group.getChildCount();
1978 for (int i = 0; i < count; i++) {
1979 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1980 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001981 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1982 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1983
1984 v.setTag(R.id.workspace, null);
1985 v.setTag(R.id.icon, null);
1986 window.setOnDismissListener(null);
1987 }
1988 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001989 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001990 }
1991 v.setTag(null);
1992 }
1993
Romain Guyf8e6a802009-12-07 17:48:02 -08001994 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001995 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001996 }
1997
Romain Guya6abce82009-11-10 02:54:41 -08001998 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001999 final Resources resources = getResources();
2000 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002001
Romain Guya6abce82009-11-10 02:54:41 -08002002 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002003
Romain Guy9d31e9f2009-11-11 18:54:28 -08002004 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002005
Romain Guyf8e6a802009-12-07 17:48:02 -08002006 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002007 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002008 int extraW = (int) ((r.left + r.right) * max);
2009 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002010
2011 int aW = cell.getWidth() - extraW;
2012 float w = aW / max;
2013
2014 int width = cell.getWidth();
2015 int height = cell.getHeight();
2016 int x = cell.getLeftPadding();
2017 int y = cell.getTopPadding();
2018 width -= (x + cell.getRightPadding());
2019 height -= (y + cell.getBottomPadding());
2020
2021 float scale = w / width;
2022
2023 int count = end - start;
2024
2025 final float sWidth = width * scale;
2026 float sHeight = height * scale;
2027
Romain Guye6b8e2f2009-11-10 11:56:55 -08002028 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002029
Romain Guye6b8e2f2009-11-10 11:56:55 -08002030 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2031 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002032
2033 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002034 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002035 cell = (CellLayout) workspace.getChildAt(i);
2036
Romain Guyf8e6a802009-12-07 17:48:02 -08002037 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002038 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002039
2040 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002041 c.scale(scale, scale);
2042 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002043 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002044
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002046 image.setImageBitmap(bitmap);
2047 image.setTag(i);
2048 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002049 image.setOnFocusChangeListener(handler);
2050 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002051 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002052
2053 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002054 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2055
Winson Chungaafa03c2010-06-11 17:34:16 -07002056 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002057 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002058
2059 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002060 p.setContentView(preview);
2061 p.setWidth((int) (sWidth * count + extraW));
2062 p.setHeight((int) (sHeight + extraH));
2063 p.setAnimationStyle(R.style.AnimationPreview);
2064 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002065 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002066 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002067 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002068
Romain Guye6b8e2f2009-11-10 11:56:55 -08002069 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2070 public void onDismiss() {
2071 dismissPreview(anchor);
2072 }
2073 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002074
2075 anchor.setTag(p);
2076 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002077 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002078 }
2079
Romain Guy9d31e9f2009-11-11 18:54:28 -08002080 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002081 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002082
Romain Guye6b8e2f2009-11-10 11:56:55 -08002083 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002084 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002085 }
2086
2087 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002088 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002089 v.post(this);
2090 }
2091
2092 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002093 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002094 }
2095
2096 public void onFocusChange(View v, boolean hasFocus) {
2097 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002098 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002099 }
Romain Guya6abce82009-11-10 02:54:41 -08002100 }
2101 }
2102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 Workspace getWorkspace() {
2104 return mWorkspace;
2105 }
2106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 @Override
2108 protected Dialog onCreateDialog(int id) {
2109 switch (id) {
2110 case DIALOG_CREATE_SHORTCUT:
2111 return new CreateShortcut().createDialog();
2112 case DIALOG_RENAME_FOLDER:
2113 return new RenameFolder().createDialog();
2114 }
2115
2116 return super.onCreateDialog(id);
2117 }
2118
2119 @Override
2120 protected void onPrepareDialog(int id, Dialog dialog) {
2121 switch (id) {
2122 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 break;
2124 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002125 if (mFolderInfo != null) {
2126 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2127 final CharSequence text = mFolderInfo.title;
2128 input.setText(text);
2129 input.setSelection(0, text.length());
2130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 break;
2132 }
2133 }
2134
2135 void showRenameDialog(FolderInfo info) {
2136 mFolderInfo = info;
2137 mWaitingForResult = true;
2138 showDialog(DIALOG_RENAME_FOLDER);
2139 }
2140
Michael Jurka0280c3b2010-09-17 15:00:07 -07002141 private void showAddDialog(int intersectX, int intersectY) {
2142 resetAddInfo();
2143 mAddIntersectCellX = intersectX;
2144 mAddIntersectCellY = intersectY;
2145 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 mWaitingForResult = true;
2147 showDialog(DIALOG_CREATE_SHORTCUT);
2148 }
2149
Joe Onoratodeb98af2010-02-19 14:59:39 -08002150 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002151 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002152 Bundle bundle = new Bundle();
2153
2154 ArrayList<String> shortcutNames = new ArrayList<String>();
2155 shortcutNames.add(getString(R.string.group_applications));
2156 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2157
2158 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2159 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2160 R.drawable.ic_launcher_application));
2161 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2162
2163 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2164 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002165 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002166 pickIntent.putExtras(bundle);
2167
Joe Onoratodeb98af2010-02-19 14:59:39 -08002168 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002169 }
2170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 private class RenameFolder {
2172 private EditText mInput;
2173
2174 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2176 mInput = (EditText) layout.findViewById(R.id.folder_name);
2177
2178 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2179 builder.setIcon(0);
2180 builder.setTitle(getString(R.string.rename_folder_title));
2181 builder.setCancelable(true);
2182 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2183 public void onCancel(DialogInterface dialog) {
2184 cleanup();
2185 }
2186 });
2187 builder.setNegativeButton(getString(R.string.cancel_action),
2188 new Dialog.OnClickListener() {
2189 public void onClick(DialogInterface dialog, int which) {
2190 cleanup();
2191 }
2192 }
2193 );
2194 builder.setPositiveButton(getString(R.string.rename_action),
2195 new Dialog.OnClickListener() {
2196 public void onClick(DialogInterface dialog, int which) {
2197 changeFolderName();
2198 }
2199 }
2200 );
2201 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002202
2203 final AlertDialog dialog = builder.create();
2204 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2205 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002206 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002207 mInput.requestFocus();
2208 InputMethodManager inputManager = (InputMethodManager)
2209 getSystemService(Context.INPUT_METHOD_SERVICE);
2210 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002211 }
2212 });
2213
2214 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 }
2216
2217 private void changeFolderName() {
2218 final String name = mInput.getText().toString();
2219 if (!TextUtils.isEmpty(name)) {
2220 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002221 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 mFolderInfo.title = name;
2223 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2224
Joe Onorato9c1289c2009-08-17 11:03:03 -04002225 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002226 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 } else {
2229 final FolderIcon folderIcon = (FolderIcon)
2230 mWorkspace.getViewForTag(mFolderInfo);
2231 if (folderIcon != null) {
2232 folderIcon.setText(name);
2233 getWorkspace().requestLayout();
2234 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002235 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002236 mWorkspaceLoading = true;
2237 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 }
2239 }
2240 }
2241 cleanup();
2242 }
2243
2244 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 dismissDialog(DIALOG_RENAME_FOLDER);
2246 mWaitingForResult = false;
2247 mFolderInfo = null;
2248 }
2249 }
2250
Daniel Sandler843e8602010-06-07 14:59:01 -04002251 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2252 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002253 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002254 }
2255
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002256 // AllAppsView.Watcher
2257 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002258 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2259 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002260 mWorkspace.setVisibility(View.GONE);
2261 }
2262 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002263
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002264 private void showToolbarButton(View button) {
2265 button.setAlpha(1.0f);
2266 button.setVisibility(View.VISIBLE);
2267 button.setFocusable(true);
2268 button.setClickable(true);
2269 }
2270
2271 private void hideToolbarButton(View button) {
2272 button.setAlpha(0.0f);
2273 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2274 button.setVisibility(View.INVISIBLE);
2275 button.setFocusable(false);
2276 button.setClickable(false);
2277 }
2278
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002279 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002280 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002281 *
2282 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2283 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002284 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002285 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002286 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002287 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002288 final boolean showing = show;
2289 final boolean hiding = !show;
2290
2291 final int duration = show ?
2292 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2293 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2294
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002295 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002296 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2297 anim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -07002298 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002299 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002300 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002301 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002302 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002303 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002304 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002305 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 });
2308 seq.play(anim);
2309 } else {
2310 if (showing) {
2311 showToolbarButton(view);
2312 } else {
2313 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002314 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002315 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002316 }
2317
2318 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002319 * Show/hide the appropriate toolbar buttons for newState.
2320 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002321 *
2322 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002323 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2324 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002325 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002326 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002327 final View searchButton = findViewById(R.id.search_button);
2328 final View allAppsButton = findViewById(R.id.all_apps_button);
2329 final View marketButton = findViewById(R.id.market_button);
2330 final View configureButton = findViewById(R.id.configure_button);
2331
2332 switch (newState) {
2333 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002334 hideOrShowToolbarButton(true, searchButton, showSeq);
2335 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2336 hideOrShowToolbarButton(true, configureButton, showSeq);
2337 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002338 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002339 break;
2340 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002341 hideOrShowToolbarButton(true, configureButton, showSeq);
2342 hideOrShowToolbarButton(true, marketButton, showSeq);
2343 hideOrShowToolbarButton(false, searchButton, hideSeq);
2344 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002345 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002346 break;
2347 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002348 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2349 hideOrShowToolbarButton(false, searchButton, hideSeq);
2350 hideOrShowToolbarButton(false, marketButton, hideSeq);
2351 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002352 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002353 break;
2354 }
2355 }
2356
2357 /**
2358 * Helper method for the cameraZoomIn/cameraZoomOut animations
2359 * @param view The view being animated
2360 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2361 * @param scaleFactor The scale factor used for the zoom
2362 */
2363 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2364 final int height = view.getHeight();
2365 view.setPivotX(view.getWidth() / 2.0f);
2366 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2367 // Assumes that the view is normally anchored to either the top or bottom of the screen
2368 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2369 if (state == State.ALL_APPS) {
2370 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2371 } else {
2372 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2373 }
2374 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002375
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002376 /**
2377 * Zoom the camera out from the workspace to reveal 'toView'.
2378 * Assumes that the view to show is anchored at either the very top or very bottom
2379 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002380 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002381 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002382 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002383 final Resources res = getResources();
2384 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2385 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002386 final boolean toAllApps = (toState == State.ALL_APPS);
2387 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002388
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002389 setPivotsForZoom(toView, toState, scale);
2390
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002391 if (toState == State.ALL_APPS) {
2392 mWorkspace.shrinkToBottom(animated);
2393 } else {
2394 mWorkspace.shrinkToTop(animated);
2395 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002396
2397 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002398 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2399 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2400 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2401 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002402 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002403 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002404 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002405 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406 // Prepare the position
2407 toView.setTranslationX(0.0f);
2408 toView.setTranslationY(0.0f);
2409 toView.setVisibility(View.VISIBLE);
2410 }
2411 });
2412
Chet Haaseb1254a62010-09-07 13:35:00 -07002413 AnimatorSet toolbarHideAnim = new AnimatorSet();
2414 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2416
2417 // toView should appear right at the end of the workspace shrink animation
2418 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2419
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002420 if (mStateAnimation != null) mStateAnimation.cancel();
2421 mStateAnimation = new AnimatorSet();
2422 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2423 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002424
2425 // Show the new toolbar buttons just as the main animation is ending
2426 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002427 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2428 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002429 } else {
2430 toView.setTranslationX(0.0f);
2431 toView.setTranslationY(0.0f);
2432 toView.setScaleX(1.0f);
2433 toView.setScaleY(1.0f);
2434 toView.setVisibility(View.VISIBLE);
2435 hideAndShowToolbarButtons(toState, null, null);
2436 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002437 }
2438
2439 /**
2440 * Zoom the camera back into the workspace, hiding 'fromView'.
2441 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002442 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2443 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002444 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002445 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002446 Resources res = getResources();
2447 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2448 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002449 final View fromView =
2450 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2451
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002452 mCustomizePagedView.endChoiceMode();
2453 mAllAppsPagedView.endChoiceMode();
2454
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002455 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002457 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002458
2459 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002460 if (mStateAnimation != null) mStateAnimation.cancel();
2461 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002462 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2463 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2464 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2465 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002466 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002467 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002468 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002469 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002470 fromView.setVisibility(View.GONE);
2471 fromView.setScaleX(1.0f);
2472 fromView.setScaleY(1.0f);
2473 }
2474 });
2475
Chet Haaseb1254a62010-09-07 13:35:00 -07002476 AnimatorSet toolbarHideAnim = new AnimatorSet();
2477 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002478 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2479
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002480 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002481
2482 // Show the new toolbar buttons at the very end of the whole animation
2483 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2484 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002485 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2486 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002487 } else {
2488 fromView.setVisibility(View.GONE);
2489 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2490 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002491 }
2492
2493 /**
2494 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2495 * This is the transition used on xlarge screens to go between all apps and
2496 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002497 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2498 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2499 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002500 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002501 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002502 final Resources res = getResources();
2503 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002504 final int workspaceHeight = mWorkspace.getHeight();
2505
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002506 final boolean fromAllApps = (fromState == State.ALL_APPS);
2507 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2508 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002509
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2511 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2512 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2513 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002514
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002515 mCustomizePagedView.endChoiceMode();
2516 mAllAppsPagedView.endChoiceMode();
2517
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002518 if (toState == State.ALL_APPS) {
2519 mWorkspace.shrinkToBottom(animated);
2520 } else {
2521 mWorkspace.shrinkToTop(animated);
2522 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523
2524 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002525 if (mStateAnimation != null) mStateAnimation.cancel();
2526 mStateAnimation = new AnimatorSet();
2527 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002528 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002529 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 toView.setVisibility(View.VISIBLE);
2531 toView.setY(toViewStartY);
2532 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002533 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002534 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002535 fromView.setVisibility(View.GONE);
2536 }
2537 });
2538
Chet Haaseb1254a62010-09-07 13:35:00 -07002539 AnimatorSet toolbarHideAnim = new AnimatorSet();
2540 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2542
Chet Haase472b2812010-10-14 07:02:04 -07002543 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2544 fromViewStartY, fromViewEndY);
2545 fromAnim.setDuration(duration);
2546 ObjectAnimator toAnim = ObjectAnimator.ofFloat(toView, "y",
2547 toViewStartY, toViewEndY);
2548 fromAnim.setDuration(duration);
2549 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550
2551 // Show the new toolbar buttons just as the main animation is ending
2552 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002553 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2554 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002555 } else {
2556 fromView.setY(fromViewEndY);
2557 fromView.setVisibility(View.GONE);
2558 toView.setY(toViewEndY);
2559 toView.setVisibility(View.VISIBLE);
2560 hideAndShowToolbarButtons(toState, null, null);
2561 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002562 }
2563
2564 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002565 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002566 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002567 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002568
Michael Jurka79212d82010-07-30 16:36:20 -07002569 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002570 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002571 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002572 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002573 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002574 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002575 } else {
2576 mAllAppsGrid.zoom(1.0f, animated);
2577 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002578
Romain Guyc16fea72010-03-12 17:17:56 -08002579 ((View) mAllAppsGrid).setFocusable(true);
2580 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002581
Joe Onorato7c312c12009-08-13 21:36:53 -07002582 // TODO: fade these two too
2583 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002584 // Change the state *after* we've called all the transition code
2585 mState = State.ALL_APPS;
2586 }
2587
2588
2589 void showWorkspace(boolean animated) {
2590 showWorkspace(animated, null);
2591 }
2592
2593 void showWorkspace(boolean animated, CellLayout layout) {
2594 if (layout != null && animated) {
2595 mWorkspace.unshrink(layout);
2596 } else {
2597 mWorkspace.unshrink(animated);
2598 }
2599 if (mState == State.ALL_APPS) {
2600 closeAllApps(animated);
2601 } else if (mState == State.CUSTOMIZE) {
2602 hideCustomizationDrawer(animated);
2603 }
2604 // Change the state *after* we've called all the transition code
2605 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002606 }
2607
Joe Onoratob2061212009-11-24 16:13:54 -05002608 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002609 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002610 * - Home from workspace
2611 * - from center screen
2612 * - from other screens
2613 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002614 * - from center screen
2615 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002616 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002617 * - from center screen
2618 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002619 * - Launch app from workspace and quit
2620 * - with back
2621 * - with home
2622 * - Launch app from all apps and quit
2623 * - with back
2624 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002625 * - Go to a screen that's not the default, then all
2626 * apps, and launch and app, and go back
2627 * - with back
2628 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002629 * - On workspace, long press power and go back
2630 * - with back
2631 * - with home
2632 * - On all apps, long press power and go back
2633 * - with back
2634 * - with home
2635 * - On workspace, power off
2636 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002637 * - Launch an app and turn off the screen while in that app
2638 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002639 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002640 * - From all apps
2641 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002642 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2643 * - From all apps
2644 * - From the center workspace
2645 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002646 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002647 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002648 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002649 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002652 } else {
2653 mAllAppsGrid.zoom(0.0f, animated);
2654 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002655 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002656 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002657 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002658 }
2659
Joe Onorato7c312c12009-08-13 21:36:53 -07002660 void lockAllApps() {
2661 // TODO
2662 }
2663
2664 void unlockAllApps() {
2665 // TODO
2666 }
2667
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 // Show the customization drawer (only exists in x-large configuration)
2669 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002670 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002672 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002674 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002675 // Change the state *after* we've called all the transition code
2676 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002677 }
2678
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 // Hide the customization drawer (only exists in x-large configuration)
2680 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002681 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002683 }
2684 }
2685
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002686 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2687 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2688 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002689 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002690 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002691
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002692 void onWorkspaceClick(CellLayout layout) {
2693 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002694 }
2695
Michael Jurka0423dcf2010-10-05 14:56:18 -07002696 private void updateButtonWithIconFromExternalActivity(
2697 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2698 ImageView button = (ImageView) findViewById(buttonId);
2699 Drawable toolbarIcon = null;
2700 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002701 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002702 // Look for the toolbar icon specified in the activity meta-data
2703 Bundle metaData = packageManager.getActivityInfo(
2704 activityName, PackageManager.GET_META_DATA).metaData;
2705 if (metaData != null) {
2706 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2707 if (iconResId != 0) {
2708 Resources res = packageManager.getResourcesForActivity(activityName);
2709 toolbarIcon = res.getDrawable(iconResId);
2710 }
2711 }
2712 } catch (NameNotFoundException e) {
2713 // Do nothing
2714 }
2715 // If we were unable to find the icon via the meta-data, use a generic one
2716 if (toolbarIcon == null) {
2717 button.setImageResource(fallbackDrawableId);
2718 } else {
2719 button.setImageDrawable(toolbarIcon);
2720 }
2721 }
2722
2723 private void updateGlobalSearchIcon() {
2724 if (LauncherApplication.isScreenXLarge()) {
2725 final SearchManager searchManager =
2726 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2727 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002728 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002729 updateButtonWithIconFromExternalActivity(
2730 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002731 }
2732 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002733 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002734
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002735 /**
2736 * Sets the app market icon (shown when all apps is visible on x-large screens)
2737 */
2738 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002739 if (LauncherApplication.isScreenXLarge()) {
2740 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2741 // Find the app market activity by resolving an intent.
2742 // (If multiple app markets are installed, it will return the ResolverActivity.)
2743 ComponentName activityName = intent.resolveActivity(getPackageManager());
2744 if (activityName != null) {
2745 mAppMarketIntent = intent;
2746 updateButtonWithIconFromExternalActivity(
2747 R.id.market_button, activityName, R.drawable.app_market_generic);
2748 }
2749 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002750 }
2751
2752 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 * Displays the shortcut creation dialog and launches, if necessary, the
2754 * appropriate activity.
2755 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002756 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002757 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2758 DialogInterface.OnShowListener {
2759
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002764
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2766 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002767 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 builder.setInverseBackgroundForced(true);
2770
2771 AlertDialog dialog = builder.create();
2772 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002773 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002774 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 return dialog;
2777 }
2778
2779 public void onCancel(DialogInterface dialog) {
2780 mWaitingForResult = false;
2781 cleanup();
2782 }
2783
Romain Guycbb89e42009-06-08 15:52:54 -07002784 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002785 }
2786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002787 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002788 try {
2789 dismissDialog(DIALOG_CREATE_SHORTCUT);
2790 } catch (Exception e) {
2791 // An exception is thrown if the dialog is not visible, which is fine
2792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002793 }
2794
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002795 /**
2796 * Handle the action clicked in the "Add to home" dialog.
2797 */
2798 public void onClick(DialogInterface dialog, int which) {
2799 Resources res = getResources();
2800 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002801
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002802 switch (which) {
2803 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002804 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002805 break;
2806 }
Romain Guycbb89e42009-06-08 15:52:54 -07002807
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002808 case AddAdapter.ITEM_APPWIDGET: {
2809 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002811 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2812 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002813 // start the pick activity
2814 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2815 break;
2816 }
Romain Guycbb89e42009-06-08 15:52:54 -07002817
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002818 case AddAdapter.ITEM_LIVE_FOLDER: {
2819 // Insert extra item to handle inserting folder
2820 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002821
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002822 ArrayList<String> shortcutNames = new ArrayList<String>();
2823 shortcutNames.add(res.getString(R.string.group_folder));
2824 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002825
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002826 ArrayList<ShortcutIconResource> shortcutIcons =
2827 new ArrayList<ShortcutIconResource>();
2828 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2829 R.drawable.ic_launcher_folder));
2830 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2831
2832 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2833 pickIntent.putExtra(Intent.EXTRA_INTENT,
2834 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2835 pickIntent.putExtra(Intent.EXTRA_TITLE,
2836 getText(R.string.title_select_live_folder));
2837 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002838
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002839 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2840 break;
2841 }
2842
2843 case AddAdapter.ITEM_WALLPAPER: {
2844 startWallpaper();
2845 break;
2846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 }
2848 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002849
2850 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002851 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 }
2854
2855 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002856 * Receives notifications when applications are added/removed.
2857 */
2858 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2859 @Override
2860 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002861 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002862 String reason = intent.getStringExtra("reason");
2863 if (!"homekey".equals(reason)) {
2864 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002865 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002866 animate = false;
2867 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002868 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002869 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002870 }
2871 }
2872
2873 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002874 * Receives notifications whenever the appwidgets are reset.
2875 */
2876 private class AppWidgetResetObserver extends ContentObserver {
2877 public AppWidgetResetObserver() {
2878 super(new Handler());
2879 }
2880
2881 @Override
2882 public void onChange(boolean selfChange) {
2883 onAppWidgetReset();
2884 }
2885 }
2886
2887 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002888 * If the activity is currently paused, signal that we need to re-run the loader
2889 * in onResume.
2890 *
2891 * This needs to be called from incoming places where resources might have been loaded
2892 * while we are paused. That is becaues the Configuration might be wrong
2893 * when we're not running, and if it comes back to what it was when we
2894 * were paused, we are not restarted.
2895 *
2896 * Implementation of the method from LauncherModel.Callbacks.
2897 *
2898 * @return true if we are currently paused. The caller might be able to
2899 * skip some work in that case since we will come back again.
2900 */
2901 public boolean setLoadOnResume() {
2902 if (mPaused) {
2903 Log.i(TAG, "setLoadOnResume");
2904 mOnResumeNeedsLoad = true;
2905 return true;
2906 } else {
2907 return false;
2908 }
2909 }
2910
2911 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002912 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002915 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002916 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002917 } else {
2918 return SCREEN_COUNT / 2;
2919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002921
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002922 void setAllAppsPagedView(PagedView view) {
2923 mAllAppsPagedView = view;
2924 }
2925
Joe Onorato9c1289c2009-08-17 11:03:03 -04002926 /**
2927 * Refreshes the shortcuts shown on the workspace.
2928 *
2929 * Implementation of the method from LauncherModel.Callbacks.
2930 */
2931 public void startBinding() {
2932 final Workspace workspace = mWorkspace;
2933 int count = workspace.getChildCount();
2934 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002935 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002936 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2937 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002938
Joe Onorato9c1289c2009-08-17 11:03:03 -04002939 if (DEBUG_USER_INTERFACE) {
2940 android.widget.Button finishButton = new android.widget.Button(this);
2941 finishButton.setText("Finish");
2942 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2943
2944 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2945 public void onClick(View v) {
2946 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002947 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 }
2950 }
2951
2952 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 * Bind the items start-end from the list.
2954 *
2955 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2958
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002959 setLoadOnResume();
2960
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 final Workspace workspace = mWorkspace;
2962
2963 for (int i=start; i<end; i++) {
2964 final ItemInfo item = shortcuts.get(i);
2965 mDesktopItems.add(item);
2966 switch (item.itemType) {
2967 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2968 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002969 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002970 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2971 false);
2972 break;
2973 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2974 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002975 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 (UserFolderInfo) item);
2977 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2978 false);
2979 break;
2980 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2981 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2982 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002983 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 (LiveFolderInfo) item);
2985 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2986 false);
2987 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 }
2990
Joe Onorato9c1289c2009-08-17 11:03:03 -04002991 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 }
2993
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 /**
2995 * Implementation of the method from LauncherModel.Callbacks.
2996 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002997 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002998 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002999 sFolders.clear();
3000 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002
3003 /**
3004 * Add the views for a widget to the workspace.
3005 *
3006 * Implementation of the method from LauncherModel.Callbacks.
3007 */
3008 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003009 setLoadOnResume();
3010
Daniel Sandler843e8602010-06-07 14:59:01 -04003011 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3012 if (DEBUG_WIDGETS) {
3013 Log.d(TAG, "bindAppWidget: " + item);
3014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 final Workspace workspace = mWorkspace;
3016
3017 final int appWidgetId = item.appWidgetId;
3018 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003019 if (DEBUG_WIDGETS) {
3020 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3021 }
3022
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3024
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3026 item.hostView.setTag(item);
3027
3028 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3029 item.cellY, item.spanX, item.spanY, false);
3030
3031 workspace.requestLayout();
3032
3033 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003034
3035 if (DEBUG_WIDGETS) {
3036 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3037 + (SystemClock.uptimeMillis()-start) + "ms");
3038 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 }
3040
3041 /**
3042 * Callback saying that there aren't any more items to bind.
3043 *
3044 * Implementation of the method from LauncherModel.Callbacks.
3045 */
3046 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003047 setLoadOnResume();
3048
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 if (mSavedState != null) {
3050 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003051 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 }
3053
3054 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3055 if (userFolders != null) {
3056 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003057 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003058 if (info != null) {
3059 openFolder(info);
3060 }
3061 }
3062 final Folder openFolder = mWorkspace.getOpenFolder();
3063 if (openFolder != null) {
3064 openFolder.requestFocus();
3065 }
3066 }
3067
Joe Onorato9c1289c2009-08-17 11:03:03 -04003068 mSavedState = null;
3069 }
3070
3071 if (mSavedInstanceState != null) {
3072 super.onRestoreInstanceState(mSavedInstanceState);
3073 mSavedInstanceState = null;
3074 }
3075
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 mWorkspaceLoading = false;
3077 }
3078
3079 /**
3080 * Add the icons for all apps.
3081 *
3082 * Implementation of the method from LauncherModel.Callbacks.
3083 */
3084 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003085 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003086 if (mCustomizePagedView != null) {
3087 mCustomizePagedView.setApps(apps);
3088 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003089 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003090 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 }
3092
3093 /**
3094 * A package was installed.
3095 *
3096 * Implementation of the method from LauncherModel.Callbacks.
3097 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003098 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003099 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003101 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003102 if (mCustomizePagedView != null) {
3103 mCustomizePagedView.addApps(apps);
3104 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003105 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003106 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 }
3108
3109 /**
3110 * A package was updated.
3111 *
3112 * Implementation of the method from LauncherModel.Callbacks.
3113 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003114 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003115 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003117 mWorkspace.updateShortcuts(apps);
3118 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003119 if (mCustomizePagedView != null) {
3120 mCustomizePagedView.updateApps(apps);
3121 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003122 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003123 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 }
3125
3126 /**
3127 * A package was uninstalled.
3128 *
3129 * Implementation of the method from LauncherModel.Callbacks.
3130 */
Joe Onorato36115782010-06-17 13:28:48 -04003131 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003133 if (permanent) {
3134 mWorkspace.removeItems(apps);
3135 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003136 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003137 if (mCustomizePagedView != null) {
3138 mCustomizePagedView.removeApps(apps);
3139 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003140 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003141 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 }
Joe Onoratobe386092009-11-17 17:32:16 -08003143
3144 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003145 * A number of packages were updated.
3146 */
3147 public void bindPackagesUpdated() {
3148 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003149 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003150 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003151 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003152 }
3153
3154 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003155 * Prints out out state for debugging.
3156 */
3157 public void dumpState() {
3158 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003159 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003160 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3161 Log.d(TAG, "mRestoring=" + mRestoring);
3162 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3163 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3164 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003165 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003166 mModel.dumpState();
3167 mAllAppsGrid.dumpState();
3168 Log.d(TAG, "END launcher2 dump state");
3169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170}