blob: cb627ddd0e1f7c65133555fae815bf2911478477 [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
Patrick Dubroy7247f632010-08-04 16:02:59 -070023import android.animation.Animatable;
24import android.animation.AnimatableListenerAdapter;
25import android.animation.Animator;
26import android.animation.PropertyAnimator;
27import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
32import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070033import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070043import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070044import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070054import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080063import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070064import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
68import android.text.TextUtils;
69import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070079import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070081import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070082import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070083import android.view.animation.DecelerateInterpolator;
84import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070087import android.widget.ImageView;
88import android.widget.LinearLayout;
89import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070090import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070091import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070092import android.widget.TabHost.OnTabChangeListener;
93import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070094import android.widget.TabWidget;
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 WALLPAPER_SCREENS_SPAN = 2;
120
121 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800122 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_ADD = Menu.FIRST + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
131 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_PICK_APPLICATION = 6;
134 private static final int REQUEST_PICK_SHORTCUT = 7;
135 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700137 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Joe Onorato7c312c12009-08-13 21:36:53 -0700144 static final int DIALOG_CREATE_SHORTCUT = 1;
145 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 // Type: int
150 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
151 // Type: boolean
152 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
169 // Type: int[]
170 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
175
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 // tags for the customization tabs
177 private static final String WIDGETS_TAG = "widgets";
178 private static final String FOLDERS_TAG = "folders";
179 private static final String SHORTCUTS_TAG = "shortcuts";
180 private static final String WALLPAPERS_TAG = "wallpapers";
181
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182 /** The different states that Launcher can be in. */
183 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800188 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800190 private final BroadcastReceiver mCloseSystemDialogsReceiver
191 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800192 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private LayoutInflater mInflater;
195
Joe Onorato00acb122009-08-04 16:04:30 -0400196 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700199 private AppWidgetManager mAppWidgetManager;
200 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private CellLayout.CellInfo mAddItemCellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700203 private int[] mAddItemCoordinates;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private CellLayout.CellInfo mMenuAddInfo;
205 private final int[] mCellCoordinates = new int[2];
206 private FolderInfo mFolderInfo;
207
Joe Onorato7c312c12009-08-13 21:36:53 -0700208 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700210 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700211 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700212 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private Bundle mSavedState;
215
216 private SpannableStringBuilder mDefaultKeySsb = null;
217
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 private boolean mWorkspaceLoading = true;
219
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800220 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private boolean mRestoring;
222 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
224 private Bundle mSavedInstanceState;
225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228
Romain Guy84f296c2009-11-04 15:00:44 -0800229 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
230 private static HashMap<Long, FolderInfo> mFolders = 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 @Override
242 protected void onCreate(Bundle savedInstanceState) {
243 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700244
Winson Chungaafa03c2010-06-11 17:34:16 -0700245 if (LauncherApplication.isInPlaceRotationEnabled()) {
246 // hide the status bar (temporary until we get the status bar design figured out)
247 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
248 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
249 }
250
Joe Onorato0589f0f2010-02-08 13:44:00 -0800251 LauncherApplication app = ((LauncherApplication)getApplication());
252 mModel = app.setLauncher(this);
253 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400254 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700258 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
259 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 if (PROFILE_STARTUP) {
262 android.os.Debug.startMethodTracing("/sdcard/launcher");
263 }
264
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400265 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 checkForLocaleChange();
267 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700269 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
270 if (mHomeCustomizationDrawer != null) {
271 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700272
Winson Chung80baf5a2010-08-09 16:03:15 -0700273 // share the same customization workspace across all the tabs
274 mCustomizePagedView = new CustomizePagedView(this);
275 TabContentFactory contentFactory = new TabContentFactory() {
276 public View createTabContent(String tag) {
277 return mCustomizePagedView;
278 }
279 };
280
Michael Jurka946ad472010-07-09 18:05:18 -0700281 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700282 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
283 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700284 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700285 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
286 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700287 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700288 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
289 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700290 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700291 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
292 .setIndicator(wallpapersLabel).setContent(contentFactory));
293
294 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
295 // new assets
296 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
297 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
298 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
299 TextView text = (TextView) tab.getChildAt(1);
300 text.setTextSize(20.0f);
301 text.setPadding(20, 0, 20, 0);
302 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
303 tab.setBackgroundDrawable(null);
304 }
305
306 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
307 public void onTabChanged(String tabId) {
308 // animate the changing of the tab content by fading pages in and out
309 final int duration = 150;
310 final float alpha = mCustomizePagedView.getAlpha();
311 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
312 "alpha", alpha, 0.0f);
313 alphaAnim.addListener(new AnimatableListenerAdapter() {
314 public void onAnimationEnd(Animatable animation) {
315 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
316 if (tag == WIDGETS_TAG) {
317 mCustomizePagedView.setCustomizationFilter(
318 CustomizePagedView.CustomizationType.WidgetCustomization);
319 } else if (tag == FOLDERS_TAG) {
320 mCustomizePagedView.setCustomizationFilter(
321 CustomizePagedView.CustomizationType.FolderCustomization);
322 } else if (tag == SHORTCUTS_TAG) {
323 mCustomizePagedView.setCustomizationFilter(
324 CustomizePagedView.CustomizationType.ShortcutCustomization);
325 } else if (tag == WALLPAPERS_TAG) {
326 mCustomizePagedView.setCustomizationFilter(
327 CustomizePagedView.CustomizationType.WallpaperCustomization);
328 }
329
330 final float alpha = mCustomizePagedView.getAlpha();
331 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
332 "alpha", alpha, 1.0f);
333 alphaAnim.start();
334 }
335 });
336 alphaAnim.start();
337 }
338 });
Michael Jurka946ad472010-07-09 18:05:18 -0700339
340 mHomeCustomizationDrawer.setCurrentTab(0);
341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 setupViews();
343
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800344 registerContentObservers();
345
Joe Onorato7c312c12009-08-13 21:36:53 -0700346 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 mSavedState = savedInstanceState;
349 restoreState(mSavedState);
350
351 if (PROFILE_STARTUP) {
352 android.os.Debug.stopMethodTracing();
353 }
354
355 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400356 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 }
358
359 // For handling default keys
360 mDefaultKeySsb = new SpannableStringBuilder();
361 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800362
363 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
364 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
Romain Guycbb89e42009-06-08 15:52:54 -0700366
Winson Chungaafa03c2010-06-11 17:34:16 -0700367 @Override
368 public void onConfigurationChanged(Configuration newConfig) {
369 // TODO Auto-generated method stub
370 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700371 }
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700374 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
375 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 final Configuration configuration = getResources().getConfiguration();
378
Romain Guy98d01652009-06-30 16:21:04 -0700379 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 final String locale = configuration.locale.toString();
381
Romain Guy98d01652009-06-30 16:21:04 -0700382 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 final int mcc = configuration.mcc;
384
Romain Guy98d01652009-06-30 16:21:04 -0700385 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 final int mnc = configuration.mnc;
387
Romain Guy84f296c2009-11-04 15:00:44 -0800388 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389
Romain Guy84f296c2009-11-04 15:00:44 -0800390 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700391 localeConfiguration.locale = locale;
392 localeConfiguration.mcc = mcc;
393 localeConfiguration.mnc = mnc;
394
395 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800396 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400397
398 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700399 }
400 }
401
402 private static class LocaleConfiguration {
403 public String locale;
404 public int mcc = -1;
405 public int mnc = -1;
406 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700407
Romain Guy98d01652009-06-30 16:21:04 -0700408 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
409 DataInputStream in = null;
410 try {
411 in = new DataInputStream(context.openFileInput(PREFERENCES));
412 configuration.locale = in.readUTF();
413 configuration.mcc = in.readInt();
414 configuration.mnc = in.readInt();
415 } catch (FileNotFoundException e) {
416 // Ignore
417 } catch (IOException e) {
418 // Ignore
419 } finally {
420 if (in != null) {
421 try {
422 in.close();
423 } catch (IOException e) {
424 // Ignore
425 }
426 }
427 }
428 }
429
430 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
431 DataOutputStream out = null;
432 try {
433 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
434 out.writeUTF(configuration.locale);
435 out.writeInt(configuration.mcc);
436 out.writeInt(configuration.mnc);
437 out.flush();
438 } catch (FileNotFoundException e) {
439 // Ignore
440 } catch (IOException e) {
441 //noinspection ResultOfMethodCallIgnored
442 context.getFileStreamPath(PREFERENCES).delete();
443 } finally {
444 if (out != null) {
445 try {
446 out.close();
447 } catch (IOException e) {
448 // Ignore
449 }
450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
452 }
453
454 static int getScreen() {
455 synchronized (sLock) {
456 return sScreen;
457 }
458 }
459
460 static void setScreen(int screen) {
461 synchronized (sLock) {
462 sScreen = screen;
463 }
464 }
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700467 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468
469 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700470 // TODO: Put back when we decide about scrolling the wallpaper
471 // boolean isPortrait = display.getWidth() < display.getHeight();
472 // final int width = isPortrait ? display.getWidth() : display.getHeight();
473 // final int height = isPortrait ? display.getHeight() : display.getWidth();
474 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
475 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 }
477
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400478 // Note: This doesn't do all the client-id magic that BrowserProvider does
479 // in Browser. (http://b/2425179)
480 private Uri getDefaultBrowserUri() {
481 String url = getString(R.string.default_browser_url);
482 if (url.indexOf("{CID}") != -1) {
483 url = url.replace("{CID}", "android-google");
484 }
485 return Uri.parse(url);
486 }
487
488 // Load the Intent templates from arrays.xml to populate the hotseats. For
489 // each Intent, if it resolves to a single app, use that as the launch
490 // intent & use that app's label as the contentDescription. Otherwise,
491 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400492 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400493 if (mHotseatConfig == null) {
494 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
495 if (mHotseatConfig.length > 0) {
496 mHotseats = new Intent[mHotseatConfig.length];
497 mHotseatLabels = new CharSequence[mHotseatConfig.length];
498 mHotseatIcons = new Drawable[mHotseatConfig.length];
499 } else {
500 mHotseats = null;
501 mHotseatIcons = null;
502 mHotseatLabels = null;
503 }
504
505 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
506 for (int i=0; i<mHotseatConfig.length; i++) {
507 // load icon for this slot; currently unrelated to the actual activity
508 try {
509 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
510 } catch (ArrayIndexOutOfBoundsException ex) {
511 Log.w(TAG, "Missing hotseat_icons array item #" + i);
512 mHotseatIcons[i] = null;
513 }
514 }
515 hotseatIconDrawables.recycle();
516 }
517
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400518 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400519 for (int i=0; i<mHotseatConfig.length; i++) {
520 Intent intent = null;
521 if (mHotseatConfig[i].equals("*BROWSER*")) {
522 // magic value meaning "launch user's default web browser"
523 // replace it with a generic web request so we can see if there is indeed a default
524 String defaultUri = getString(R.string.default_browser_url);
525 intent = new Intent(
526 Intent.ACTION_VIEW,
527 ((defaultUri != null)
528 ? Uri.parse(defaultUri)
529 : getDefaultBrowserUri())
530 ).addCategory(Intent.CATEGORY_BROWSABLE);
531 // note: if the user launches this without a default set, she
532 // will always be taken to the default URL above; this is
533 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700534 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400535 // URL is unavoidably sent to the chosen app.
536 } else {
537 try {
538 intent = Intent.parseUri(mHotseatConfig[i], 0);
539 } catch (java.net.URISyntaxException ex) {
540 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
541 // bogus; leave intent=null
542 }
543 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700544
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 if (intent == null) {
546 mHotseats[i] = null;
547 mHotseatLabels[i] = getText(R.string.activity_not_found);
548 continue;
549 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400550
551 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700552 Log.d(TAG, "loadHotseats: hotseat " + i
553 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400554 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400555 + "]");
556 }
557
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400558 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
559 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 Log.d(TAG, "Best match for intent: " + bestMatch);
562 Log.d(TAG, "All matches: ");
563 for (ResolveInfo ri : allMatches) {
564 Log.d(TAG, " --> " + ri);
565 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400566 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 // did this resolve to a single app, or the resolver?
568 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 // can't find any activity to handle this. let's leave the
570 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400571 // to launch.
572 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 // set accessibility text to "Not installed"
575 mHotseatLabels[i] = getText(R.string.activity_not_found);
576 } else {
577 boolean found = false;
578 for (ResolveInfo ri : allMatches) {
579 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
580 && bestMatch.activityInfo.applicationInfo.packageName
581 .equals(ri.activityInfo.applicationInfo.packageName)) {
582 found = true;
583 break;
584 }
585 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700586
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400587 if (!found) {
588 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
589 // the bestMatch is probably the ResolveActivity, meaning the
590 // user has not yet selected a default
591 // so: we'll keep the original intent for now
592 mHotseats[i] = intent;
593
594 // set the accessibility text to "Select shortcut"
595 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
596 } else {
597 // we have an app!
598 // now reconstruct the intent to launch it through the front
599 // door
600 ComponentName com = new ComponentName(
601 bestMatch.activityInfo.applicationInfo.packageName,
602 bestMatch.activityInfo.name);
603 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
604
605 // load the app label for accessibility
606 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
607 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400608 }
609
610 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700611 Log.d(TAG, "loadHotseats: hotseat " + i
612 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400613 + ((mHotseats[i] == null)
614 ? "null"
615 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400616 + "] label=[" + mHotseatLabels[i]
617 + "]"
618 );
619 }
620 }
621 }
622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 @Override
624 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700625 mWaitingForResult = false;
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 // The pattern used here is that a user PICKs a specific application,
628 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
631 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700632
Romain Guy94dabf12009-07-21 10:55:43 -0700633 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 switch (requestCode) {
635 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400636 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 break;
638 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800639 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 break;
641 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 break;
644 case REQUEST_PICK_LIVE_FOLDER:
645 addLiveFolder(data);
646 break;
647 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700650 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700651 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700653 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700654 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700655 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700657 case REQUEST_PICK_WALLPAPER:
658 // We just wanted the activity result here so we can clear mWaitingForResult
659 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Romain Guy18042c82009-11-06 11:44:55 -0800661 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
662 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
663 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700664 // Clean up the appWidgetId if we canceled
665 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
666 if (appWidgetId != -1) {
667 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671
672 @Override
673 protected void onResume() {
674 super.onResume();
675
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800676 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 mWorkspaceLoading = true;
680 mModel.startLoader(this, true);
681 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683 }
684
685 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 protected void onPause() {
687 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700688 // Some launcher layouts don't have a previous and next view
689 if (mPreviousView != null) {
690 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700691 }
692 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700693 dismissPreview(mNextView);
694 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800695 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700696 }
Romain Guycbb89e42009-06-08 15:52:54 -0700697
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700698 @Override
699 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 // Flag the loader to stop early before switching
701 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800702 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800703 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700704 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800706 // We can't hide the IME if it was forced open. So don't bother
707 /*
708 @Override
709 public void onWindowFocusChanged(boolean hasFocus) {
710 super.onWindowFocusChanged(hasFocus);
711
712 if (hasFocus) {
713 final InputMethodManager inputManager = (InputMethodManager)
714 getSystemService(Context.INPUT_METHOD_SERVICE);
715 WindowManager.LayoutParams lp = getWindow().getAttributes();
716 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
717 android.os.Handler()) {
718 protected void onReceiveResult(int resultCode, Bundle resultData) {
719 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
720 }
721 });
722 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
723 }
724 }
725 */
726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 private boolean acceptFilter() {
728 final InputMethodManager inputManager = (InputMethodManager)
729 getSystemService(Context.INPUT_METHOD_SERVICE);
730 return !inputManager.isFullscreenMode();
731 }
732
733 @Override
734 public boolean onKeyDown(int keyCode, KeyEvent event) {
735 boolean handled = super.onKeyDown(keyCode, event);
736 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
737 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
738 keyCode, event);
739 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700740 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700741 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700742 // showSearchDialog()
743 // If there are multiple keystrokes before the search dialog takes focus,
744 // onSearchRequested() will be called for every keystroke,
745 // but it is idempotent, so it's fine.
746 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
748 }
749
Joe Onorato8a9625e2010-01-28 15:55:35 -0800750 // Eat the long press event so the keyboard doesn't come up.
751 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
752 return true;
753 }
754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 return handled;
756 }
757
Karl Rosaen138a0412009-04-23 19:00:21 -0700758 private String getTypedText() {
759 return mDefaultKeySsb.toString();
760 }
761
762 private void clearTypedText() {
763 mDefaultKeySsb.clear();
764 mDefaultKeySsb.clearSpans();
765 Selection.setSelection(mDefaultKeySsb, 0);
766 }
767
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 /**
769 * Restores the previous state, if it exists.
770 *
771 * @param savedState The previous state.
772 */
773 private void restoreState(Bundle savedState) {
774 if (savedState == null) {
775 return;
776 }
777
Joe Onorato3a8820b2009-11-10 15:06:42 -0800778 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
779 if (allApps) {
780 showAllApps(false);
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
784 if (currentScreen > -1) {
785 mWorkspace.setCurrentScreen(currentScreen);
786 }
787
788 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
789 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700790 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 mAddItemCellInfo = new CellLayout.CellInfo();
792 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
793 addItemCellInfo.valid = true;
794 addItemCellInfo.screen = addScreen;
795 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
796 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
797 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
798 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700799 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
801 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
802 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
803 mRestoring = true;
804 }
805
806 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
807 if (renameFolder) {
808 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 mRestoring = true;
811 }
812 }
813
814 /**
815 * Finds all the views we need and configure them properly.
816 */
817 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700818 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400819
Romain Guyb1b69f52009-08-10 15:10:15 -0700820 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400821 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822
Joe Onorato7c312c12009-08-13 21:36:53 -0700823 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700824 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700825 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800826 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700827 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700828 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700829
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700830 if (LauncherApplication.isScreenXLarge()) {
831 // They need to be INVISIBLE initially so that they will be measured in the layout.
832 // Otherwise the animations are messed up when we show them for the first time.
833 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
834 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
838 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500839 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700840
Joe Onorato7c312c12009-08-13 21:36:53 -0700841 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
842 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700844 View handleView = findViewById(R.id.all_apps_button);
845 if (handleView != null && handleView instanceof HandleView) {
846 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700847 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700848 mHandleView.setLauncher(this);
849 mHandleView.setOnClickListener(this);
850 mHandleView.setOnLongClickListener(this);
851 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800852
Winson Chung80baf5a2010-08-09 16:03:15 -0700853 if (mCustomizePagedView != null) {
854 mCustomizePagedView.setLauncher(this);
855 mCustomizePagedView.setDragController(dragController);
856 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700857 } else {
858 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
859 hotseatLeft.setContentDescription(mHotseatLabels[0]);
860 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
861 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
862 hotseatRight.setContentDescription(mHotseatLabels[1]);
863 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400864
Michael Jurkaaf442092010-06-10 17:01:57 -0700865 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
866 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800867
Michael Jurkaaf442092010-06-10 17:01:57 -0700868 Drawable previous = mPreviousView.getDrawable();
869 Drawable next = mNextView.getDrawable();
870 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
Michael Jurkaaf442092010-06-10 17:01:57 -0700872 mPreviousView.setHapticFeedbackEnabled(false);
873 mPreviousView.setOnLongClickListener(this);
874 mNextView.setHapticFeedbackEnabled(false);
875 mNextView.setOnLongClickListener(this);
876 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400879 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881
882 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400883 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700884 int deleteZoneHandleId;
885 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700886 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700887 } else {
888 deleteZoneHandleId = R.id.all_apps_button_cluster;
889 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700890 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700891 dragController.addDragListener(deleteZone);
892
893 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
894 if (infoButton != null) {
895 infoButton.setLauncher(this);
896 infoButton.setHandle(findViewById(R.id.configure_button));
897 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
898 dragController.addDragListener(infoButton);
899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900
Joe Onorato00acb122009-08-04 16:04:30 -0400901 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400902 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800903 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700904
Joe Onorato00acb122009-08-04 16:04:30 -0400905 // The order here is bottom to top.
906 dragController.addDropTarget(workspace);
907 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700908 if (infoButton != null) {
909 dragController.addDropTarget(infoButton);
910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912
Romain Guy8a73c512009-11-09 19:19:59 -0800913 @SuppressWarnings({"UnusedDeclaration"})
914 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800915 if (!isAllAppsVisible()) {
916 mWorkspace.scrollLeft();
917 }
Romain Guy8a73c512009-11-09 19:19:59 -0800918 }
919
920 @SuppressWarnings({"UnusedDeclaration"})
921 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800922 if (!isAllAppsVisible()) {
923 mWorkspace.scrollRight();
924 }
Romain Guy8a73c512009-11-09 19:19:59 -0800925 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400926
927 @SuppressWarnings({"UnusedDeclaration"})
928 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400929 if (isAllAppsVisible()) return;
930
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400931 int index = -1;
932 if (v.getId() == R.id.hotseat_left) {
933 index = 0;
934 } else if (v.getId() == R.id.hotseat_right) {
935 index = 1;
936 }
937
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400938 // reload these every tap; you never know when they might change
939 loadHotseats();
940 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
941 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400942 startActivitySafely(
943 mHotseats[index],
944 "hotseat"
945 );
946 }
947 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 /**
950 * Creates a view representing a shortcut.
951 *
952 * @param info The data structure describing the shortcut.
953 *
954 * @return A View inflated from R.layout.application.
955 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800956 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 return createShortcut(R.layout.application,
958 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
959 }
960
961 /**
962 * Creates a view representing a shortcut inflated from the specified resource.
963 *
964 * @param layoutResId The id of the XML layout used to create the shortcut.
965 * @param parent The group the shortcut belongs to.
966 * @param info The data structure describing the shortcut.
967 *
968 * @return A View inflated from layoutResId.
969 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800970 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
972
Joe Onorato0589f0f2010-02-08 13:44:00 -0800973 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
974 new FastBitmapDrawable(info.getIcon(mIconCache)),
975 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 favorite.setText(info.title);
977 favorite.setTag(info);
978 favorite.setOnClickListener(this);
979
980 return favorite;
981 }
982
983 /**
984 * Add an application shortcut to the workspace.
985 *
986 * @param data The intent describing the application.
987 * @param cellInfo The position on screen where to create the shortcut.
988 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 cellInfo.screen = mWorkspace.getCurrentScreen();
991 if (!findSingleSlot(cellInfo)) return;
992
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
994 data, context);
995
Romain Guy73b979d2009-06-09 12:57:21 -0700996 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 info.container = ItemInfo.NO_ID;
1000 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1001 } else {
1002 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004 }
Romain Guycbb89e42009-06-08 15:52:54 -07001005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 /**
1007 * Add a shortcut to the workspace.
1008 *
1009 * @param data The intent describing the shortcut.
1010 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 cellInfo.screen = mWorkspace.getCurrentScreen();
1014 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001015
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017
1018 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1021 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
Romain Guycbb89e42009-06-08 15:52:54 -07001025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001027 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001029 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001032 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001033 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001034
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001037 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001040 // We have saved the position to which the widget was dragged-- this really only matters
1041 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001043
1044 // For now, we don't save the coordinate where we dropped the icon because we're not
1045 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1046 // a widget on the home screen in case we want to add it in the future
1047 final int[] xyTouch = null;
1048 //final int[] xyTouch = mAddItemCoordinates;
1049 boolean findNearestVacantAreaFailed = false;
1050 if (xyTouch != null) {
1051 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1052 int[] result = screen.findNearestVacantArea(
1053 mAddItemCoordinates[0], mAddItemCoordinates[1],
1054 spans[0], spans[1], cellInfo, xy);
1055 findNearestVacantAreaFailed = (result == null);
1056 }
1057
1058 if (findNearestVacantAreaFailed ||
1059 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001060 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1061 return;
1062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 // Build Launcher-specific widget info and save to database
1065 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 launcherInfo.spanX = spans[0];
1067 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 LauncherModel.addItemToDatabase(this, launcherInfo,
1070 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072
1073 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001078
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001081
Michael Jurkaa63c4522010-08-19 13:52:27 -07001082 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085 }
Romain Guycbb89e42009-06-08 15:52:54 -07001086
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1088 mDesktopItems.remove(launcherInfo);
1089 launcherInfo.hostView = null;
1090 }
1091
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001092 public LauncherAppWidgetHost getAppWidgetHost() {
1093 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001096 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001097 getWindow().closeAllPanels();
1098
1099 try {
1100 dismissDialog(DIALOG_CREATE_SHORTCUT);
1101 // Unlock the workspace if the dialog was showing
1102 } catch (Exception e) {
1103 // An exception is thrown if the dialog is not visible, which is fine
1104 }
1105
1106 try {
1107 dismissDialog(DIALOG_RENAME_FOLDER);
1108 // Unlock the workspace if the dialog was showing
1109 } catch (Exception e) {
1110 // An exception is thrown if the dialog is not visible, which is fine
1111 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001112
1113 // Whatever we were doing is hereby canceled.
1114 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001115 }
1116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 @Override
1118 protected void onNewIntent(Intent intent) {
1119 super.onNewIntent(intent);
1120
1121 // Close the menu
1122 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001123 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001124 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001125
Joe Onorato14f122b2009-11-19 14:06:36 -08001126 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1127 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1128 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001129 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1130
Patrick Dubroy558654c2010-07-23 16:48:11 -07001131
Michael Jurka4cb37242010-08-09 21:05:32 -07001132 // in all these cases, only animate if we're already on home
1133 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001134 if (alreadyOnHome && !mWorkspace.isSmall() &&
1135 !allAppsVisible && !customizationDrawerVisible) {
1136 mWorkspace.shrinkToMiddle();
1137 } else {
1138 mWorkspace.unshrink(alreadyOnHome);
1139 }
1140 } else if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001141 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001142 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001143 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001144 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001145 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001146
Joe Onorato3a8820b2009-11-10 15:06:42 -08001147 final View v = getWindow().peekDecorView();
1148 if (v != null && v.getWindowToken() != null) {
1149 InputMethodManager imm = (InputMethodManager)getSystemService(
1150 INPUT_METHOD_SERVICE);
1151 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153 }
1154 }
1155
1156 @Override
1157 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1158 // Do not call super here
1159 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001160
1161 if (mHomeCustomizationDrawer != null) {
1162 String cur = savedInstanceState.getString("currentTab");
1163 if (cur != null) {
1164 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1165 }
1166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 }
1168
1169 @Override
1170 protected void onSaveInstanceState(Bundle outState) {
1171 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1172
1173 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1174 if (folders.size() > 0) {
1175 final int count = folders.size();
1176 long[] ids = new long[count];
1177 for (int i = 0; i < count; i++) {
1178 final FolderInfo info = folders.get(i).getInfo();
1179 ids[i] = info.id;
1180 }
1181 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1182 } else {
1183 super.onSaveInstanceState(outState);
1184 }
1185
Joe Onoratofb0ca672009-09-14 17:55:46 -04001186 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001187 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190
1191 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1192 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1193 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1194
1195 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1196 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1197 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1198 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1199 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1200 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1201 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1202 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001203 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 }
1205
1206 if (mFolderInfo != null && mWaitingForResult) {
1207 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1208 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1209 }
Michael Jurka946ad472010-07-09 18:05:18 -07001210
1211 if (mHomeCustomizationDrawer != null) {
1212 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1213 if (currentTabTag != null) {
1214 outState.putString("currentTab", currentTabTag);
1215 }
1216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
1218
1219 @Override
1220 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001226 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
1229 TextKeyListener.getInstance().release();
1230
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001234
1235 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001236
Patrick Dubroyab962b72010-07-26 12:10:10 -07001237 // Some launcher layouts don't have a previous and next view
1238 if (mPreviousView != null) {
1239 dismissPreview(mPreviousView);
1240 }
1241 if (mNextView != null) {
1242 dismissPreview(mNextView);
1243 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001244
1245 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 }
1247
1248 @Override
1249 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001250 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 super.startActivityForResult(intent, requestCode);
1252 }
1253
1254 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001255 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001257
Joe Onorato7bb17492009-09-24 17:51:01 -07001258 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001259
Karl Rosaen138a0412009-04-23 19:00:21 -07001260 if (initialQuery == null) {
1261 // Use any text typed in the launcher as the initial query
1262 initialQuery = getTypedText();
1263 clearTypedText();
1264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 if (appSearchData == null) {
1266 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001267 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
Romain Guycbb89e42009-06-08 15:52:54 -07001269
Karl Rosaen138a0412009-04-23 19:00:21 -07001270 final SearchManager searchManager =
1271 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001272 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001273 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
1275
1276 @Override
1277 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 if (isWorkspaceLocked()) {
1279 return false;
1280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281
1282 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1285 .setIcon(android.R.drawable.ic_menu_add)
1286 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001287 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 .setIcon(android.R.drawable.ic_menu_gallery)
1289 .setAlphabeticShortcut('W');
1290 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1291 .setIcon(android.R.drawable.ic_search_category_default)
1292 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1293 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1294 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1295 .setAlphabeticShortcut('N');
1296
1297 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001298 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1299 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300
1301 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1302 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1303 .setIntent(settings);
1304
1305 return true;
1306 }
1307
1308 @Override
1309 public boolean onPrepareOptionsMenu(Menu menu) {
1310 super.onPrepareOptionsMenu(menu);
1311
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001312 // If all apps is animating, don't show the menu, because we don't know
1313 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001314 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001315 return false;
1316 }
1317
1318 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001319 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001320 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1321 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1322
1323 // Disable add if the workspace is full.
1324 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001325 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001326 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328
1329 return true;
1330 }
1331
Michael Jurka0e260592010-06-30 17:07:39 -07001332 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1333 // using the settings menu to add an item, something similar happens in showAddDialog
1334 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001335 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001336 mAddItemCellInfo = mMenuAddInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001337 mAddItemCellInfo = null;
Michael Jurka0e260592010-06-30 17:07:39 -07001338 }
1339
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 @Override
1341 public boolean onOptionsItemSelected(MenuItem item) {
1342 switch (item.getItemId()) {
1343 case MENU_ADD:
1344 addItems();
1345 return true;
1346 case MENU_WALLPAPER_SETTINGS:
1347 startWallpaper();
1348 return true;
1349 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001350 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 return true;
1352 case MENU_NOTIFICATIONS:
1353 showNotifications();
1354 return true;
1355 }
1356
1357 return super.onOptionsItemSelected(item);
1358 }
Romain Guycbb89e42009-06-08 15:52:54 -07001359
Karl Rosaen138a0412009-04-23 19:00:21 -07001360 /**
1361 * Indicates that we want global search for this activity by setting the globalSearch
1362 * argument for {@link #startSearch} to true.
1363 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001365 @Override
1366 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001367 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001368 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001369 }
1370
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 public boolean isWorkspaceLocked() {
1372 return mWorkspaceLoading || mWaitingForResult;
1373 }
1374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001376 if (LauncherApplication.isScreenXLarge()) {
1377 // Animate the widget chooser up from the bottom of the screen
1378 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001379 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001380 }
1381 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001382 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001383 showAddDialog(mMenuAddInfo);
1384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
Michael Jurkaa63c4522010-08-19 13:52:27 -07001387 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1388 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001389 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001390
1391 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1392 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001393 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1394 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1395 addAppWidgetImpl(appWidgetId);
1396 }
1397
1398 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001399 // TODO: catch bad widget exception when sent
1400 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001401 // TODO: Is this log message meaningful?
1402 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1403 addAppWidgetImpl(appWidgetId);
1404 }
1405
1406 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001407 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408
Bjorn Bringert7984c942009-12-09 15:38:25 +00001409 if (appWidget.configure != null) {
1410 // Launch over to configure widget, if needed
1411 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1412 intent.setComponent(appWidget.configure);
1413 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1414
Romain Guy8e633c52010-03-04 12:51:36 -08001415 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001417 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001418 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420 }
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Joe Onoratodeb98af2010-02-19 14:59:39 -08001422 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001423 // Handle case where user selected "Applications"
1424 String applicationName = getResources().getString(R.string.group_applications);
1425 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001427 if (applicationName != null && applicationName.equals(shortcutName)) {
1428 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1429 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001431 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1432 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001433 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001434 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001435 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 }
1438
1439 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001440 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001441 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001442 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001444 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001446 } else {
1447 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 UserFolderInfo folderInfo = new UserFolderInfo();
1453 folderInfo.title = getText(R.string.folder_name);
1454
1455 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1456 cellInfo.screen = mWorkspace.getCurrentScreen();
1457 if (!findSingleSlot(cellInfo)) return;
1458
1459 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 LauncherModel.addItemToDatabase(this, folderInfo,
1461 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001463 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464
1465 // Create the view
1466 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1467 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1468 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 }
Romain Guycbb89e42009-06-08 15:52:54 -07001471
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472 void removeFolder(FolderInfo folder) {
1473 mFolders.remove(folder.id);
1474 }
1475
1476 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 cellInfo.screen = mWorkspace.getCurrentScreen();
1478 if (!findSingleSlot(cellInfo)) return;
1479
1480 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1481
1482 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1484 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1486 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488 }
1489
1490 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1491 CellLayout.CellInfo cellInfo, boolean notify) {
1492
1493 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1494 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1495
1496 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 Intent.ShortcutIconResource iconResource = null;
1498
1499 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1500 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1501 try {
1502 iconResource = (Intent.ShortcutIconResource) extra;
1503 final PackageManager packageManager = context.getPackageManager();
1504 Resources resources = packageManager.getResourcesForApplication(
1505 iconResource.packageName);
1506 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1507 icon = resources.getDrawable(id);
1508 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001509 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511 }
1512
1513 if (icon == null) {
1514 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1515 }
1516
1517 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001518 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 info.title = name;
1520 info.iconResource = iconResource;
1521 info.uri = data.getData();
1522 info.baseIntent = baseIntent;
1523 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1524 LiveFolders.DISPLAY_MODE_GRID);
1525
1526 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1527 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 return info;
1531 }
1532
1533 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1534 final int[] xy = new int[2];
1535 if (findSlot(cellInfo, xy, 1, 1)) {
1536 cellInfo.cellX = xy[0];
1537 cellInfo.cellY = xy[1];
1538 return true;
1539 }
1540 return false;
1541 }
1542
1543 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1544 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001545 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1546 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1548 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1549 return false;
1550 }
1551 }
1552 return true;
1553 }
1554
1555 private void showNotifications() {
1556 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1557 if (statusBar != null) {
1558 statusBar.expand();
1559 }
1560 }
1561
1562 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001563 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001565 Intent chooser = Intent.createChooser(pickWallpaper,
1566 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001567 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1568 // Removed in Eclair MR1
1569// WallpaperManager wm = (WallpaperManager)
1570// getSystemService(Context.WALLPAPER_SERVICE);
1571// WallpaperInfo wi = wm.getWallpaperInfo();
1572// if (wi != null && wi.getSettingsActivity() != null) {
1573// LabeledIntent li = new LabeledIntent(getPackageName(),
1574// R.string.configure_wallpaper, 0);
1575// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1576// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1577// }
Mike Clerona0618e42009-10-22 13:55:21 -07001578 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
1580
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001581 /**
1582 * Registers various content observers. The current implementation registers
1583 * only a favorites observer to keep track of the favorites applications.
1584 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001585 private void registerContentObservers() {
1586 ContentResolver resolver = getContentResolver();
1587 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1588 true, mWidgetObserver);
1589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 @Override
1592 public boolean dispatchKeyEvent(KeyEvent event) {
1593 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1594 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001596 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001597 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001598 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001599 dumpState();
1600 return true;
1601 }
1602 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001603 }
1604 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1605 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001606 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 return true;
1608 }
1609 }
1610
1611 return super.dispatchKeyEvent(event);
1612 }
1613
Joe Onorato88ec0992009-11-19 13:16:06 -08001614 @Override
1615 public void onBackPressed() {
1616 if (isAllAppsVisible()) {
1617 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001618 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001619 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001620 } else {
1621 closeFolder();
1622 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001623 // Some launcher layouts don't have a previous and next view
1624 if (mPreviousView != null) {
1625 dismissPreview(mPreviousView);
1626 dismissPreview(mNextView);
1627 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001628 }
1629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 private void closeFolder() {
1631 Folder folder = mWorkspace.getOpenFolder();
1632 if (folder != null) {
1633 closeFolder(folder);
1634 }
1635 }
1636
1637 void closeFolder(Folder folder) {
1638 folder.getInfo().opened = false;
1639 ViewGroup parent = (ViewGroup) folder.getParent();
1640 if (parent != null) {
1641 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001642 if (folder instanceof DropTarget) {
1643 // Live folders aren't DropTargets.
1644 mDragController.removeDropTarget((DropTarget)folder);
1645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647 folder.onClose();
1648 }
1649
1650 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001651 * Re-listen when widgets are reset.
1652 */
1653 private void onAppWidgetReset() {
1654 mAppWidgetHost.startListening();
1655 }
1656
1657 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1659 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 private void unbindDesktopItems() {
1662 for (ItemInfo item: mDesktopItems) {
1663 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 /**
1668 * Launches the intent referred by the clicked shortcut.
1669 *
1670 * @param v The view representing the clicked shortcut.
1671 */
1672 public void onClick(View v) {
1673 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001674 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001676 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001677 int[] pos = new int[2];
1678 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001679 intent.setSourceBounds(new Rect(pos[0], pos[1],
1680 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001681 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 } else if (tag instanceof FolderInfo) {
1683 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001684 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001685 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001686 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001687 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001688 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 }
1691 }
1692
Michael Jurka0e260592010-06-30 17:07:39 -07001693 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001694 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001695 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001696 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001697 return false;
1698 }
1699
Michael Jurkaaf442092010-06-10 17:01:57 -07001700 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001701 * Event handler for the search button
1702 *
1703 * @param v The view that was clicked.
1704 */
1705 public void onClickSearchButton(View v) {
1706 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1707 View button = findViewById(R.id.search_button);
1708 i.setSourceBounds(
1709 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1710 startActivity(i);
1711 }
1712
1713 /**
1714 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 * enters home screen customization mode.
1716 *
1717 * @param v The view that was clicked.
1718 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001719 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001720 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 }
1722
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001723 /**
1724 * Event handler for the "grid" button that appears on the home screen, which
1725 * enters all apps mode.
1726 *
1727 * @param v The view that was clicked.
1728 */
1729 public void onClickAllAppsButton(View v) {
1730 showAllApps(true);
1731 }
1732
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001733 void startApplicationDetailsActivity(String packageName) {
1734 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1735 Uri.fromParts("package", packageName, null));
1736 startActivity(intent);
1737 }
1738
Joe Onoratof984e852010-03-25 09:47:45 -07001739 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1741 try {
1742 startActivity(intent);
1743 } catch (ActivityNotFoundException e) {
1744 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001745 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 } catch (SecurityException e) {
1747 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001748 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001750 "or use the exported attribute for this activity. "
1751 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 }
1753 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001754
Romain Guy8e633c52010-03-04 12:51:36 -08001755 void startActivityForResultSafely(Intent intent, int requestCode) {
1756 try {
1757 startActivityForResult(intent, requestCode);
1758 } catch (ActivityNotFoundException e) {
1759 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1760 } catch (SecurityException e) {
1761 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1762 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1763 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1764 "or use the exported attribute for this activity.", e);
1765 }
1766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767
1768 private void handleFolderClick(FolderInfo folderInfo) {
1769 if (!folderInfo.opened) {
1770 // Close any open folder
1771 closeFolder();
1772 // Open the requested folder
1773 openFolder(folderInfo);
1774 } else {
1775 // Find the open folder...
1776 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1777 int folderScreen;
1778 if (openFolder != null) {
1779 folderScreen = mWorkspace.getScreenForView(openFolder);
1780 // .. and close it
1781 closeFolder(openFolder);
1782 if (folderScreen != mWorkspace.getCurrentScreen()) {
1783 // Close any folder open on the current screen
1784 closeFolder();
1785 // Pull the folder onto this screen
1786 openFolder(folderInfo);
1787 }
1788 }
1789 }
1790 }
1791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 /**
1793 * Opens the user fodler described by the specified tag. The opening of the folder
1794 * is animated relative to the specified View. If the View is null, no animation
1795 * is played.
1796 *
1797 * @param folderInfo The FolderInfo describing the folder to open.
1798 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001799 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 Folder openFolder;
1801
1802 if (folderInfo instanceof UserFolderInfo) {
1803 openFolder = UserFolder.fromXml(this);
1804 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001805 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 } else {
1807 return;
1808 }
1809
Joe Onorato00acb122009-08-04 16:04:30 -04001810 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 openFolder.setLauncher(this);
1812
1813 openFolder.bind(folderInfo);
1814 folderInfo.opened = true;
1815
Winson Chungaafa03c2010-06-11 17:34:16 -07001816 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1817
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 openFolder.onOpen();
1819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001822 switch (v.getId()) {
1823 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001824 if (!isAllAppsVisible()) {
1825 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1826 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001827 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001828 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001829 return true;
1830 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001831 if (!isAllAppsVisible()) {
1832 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1833 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001834 showPreviews(v);
1835 }
1836 return true;
1837 case R.id.all_apps_button:
1838 if (!isAllAppsVisible()) {
1839 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1840 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1841 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001842 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001843 return true;
1844 }
1845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 return false;
1848 }
1849
1850 if (!(v instanceof CellLayout)) {
1851 v = (View) v.getParent();
1852 }
1853
1854 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1855
1856 // This happens when long clicking an item with the dpad/trackball
1857 if (cellInfo == null) {
1858 return true;
1859 }
1860
1861 if (mWorkspace.allowLongPress()) {
1862 if (cellInfo.cell == null) {
1863 if (cellInfo.valid) {
1864 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001865 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001866 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1867 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 showAddDialog(cellInfo);
1869 }
1870 } else {
1871 if (!(cellInfo.cell instanceof Folder)) {
1872 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001873 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1874 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 mWorkspace.startDrag(cellInfo);
1876 }
1877 }
1878 }
1879 return true;
1880 }
1881
Romain Guye6b8e2f2009-11-10 11:56:55 -08001882 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001883 private void dismissPreview(final View v) {
1884 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001885 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001886 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1887 public void onDismiss() {
1888 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1889 int count = group.getChildCount();
1890 for (int i = 0; i < count; i++) {
1891 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1892 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001893 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1894 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1895
1896 v.setTag(R.id.workspace, null);
1897 v.setTag(R.id.icon, null);
1898 window.setOnDismissListener(null);
1899 }
1900 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001901 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001902 }
1903 v.setTag(null);
1904 }
1905
Romain Guyf8e6a802009-12-07 17:48:02 -08001906 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001907 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001908 }
1909
Romain Guya6abce82009-11-10 02:54:41 -08001910 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001911 final Resources resources = getResources();
1912 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001913
Romain Guya6abce82009-11-10 02:54:41 -08001914 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001915
Romain Guy9d31e9f2009-11-11 18:54:28 -08001916 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001917
Romain Guyf8e6a802009-12-07 17:48:02 -08001918 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001919 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001920 int extraW = (int) ((r.left + r.right) * max);
1921 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001922
1923 int aW = cell.getWidth() - extraW;
1924 float w = aW / max;
1925
1926 int width = cell.getWidth();
1927 int height = cell.getHeight();
1928 int x = cell.getLeftPadding();
1929 int y = cell.getTopPadding();
1930 width -= (x + cell.getRightPadding());
1931 height -= (y + cell.getBottomPadding());
1932
1933 float scale = w / width;
1934
1935 int count = end - start;
1936
1937 final float sWidth = width * scale;
1938 float sHeight = height * scale;
1939
Romain Guye6b8e2f2009-11-10 11:56:55 -08001940 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001941
Romain Guye6b8e2f2009-11-10 11:56:55 -08001942 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1943 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001944
1945 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001946 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001947 cell = (CellLayout) workspace.getChildAt(i);
1948
Romain Guyf8e6a802009-12-07 17:48:02 -08001949 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001950 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001951
1952 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001953 c.scale(scale, scale);
1954 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1955 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001956
Romain Guy9d31e9f2009-11-11 18:54:28 -08001957 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001958 image.setImageBitmap(bitmap);
1959 image.setTag(i);
1960 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001961 image.setOnFocusChangeListener(handler);
1962 image.setFocusable(true);
1963 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001964
1965 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001966 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1967
Winson Chungaafa03c2010-06-11 17:34:16 -07001968 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001969 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001970
1971 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001972 p.setContentView(preview);
1973 p.setWidth((int) (sWidth * count + extraW));
1974 p.setHeight((int) (sHeight + extraH));
1975 p.setAnimationStyle(R.style.AnimationPreview);
1976 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001977 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001978 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001979 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001980
Romain Guye6b8e2f2009-11-10 11:56:55 -08001981 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1982 public void onDismiss() {
1983 dismissPreview(anchor);
1984 }
1985 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001986
1987 anchor.setTag(p);
1988 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001989 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001990 }
1991
Romain Guy9d31e9f2009-11-11 18:54:28 -08001992 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001993 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001994
Romain Guye6b8e2f2009-11-10 11:56:55 -08001995 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001996 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001997 }
1998
1999 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002000 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002001 v.post(this);
2002 }
2003
2004 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002005 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002006 }
2007
2008 public void onFocusChange(View v, boolean hasFocus) {
2009 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08002010 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002011 }
Romain Guya6abce82009-11-10 02:54:41 -08002012 }
2013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 Workspace getWorkspace() {
2016 return mWorkspace;
2017 }
2018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 @Override
2020 protected Dialog onCreateDialog(int id) {
2021 switch (id) {
2022 case DIALOG_CREATE_SHORTCUT:
2023 return new CreateShortcut().createDialog();
2024 case DIALOG_RENAME_FOLDER:
2025 return new RenameFolder().createDialog();
2026 }
2027
2028 return super.onCreateDialog(id);
2029 }
2030
2031 @Override
2032 protected void onPrepareDialog(int id, Dialog dialog) {
2033 switch (id) {
2034 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 break;
2036 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002037 if (mFolderInfo != null) {
2038 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2039 final CharSequence text = mFolderInfo.title;
2040 input.setText(text);
2041 input.setSelection(0, text.length());
2042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 break;
2044 }
2045 }
2046
2047 void showRenameDialog(FolderInfo info) {
2048 mFolderInfo = info;
2049 mWaitingForResult = true;
2050 showDialog(DIALOG_RENAME_FOLDER);
2051 }
2052
2053 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2054 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002055 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 mWaitingForResult = true;
2057 showDialog(DIALOG_CREATE_SHORTCUT);
2058 }
2059
Joe Onoratodeb98af2010-02-19 14:59:39 -08002060 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002061 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002062 Bundle bundle = new Bundle();
2063
2064 ArrayList<String> shortcutNames = new ArrayList<String>();
2065 shortcutNames.add(getString(R.string.group_applications));
2066 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2067
2068 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2069 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2070 R.drawable.ic_launcher_application));
2071 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2072
2073 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2074 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002075 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002076 pickIntent.putExtras(bundle);
2077
Joe Onoratodeb98af2010-02-19 14:59:39 -08002078 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002079 }
2080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 private class RenameFolder {
2082 private EditText mInput;
2083
2084 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2086 mInput = (EditText) layout.findViewById(R.id.folder_name);
2087
2088 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2089 builder.setIcon(0);
2090 builder.setTitle(getString(R.string.rename_folder_title));
2091 builder.setCancelable(true);
2092 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2093 public void onCancel(DialogInterface dialog) {
2094 cleanup();
2095 }
2096 });
2097 builder.setNegativeButton(getString(R.string.cancel_action),
2098 new Dialog.OnClickListener() {
2099 public void onClick(DialogInterface dialog, int which) {
2100 cleanup();
2101 }
2102 }
2103 );
2104 builder.setPositiveButton(getString(R.string.rename_action),
2105 new Dialog.OnClickListener() {
2106 public void onClick(DialogInterface dialog, int which) {
2107 changeFolderName();
2108 }
2109 }
2110 );
2111 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002112
2113 final AlertDialog dialog = builder.create();
2114 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2115 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002116 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002117 mInput.requestFocus();
2118 InputMethodManager inputManager = (InputMethodManager)
2119 getSystemService(Context.INPUT_METHOD_SERVICE);
2120 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002121 }
2122 });
2123
2124 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
2126
2127 private void changeFolderName() {
2128 final String name = mInput.getText().toString();
2129 if (!TextUtils.isEmpty(name)) {
2130 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 mFolderInfo.title = name;
2133 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2134
Joe Onorato9c1289c2009-08-17 11:03:03 -04002135 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002136 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 } else {
2139 final FolderIcon folderIcon = (FolderIcon)
2140 mWorkspace.getViewForTag(mFolderInfo);
2141 if (folderIcon != null) {
2142 folderIcon.setText(name);
2143 getWorkspace().requestLayout();
2144 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002145 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002146 mWorkspaceLoading = true;
2147 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 }
2149 }
2150 }
2151 cleanup();
2152 }
2153
2154 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 dismissDialog(DIALOG_RENAME_FOLDER);
2156 mWaitingForResult = false;
2157 mFolderInfo = null;
2158 }
2159 }
2160
Daniel Sandler843e8602010-06-07 14:59:01 -04002161 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2162 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002163 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002164 }
2165
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002166 // AllAppsView.Watcher
2167 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002168 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2169 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002170 mWorkspace.setVisibility(View.GONE);
2171 }
2172 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002173
2174 /**
2175 * Helper function for creating a show or hide animations for a toolbar button.
2176 *
2177 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2178 * @param view The toolbar button to be animated
2179 * @return An Animatable that will perform the show or hide action
2180 */
2181 private Animatable getToolbarButtonAnim(boolean show, final View view) {
2182 final boolean showing = show;
2183 final boolean hiding = !show;
2184
2185 final int duration = show ?
2186 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2187 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2188
2189 Animatable anim = new PropertyAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2190 anim.addListener(new AnimatableListenerAdapter() {
2191 public void onAnimationStart(Animatable animation) {
2192 if (showing) {
2193 view.setVisibility(View.VISIBLE);
2194 view.setFocusable(true);
2195 view.setClickable(true);
2196 }
2197 }
2198 public void onAnimationEnd(Animatable animation) {
2199 if (hiding) {
2200 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2201 view.setVisibility(View.INVISIBLE);
2202 view.setFocusable(false);
2203 view.setClickable(false);
2204 }
2205 }
2206 });
2207 return anim;
2208 }
2209
2210 /**
2211 * Create the animations to show/hide the appropriate toolbar buttons for the new state.
2212 *
2213 * @param newState The state that is being switched to
2214 * @param showSeq Sequencer in which to put "show" animations
2215 * @param hideSeq Sequencer in which to put "hide" animations
2216 */
2217 private void getToolbarButtonAnimations(State newState, Sequencer showSeq, Sequencer hideSeq) {
2218 final View searchButton = findViewById(R.id.search_button);
2219 final View allAppsButton = findViewById(R.id.all_apps_button);
2220 final View marketButton = findViewById(R.id.market_button);
2221 final View configureButton = findViewById(R.id.configure_button);
2222
2223 switch (newState) {
2224 case WORKSPACE:
2225 showSeq.playTogether(new Animatable[] {
2226 getToolbarButtonAnim(true, searchButton),
2227 getToolbarButtonAnim(true, allAppsButton),
2228 getToolbarButtonAnim(true, configureButton)
2229 });
2230 hideSeq.play(getToolbarButtonAnim(false, marketButton));
2231 break;
2232 case ALL_APPS:
2233 // TODO: Market button should also be shown
2234 showSeq.play(getToolbarButtonAnim(true, configureButton))
2235 .with(getToolbarButtonAnim(true, marketButton));
2236 hideSeq.play(getToolbarButtonAnim(false, searchButton))
2237 .with(getToolbarButtonAnim(false, allAppsButton));
2238 break;
2239 case CUSTOMIZE:
2240 showSeq.play(getToolbarButtonAnim(true, allAppsButton));
2241 hideSeq.playTogether(new Animatable[] {
2242 getToolbarButtonAnim(false, searchButton),
2243 getToolbarButtonAnim(false, marketButton),
2244 getToolbarButtonAnim(false, configureButton)
2245 });
2246 break;
2247 }
2248 }
2249
2250 /**
2251 * Helper method for the cameraZoomIn/cameraZoomOut animations
2252 * @param view The view being animated
2253 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2254 * @param scaleFactor The scale factor used for the zoom
2255 */
2256 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2257 final int height = view.getHeight();
2258 view.setPivotX(view.getWidth() / 2.0f);
2259 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2260 // Assumes that the view is normally anchored to either the top or bottom of the screen
2261 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2262 if (state == State.ALL_APPS) {
2263 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2264 } else {
2265 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2266 }
2267 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002268
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002269 /**
2270 * Zoom the camera out from the workspace to reveal 'toView'.
2271 * Assumes that the view to show is anchored at either the very top or very bottom
2272 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002274 */
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002275 private void cameraZoomOut(State toState, final boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002276 final Resources res = getResources();
2277 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2278 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002279 final boolean toAllApps = (toState == State.ALL_APPS);
2280 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002281 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002282
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002283 // toView should appear right at the end of the workspace shrink animation
2284 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2285
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002286 setPivotsForZoom(toView, toState, scale);
2287
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002288 Interpolator interp = new DecelerateInterpolator();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002289 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2290 scaleXAnim.setInterpolator(interp);
2291 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2292 public void onAnimationStart(Animatable animation) {
2293 // Prepare the position
2294 toView.setTranslationX(0.0f);
2295 toView.setTranslationY(0.0f);
2296 toView.setVisibility(View.VISIBLE);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002297
2298 if (!animated) animation.end(); // Go immediately to the final state
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002299 }
2300 });
2301
2302 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2303 scaleYAnim.setInterpolator(interp);
2304
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002305 Sequencer toolbarHideAnim = new Sequencer();
2306 Sequencer toolbarShowAnim = new Sequencer();
2307 getToolbarButtonAnimations(toState, toolbarShowAnim, toolbarHideAnim);
2308
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002309 Sequencer s = new Sequencer();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002310 s.playTogether(scaleXAnim, scaleYAnim, toolbarHideAnim);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002311 s.play(scaleXAnim).after(startDelay);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002312
2313 // Show the new toolbar buttons just as the main animation is ending
2314 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2315 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2316
2317 if (toState == State.ALL_APPS) {
2318 mWorkspace.shrinkToBottom(animated);
2319 } else {
2320 mWorkspace.shrinkToTop(animated);
2321 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002322 s.start();
2323 }
2324
2325 /**
2326 * Zoom the camera back into the workspace, hiding 'fromView'.
2327 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002328 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2329 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002330 */
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002331 private void cameraZoomIn(State fromState, final boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002332 Resources res = getResources();
2333 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2334 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002335 final View fromView =
2336 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2337
2338 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339
2340 Interpolator interp = new AccelerateInterpolator();
2341
2342 Sequencer s = new Sequencer();
2343 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2344 scaleXAnim.setInterpolator(interp);
2345 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2346 scaleYAnim.setInterpolator(interp);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002347 s.addListener(new AnimatableListenerAdapter() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002348 public void onAnimationStart(Animatable animation) {
2349 if (!animated) animation.end(); // Go immediately to the final state
2350 }
2351
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002352 public void onAnimationEnd(Animatable animation) {
2353 fromView.setVisibility(View.GONE);
2354 fromView.setScaleX(1.0f);
2355 fromView.setScaleY(1.0f);
2356 }
2357 });
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002358
2359 Sequencer toolbarHideAnim = new Sequencer();
2360 Sequencer toolbarShowAnim = new Sequencer();
2361 getToolbarButtonAnimations(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2362
2363 s.playTogether(scaleXAnim, scaleYAnim, toolbarHideAnim);
2364
2365 // Show the new toolbar buttons at the very end of the whole animation
2366 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2367 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2368 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2369
2370 mWorkspace.unshrink(animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002371 s.start();
2372 }
2373
2374 /**
2375 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2376 * This is the transition used on xlarge screens to go between all apps and
2377 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002378 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2379 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2380 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002381 */
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002382 private void cameraPan(State fromState, State toState, final boolean animated) {
2383 final Resources res = getResources();
2384 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002385 final int workspaceHeight = mWorkspace.getHeight();
2386
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002387 final boolean fromAllApps = (fromState == State.ALL_APPS);
2388 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2389 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002390
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002391 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2392 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2393 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2394 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002395
2396 Sequencer s = new Sequencer();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002397 s.addListener(new AnimatableListenerAdapter() {
2398 public void onAnimationStart(Animatable animation) {
2399 toView.setVisibility(View.VISIBLE);
2400 toView.setY(toViewStartY);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002401
2402 if (!animated) animation.end(); // Go immediately to the final state
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002403 }
2404 public void onAnimationEnd(Animatable animation) {
2405 fromView.setVisibility(View.GONE);
2406 }
2407 });
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002408
2409 Sequencer toolbarHideAnim = new Sequencer();
2410 Sequencer toolbarShowAnim = new Sequencer();
2411 getToolbarButtonAnimations(toState, toolbarShowAnim, toolbarHideAnim);
2412
2413 s.playTogether(
2414 toolbarHideAnim,
2415 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2416 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2417
2418 // Show the new toolbar buttons just as the main animation is ending
2419 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2420 s.play(toolbarShowAnim).after(duration - fadeInTime);
2421
2422 if (toState == State.ALL_APPS) {
2423 mWorkspace.shrinkToBottom(animated);
2424 } else {
2425 mWorkspace.shrinkToTop(animated);
2426 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002427 s.start();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002428 if (!animated) s.end(); // Go immediately to the final state
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002429 }
2430
2431 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002432 if (mAllAppsGrid.isVisible())
2433 return;
2434
Michael Jurka79212d82010-07-30 16:36:20 -07002435 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002436 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002437 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002438 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002439 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002440 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002441 } else {
2442 mAllAppsGrid.zoom(1.0f, animated);
2443 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002444
Romain Guyc16fea72010-03-12 17:17:56 -08002445 ((View) mAllAppsGrid).setFocusable(true);
2446 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002447
Joe Onorato7c312c12009-08-13 21:36:53 -07002448 // TODO: fade these two too
2449 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002450 }
2451
Joe Onoratob2061212009-11-24 16:13:54 -05002452 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002453 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002454 * - Home from workspace
2455 * - from center screen
2456 * - from other screens
2457 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002458 * - from center screen
2459 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002460 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002461 * - from center screen
2462 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002463 * - Launch app from workspace and quit
2464 * - with back
2465 * - with home
2466 * - Launch app from all apps and quit
2467 * - with back
2468 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002469 * - Go to a screen that's not the default, then all
2470 * apps, and launch and app, and go back
2471 * - with back
2472 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002473 * - On workspace, long press power and go back
2474 * - with back
2475 * - with home
2476 * - On all apps, long press power and go back
2477 * - with back
2478 * - with home
2479 * - On workspace, power off
2480 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002481 * - Launch an app and turn off the screen while in that app
2482 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002483 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002484 * - From all apps
2485 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002486 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2487 * - From all apps
2488 * - From the center workspace
2489 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002490 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002491 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002492 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002493 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002494 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002495 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002496 } else {
2497 mAllAppsGrid.zoom(0.0f, animated);
2498 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002499 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002500 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002501 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002502 }
2503
Joe Onorato7c312c12009-08-13 21:36:53 -07002504 void lockAllApps() {
2505 // TODO
2506 }
2507
2508 void unlockAllApps() {
2509 // TODO
2510 }
2511
Patrick Dubroy558654c2010-07-23 16:48:11 -07002512 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002513 return mHomeCustomizationDrawer != null &&
2514 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002515 }
2516
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002517 // Show the customization drawer (only exists in x-large configuration)
2518 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002519 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002520 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002521 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002522 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002523 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002524 }
2525
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002526 // Hide the customization drawer (only exists in x-large configuration)
2527 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002528 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002529 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002530 }
2531 }
2532
Michael Jurka213d9632010-07-28 11:29:25 -07002533 void onWorkspaceUnshrink() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002534 final boolean animated = true;
Michael Jurka213d9632010-07-28 11:29:25 -07002535 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002536 closeAllApps(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002537 }
2538 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002539 hideCustomizationDrawer(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002540 }
2541 }
2542
Joe Onorato7404ee42009-07-31 11:54:44 -07002543 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 * Displays the shortcut creation dialog and launches, if necessary, the
2545 * appropriate activity.
2546 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002547 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002548 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2549 DialogInterface.OnShowListener {
2550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2557 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002558 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 builder.setInverseBackgroundForced(true);
2561
2562 AlertDialog dialog = builder.create();
2563 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002564 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002565 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 return dialog;
2568 }
2569
2570 public void onCancel(DialogInterface dialog) {
2571 mWaitingForResult = false;
2572 cleanup();
2573 }
2574
Romain Guycbb89e42009-06-08 15:52:54 -07002575 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002576 }
2577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002579 try {
2580 dismissDialog(DIALOG_CREATE_SHORTCUT);
2581 } catch (Exception e) {
2582 // An exception is thrown if the dialog is not visible, which is fine
2583 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 }
2585
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002586 /**
2587 * Handle the action clicked in the "Add to home" dialog.
2588 */
2589 public void onClick(DialogInterface dialog, int which) {
2590 Resources res = getResources();
2591 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002592
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002593 switch (which) {
2594 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002595 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002596 break;
2597 }
Romain Guycbb89e42009-06-08 15:52:54 -07002598
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002599 case AddAdapter.ITEM_APPWIDGET: {
2600 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002601
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002602 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2603 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002604 // start the pick activity
2605 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2606 break;
2607 }
Romain Guycbb89e42009-06-08 15:52:54 -07002608
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002609 case AddAdapter.ITEM_LIVE_FOLDER: {
2610 // Insert extra item to handle inserting folder
2611 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002612
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002613 ArrayList<String> shortcutNames = new ArrayList<String>();
2614 shortcutNames.add(res.getString(R.string.group_folder));
2615 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002616
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002617 ArrayList<ShortcutIconResource> shortcutIcons =
2618 new ArrayList<ShortcutIconResource>();
2619 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2620 R.drawable.ic_launcher_folder));
2621 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2622
2623 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2624 pickIntent.putExtra(Intent.EXTRA_INTENT,
2625 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2626 pickIntent.putExtra(Intent.EXTRA_TITLE,
2627 getText(R.string.title_select_live_folder));
2628 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002629
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002630 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2631 break;
2632 }
2633
2634 case AddAdapter.ITEM_WALLPAPER: {
2635 startWallpaper();
2636 break;
2637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 }
2639 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002640
2641 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002642 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644 }
2645
2646 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002647 * Receives notifications when applications are added/removed.
2648 */
2649 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2650 @Override
2651 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002652 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002653 String reason = intent.getStringExtra("reason");
2654 if (!"homekey".equals(reason)) {
2655 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002656 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002657 animate = false;
2658 }
Joe Onoratob2061212009-11-24 16:13:54 -05002659 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002661 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002662 }
2663 }
2664
2665 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002666 * Receives notifications whenever the appwidgets are reset.
2667 */
2668 private class AppWidgetResetObserver extends ContentObserver {
2669 public AppWidgetResetObserver() {
2670 super(new Handler());
2671 }
2672
2673 @Override
2674 public void onChange(boolean selfChange) {
2675 onAppWidgetReset();
2676 }
2677 }
2678
2679 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002680 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002682 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002683 if (mWorkspace != null) {
2684 return mWorkspace.getCurrentScreen();
2685 } else {
2686 return SCREEN_COUNT / 2;
2687 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002688 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002689
Joe Onorato9c1289c2009-08-17 11:03:03 -04002690 /**
2691 * Refreshes the shortcuts shown on the workspace.
2692 *
2693 * Implementation of the method from LauncherModel.Callbacks.
2694 */
2695 public void startBinding() {
2696 final Workspace workspace = mWorkspace;
2697 int count = workspace.getChildCount();
2698 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002699 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002700 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2701 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002702
Joe Onorato9c1289c2009-08-17 11:03:03 -04002703 if (DEBUG_USER_INTERFACE) {
2704 android.widget.Button finishButton = new android.widget.Button(this);
2705 finishButton.setText("Finish");
2706 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2707
2708 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2709 public void onClick(View v) {
2710 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002711 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002712 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 }
2714 }
2715
2716 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002717 * Bind the items start-end from the list.
2718 *
2719 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002721 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2722
2723 final Workspace workspace = mWorkspace;
2724
2725 for (int i=start; i<end; i++) {
2726 final ItemInfo item = shortcuts.get(i);
2727 mDesktopItems.add(item);
2728 switch (item.itemType) {
2729 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2730 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002731 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002732 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2733 false);
2734 break;
2735 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2736 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2737 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2738 (UserFolderInfo) item);
2739 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2740 false);
2741 break;
2742 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2743 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2744 R.layout.live_folder_icon, this,
2745 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2746 (LiveFolderInfo) item);
2747 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2748 false);
2749 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 }
2752
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 }
2755
Joe Onorato9c1289c2009-08-17 11:03:03 -04002756 /**
2757 * Implementation of the method from LauncherModel.Callbacks.
2758 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002759 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2760 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002761 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002763
2764 /**
2765 * Add the views for a widget to the workspace.
2766 *
2767 * Implementation of the method from LauncherModel.Callbacks.
2768 */
2769 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002770 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2771 if (DEBUG_WIDGETS) {
2772 Log.d(TAG, "bindAppWidget: " + item);
2773 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 final Workspace workspace = mWorkspace;
2775
2776 final int appWidgetId = item.appWidgetId;
2777 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002778 if (DEBUG_WIDGETS) {
2779 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2780 }
2781
Joe Onorato9c1289c2009-08-17 11:03:03 -04002782 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2783
Joe Onorato9c1289c2009-08-17 11:03:03 -04002784 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2785 item.hostView.setTag(item);
2786
2787 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2788 item.cellY, item.spanX, item.spanY, false);
2789
2790 workspace.requestLayout();
2791
2792 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002793
2794 if (DEBUG_WIDGETS) {
2795 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2796 + (SystemClock.uptimeMillis()-start) + "ms");
2797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 }
2799
2800 /**
2801 * Callback saying that there aren't any more items to bind.
2802 *
2803 * Implementation of the method from LauncherModel.Callbacks.
2804 */
2805 public void finishBindingItems() {
2806 if (mSavedState != null) {
2807 if (!mWorkspace.hasFocus()) {
2808 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2809 }
2810
2811 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2812 if (userFolders != null) {
2813 for (long folderId : userFolders) {
2814 final FolderInfo info = mFolders.get(folderId);
2815 if (info != null) {
2816 openFolder(info);
2817 }
2818 }
2819 final Folder openFolder = mWorkspace.getOpenFolder();
2820 if (openFolder != null) {
2821 openFolder.requestFocus();
2822 }
2823 }
2824
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 mSavedState = null;
2826 }
2827
2828 if (mSavedInstanceState != null) {
2829 super.onRestoreInstanceState(mSavedInstanceState);
2830 mSavedInstanceState = null;
2831 }
2832
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 mWorkspaceLoading = false;
2834 }
2835
2836 /**
2837 * Add the icons for all apps.
2838 *
2839 * Implementation of the method from LauncherModel.Callbacks.
2840 */
2841 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002842 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 }
2844
2845 /**
2846 * A package was installed.
2847 *
2848 * Implementation of the method from LauncherModel.Callbacks.
2849 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002850 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002851 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002852 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 }
2854
2855 /**
2856 * A package was updated.
2857 *
2858 * Implementation of the method from LauncherModel.Callbacks.
2859 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002860 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002862 mWorkspace.updateShortcuts(apps);
2863 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 }
2865
2866 /**
2867 * A package was uninstalled.
2868 *
2869 * Implementation of the method from LauncherModel.Callbacks.
2870 */
Joe Onorato36115782010-06-17 13:28:48 -04002871 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002873 if (permanent) {
2874 mWorkspace.removeItems(apps);
2875 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002876 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 }
Joe Onoratobe386092009-11-17 17:32:16 -08002878
2879 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002880 * A number of packages were updated.
2881 */
2882 public void bindPackagesUpdated() {
2883 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002884 if (mCustomizePagedView != null)
2885 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002886 }
2887
2888 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002889 * Prints out out state for debugging.
2890 */
2891 public void dumpState() {
2892 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002893 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002894 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2895 Log.d(TAG, "mRestoring=" + mRestoring);
2896 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2897 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2898 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2899 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002900 mModel.dumpState();
2901 mAllAppsGrid.dumpState();
2902 Log.d(TAG, "END launcher2 dump state");
2903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904}