blob: 80e148124a4c6b12b8d8c619a57959ca1bb651e9 [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;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Patrick Dubroy7247f632010-08-04 16:02:59 -070028import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070034import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070035import android.appwidget.AppWidgetManager;
36import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070045import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040051import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080054import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070055import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070065import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070080import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070082import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070083import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070084import android.view.animation.DecelerateInterpolator;
85import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
89import android.widget.LinearLayout;
90import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070091import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070092import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070093import android.widget.TabHost.OnTabChangeListener;
94import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070095import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.TextView;
97import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070098
99import java.io.DataInputStream;
100import java.io.DataOutputStream;
101import java.io.FileNotFoundException;
102import java.io.IOException;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int WALLPAPER_SCREENS_SPAN = 2;
121
122 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800123 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int MENU_ADD = Menu.FIRST + 1;
126 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
127 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
128 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700129 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 private static final int REQUEST_CREATE_SHORTCUT = 1;
132 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_PICK_APPLICATION = 6;
135 private static final int REQUEST_PICK_SHORTCUT = 7;
136 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
150 // Type: int
151 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
152 // Type: boolean
153 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
154 // Type: long
155 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
170 // Type: int[]
171 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
172 // Type: boolean
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
174 // Type: long
175 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
176
Winson Chung80baf5a2010-08-09 16:03:15 -0700177 // tags for the customization tabs
178 private static final String WIDGETS_TAG = "widgets";
179 private static final String FOLDERS_TAG = "folders";
180 private static final String SHORTCUTS_TAG = "shortcuts";
181 private static final String WALLPAPERS_TAG = "wallpapers";
182
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183 /** The different states that Launcher can be in. */
184 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800189 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800191 private final BroadcastReceiver mCloseSystemDialogsReceiver
192 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800193 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private LayoutInflater mInflater;
196
Joe Onorato00acb122009-08-04 16:04:30 -0400197 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 private AppWidgetManager mAppWidgetManager;
201 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private CellLayout.CellInfo mAddItemCellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700204 private int[] mAddItemCoordinates;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private CellLayout.CellInfo mMenuAddInfo;
206 private final int[] mCellCoordinates = new int[2];
207 private FolderInfo mFolderInfo;
208
Joe Onorato7c312c12009-08-13 21:36:53 -0700209 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700211 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700212 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700213 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private Bundle mSavedState;
216
217 private SpannableStringBuilder mDefaultKeySsb = null;
218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 private boolean mWorkspaceLoading = true;
220
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800221 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private boolean mRestoring;
223 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
225 private Bundle mSavedInstanceState;
226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800228 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229
Romain Guy84f296c2009-11-04 15:00:44 -0800230 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
231 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Romain Guy1fbc1c82009-11-09 20:43:08 -0800233 private ImageView mPreviousView;
234 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500235
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400236 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400237 private String[] mHotseatConfig = null;
238 private Intent[] mHotseats = null;
239 private Drawable[] mHotseatIcons = null;
240 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 @Override
243 protected void onCreate(Bundle savedInstanceState) {
244 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700245
Winson Chungaafa03c2010-06-11 17:34:16 -0700246 if (LauncherApplication.isInPlaceRotationEnabled()) {
247 // hide the status bar (temporary until we get the status bar design figured out)
248 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
249 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
250 }
251
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 LauncherApplication app = ((LauncherApplication)getApplication());
253 mModel = app.setLauncher(this);
254 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400255 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700257
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700258 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
260 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 if (PROFILE_STARTUP) {
263 android.os.Debug.startMethodTracing("/sdcard/launcher");
264 }
265
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400266 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 checkForLocaleChange();
268 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700270 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
271 if (mHomeCustomizationDrawer != null) {
272 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700273
Winson Chung80baf5a2010-08-09 16:03:15 -0700274 // share the same customization workspace across all the tabs
275 mCustomizePagedView = new CustomizePagedView(this);
276 TabContentFactory contentFactory = new TabContentFactory() {
277 public View createTabContent(String tag) {
278 return mCustomizePagedView;
279 }
280 };
281
Michael Jurka946ad472010-07-09 18:05:18 -0700282 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700283 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
284 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700285 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
287 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700288 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700289 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
290 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700291 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
293 .setIndicator(wallpapersLabel).setContent(contentFactory));
294
295 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
296 // new assets
297 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
298 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
299 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
300 TextView text = (TextView) tab.getChildAt(1);
301 text.setTextSize(20.0f);
302 text.setPadding(20, 0, 20, 0);
303 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
304 tab.setBackgroundDrawable(null);
305 }
306
307 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
308 public void onTabChanged(String tabId) {
309 // animate the changing of the tab content by fading pages in and out
310 final int duration = 150;
311 final float alpha = mCustomizePagedView.getAlpha();
312 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
313 "alpha", alpha, 0.0f);
314 alphaAnim.addListener(new AnimatableListenerAdapter() {
315 public void onAnimationEnd(Animatable animation) {
316 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
317 if (tag == WIDGETS_TAG) {
318 mCustomizePagedView.setCustomizationFilter(
319 CustomizePagedView.CustomizationType.WidgetCustomization);
320 } else if (tag == FOLDERS_TAG) {
321 mCustomizePagedView.setCustomizationFilter(
322 CustomizePagedView.CustomizationType.FolderCustomization);
323 } else if (tag == SHORTCUTS_TAG) {
324 mCustomizePagedView.setCustomizationFilter(
325 CustomizePagedView.CustomizationType.ShortcutCustomization);
326 } else if (tag == WALLPAPERS_TAG) {
327 mCustomizePagedView.setCustomizationFilter(
328 CustomizePagedView.CustomizationType.WallpaperCustomization);
329 }
330
331 final float alpha = mCustomizePagedView.getAlpha();
332 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
333 "alpha", alpha, 1.0f);
334 alphaAnim.start();
335 }
336 });
337 alphaAnim.start();
338 }
339 });
Michael Jurka946ad472010-07-09 18:05:18 -0700340
341 mHomeCustomizationDrawer.setCurrentTab(0);
342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 setupViews();
344
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800345 registerContentObservers();
346
Joe Onorato7c312c12009-08-13 21:36:53 -0700347 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 mSavedState = savedInstanceState;
350 restoreState(mSavedState);
351
352 if (PROFILE_STARTUP) {
353 android.os.Debug.stopMethodTracing();
354 }
355
356 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400357 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
359
360 // For handling default keys
361 mDefaultKeySsb = new SpannableStringBuilder();
362 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800363
364 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
365 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 }
Romain Guycbb89e42009-06-08 15:52:54 -0700367
Winson Chungaafa03c2010-06-11 17:34:16 -0700368 @Override
369 public void onConfigurationChanged(Configuration newConfig) {
370 // TODO Auto-generated method stub
371 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700375 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
376 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 final Configuration configuration = getResources().getConfiguration();
379
Romain Guy98d01652009-06-30 16:21:04 -0700380 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 final String locale = configuration.locale.toString();
382
Romain Guy98d01652009-06-30 16:21:04 -0700383 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 final int mcc = configuration.mcc;
385
Romain Guy98d01652009-06-30 16:21:04 -0700386 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 final int mnc = configuration.mnc;
388
Romain Guy84f296c2009-11-04 15:00:44 -0800389 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390
Romain Guy84f296c2009-11-04 15:00:44 -0800391 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700392 localeConfiguration.locale = locale;
393 localeConfiguration.mcc = mcc;
394 localeConfiguration.mnc = mnc;
395
396 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800397 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400398
399 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700400 }
401 }
402
403 private static class LocaleConfiguration {
404 public String locale;
405 public int mcc = -1;
406 public int mnc = -1;
407 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700408
Romain Guy98d01652009-06-30 16:21:04 -0700409 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
410 DataInputStream in = null;
411 try {
412 in = new DataInputStream(context.openFileInput(PREFERENCES));
413 configuration.locale = in.readUTF();
414 configuration.mcc = in.readInt();
415 configuration.mnc = in.readInt();
416 } catch (FileNotFoundException e) {
417 // Ignore
418 } catch (IOException e) {
419 // Ignore
420 } finally {
421 if (in != null) {
422 try {
423 in.close();
424 } catch (IOException e) {
425 // Ignore
426 }
427 }
428 }
429 }
430
431 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
432 DataOutputStream out = null;
433 try {
434 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
435 out.writeUTF(configuration.locale);
436 out.writeInt(configuration.mcc);
437 out.writeInt(configuration.mnc);
438 out.flush();
439 } catch (FileNotFoundException e) {
440 // Ignore
441 } catch (IOException e) {
442 //noinspection ResultOfMethodCallIgnored
443 context.getFileStreamPath(PREFERENCES).delete();
444 } finally {
445 if (out != null) {
446 try {
447 out.close();
448 } catch (IOException e) {
449 // Ignore
450 }
451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453 }
454
455 static int getScreen() {
456 synchronized (sLock) {
457 return sScreen;
458 }
459 }
460
461 static void setScreen(int screen) {
462 synchronized (sLock) {
463 sScreen = screen;
464 }
465 }
466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700468 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469
470 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700471 // TODO: Put back when we decide about scrolling the wallpaper
472 // boolean isPortrait = display.getWidth() < display.getHeight();
473 // final int width = isPortrait ? display.getWidth() : display.getHeight();
474 // final int height = isPortrait ? display.getHeight() : display.getWidth();
475 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
476 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400479 // Note: This doesn't do all the client-id magic that BrowserProvider does
480 // in Browser. (http://b/2425179)
481 private Uri getDefaultBrowserUri() {
482 String url = getString(R.string.default_browser_url);
483 if (url.indexOf("{CID}") != -1) {
484 url = url.replace("{CID}", "android-google");
485 }
486 return Uri.parse(url);
487 }
488
489 // Load the Intent templates from arrays.xml to populate the hotseats. For
490 // each Intent, if it resolves to a single app, use that as the launch
491 // intent & use that app's label as the contentDescription. Otherwise,
492 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400493 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400494 if (mHotseatConfig == null) {
495 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
496 if (mHotseatConfig.length > 0) {
497 mHotseats = new Intent[mHotseatConfig.length];
498 mHotseatLabels = new CharSequence[mHotseatConfig.length];
499 mHotseatIcons = new Drawable[mHotseatConfig.length];
500 } else {
501 mHotseats = null;
502 mHotseatIcons = null;
503 mHotseatLabels = null;
504 }
505
506 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
507 for (int i=0; i<mHotseatConfig.length; i++) {
508 // load icon for this slot; currently unrelated to the actual activity
509 try {
510 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
511 } catch (ArrayIndexOutOfBoundsException ex) {
512 Log.w(TAG, "Missing hotseat_icons array item #" + i);
513 mHotseatIcons[i] = null;
514 }
515 }
516 hotseatIconDrawables.recycle();
517 }
518
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400519 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400520 for (int i=0; i<mHotseatConfig.length; i++) {
521 Intent intent = null;
522 if (mHotseatConfig[i].equals("*BROWSER*")) {
523 // magic value meaning "launch user's default web browser"
524 // replace it with a generic web request so we can see if there is indeed a default
525 String defaultUri = getString(R.string.default_browser_url);
526 intent = new Intent(
527 Intent.ACTION_VIEW,
528 ((defaultUri != null)
529 ? Uri.parse(defaultUri)
530 : getDefaultBrowserUri())
531 ).addCategory(Intent.CATEGORY_BROWSABLE);
532 // note: if the user launches this without a default set, she
533 // will always be taken to the default URL above; this is
534 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700535 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400536 // URL is unavoidably sent to the chosen app.
537 } else {
538 try {
539 intent = Intent.parseUri(mHotseatConfig[i], 0);
540 } catch (java.net.URISyntaxException ex) {
541 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
542 // bogus; leave intent=null
543 }
544 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700545
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 if (intent == null) {
547 mHotseats[i] = null;
548 mHotseatLabels[i] = getText(R.string.activity_not_found);
549 continue;
550 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400551
552 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700553 Log.d(TAG, "loadHotseats: hotseat " + i
554 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400555 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400556 + "]");
557 }
558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
560 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 Log.d(TAG, "Best match for intent: " + bestMatch);
563 Log.d(TAG, "All matches: ");
564 for (ResolveInfo ri : allMatches) {
565 Log.d(TAG, " --> " + ri);
566 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400567 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 // did this resolve to a single app, or the resolver?
569 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700570 // can't find any activity to handle this. let's leave the
571 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 // to launch.
573 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 // set accessibility text to "Not installed"
576 mHotseatLabels[i] = getText(R.string.activity_not_found);
577 } else {
578 boolean found = false;
579 for (ResolveInfo ri : allMatches) {
580 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
581 && bestMatch.activityInfo.applicationInfo.packageName
582 .equals(ri.activityInfo.applicationInfo.packageName)) {
583 found = true;
584 break;
585 }
586 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700587
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 if (!found) {
589 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
590 // the bestMatch is probably the ResolveActivity, meaning the
591 // user has not yet selected a default
592 // so: we'll keep the original intent for now
593 mHotseats[i] = intent;
594
595 // set the accessibility text to "Select shortcut"
596 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
597 } else {
598 // we have an app!
599 // now reconstruct the intent to launch it through the front
600 // door
601 ComponentName com = new ComponentName(
602 bestMatch.activityInfo.applicationInfo.packageName,
603 bestMatch.activityInfo.name);
604 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
605
606 // load the app label for accessibility
607 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
608 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400609 }
610
611 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700612 Log.d(TAG, "loadHotseats: hotseat " + i
613 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400614 + ((mHotseats[i] == null)
615 ? "null"
616 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400617 + "] label=[" + mHotseatLabels[i]
618 + "]"
619 );
620 }
621 }
622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 @Override
625 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700626 mWaitingForResult = false;
627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 // The pattern used here is that a user PICKs a specific application,
629 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
632 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700633
Romain Guy94dabf12009-07-21 10:55:43 -0700634 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 switch (requestCode) {
636 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 break;
639 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800640 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 break;
642 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400643 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 break;
645 case REQUEST_PICK_LIVE_FOLDER:
646 addLiveFolder(data);
647 break;
648 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700651 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700652 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700654 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700655 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700656 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700658 case REQUEST_PICK_WALLPAPER:
659 // We just wanted the activity result here so we can clear mWaitingForResult
660 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Romain Guy18042c82009-11-06 11:44:55 -0800662 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
663 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
664 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700665 // Clean up the appWidgetId if we canceled
666 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
667 if (appWidgetId != -1) {
668 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
672
673 @Override
674 protected void onResume() {
675 super.onResume();
676
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800677 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 mWorkspaceLoading = true;
681 mModel.startLoader(this, true);
682 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684 }
685
686 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 protected void onPause() {
688 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700689 // Some launcher layouts don't have a previous and next view
690 if (mPreviousView != null) {
691 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700692 }
693 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700694 dismissPreview(mNextView);
695 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800696 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 }
Romain Guycbb89e42009-06-08 15:52:54 -0700698
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700699 @Override
700 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 // Flag the loader to stop early before switching
702 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800703 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800704 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700705 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800707 // We can't hide the IME if it was forced open. So don't bother
708 /*
709 @Override
710 public void onWindowFocusChanged(boolean hasFocus) {
711 super.onWindowFocusChanged(hasFocus);
712
713 if (hasFocus) {
714 final InputMethodManager inputManager = (InputMethodManager)
715 getSystemService(Context.INPUT_METHOD_SERVICE);
716 WindowManager.LayoutParams lp = getWindow().getAttributes();
717 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
718 android.os.Handler()) {
719 protected void onReceiveResult(int resultCode, Bundle resultData) {
720 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
721 }
722 });
723 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
724 }
725 }
726 */
727
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 private boolean acceptFilter() {
729 final InputMethodManager inputManager = (InputMethodManager)
730 getSystemService(Context.INPUT_METHOD_SERVICE);
731 return !inputManager.isFullscreenMode();
732 }
733
734 @Override
735 public boolean onKeyDown(int keyCode, KeyEvent event) {
736 boolean handled = super.onKeyDown(keyCode, event);
737 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
738 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
739 keyCode, event);
740 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700741 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700742 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700743 // showSearchDialog()
744 // If there are multiple keystrokes before the search dialog takes focus,
745 // onSearchRequested() will be called for every keystroke,
746 // but it is idempotent, so it's fine.
747 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
749 }
750
Joe Onorato8a9625e2010-01-28 15:55:35 -0800751 // Eat the long press event so the keyboard doesn't come up.
752 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
753 return true;
754 }
755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 return handled;
757 }
758
Karl Rosaen138a0412009-04-23 19:00:21 -0700759 private String getTypedText() {
760 return mDefaultKeySsb.toString();
761 }
762
763 private void clearTypedText() {
764 mDefaultKeySsb.clear();
765 mDefaultKeySsb.clearSpans();
766 Selection.setSelection(mDefaultKeySsb, 0);
767 }
768
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 /**
770 * Restores the previous state, if it exists.
771 *
772 * @param savedState The previous state.
773 */
774 private void restoreState(Bundle savedState) {
775 if (savedState == null) {
776 return;
777 }
778
Joe Onorato3a8820b2009-11-10 15:06:42 -0800779 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
780 if (allApps) {
781 showAllApps(false);
782 }
783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
785 if (currentScreen > -1) {
786 mWorkspace.setCurrentScreen(currentScreen);
787 }
788
789 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
790 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700791 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 mAddItemCellInfo = new CellLayout.CellInfo();
793 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
794 addItemCellInfo.valid = true;
795 addItemCellInfo.screen = addScreen;
796 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
797 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
798 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
799 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700800 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
802 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
803 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
804 mRestoring = true;
805 }
806
807 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
808 if (renameFolder) {
809 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 mRestoring = true;
812 }
813 }
814
815 /**
816 * Finds all the views we need and configure them properly.
817 */
818 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700819 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400820
Romain Guyb1b69f52009-08-10 15:10:15 -0700821 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400822 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
Joe Onorato7c312c12009-08-13 21:36:53 -0700824 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700825 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700826 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800827 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700828 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700829 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700830
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700831 if (LauncherApplication.isScreenXLarge()) {
832 // They need to be INVISIBLE initially so that they will be measured in the layout.
833 // Otherwise the animations are messed up when we show them for the first time.
834 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
835 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
836 }
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
839 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500840 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700841
Joe Onorato7c312c12009-08-13 21:36:53 -0700842 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
843 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700845 View handleView = findViewById(R.id.all_apps_button);
846 if (handleView != null && handleView instanceof HandleView) {
847 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700848 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700849 mHandleView.setLauncher(this);
850 mHandleView.setOnClickListener(this);
851 mHandleView.setOnLongClickListener(this);
852 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800853
Winson Chung80baf5a2010-08-09 16:03:15 -0700854 if (mCustomizePagedView != null) {
855 mCustomizePagedView.setLauncher(this);
856 mCustomizePagedView.setDragController(dragController);
857 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700858 } else {
859 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
860 hotseatLeft.setContentDescription(mHotseatLabels[0]);
861 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
862 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
863 hotseatRight.setContentDescription(mHotseatLabels[1]);
864 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400865
Michael Jurkaaf442092010-06-10 17:01:57 -0700866 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
867 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800868
Michael Jurkaaf442092010-06-10 17:01:57 -0700869 Drawable previous = mPreviousView.getDrawable();
870 Drawable next = mNextView.getDrawable();
871 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872
Michael Jurkaaf442092010-06-10 17:01:57 -0700873 mPreviousView.setHapticFeedbackEnabled(false);
874 mPreviousView.setOnLongClickListener(this);
875 mNextView.setHapticFeedbackEnabled(false);
876 mNextView.setOnLongClickListener(this);
877 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800878
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400880 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882
883 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400884 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700885 int deleteZoneHandleId;
886 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700887 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700888 } else {
889 deleteZoneHandleId = R.id.all_apps_button_cluster;
890 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700891 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700892 dragController.addDragListener(deleteZone);
893
894 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
895 if (infoButton != null) {
896 infoButton.setLauncher(this);
897 infoButton.setHandle(findViewById(R.id.configure_button));
898 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
899 dragController.addDragListener(infoButton);
900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901
Joe Onorato00acb122009-08-04 16:04:30 -0400902 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400903 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800904 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700905
Joe Onorato00acb122009-08-04 16:04:30 -0400906 // The order here is bottom to top.
907 dragController.addDropTarget(workspace);
908 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700909 if (infoButton != null) {
910 dragController.addDropTarget(infoButton);
911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 }
913
Romain Guy8a73c512009-11-09 19:19:59 -0800914 @SuppressWarnings({"UnusedDeclaration"})
915 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800916 if (!isAllAppsVisible()) {
917 mWorkspace.scrollLeft();
918 }
Romain Guy8a73c512009-11-09 19:19:59 -0800919 }
920
921 @SuppressWarnings({"UnusedDeclaration"})
922 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800923 if (!isAllAppsVisible()) {
924 mWorkspace.scrollRight();
925 }
Romain Guy8a73c512009-11-09 19:19:59 -0800926 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400927
928 @SuppressWarnings({"UnusedDeclaration"})
929 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400930 if (isAllAppsVisible()) return;
931
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400932 int index = -1;
933 if (v.getId() == R.id.hotseat_left) {
934 index = 0;
935 } else if (v.getId() == R.id.hotseat_right) {
936 index = 1;
937 }
938
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400939 // reload these every tap; you never know when they might change
940 loadHotseats();
941 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
942 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400943 startActivitySafely(
944 mHotseats[index],
945 "hotseat"
946 );
947 }
948 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700949
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 /**
951 * Creates a view representing a shortcut.
952 *
953 * @param info The data structure describing the shortcut.
954 *
955 * @return A View inflated from R.layout.application.
956 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800957 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 return createShortcut(R.layout.application,
959 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
960 }
961
962 /**
963 * Creates a view representing a shortcut inflated from the specified resource.
964 *
965 * @param layoutResId The id of the XML layout used to create the shortcut.
966 * @param parent The group the shortcut belongs to.
967 * @param info The data structure describing the shortcut.
968 *
969 * @return A View inflated from layoutResId.
970 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800971 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
973
Joe Onorato0589f0f2010-02-08 13:44:00 -0800974 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
975 new FastBitmapDrawable(info.getIcon(mIconCache)),
976 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 favorite.setText(info.title);
978 favorite.setTag(info);
979 favorite.setOnClickListener(this);
980
981 return favorite;
982 }
983
984 /**
985 * Add an application shortcut to the workspace.
986 *
987 * @param data The intent describing the application.
988 * @param cellInfo The position on screen where to create the shortcut.
989 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 cellInfo.screen = mWorkspace.getCurrentScreen();
992 if (!findSingleSlot(cellInfo)) return;
993
Joe Onorato0589f0f2010-02-08 13:44:00 -0800994 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
995 data, context);
996
Romain Guy73b979d2009-06-09 12:57:21 -0700997 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 info.container = ItemInfo.NO_ID;
1001 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1002 } else {
1003 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
1005 }
Romain Guycbb89e42009-06-08 15:52:54 -07001006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 /**
1008 * Add a shortcut to the workspace.
1009 *
1010 * @param data The intent describing the shortcut.
1011 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 cellInfo.screen = mWorkspace.getCurrentScreen();
1015 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001016
Joe Onorato0589f0f2010-02-08 13:44:00 -08001017 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018
1019 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1022 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
1024 }
1025
Romain Guycbb89e42009-06-08 15:52:54 -07001026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001028 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001030 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001031 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001033 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001035
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001038 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001041 // We have saved the position to which the widget was dragged-- this really only matters
1042 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001044
1045 // For now, we don't save the coordinate where we dropped the icon because we're not
1046 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1047 // a widget on the home screen in case we want to add it in the future
1048 final int[] xyTouch = null;
1049 //final int[] xyTouch = mAddItemCoordinates;
1050 boolean findNearestVacantAreaFailed = false;
1051 if (xyTouch != null) {
1052 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1053 int[] result = screen.findNearestVacantArea(
1054 mAddItemCoordinates[0], mAddItemCoordinates[1],
1055 spans[0], spans[1], cellInfo, xy);
1056 findNearestVacantAreaFailed = (result == null);
1057 }
1058
1059 if (findNearestVacantAreaFailed ||
1060 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001061 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1062 return;
1063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 // Build Launcher-specific widget info and save to database
1066 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 launcherInfo.spanX = spans[0];
1068 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 LauncherModel.addItemToDatabase(this, launcherInfo,
1071 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001072 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073
1074 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001082
Michael Jurkaa63c4522010-08-19 13:52:27 -07001083 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086 }
Romain Guycbb89e42009-06-08 15:52:54 -07001087
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1089 mDesktopItems.remove(launcherInfo);
1090 launcherInfo.hostView = null;
1091 }
1092
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 public LauncherAppWidgetHost getAppWidgetHost() {
1094 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001097 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001098 getWindow().closeAllPanels();
1099
1100 try {
1101 dismissDialog(DIALOG_CREATE_SHORTCUT);
1102 // Unlock the workspace if the dialog was showing
1103 } catch (Exception e) {
1104 // An exception is thrown if the dialog is not visible, which is fine
1105 }
1106
1107 try {
1108 dismissDialog(DIALOG_RENAME_FOLDER);
1109 // Unlock the workspace if the dialog was showing
1110 } catch (Exception e) {
1111 // An exception is thrown if the dialog is not visible, which is fine
1112 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001113
1114 // Whatever we were doing is hereby canceled.
1115 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001116 }
1117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 @Override
1119 protected void onNewIntent(Intent intent) {
1120 super.onNewIntent(intent);
1121
1122 // Close the menu
1123 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001124 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001125 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001126
Joe Onorato14f122b2009-11-19 14:06:36 -08001127 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1128 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1129 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001130 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1131
Patrick Dubroy558654c2010-07-23 16:48:11 -07001132
Michael Jurka4cb37242010-08-09 21:05:32 -07001133 // in all these cases, only animate if we're already on home
1134 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001135 if (alreadyOnHome && !mWorkspace.isSmall() &&
1136 !allAppsVisible && !customizationDrawerVisible) {
1137 mWorkspace.shrinkToMiddle();
1138 } else {
1139 mWorkspace.unshrink(alreadyOnHome);
1140 }
1141 } else if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001142 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001143 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001144 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001145 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001146 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001147
Joe Onorato3a8820b2009-11-10 15:06:42 -08001148 final View v = getWindow().peekDecorView();
1149 if (v != null && v.getWindowToken() != null) {
1150 InputMethodManager imm = (InputMethodManager)getSystemService(
1151 INPUT_METHOD_SERVICE);
1152 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154 }
1155 }
1156
1157 @Override
1158 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1159 // Do not call super here
1160 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001161
1162 if (mHomeCustomizationDrawer != null) {
1163 String cur = savedInstanceState.getString("currentTab");
1164 if (cur != null) {
1165 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1166 }
1167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
1169
1170 @Override
1171 protected void onSaveInstanceState(Bundle outState) {
1172 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1173
1174 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1175 if (folders.size() > 0) {
1176 final int count = folders.size();
1177 long[] ids = new long[count];
1178 for (int i = 0; i < count; i++) {
1179 final FolderInfo info = folders.get(i).getInfo();
1180 ids[i] = info.id;
1181 }
1182 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1183 } else {
1184 super.onSaveInstanceState(outState);
1185 }
1186
Joe Onoratofb0ca672009-09-14 17:55:46 -04001187 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001188 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
1192 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1193 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1194 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1195
1196 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1197 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1198 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1199 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1200 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1201 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1202 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1203 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001204 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206
1207 if (mFolderInfo != null && mWaitingForResult) {
1208 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1209 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1210 }
Michael Jurka946ad472010-07-09 18:05:18 -07001211
1212 if (mHomeCustomizationDrawer != null) {
1213 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1214 if (currentTabTag != null) {
1215 outState.putString("currentTab", currentTabTag);
1216 }
1217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 }
1219
1220 @Override
1221 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001225 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001227 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229
1230 TextKeyListener.getInstance().release();
1231
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001235
1236 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001237
Patrick Dubroyab962b72010-07-26 12:10:10 -07001238 // Some launcher layouts don't have a previous and next view
1239 if (mPreviousView != null) {
1240 dismissPreview(mPreviousView);
1241 }
1242 if (mNextView != null) {
1243 dismissPreview(mNextView);
1244 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001245
1246 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248
1249 @Override
1250 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001251 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 super.startActivityForResult(intent, requestCode);
1253 }
1254
1255 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001256 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001258
Joe Onorato7bb17492009-09-24 17:51:01 -07001259 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 if (initialQuery == null) {
1262 // Use any text typed in the launcher as the initial query
1263 initialQuery = getTypedText();
1264 clearTypedText();
1265 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 if (appSearchData == null) {
1267 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001268 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
Romain Guycbb89e42009-06-08 15:52:54 -07001270
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 final SearchManager searchManager =
1272 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001274 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
1276
1277 @Override
1278 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 if (isWorkspaceLocked()) {
1280 return false;
1281 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282
1283 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001284
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1286 .setIcon(android.R.drawable.ic_menu_add)
1287 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001288 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 .setIcon(android.R.drawable.ic_menu_gallery)
1290 .setAlphabeticShortcut('W');
1291 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1292 .setIcon(android.R.drawable.ic_search_category_default)
1293 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1294 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1295 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1296 .setAlphabeticShortcut('N');
1297
1298 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001299 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1300 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301
1302 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1303 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1304 .setIntent(settings);
1305
1306 return true;
1307 }
1308
1309 @Override
1310 public boolean onPrepareOptionsMenu(Menu menu) {
1311 super.onPrepareOptionsMenu(menu);
1312
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001313 // If all apps is animating, don't show the menu, because we don't know
1314 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001315 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001316 return false;
1317 }
1318
1319 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001320 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001321 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1322 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1323
1324 // Disable add if the workspace is full.
1325 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001326 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001327 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329
1330 return true;
1331 }
1332
Michael Jurka0e260592010-06-30 17:07:39 -07001333 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1334 // using the settings menu to add an item, something similar happens in showAddDialog
1335 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001336 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001337 mAddItemCellInfo = mMenuAddInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001338 mAddItemCellInfo = null;
Michael Jurka0e260592010-06-30 17:07:39 -07001339 }
1340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 @Override
1342 public boolean onOptionsItemSelected(MenuItem item) {
1343 switch (item.getItemId()) {
1344 case MENU_ADD:
1345 addItems();
1346 return true;
1347 case MENU_WALLPAPER_SETTINGS:
1348 startWallpaper();
1349 return true;
1350 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001351 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 return true;
1353 case MENU_NOTIFICATIONS:
1354 showNotifications();
1355 return true;
1356 }
1357
1358 return super.onOptionsItemSelected(item);
1359 }
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Karl Rosaen138a0412009-04-23 19:00:21 -07001361 /**
1362 * Indicates that we want global search for this activity by setting the globalSearch
1363 * argument for {@link #startSearch} to true.
1364 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001366 @Override
1367 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001368 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001369 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001370 }
1371
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 public boolean isWorkspaceLocked() {
1373 return mWorkspaceLoading || mWaitingForResult;
1374 }
1375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001377 if (LauncherApplication.isScreenXLarge()) {
1378 // Animate the widget chooser up from the bottom of the screen
1379 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001380 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001381 }
1382 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001383 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001384 showAddDialog(mMenuAddInfo);
1385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
Michael Jurkaa63c4522010-08-19 13:52:27 -07001388 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1389 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001390 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001391
1392 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1393 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001394 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1395 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1396 addAppWidgetImpl(appWidgetId);
1397 }
1398
1399 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001400 // TODO: catch bad widget exception when sent
1401 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001402 // TODO: Is this log message meaningful?
1403 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1404 addAppWidgetImpl(appWidgetId);
1405 }
1406
1407 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001408 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409
Bjorn Bringert7984c942009-12-09 15:38:25 +00001410 if (appWidget.configure != null) {
1411 // Launch over to configure widget, if needed
1412 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1413 intent.setComponent(appWidget.configure);
1414 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1415
Romain Guy8e633c52010-03-04 12:51:36 -08001416 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001418 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001419 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421 }
Romain Guycbb89e42009-06-08 15:52:54 -07001422
Joe Onoratodeb98af2010-02-19 14:59:39 -08001423 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001424 // Handle case where user selected "Applications"
1425 String applicationName = getResources().getString(R.string.group_applications);
1426 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001427
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001428 if (applicationName != null && applicationName.equals(shortcutName)) {
1429 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1430 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001431
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001432 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1433 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001434 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001435 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001436 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001437 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
1440 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001441 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001442 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001443 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001445 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001447 } else {
1448 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 UserFolderInfo folderInfo = new UserFolderInfo();
1454 folderInfo.title = getText(R.string.folder_name);
1455
1456 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1457 cellInfo.screen = mWorkspace.getCurrentScreen();
1458 if (!findSingleSlot(cellInfo)) return;
1459
1460 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001461 LauncherModel.addItemToDatabase(this, folderInfo,
1462 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001464 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
1466 // Create the view
1467 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1468 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1469 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001470 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473 void removeFolder(FolderInfo folder) {
1474 mFolders.remove(folder.id);
1475 }
1476
1477 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 cellInfo.screen = mWorkspace.getCurrentScreen();
1479 if (!findSingleSlot(cellInfo)) return;
1480
1481 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1482
1483 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1485 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001486 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1487 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489 }
1490
1491 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1492 CellLayout.CellInfo cellInfo, boolean notify) {
1493
1494 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1495 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1496
1497 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 Intent.ShortcutIconResource iconResource = null;
1499
1500 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1501 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1502 try {
1503 iconResource = (Intent.ShortcutIconResource) extra;
1504 final PackageManager packageManager = context.getPackageManager();
1505 Resources resources = packageManager.getResourcesForApplication(
1506 iconResource.packageName);
1507 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1508 icon = resources.getDrawable(id);
1509 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001510 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512 }
1513
1514 if (icon == null) {
1515 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1516 }
1517
1518 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001519 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 info.title = name;
1521 info.iconResource = iconResource;
1522 info.uri = data.getData();
1523 info.baseIntent = baseIntent;
1524 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1525 LiveFolders.DISPLAY_MODE_GRID);
1526
1527 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1528 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001529 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
1531 return info;
1532 }
1533
1534 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1535 final int[] xy = new int[2];
1536 if (findSlot(cellInfo, xy, 1, 1)) {
1537 cellInfo.cellX = xy[0];
1538 cellInfo.cellY = xy[1];
1539 return true;
1540 }
1541 return false;
1542 }
1543
1544 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1545 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001546 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1547 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1549 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1550 return false;
1551 }
1552 }
1553 return true;
1554 }
1555
1556 private void showNotifications() {
1557 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1558 if (statusBar != null) {
1559 statusBar.expand();
1560 }
1561 }
1562
1563 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001564 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001566 Intent chooser = Intent.createChooser(pickWallpaper,
1567 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001568 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1569 // Removed in Eclair MR1
1570// WallpaperManager wm = (WallpaperManager)
1571// getSystemService(Context.WALLPAPER_SERVICE);
1572// WallpaperInfo wi = wm.getWallpaperInfo();
1573// if (wi != null && wi.getSettingsActivity() != null) {
1574// LabeledIntent li = new LabeledIntent(getPackageName(),
1575// R.string.configure_wallpaper, 0);
1576// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1577// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1578// }
Mike Clerona0618e42009-10-22 13:55:21 -07001579 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001582 /**
1583 * Registers various content observers. The current implementation registers
1584 * only a favorites observer to keep track of the favorites applications.
1585 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001586 private void registerContentObservers() {
1587 ContentResolver resolver = getContentResolver();
1588 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1589 true, mWidgetObserver);
1590 }
1591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 @Override
1593 public boolean dispatchKeyEvent(KeyEvent event) {
1594 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1595 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001597 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001598 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001599 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001600 dumpState();
1601 return true;
1602 }
1603 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001604 }
1605 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1606 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001607 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 return true;
1609 }
1610 }
1611
1612 return super.dispatchKeyEvent(event);
1613 }
1614
Joe Onorato88ec0992009-11-19 13:16:06 -08001615 @Override
1616 public void onBackPressed() {
1617 if (isAllAppsVisible()) {
1618 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001619 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001620 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001621 } else {
1622 closeFolder();
1623 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001624 // Some launcher layouts don't have a previous and next view
1625 if (mPreviousView != null) {
1626 dismissPreview(mPreviousView);
1627 dismissPreview(mNextView);
1628 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001629 }
1630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 private void closeFolder() {
1632 Folder folder = mWorkspace.getOpenFolder();
1633 if (folder != null) {
1634 closeFolder(folder);
1635 }
1636 }
1637
1638 void closeFolder(Folder folder) {
1639 folder.getInfo().opened = false;
1640 ViewGroup parent = (ViewGroup) folder.getParent();
1641 if (parent != null) {
1642 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001643 if (folder instanceof DropTarget) {
1644 // Live folders aren't DropTargets.
1645 mDragController.removeDropTarget((DropTarget)folder);
1646 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
1648 folder.onClose();
1649 }
1650
1651 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001652 * Re-listen when widgets are reset.
1653 */
1654 private void onAppWidgetReset() {
1655 mAppWidgetHost.startListening();
1656 }
1657
1658 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1660 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662 private void unbindDesktopItems() {
1663 for (ItemInfo item: mDesktopItems) {
1664 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
1666 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 /**
1669 * Launches the intent referred by the clicked shortcut.
1670 *
1671 * @param v The view representing the clicked shortcut.
1672 */
1673 public void onClick(View v) {
1674 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001675 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001677 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001678 int[] pos = new int[2];
1679 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001680 intent.setSourceBounds(new Rect(pos[0], pos[1],
1681 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001682 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 } else if (tag instanceof FolderInfo) {
1684 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001685 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001686 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001687 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001688 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001689 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
1692 }
1693
Michael Jurka0e260592010-06-30 17:07:39 -07001694 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001695 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001696 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001697 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001698 return false;
1699 }
1700
Michael Jurkaaf442092010-06-10 17:01:57 -07001701 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001702 * Event handler for the search button
1703 *
1704 * @param v The view that was clicked.
1705 */
1706 public void onClickSearchButton(View v) {
1707 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1708 View button = findViewById(R.id.search_button);
1709 i.setSourceBounds(
1710 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1711 startActivity(i);
1712 }
1713
1714 /**
1715 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001716 * enters home screen customization mode.
1717 *
1718 * @param v The view that was clicked.
1719 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001720 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001721 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001722 }
1723
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001724 /**
1725 * Event handler for the "grid" button that appears on the home screen, which
1726 * enters all apps mode.
1727 *
1728 * @param v The view that was clicked.
1729 */
1730 public void onClickAllAppsButton(View v) {
1731 showAllApps(true);
1732 }
1733
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001734 void startApplicationDetailsActivity(String packageName) {
1735 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1736 Uri.fromParts("package", packageName, null));
1737 startActivity(intent);
1738 }
1739
Joe Onoratof984e852010-03-25 09:47:45 -07001740 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1742 try {
1743 startActivity(intent);
1744 } catch (ActivityNotFoundException e) {
1745 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001746 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 } catch (SecurityException e) {
1748 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001749 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001751 "or use the exported attribute for this activity. "
1752 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001755
Romain Guy8e633c52010-03-04 12:51:36 -08001756 void startActivityForResultSafely(Intent intent, int requestCode) {
1757 try {
1758 startActivityForResult(intent, requestCode);
1759 } catch (ActivityNotFoundException e) {
1760 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1761 } catch (SecurityException e) {
1762 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1763 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1764 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1765 "or use the exported attribute for this activity.", e);
1766 }
1767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768
1769 private void handleFolderClick(FolderInfo folderInfo) {
1770 if (!folderInfo.opened) {
1771 // Close any open folder
1772 closeFolder();
1773 // Open the requested folder
1774 openFolder(folderInfo);
1775 } else {
1776 // Find the open folder...
1777 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1778 int folderScreen;
1779 if (openFolder != null) {
1780 folderScreen = mWorkspace.getScreenForView(openFolder);
1781 // .. and close it
1782 closeFolder(openFolder);
1783 if (folderScreen != mWorkspace.getCurrentScreen()) {
1784 // Close any folder open on the current screen
1785 closeFolder();
1786 // Pull the folder onto this screen
1787 openFolder(folderInfo);
1788 }
1789 }
1790 }
1791 }
1792
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 /**
1794 * Opens the user fodler described by the specified tag. The opening of the folder
1795 * is animated relative to the specified View. If the View is null, no animation
1796 * is played.
1797 *
1798 * @param folderInfo The FolderInfo describing the folder to open.
1799 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001800 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 Folder openFolder;
1802
1803 if (folderInfo instanceof UserFolderInfo) {
1804 openFolder = UserFolder.fromXml(this);
1805 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001806 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 } else {
1808 return;
1809 }
1810
Joe Onorato00acb122009-08-04 16:04:30 -04001811 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 openFolder.setLauncher(this);
1813
1814 openFolder.bind(folderInfo);
1815 folderInfo.opened = true;
1816
Winson Chungaafa03c2010-06-11 17:34:16 -07001817 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1818
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 openFolder.onOpen();
1820 }
1821
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001823 switch (v.getId()) {
1824 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001825 if (!isAllAppsVisible()) {
1826 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1827 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001828 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001829 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001830 return true;
1831 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001832 if (!isAllAppsVisible()) {
1833 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1834 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001835 showPreviews(v);
1836 }
1837 return true;
1838 case R.id.all_apps_button:
1839 if (!isAllAppsVisible()) {
1840 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1841 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1842 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001843 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001844 return true;
1845 }
1846
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 return false;
1849 }
1850
1851 if (!(v instanceof CellLayout)) {
1852 v = (View) v.getParent();
1853 }
1854
1855 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1856
1857 // This happens when long clicking an item with the dpad/trackball
1858 if (cellInfo == null) {
1859 return true;
1860 }
1861
1862 if (mWorkspace.allowLongPress()) {
1863 if (cellInfo.cell == null) {
1864 if (cellInfo.valid) {
1865 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001866 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001867 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1868 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 showAddDialog(cellInfo);
1870 }
1871 } else {
1872 if (!(cellInfo.cell instanceof Folder)) {
1873 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001874 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1875 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 mWorkspace.startDrag(cellInfo);
1877 }
1878 }
1879 }
1880 return true;
1881 }
1882
Romain Guye6b8e2f2009-11-10 11:56:55 -08001883 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001884 private void dismissPreview(final View v) {
1885 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001886 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001887 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1888 public void onDismiss() {
1889 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1890 int count = group.getChildCount();
1891 for (int i = 0; i < count; i++) {
1892 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1893 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001894 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1895 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1896
1897 v.setTag(R.id.workspace, null);
1898 v.setTag(R.id.icon, null);
1899 window.setOnDismissListener(null);
1900 }
1901 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001902 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001903 }
1904 v.setTag(null);
1905 }
1906
Romain Guyf8e6a802009-12-07 17:48:02 -08001907 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001908 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001909 }
1910
Romain Guya6abce82009-11-10 02:54:41 -08001911 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001912 final Resources resources = getResources();
1913 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001914
Romain Guya6abce82009-11-10 02:54:41 -08001915 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001916
Romain Guy9d31e9f2009-11-11 18:54:28 -08001917 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001918
Romain Guyf8e6a802009-12-07 17:48:02 -08001919 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001920 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001921 int extraW = (int) ((r.left + r.right) * max);
1922 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001923
1924 int aW = cell.getWidth() - extraW;
1925 float w = aW / max;
1926
1927 int width = cell.getWidth();
1928 int height = cell.getHeight();
1929 int x = cell.getLeftPadding();
1930 int y = cell.getTopPadding();
1931 width -= (x + cell.getRightPadding());
1932 height -= (y + cell.getBottomPadding());
1933
1934 float scale = w / width;
1935
1936 int count = end - start;
1937
1938 final float sWidth = width * scale;
1939 float sHeight = height * scale;
1940
Romain Guye6b8e2f2009-11-10 11:56:55 -08001941 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001942
Romain Guye6b8e2f2009-11-10 11:56:55 -08001943 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1944 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001945
1946 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001947 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001948 cell = (CellLayout) workspace.getChildAt(i);
1949
Romain Guyf8e6a802009-12-07 17:48:02 -08001950 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001951 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001952
1953 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001954 c.scale(scale, scale);
1955 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1956 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001957
Romain Guy9d31e9f2009-11-11 18:54:28 -08001958 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001959 image.setImageBitmap(bitmap);
1960 image.setTag(i);
1961 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001962 image.setOnFocusChangeListener(handler);
1963 image.setFocusable(true);
1964 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001965
1966 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001967 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1968
Winson Chungaafa03c2010-06-11 17:34:16 -07001969 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001970 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001971
1972 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001973 p.setContentView(preview);
1974 p.setWidth((int) (sWidth * count + extraW));
1975 p.setHeight((int) (sHeight + extraH));
1976 p.setAnimationStyle(R.style.AnimationPreview);
1977 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001978 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001979 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001980 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001981
Romain Guye6b8e2f2009-11-10 11:56:55 -08001982 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1983 public void onDismiss() {
1984 dismissPreview(anchor);
1985 }
1986 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001987
1988 anchor.setTag(p);
1989 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001990 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001991 }
1992
Romain Guy9d31e9f2009-11-11 18:54:28 -08001993 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001994 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001995
Romain Guye6b8e2f2009-11-10 11:56:55 -08001996 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001997 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001998 }
1999
2000 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002001 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002002 v.post(this);
2003 }
2004
2005 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002006 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002007 }
2008
2009 public void onFocusChange(View v, boolean hasFocus) {
2010 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08002011 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002012 }
Romain Guya6abce82009-11-10 02:54:41 -08002013 }
2014 }
2015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 Workspace getWorkspace() {
2017 return mWorkspace;
2018 }
2019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 @Override
2021 protected Dialog onCreateDialog(int id) {
2022 switch (id) {
2023 case DIALOG_CREATE_SHORTCUT:
2024 return new CreateShortcut().createDialog();
2025 case DIALOG_RENAME_FOLDER:
2026 return new RenameFolder().createDialog();
2027 }
2028
2029 return super.onCreateDialog(id);
2030 }
2031
2032 @Override
2033 protected void onPrepareDialog(int id, Dialog dialog) {
2034 switch (id) {
2035 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 break;
2037 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002038 if (mFolderInfo != null) {
2039 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2040 final CharSequence text = mFolderInfo.title;
2041 input.setText(text);
2042 input.setSelection(0, text.length());
2043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 break;
2045 }
2046 }
2047
2048 void showRenameDialog(FolderInfo info) {
2049 mFolderInfo = info;
2050 mWaitingForResult = true;
2051 showDialog(DIALOG_RENAME_FOLDER);
2052 }
2053
2054 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2055 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002056 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 mWaitingForResult = true;
2058 showDialog(DIALOG_CREATE_SHORTCUT);
2059 }
2060
Joe Onoratodeb98af2010-02-19 14:59:39 -08002061 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002062 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002063 Bundle bundle = new Bundle();
2064
2065 ArrayList<String> shortcutNames = new ArrayList<String>();
2066 shortcutNames.add(getString(R.string.group_applications));
2067 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2068
2069 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2070 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2071 R.drawable.ic_launcher_application));
2072 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2073
2074 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2075 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002076 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002077 pickIntent.putExtras(bundle);
2078
Joe Onoratodeb98af2010-02-19 14:59:39 -08002079 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002080 }
2081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 private class RenameFolder {
2083 private EditText mInput;
2084
2085 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2087 mInput = (EditText) layout.findViewById(R.id.folder_name);
2088
2089 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2090 builder.setIcon(0);
2091 builder.setTitle(getString(R.string.rename_folder_title));
2092 builder.setCancelable(true);
2093 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2094 public void onCancel(DialogInterface dialog) {
2095 cleanup();
2096 }
2097 });
2098 builder.setNegativeButton(getString(R.string.cancel_action),
2099 new Dialog.OnClickListener() {
2100 public void onClick(DialogInterface dialog, int which) {
2101 cleanup();
2102 }
2103 }
2104 );
2105 builder.setPositiveButton(getString(R.string.rename_action),
2106 new Dialog.OnClickListener() {
2107 public void onClick(DialogInterface dialog, int which) {
2108 changeFolderName();
2109 }
2110 }
2111 );
2112 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002113
2114 final AlertDialog dialog = builder.create();
2115 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2116 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002117 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002118 mInput.requestFocus();
2119 InputMethodManager inputManager = (InputMethodManager)
2120 getSystemService(Context.INPUT_METHOD_SERVICE);
2121 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002122 }
2123 });
2124
2125 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127
2128 private void changeFolderName() {
2129 final String name = mInput.getText().toString();
2130 if (!TextUtils.isEmpty(name)) {
2131 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002132 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 mFolderInfo.title = name;
2134 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2135
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002137 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 } else {
2140 final FolderIcon folderIcon = (FolderIcon)
2141 mWorkspace.getViewForTag(mFolderInfo);
2142 if (folderIcon != null) {
2143 folderIcon.setText(name);
2144 getWorkspace().requestLayout();
2145 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002146 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 mWorkspaceLoading = true;
2148 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 }
2150 }
2151 }
2152 cleanup();
2153 }
2154
2155 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 dismissDialog(DIALOG_RENAME_FOLDER);
2157 mWaitingForResult = false;
2158 mFolderInfo = null;
2159 }
2160 }
2161
Daniel Sandler843e8602010-06-07 14:59:01 -04002162 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2163 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002164 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002165 }
2166
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002167 // AllAppsView.Watcher
2168 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002169 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2170 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002171 mWorkspace.setVisibility(View.GONE);
2172 }
2173 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002174
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002175 private void showToolbarButton(View button) {
2176 button.setAlpha(1.0f);
2177 button.setVisibility(View.VISIBLE);
2178 button.setFocusable(true);
2179 button.setClickable(true);
2180 }
2181
2182 private void hideToolbarButton(View button) {
2183 button.setAlpha(0.0f);
2184 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2185 button.setVisibility(View.INVISIBLE);
2186 button.setFocusable(false);
2187 button.setClickable(false);
2188 }
2189
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002190 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002191 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002192 *
2193 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2194 * @param view The toolbar button to be animated
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002195 * @param seq A Sequencer that will be used to animate the transition. If null, the
2196 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002197 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002198 private void hideOrShowToolbarButton(boolean show, final View view, Sequencer seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002199 final boolean showing = show;
2200 final boolean hiding = !show;
2201
2202 final int duration = show ?
2203 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2204 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2205
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002206 if (seq != null) {
2207 Animatable anim = new PropertyAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2208 anim.addListener(new AnimatableListenerAdapter() {
2209 public void onAnimationStart(Animatable animation) {
2210 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002211 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002212 public void onAnimationEnd(Animatable animation) {
2213 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002214 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002215 });
2216 seq.play(anim);
2217 } else {
2218 if (showing) {
2219 showToolbarButton(view);
2220 } else {
2221 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002222 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002223 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002224 }
2225
2226 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227 * Show/hide the appropriate toolbar buttons for newState.
2228 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002229 *
2230 * @param newState The state that is being switched to
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002231 * @param showSeq Sequencer in which to put "show" animations, or null.
2232 * @param hideSeq Sequencer in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002233 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002234 private void hideAndShowToolbarButtons(State newState, Sequencer showSeq, Sequencer hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002235 final View searchButton = findViewById(R.id.search_button);
2236 final View allAppsButton = findViewById(R.id.all_apps_button);
2237 final View marketButton = findViewById(R.id.market_button);
2238 final View configureButton = findViewById(R.id.configure_button);
2239
2240 switch (newState) {
2241 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002242 hideOrShowToolbarButton(true, searchButton, showSeq);
2243 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2244 hideOrShowToolbarButton(true, configureButton, showSeq);
2245 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002246 break;
2247 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002248 hideOrShowToolbarButton(true, configureButton, showSeq);
2249 hideOrShowToolbarButton(true, marketButton, showSeq);
2250 hideOrShowToolbarButton(false, searchButton, hideSeq);
2251 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002252 break;
2253 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002254 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2255 hideOrShowToolbarButton(false, searchButton, hideSeq);
2256 hideOrShowToolbarButton(false, marketButton, hideSeq);
2257 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002258 break;
2259 }
2260 }
2261
2262 /**
2263 * Helper method for the cameraZoomIn/cameraZoomOut animations
2264 * @param view The view being animated
2265 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2266 * @param scaleFactor The scale factor used for the zoom
2267 */
2268 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2269 final int height = view.getHeight();
2270 view.setPivotX(view.getWidth() / 2.0f);
2271 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2272 // Assumes that the view is normally anchored to either the top or bottom of the screen
2273 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2274 if (state == State.ALL_APPS) {
2275 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2276 } else {
2277 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2278 }
2279 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002280
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002281 /**
2282 * Zoom the camera out from the workspace to reveal 'toView'.
2283 * Assumes that the view to show is anchored at either the very top or very bottom
2284 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002285 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002286 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002287 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002288 final Resources res = getResources();
2289 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2290 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002291 final boolean toAllApps = (toState == State.ALL_APPS);
2292 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002293
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002294 setPivotsForZoom(toView, toState, scale);
2295
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002296 if (toState == State.ALL_APPS) {
2297 mWorkspace.shrinkToBottom(animated);
2298 } else {
2299 mWorkspace.shrinkToTop(animated);
2300 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002301
2302 if (animated) {
2303 Animator scaleAnim = new PropertyAnimator(duration, toView,
2304 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2305 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2306 scaleAnim.setInterpolator(new DecelerateInterpolator());
2307 scaleAnim.addListener(new AnimatableListenerAdapter() {
2308 public void onAnimationStart(Animatable animation) {
2309 // Prepare the position
2310 toView.setTranslationX(0.0f);
2311 toView.setTranslationY(0.0f);
2312 toView.setVisibility(View.VISIBLE);
2313 }
2314 });
2315
2316 Sequencer toolbarHideAnim = new Sequencer();
2317 Sequencer toolbarShowAnim = new Sequencer();
2318 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2319
2320 // toView should appear right at the end of the workspace shrink animation
2321 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2322
2323 Sequencer s = new Sequencer();
2324 s.playTogether(scaleAnim, toolbarHideAnim);
2325 s.play(scaleAnim).after(startDelay);
2326
2327 // Show the new toolbar buttons just as the main animation is ending
2328 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2329 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2330 s.start();
2331 } else {
2332 toView.setTranslationX(0.0f);
2333 toView.setTranslationY(0.0f);
2334 toView.setScaleX(1.0f);
2335 toView.setScaleY(1.0f);
2336 toView.setVisibility(View.VISIBLE);
2337 hideAndShowToolbarButtons(toState, null, null);
2338 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339 }
2340
2341 /**
2342 * Zoom the camera back into the workspace, hiding 'fromView'.
2343 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002344 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2345 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002346 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002347 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002348 Resources res = getResources();
2349 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2350 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351 final View fromView =
2352 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2353
2354 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002355
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002356 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002357
2358 if (animated) {
2359 Sequencer s = new Sequencer();
2360 Animator scaleAnim = new PropertyAnimator(duration, fromView,
2361 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2362 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2363 scaleAnim.setInterpolator(new AccelerateInterpolator());
2364 s.addListener(new AnimatableListenerAdapter() {
2365 public void onAnimationEnd(Animatable animation) {
2366 fromView.setVisibility(View.GONE);
2367 fromView.setScaleX(1.0f);
2368 fromView.setScaleY(1.0f);
2369 }
2370 });
2371
2372 Sequencer toolbarHideAnim = new Sequencer();
2373 Sequencer toolbarShowAnim = new Sequencer();
2374 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2375
2376 s.playTogether(scaleAnim, toolbarHideAnim);
2377
2378 // Show the new toolbar buttons at the very end of the whole animation
2379 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2380 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2381 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2382 s.start();
2383 } else {
2384 fromView.setVisibility(View.GONE);
2385 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2386 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002387 }
2388
2389 /**
2390 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2391 * This is the transition used on xlarge screens to go between all apps and
2392 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002393 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2394 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2395 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002396 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002397 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002398 final Resources res = getResources();
2399 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002400 final int workspaceHeight = mWorkspace.getHeight();
2401
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002402 final boolean fromAllApps = (fromState == State.ALL_APPS);
2403 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2404 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002405
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002406 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2407 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2408 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2409 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002410
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002411 if (toState == State.ALL_APPS) {
2412 mWorkspace.shrinkToBottom(animated);
2413 } else {
2414 mWorkspace.shrinkToTop(animated);
2415 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002416
2417 if (animated) {
2418 Sequencer s = new Sequencer();
2419 s.addListener(new AnimatableListenerAdapter() {
2420 public void onAnimationStart(Animatable animation) {
2421 toView.setVisibility(View.VISIBLE);
2422 toView.setY(toViewStartY);
2423 }
2424 public void onAnimationEnd(Animatable animation) {
2425 fromView.setVisibility(View.GONE);
2426 }
2427 });
2428
2429 Sequencer toolbarHideAnim = new Sequencer();
2430 Sequencer toolbarShowAnim = new Sequencer();
2431 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2432
2433 s.playTogether(
2434 toolbarHideAnim,
2435 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2436 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2437
2438 // Show the new toolbar buttons just as the main animation is ending
2439 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2440 s.play(toolbarShowAnim).after(duration - fadeInTime);
2441 s.start();
2442 } else {
2443 fromView.setY(fromViewEndY);
2444 fromView.setVisibility(View.GONE);
2445 toView.setY(toViewEndY);
2446 toView.setVisibility(View.VISIBLE);
2447 hideAndShowToolbarButtons(toState, null, null);
2448 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002449 }
2450
2451 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002452 if (mAllAppsGrid.isVisible())
2453 return;
2454
Michael Jurka79212d82010-07-30 16:36:20 -07002455 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002457 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002458 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002459 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002460 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002461 } else {
2462 mAllAppsGrid.zoom(1.0f, animated);
2463 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002464
Romain Guyc16fea72010-03-12 17:17:56 -08002465 ((View) mAllAppsGrid).setFocusable(true);
2466 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002467
Joe Onorato7c312c12009-08-13 21:36:53 -07002468 // TODO: fade these two too
2469 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002470 }
2471
Joe Onoratob2061212009-11-24 16:13:54 -05002472 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002473 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002474 * - Home from workspace
2475 * - from center screen
2476 * - from other screens
2477 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002478 * - from center screen
2479 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002480 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002481 * - from center screen
2482 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002483 * - Launch app from workspace and quit
2484 * - with back
2485 * - with home
2486 * - Launch app from all apps and quit
2487 * - with back
2488 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002489 * - Go to a screen that's not the default, then all
2490 * apps, and launch and app, and go back
2491 * - with back
2492 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002493 * - On workspace, long press power and go back
2494 * - with back
2495 * - with home
2496 * - On all apps, long press power and go back
2497 * - with back
2498 * - with home
2499 * - On workspace, power off
2500 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002501 * - Launch an app and turn off the screen while in that app
2502 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002503 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002504 * - From all apps
2505 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002506 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2507 * - From all apps
2508 * - From the center workspace
2509 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002510 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002511 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002512 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002513 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002514 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002515 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002516 } else {
2517 mAllAppsGrid.zoom(0.0f, animated);
2518 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002519 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002520 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002521 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002522 }
2523
Joe Onorato7c312c12009-08-13 21:36:53 -07002524 void lockAllApps() {
2525 // TODO
2526 }
2527
2528 void unlockAllApps() {
2529 // TODO
2530 }
2531
Patrick Dubroy558654c2010-07-23 16:48:11 -07002532 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002533 return mHomeCustomizationDrawer != null &&
2534 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002535 }
2536
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002537 // Show the customization drawer (only exists in x-large configuration)
2538 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002539 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002541 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002542 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002543 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002544 }
2545
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002546 // Hide the customization drawer (only exists in x-large configuration)
2547 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002548 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002550 }
2551 }
2552
Michael Jurka213d9632010-07-28 11:29:25 -07002553 void onWorkspaceUnshrink() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002554 final boolean animated = true;
Michael Jurka213d9632010-07-28 11:29:25 -07002555 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002556 closeAllApps(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002557 }
2558 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002559 hideCustomizationDrawer(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002560 }
2561 }
2562
Joe Onorato7404ee42009-07-31 11:54:44 -07002563 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 * Displays the shortcut creation dialog and launches, if necessary, the
2565 * appropriate activity.
2566 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002567 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002568 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2569 DialogInterface.OnShowListener {
2570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2577 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002578 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 builder.setInverseBackgroundForced(true);
2581
2582 AlertDialog dialog = builder.create();
2583 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002584 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002585 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 return dialog;
2588 }
2589
2590 public void onCancel(DialogInterface dialog) {
2591 mWaitingForResult = false;
2592 cleanup();
2593 }
2594
Romain Guycbb89e42009-06-08 15:52:54 -07002595 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002596 }
2597
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002599 try {
2600 dismissDialog(DIALOG_CREATE_SHORTCUT);
2601 } catch (Exception e) {
2602 // An exception is thrown if the dialog is not visible, which is fine
2603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 }
2605
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002606 /**
2607 * Handle the action clicked in the "Add to home" dialog.
2608 */
2609 public void onClick(DialogInterface dialog, int which) {
2610 Resources res = getResources();
2611 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002612
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002613 switch (which) {
2614 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002615 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002616 break;
2617 }
Romain Guycbb89e42009-06-08 15:52:54 -07002618
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002619 case AddAdapter.ITEM_APPWIDGET: {
2620 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002621
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002622 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2623 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002624 // start the pick activity
2625 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2626 break;
2627 }
Romain Guycbb89e42009-06-08 15:52:54 -07002628
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002629 case AddAdapter.ITEM_LIVE_FOLDER: {
2630 // Insert extra item to handle inserting folder
2631 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002632
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002633 ArrayList<String> shortcutNames = new ArrayList<String>();
2634 shortcutNames.add(res.getString(R.string.group_folder));
2635 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002636
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002637 ArrayList<ShortcutIconResource> shortcutIcons =
2638 new ArrayList<ShortcutIconResource>();
2639 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2640 R.drawable.ic_launcher_folder));
2641 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2642
2643 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2644 pickIntent.putExtra(Intent.EXTRA_INTENT,
2645 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2646 pickIntent.putExtra(Intent.EXTRA_TITLE,
2647 getText(R.string.title_select_live_folder));
2648 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002649
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002650 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2651 break;
2652 }
2653
2654 case AddAdapter.ITEM_WALLPAPER: {
2655 startWallpaper();
2656 break;
2657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 }
2659 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002660
2661 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002662 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 }
2665
2666 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002667 * Receives notifications when applications are added/removed.
2668 */
2669 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2670 @Override
2671 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002672 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002673 String reason = intent.getStringExtra("reason");
2674 if (!"homekey".equals(reason)) {
2675 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002676 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002677 animate = false;
2678 }
Joe Onoratob2061212009-11-24 16:13:54 -05002679 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002681 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002682 }
2683 }
2684
2685 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002686 * Receives notifications whenever the appwidgets are reset.
2687 */
2688 private class AppWidgetResetObserver extends ContentObserver {
2689 public AppWidgetResetObserver() {
2690 super(new Handler());
2691 }
2692
2693 @Override
2694 public void onChange(boolean selfChange) {
2695 onAppWidgetReset();
2696 }
2697 }
2698
2699 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002700 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002701 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002702 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002703 if (mWorkspace != null) {
2704 return mWorkspace.getCurrentScreen();
2705 } else {
2706 return SCREEN_COUNT / 2;
2707 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002708 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002709
Joe Onorato9c1289c2009-08-17 11:03:03 -04002710 /**
2711 * Refreshes the shortcuts shown on the workspace.
2712 *
2713 * Implementation of the method from LauncherModel.Callbacks.
2714 */
2715 public void startBinding() {
2716 final Workspace workspace = mWorkspace;
2717 int count = workspace.getChildCount();
2718 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002719 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002720 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2721 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002722
Joe Onorato9c1289c2009-08-17 11:03:03 -04002723 if (DEBUG_USER_INTERFACE) {
2724 android.widget.Button finishButton = new android.widget.Button(this);
2725 finishButton.setText("Finish");
2726 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2727
2728 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2729 public void onClick(View v) {
2730 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002732 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733 }
2734 }
2735
2736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002737 * Bind the items start-end from the list.
2738 *
2739 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2742
2743 final Workspace workspace = mWorkspace;
2744
2745 for (int i=start; i<end; i++) {
2746 final ItemInfo item = shortcuts.get(i);
2747 mDesktopItems.add(item);
2748 switch (item.itemType) {
2749 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2750 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002751 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002752 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2753 false);
2754 break;
2755 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2756 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2757 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2758 (UserFolderInfo) item);
2759 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2760 false);
2761 break;
2762 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2763 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2764 R.layout.live_folder_icon, this,
2765 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2766 (LiveFolderInfo) item);
2767 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2768 false);
2769 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 }
2772
Joe Onorato9c1289c2009-08-17 11:03:03 -04002773 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 }
2775
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 /**
2777 * Implementation of the method from LauncherModel.Callbacks.
2778 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002779 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2780 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002781 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002783
2784 /**
2785 * Add the views for a widget to the workspace.
2786 *
2787 * Implementation of the method from LauncherModel.Callbacks.
2788 */
2789 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002790 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2791 if (DEBUG_WIDGETS) {
2792 Log.d(TAG, "bindAppWidget: " + item);
2793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 final Workspace workspace = mWorkspace;
2795
2796 final int appWidgetId = item.appWidgetId;
2797 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002798 if (DEBUG_WIDGETS) {
2799 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2800 }
2801
Joe Onorato9c1289c2009-08-17 11:03:03 -04002802 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2803
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2805 item.hostView.setTag(item);
2806
2807 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2808 item.cellY, item.spanX, item.spanY, false);
2809
2810 workspace.requestLayout();
2811
2812 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002813
2814 if (DEBUG_WIDGETS) {
2815 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2816 + (SystemClock.uptimeMillis()-start) + "ms");
2817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 }
2819
2820 /**
2821 * Callback saying that there aren't any more items to bind.
2822 *
2823 * Implementation of the method from LauncherModel.Callbacks.
2824 */
2825 public void finishBindingItems() {
2826 if (mSavedState != null) {
2827 if (!mWorkspace.hasFocus()) {
2828 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2829 }
2830
2831 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2832 if (userFolders != null) {
2833 for (long folderId : userFolders) {
2834 final FolderInfo info = mFolders.get(folderId);
2835 if (info != null) {
2836 openFolder(info);
2837 }
2838 }
2839 final Folder openFolder = mWorkspace.getOpenFolder();
2840 if (openFolder != null) {
2841 openFolder.requestFocus();
2842 }
2843 }
2844
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 mSavedState = null;
2846 }
2847
2848 if (mSavedInstanceState != null) {
2849 super.onRestoreInstanceState(mSavedInstanceState);
2850 mSavedInstanceState = null;
2851 }
2852
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 mWorkspaceLoading = false;
2854 }
2855
2856 /**
2857 * Add the icons for all apps.
2858 *
2859 * Implementation of the method from LauncherModel.Callbacks.
2860 */
2861 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002862 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002863 }
2864
2865 /**
2866 * A package was installed.
2867 *
2868 * Implementation of the method from LauncherModel.Callbacks.
2869 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002870 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002872 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 }
2874
2875 /**
2876 * A package was updated.
2877 *
2878 * Implementation of the method from LauncherModel.Callbacks.
2879 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002880 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002882 mWorkspace.updateShortcuts(apps);
2883 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 }
2885
2886 /**
2887 * A package was uninstalled.
2888 *
2889 * Implementation of the method from LauncherModel.Callbacks.
2890 */
Joe Onorato36115782010-06-17 13:28:48 -04002891 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002893 if (permanent) {
2894 mWorkspace.removeItems(apps);
2895 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002896 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 }
Joe Onoratobe386092009-11-17 17:32:16 -08002898
2899 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002900 * A number of packages were updated.
2901 */
2902 public void bindPackagesUpdated() {
2903 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002904 if (mCustomizePagedView != null)
2905 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002906 }
2907
2908 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002909 * Prints out out state for debugging.
2910 */
2911 public void dumpState() {
2912 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002913 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002914 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2915 Log.d(TAG, "mRestoring=" + mRestoring);
2916 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2917 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2918 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2919 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002920 mModel.dumpState();
2921 mAllAppsGrid.dumpState();
2922 Log.d(TAG, "END launcher2 dump state");
2923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924}