blob: ae77ec8b756563eb5bd00855fefb6ae2e56cbfe7 [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;
Patrick Dubroyceae05d2010-08-30 10:40:53 -070048import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040052import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080055import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070056import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080065import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070066import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
70import android.text.TextUtils;
71import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080074import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070081import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070083import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070084import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
89import android.widget.LinearLayout;
90import android.widget.PopupWindow;
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 Dubroyceae05d2010-08-30 10:40:53 -0700183 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
184
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185 /** The different states that Launcher can be in. */
186 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800191 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800193 private final BroadcastReceiver mCloseSystemDialogsReceiver
194 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800195 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private LayoutInflater mInflater;
198
Joe Onorato00acb122009-08-04 16:04:30 -0400199 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 private AppWidgetManager mAppWidgetManager;
203 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private CellLayout.CellInfo mAddItemCellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700206 private int[] mAddItemCoordinates;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private CellLayout.CellInfo mMenuAddInfo;
208 private final int[] mCellCoordinates = new int[2];
209 private FolderInfo mFolderInfo;
210
Joe Onorato7c312c12009-08-13 21:36:53 -0700211 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700213 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700214 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700215 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Romain Guy84f296c2009-11-04 15:00:44 -0800232 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
233 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Romain Guy1fbc1c82009-11-09 20:43:08 -0800235 private ImageView mPreviousView;
236 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500237
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400238 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400239 private String[] mHotseatConfig = null;
240 private Intent[] mHotseats = null;
241 private Drawable[] mHotseatIcons = null;
242 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400243
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700244 private Intent mAppMarketIntent = null;
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 @Override
247 protected void onCreate(Bundle savedInstanceState) {
248 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700249
Winson Chungaafa03c2010-06-11 17:34:16 -0700250 if (LauncherApplication.isInPlaceRotationEnabled()) {
251 // hide the status bar (temporary until we get the status bar design figured out)
252 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
253 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
254 }
255
Joe Onorato0589f0f2010-02-08 13:44:00 -0800256 LauncherApplication app = ((LauncherApplication)getApplication());
257 mModel = app.setLauncher(this);
258 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400259 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700263 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
264 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 if (PROFILE_STARTUP) {
267 android.os.Debug.startMethodTracing("/sdcard/launcher");
268 }
269
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400270 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 checkForLocaleChange();
272 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700274 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
275 if (mHomeCustomizationDrawer != null) {
276 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700277
Winson Chung80baf5a2010-08-09 16:03:15 -0700278 // share the same customization workspace across all the tabs
279 mCustomizePagedView = new CustomizePagedView(this);
280 TabContentFactory contentFactory = new TabContentFactory() {
281 public View createTabContent(String tag) {
282 return mCustomizePagedView;
283 }
284 };
285
Michael Jurka946ad472010-07-09 18:05:18 -0700286 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700287 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
288 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700289 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700290 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
291 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700292 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
294 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700295 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700296 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
297 .setIndicator(wallpapersLabel).setContent(contentFactory));
298
299 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
300 // new assets
301 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
302 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
303 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
304 TextView text = (TextView) tab.getChildAt(1);
305 text.setTextSize(20.0f);
306 text.setPadding(20, 0, 20, 0);
307 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
308 tab.setBackgroundDrawable(null);
309 }
310
311 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
312 public void onTabChanged(String tabId) {
313 // animate the changing of the tab content by fading pages in and out
314 final int duration = 150;
315 final float alpha = mCustomizePagedView.getAlpha();
316 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
317 "alpha", alpha, 0.0f);
318 alphaAnim.addListener(new AnimatableListenerAdapter() {
319 public void onAnimationEnd(Animatable animation) {
320 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
321 if (tag == WIDGETS_TAG) {
322 mCustomizePagedView.setCustomizationFilter(
323 CustomizePagedView.CustomizationType.WidgetCustomization);
324 } else if (tag == FOLDERS_TAG) {
325 mCustomizePagedView.setCustomizationFilter(
326 CustomizePagedView.CustomizationType.FolderCustomization);
327 } else if (tag == SHORTCUTS_TAG) {
328 mCustomizePagedView.setCustomizationFilter(
329 CustomizePagedView.CustomizationType.ShortcutCustomization);
330 } else if (tag == WALLPAPERS_TAG) {
331 mCustomizePagedView.setCustomizationFilter(
332 CustomizePagedView.CustomizationType.WallpaperCustomization);
333 }
334
335 final float alpha = mCustomizePagedView.getAlpha();
336 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
337 "alpha", alpha, 1.0f);
338 alphaAnim.start();
339 }
340 });
341 alphaAnim.start();
342 }
343 });
Michael Jurka946ad472010-07-09 18:05:18 -0700344
345 mHomeCustomizationDrawer.setCurrentTab(0);
346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 setupViews();
348
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800349 registerContentObservers();
350
Joe Onorato7c312c12009-08-13 21:36:53 -0700351 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 mSavedState = savedInstanceState;
354 restoreState(mSavedState);
355
356 if (PROFILE_STARTUP) {
357 android.os.Debug.stopMethodTracing();
358 }
359
360 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 }
363
364 // For handling default keys
365 mDefaultKeySsb = new SpannableStringBuilder();
366 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800367
368 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
369 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 }
Romain Guycbb89e42009-06-08 15:52:54 -0700371
Winson Chungaafa03c2010-06-11 17:34:16 -0700372 @Override
373 public void onConfigurationChanged(Configuration newConfig) {
374 // TODO Auto-generated method stub
375 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700376 }
377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700379 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
380 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 final Configuration configuration = getResources().getConfiguration();
383
Romain Guy98d01652009-06-30 16:21:04 -0700384 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 final String locale = configuration.locale.toString();
386
Romain Guy98d01652009-06-30 16:21:04 -0700387 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final int mcc = configuration.mcc;
389
Romain Guy98d01652009-06-30 16:21:04 -0700390 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 final int mnc = configuration.mnc;
392
Romain Guy84f296c2009-11-04 15:00:44 -0800393 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394
Romain Guy84f296c2009-11-04 15:00:44 -0800395 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700396 localeConfiguration.locale = locale;
397 localeConfiguration.mcc = mcc;
398 localeConfiguration.mnc = mnc;
399
400 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800401 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400402
403 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700404 }
405 }
406
407 private static class LocaleConfiguration {
408 public String locale;
409 public int mcc = -1;
410 public int mnc = -1;
411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
Romain Guy98d01652009-06-30 16:21:04 -0700413 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
414 DataInputStream in = null;
415 try {
416 in = new DataInputStream(context.openFileInput(PREFERENCES));
417 configuration.locale = in.readUTF();
418 configuration.mcc = in.readInt();
419 configuration.mnc = in.readInt();
420 } catch (FileNotFoundException e) {
421 // Ignore
422 } catch (IOException e) {
423 // Ignore
424 } finally {
425 if (in != null) {
426 try {
427 in.close();
428 } catch (IOException e) {
429 // Ignore
430 }
431 }
432 }
433 }
434
435 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
436 DataOutputStream out = null;
437 try {
438 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
439 out.writeUTF(configuration.locale);
440 out.writeInt(configuration.mcc);
441 out.writeInt(configuration.mnc);
442 out.flush();
443 } catch (FileNotFoundException e) {
444 // Ignore
445 } catch (IOException e) {
446 //noinspection ResultOfMethodCallIgnored
447 context.getFileStreamPath(PREFERENCES).delete();
448 } finally {
449 if (out != null) {
450 try {
451 out.close();
452 } catch (IOException e) {
453 // Ignore
454 }
455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 }
457 }
458
459 static int getScreen() {
460 synchronized (sLock) {
461 return sScreen;
462 }
463 }
464
465 static void setScreen(int screen) {
466 synchronized (sLock) {
467 sScreen = screen;
468 }
469 }
470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700472 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473
474 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700475 // TODO: Put back when we decide about scrolling the wallpaper
476 // boolean isPortrait = display.getWidth() < display.getHeight();
477 // final int width = isPortrait ? display.getWidth() : display.getHeight();
478 // final int height = isPortrait ? display.getHeight() : display.getWidth();
479 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
480 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400483 // Note: This doesn't do all the client-id magic that BrowserProvider does
484 // in Browser. (http://b/2425179)
485 private Uri getDefaultBrowserUri() {
486 String url = getString(R.string.default_browser_url);
487 if (url.indexOf("{CID}") != -1) {
488 url = url.replace("{CID}", "android-google");
489 }
490 return Uri.parse(url);
491 }
492
493 // Load the Intent templates from arrays.xml to populate the hotseats. For
494 // each Intent, if it resolves to a single app, use that as the launch
495 // intent & use that app's label as the contentDescription. Otherwise,
496 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400497 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400498 if (mHotseatConfig == null) {
499 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
500 if (mHotseatConfig.length > 0) {
501 mHotseats = new Intent[mHotseatConfig.length];
502 mHotseatLabels = new CharSequence[mHotseatConfig.length];
503 mHotseatIcons = new Drawable[mHotseatConfig.length];
504 } else {
505 mHotseats = null;
506 mHotseatIcons = null;
507 mHotseatLabels = null;
508 }
509
510 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
511 for (int i=0; i<mHotseatConfig.length; i++) {
512 // load icon for this slot; currently unrelated to the actual activity
513 try {
514 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
515 } catch (ArrayIndexOutOfBoundsException ex) {
516 Log.w(TAG, "Missing hotseat_icons array item #" + i);
517 mHotseatIcons[i] = null;
518 }
519 }
520 hotseatIconDrawables.recycle();
521 }
522
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400523 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400524 for (int i=0; i<mHotseatConfig.length; i++) {
525 Intent intent = null;
526 if (mHotseatConfig[i].equals("*BROWSER*")) {
527 // magic value meaning "launch user's default web browser"
528 // replace it with a generic web request so we can see if there is indeed a default
529 String defaultUri = getString(R.string.default_browser_url);
530 intent = new Intent(
531 Intent.ACTION_VIEW,
532 ((defaultUri != null)
533 ? Uri.parse(defaultUri)
534 : getDefaultBrowserUri())
535 ).addCategory(Intent.CATEGORY_BROWSABLE);
536 // note: if the user launches this without a default set, she
537 // will always be taken to the default URL above; this is
538 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700539 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 // URL is unavoidably sent to the chosen app.
541 } else {
542 try {
543 intent = Intent.parseUri(mHotseatConfig[i], 0);
544 } catch (java.net.URISyntaxException ex) {
545 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
546 // bogus; leave intent=null
547 }
548 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700549
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400550 if (intent == null) {
551 mHotseats[i] = null;
552 mHotseatLabels[i] = getText(R.string.activity_not_found);
553 continue;
554 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400555
556 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700557 Log.d(TAG, "loadHotseats: hotseat " + i
558 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400560 + "]");
561 }
562
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
564 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700565 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400566 Log.d(TAG, "Best match for intent: " + bestMatch);
567 Log.d(TAG, "All matches: ");
568 for (ResolveInfo ri : allMatches) {
569 Log.d(TAG, " --> " + ri);
570 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400571 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 // did this resolve to a single app, or the resolver?
573 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700574 // can't find any activity to handle this. let's leave the
575 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 // to launch.
577 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400579 // set accessibility text to "Not installed"
580 mHotseatLabels[i] = getText(R.string.activity_not_found);
581 } else {
582 boolean found = false;
583 for (ResolveInfo ri : allMatches) {
584 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
585 && bestMatch.activityInfo.applicationInfo.packageName
586 .equals(ri.activityInfo.applicationInfo.packageName)) {
587 found = true;
588 break;
589 }
590 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700591
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400592 if (!found) {
593 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
594 // the bestMatch is probably the ResolveActivity, meaning the
595 // user has not yet selected a default
596 // so: we'll keep the original intent for now
597 mHotseats[i] = intent;
598
599 // set the accessibility text to "Select shortcut"
600 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
601 } else {
602 // we have an app!
603 // now reconstruct the intent to launch it through the front
604 // door
605 ComponentName com = new ComponentName(
606 bestMatch.activityInfo.applicationInfo.packageName,
607 bestMatch.activityInfo.name);
608 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
609
610 // load the app label for accessibility
611 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
612 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400613 }
614
615 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700616 Log.d(TAG, "loadHotseats: hotseat " + i
617 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400618 + ((mHotseats[i] == null)
619 ? "null"
620 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400621 + "] label=[" + mHotseatLabels[i]
622 + "]"
623 );
624 }
625 }
626 }
627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 @Override
629 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700630 mWaitingForResult = false;
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 // The pattern used here is that a user PICKs a specific application,
633 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
636 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700637
Romain Guy94dabf12009-07-21 10:55:43 -0700638 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 switch (requestCode) {
640 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 break;
643 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800644 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 break;
646 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400647 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 break;
649 case REQUEST_PICK_LIVE_FOLDER:
650 addLiveFolder(data);
651 break;
652 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700655 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700656 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700658 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700659 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700660 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700662 case REQUEST_PICK_WALLPAPER:
663 // We just wanted the activity result here so we can clear mWaitingForResult
664 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Romain Guy18042c82009-11-06 11:44:55 -0800666 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
667 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
668 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700669 // Clean up the appWidgetId if we canceled
670 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
671 if (appWidgetId != -1) {
672 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
676
677 @Override
678 protected void onResume() {
679 super.onResume();
680
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800681 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 mWorkspaceLoading = true;
685 mModel.startLoader(this, true);
686 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700688 // When we resume Launcher, a different Activity might be responsible for the app
689 // market intent, so refresh the icon
690 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
692
693 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 protected void onPause() {
695 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700696 // Some launcher layouts don't have a previous and next view
697 if (mPreviousView != null) {
698 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700699 }
700 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700701 dismissPreview(mNextView);
702 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800703 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 }
Romain Guycbb89e42009-06-08 15:52:54 -0700705
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700706 @Override
707 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 // Flag the loader to stop early before switching
709 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800710 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800711 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700712 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800714 // We can't hide the IME if it was forced open. So don't bother
715 /*
716 @Override
717 public void onWindowFocusChanged(boolean hasFocus) {
718 super.onWindowFocusChanged(hasFocus);
719
720 if (hasFocus) {
721 final InputMethodManager inputManager = (InputMethodManager)
722 getSystemService(Context.INPUT_METHOD_SERVICE);
723 WindowManager.LayoutParams lp = getWindow().getAttributes();
724 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
725 android.os.Handler()) {
726 protected void onReceiveResult(int resultCode, Bundle resultData) {
727 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
728 }
729 });
730 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
731 }
732 }
733 */
734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 private boolean acceptFilter() {
736 final InputMethodManager inputManager = (InputMethodManager)
737 getSystemService(Context.INPUT_METHOD_SERVICE);
738 return !inputManager.isFullscreenMode();
739 }
740
741 @Override
742 public boolean onKeyDown(int keyCode, KeyEvent event) {
743 boolean handled = super.onKeyDown(keyCode, event);
744 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
745 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
746 keyCode, event);
747 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700748 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700749 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700750 // showSearchDialog()
751 // If there are multiple keystrokes before the search dialog takes focus,
752 // onSearchRequested() will be called for every keystroke,
753 // but it is idempotent, so it's fine.
754 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
756 }
757
Joe Onorato8a9625e2010-01-28 15:55:35 -0800758 // Eat the long press event so the keyboard doesn't come up.
759 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
760 return true;
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 return handled;
764 }
765
Karl Rosaen138a0412009-04-23 19:00:21 -0700766 private String getTypedText() {
767 return mDefaultKeySsb.toString();
768 }
769
770 private void clearTypedText() {
771 mDefaultKeySsb.clear();
772 mDefaultKeySsb.clearSpans();
773 Selection.setSelection(mDefaultKeySsb, 0);
774 }
775
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 /**
777 * Restores the previous state, if it exists.
778 *
779 * @param savedState The previous state.
780 */
781 private void restoreState(Bundle savedState) {
782 if (savedState == null) {
783 return;
784 }
785
Joe Onorato3a8820b2009-11-10 15:06:42 -0800786 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
787 if (allApps) {
788 showAllApps(false);
789 }
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
792 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700793 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795
796 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
797 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700798 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 mAddItemCellInfo = new CellLayout.CellInfo();
800 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
801 addItemCellInfo.valid = true;
802 addItemCellInfo.screen = addScreen;
803 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
804 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
805 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
806 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700807 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
809 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
810 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
811 mRestoring = true;
812 }
813
814 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
815 if (renameFolder) {
816 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400817 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 mRestoring = true;
819 }
820 }
821
822 /**
823 * Finds all the views we need and configure them properly.
824 */
825 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700826 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400827
Romain Guyb1b69f52009-08-10 15:10:15 -0700828 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400829 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830
Joe Onorato7c312c12009-08-13 21:36:53 -0700831 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700832 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700833 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800834 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700835 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700836 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700837
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700838 if (LauncherApplication.isScreenXLarge()) {
839 // They need to be INVISIBLE initially so that they will be measured in the layout.
840 // Otherwise the animations are messed up when we show them for the first time.
841 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
842 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
846 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500847 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700848
Joe Onorato7c312c12009-08-13 21:36:53 -0700849 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
850 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700852 View handleView = findViewById(R.id.all_apps_button);
853 if (handleView != null && handleView instanceof HandleView) {
854 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700855 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700856 mHandleView.setLauncher(this);
857 mHandleView.setOnClickListener(this);
858 mHandleView.setOnLongClickListener(this);
859 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800860
Winson Chung80baf5a2010-08-09 16:03:15 -0700861 if (mCustomizePagedView != null) {
862 mCustomizePagedView.setLauncher(this);
863 mCustomizePagedView.setDragController(dragController);
864 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700865 } else {
866 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
867 hotseatLeft.setContentDescription(mHotseatLabels[0]);
868 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
869 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
870 hotseatRight.setContentDescription(mHotseatLabels[1]);
871 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400872
Michael Jurkaaf442092010-06-10 17:01:57 -0700873 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
874 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800875
Michael Jurkaaf442092010-06-10 17:01:57 -0700876 Drawable previous = mPreviousView.getDrawable();
877 Drawable next = mNextView.getDrawable();
878 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879
Michael Jurkaaf442092010-06-10 17:01:57 -0700880 mPreviousView.setHapticFeedbackEnabled(false);
881 mPreviousView.setOnLongClickListener(this);
882 mNextView.setHapticFeedbackEnabled(false);
883 mNextView.setOnLongClickListener(this);
884 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400887 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889
890 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400891 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700892 int deleteZoneHandleId;
893 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700894 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700895 } else {
896 deleteZoneHandleId = R.id.all_apps_button_cluster;
897 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700898 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700899 dragController.addDragListener(deleteZone);
900
901 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
902 if (infoButton != null) {
903 infoButton.setLauncher(this);
904 infoButton.setHandle(findViewById(R.id.configure_button));
905 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
906 dragController.addDragListener(infoButton);
907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908
Joe Onorato00acb122009-08-04 16:04:30 -0400909 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400910 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800911 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700912
Joe Onorato00acb122009-08-04 16:04:30 -0400913 // The order here is bottom to top.
914 dragController.addDropTarget(workspace);
915 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700916 if (infoButton != null) {
917 dragController.addDropTarget(infoButton);
918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920
Romain Guy8a73c512009-11-09 19:19:59 -0800921 @SuppressWarnings({"UnusedDeclaration"})
922 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800923 if (!isAllAppsVisible()) {
924 mWorkspace.scrollLeft();
925 }
Romain Guy8a73c512009-11-09 19:19:59 -0800926 }
927
928 @SuppressWarnings({"UnusedDeclaration"})
929 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800930 if (!isAllAppsVisible()) {
931 mWorkspace.scrollRight();
932 }
Romain Guy8a73c512009-11-09 19:19:59 -0800933 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400934
935 @SuppressWarnings({"UnusedDeclaration"})
936 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400937 if (isAllAppsVisible()) return;
938
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400939 int index = -1;
940 if (v.getId() == R.id.hotseat_left) {
941 index = 0;
942 } else if (v.getId() == R.id.hotseat_right) {
943 index = 1;
944 }
945
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400946 // reload these every tap; you never know when they might change
947 loadHotseats();
948 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
949 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400950 startActivitySafely(
951 mHotseats[index],
952 "hotseat"
953 );
954 }
955 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700956
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 /**
958 * Creates a view representing a shortcut.
959 *
960 * @param info The data structure describing the shortcut.
961 *
962 * @return A View inflated from R.layout.application.
963 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800964 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700966 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 /**
970 * Creates a view representing a shortcut inflated from the specified resource.
971 *
972 * @param layoutResId The id of the XML layout used to create the shortcut.
973 * @param parent The group the shortcut belongs to.
974 * @param info The data structure describing the shortcut.
975 *
976 * @return A View inflated from layoutResId.
977 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
980
Joe Onorato0589f0f2010-02-08 13:44:00 -0800981 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
982 new FastBitmapDrawable(info.getIcon(mIconCache)),
983 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 favorite.setText(info.title);
985 favorite.setTag(info);
986 favorite.setOnClickListener(this);
987
988 return favorite;
989 }
990
991 /**
992 * Add an application shortcut to the workspace.
993 *
994 * @param data The intent describing the application.
995 * @param cellInfo The position on screen where to create the shortcut.
996 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400997 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -0700998 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 if (!findSingleSlot(cellInfo)) return;
1000
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1002 data, context);
1003
Romain Guy73b979d2009-06-09 12:57:21 -07001004 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001005 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001007 info.container = ItemInfo.NO_ID;
1008 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1009 } else {
1010 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
1012 }
Romain Guycbb89e42009-06-08 15:52:54 -07001013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 /**
1015 * Add a shortcut to the workspace.
1016 *
1017 * @param data The intent describing the shortcut.
1018 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001021 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001023
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
1026 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1029 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 }
1031 }
1032
Romain Guycbb89e42009-06-08 15:52:54 -07001033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001037 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001038 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001040 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001041 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001042
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001043 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001045 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001048 // We have saved the position to which the widget was dragged-- this really only matters
1049 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001051
1052 // For now, we don't save the coordinate where we dropped the icon because we're not
1053 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1054 // a widget on the home screen in case we want to add it in the future
1055 final int[] xyTouch = null;
1056 //final int[] xyTouch = mAddItemCoordinates;
1057 boolean findNearestVacantAreaFailed = false;
1058 if (xyTouch != null) {
1059 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1060 int[] result = screen.findNearestVacantArea(
1061 mAddItemCoordinates[0], mAddItemCoordinates[1],
1062 spans[0], spans[1], cellInfo, xy);
1063 findNearestVacantAreaFailed = (result == null);
1064 }
1065
1066 if (findNearestVacantAreaFailed ||
1067 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001068 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1069 return;
1070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 // Build Launcher-specific widget info and save to database
1073 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 launcherInfo.spanX = spans[0];
1075 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 LauncherModel.addItemToDatabase(this, launcherInfo,
1078 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001079 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
1081 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001085 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Michael Jurkaa63c4522010-08-19 13:52:27 -07001090 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
1093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1096 mDesktopItems.remove(launcherInfo);
1097 launcherInfo.hostView = null;
1098 }
1099
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 public LauncherAppWidgetHost getAppWidgetHost() {
1101 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001104 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001105 getWindow().closeAllPanels();
1106
1107 try {
1108 dismissDialog(DIALOG_CREATE_SHORTCUT);
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 }
1113
1114 try {
1115 dismissDialog(DIALOG_RENAME_FOLDER);
1116 // Unlock the workspace if the dialog was showing
1117 } catch (Exception e) {
1118 // An exception is thrown if the dialog is not visible, which is fine
1119 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001120
1121 // Whatever we were doing is hereby canceled.
1122 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001123 }
1124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 @Override
1126 protected void onNewIntent(Intent intent) {
1127 super.onNewIntent(intent);
1128
1129 // Close the menu
1130 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001131 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001132 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001133
Joe Onorato14f122b2009-11-19 14:06:36 -08001134 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1135 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1136 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001137 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1138
Michael Jurka4cb37242010-08-09 21:05:32 -07001139 // in all these cases, only animate if we're already on home
1140 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001141 if (alreadyOnHome && !mWorkspace.isSmall() &&
1142 !allAppsVisible && !customizationDrawerVisible) {
1143 mWorkspace.shrinkToMiddle();
1144 } else {
1145 mWorkspace.unshrink(alreadyOnHome);
1146 }
Michael Jurka0142d492010-08-25 17:46:15 -07001147 } else if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001148 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001149 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001150 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001151 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001152 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001153
Joe Onorato3a8820b2009-11-10 15:06:42 -08001154 final View v = getWindow().peekDecorView();
1155 if (v != null && v.getWindowToken() != null) {
1156 InputMethodManager imm = (InputMethodManager)getSystemService(
1157 INPUT_METHOD_SERVICE);
1158 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160 }
1161 }
1162
1163 @Override
1164 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1165 // Do not call super here
1166 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001167
1168 if (mHomeCustomizationDrawer != null) {
1169 String cur = savedInstanceState.getString("currentTab");
1170 if (cur != null) {
1171 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1172 }
1173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175
1176 @Override
1177 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001178 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179
1180 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1181 if (folders.size() > 0) {
1182 final int count = folders.size();
1183 long[] ids = new long[count];
1184 for (int i = 0; i < count; i++) {
1185 final FolderInfo info = folders.get(i).getInfo();
1186 ids[i] = info.id;
1187 }
1188 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1189 } else {
1190 super.onSaveInstanceState(outState);
1191 }
1192
Joe Onoratofb0ca672009-09-14 17:55:46 -04001193 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001194 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197
1198 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1199 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1200 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1201
1202 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1203 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1204 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1205 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1206 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1207 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1208 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1209 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001210 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212
1213 if (mFolderInfo != null && mWaitingForResult) {
1214 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1215 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1216 }
Michael Jurka946ad472010-07-09 18:05:18 -07001217
1218 if (mHomeCustomizationDrawer != null) {
1219 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1220 if (currentTabTag != null) {
1221 outState.putString("currentTab", currentTabTag);
1222 }
1223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
1225
1226 @Override
1227 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001231 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001233 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235
1236 TextKeyListener.getInstance().release();
1237
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
Joe Onorato9c1289c2009-08-17 11:03:03 -04001240 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001241
1242 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001243
Patrick Dubroyab962b72010-07-26 12:10:10 -07001244 // Some launcher layouts don't have a previous and next view
1245 if (mPreviousView != null) {
1246 dismissPreview(mPreviousView);
1247 }
1248 if (mNextView != null) {
1249 dismissPreview(mNextView);
1250 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001251
1252 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
1255 @Override
1256 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001257 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 super.startActivityForResult(intent, requestCode);
1259 }
1260
1261 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001262 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001264
Joe Onorato7bb17492009-09-24 17:51:01 -07001265 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001266
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 if (initialQuery == null) {
1268 // Use any text typed in the launcher as the initial query
1269 initialQuery = getTypedText();
1270 clearTypedText();
1271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 if (appSearchData == null) {
1273 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001274 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
Romain Guycbb89e42009-06-08 15:52:54 -07001276
Karl Rosaen138a0412009-04-23 19:00:21 -07001277 final SearchManager searchManager =
1278 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001279 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001280 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
1282
1283 @Override
1284 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 if (isWorkspaceLocked()) {
1286 return false;
1287 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288
1289 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1292 .setIcon(android.R.drawable.ic_menu_add)
1293 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001294 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 .setIcon(android.R.drawable.ic_menu_gallery)
1296 .setAlphabeticShortcut('W');
1297 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1298 .setIcon(android.R.drawable.ic_search_category_default)
1299 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1300 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1301 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1302 .setAlphabeticShortcut('N');
1303
1304 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001305 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1306 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307
1308 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1309 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1310 .setIntent(settings);
1311
1312 return true;
1313 }
1314
1315 @Override
1316 public boolean onPrepareOptionsMenu(Menu menu) {
1317 super.onPrepareOptionsMenu(menu);
1318
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001319 // If all apps is animating, don't show the menu, because we don't know
1320 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001321 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001322 return false;
1323 }
1324
1325 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001326 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001327 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1328 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1329
1330 // Disable add if the workspace is full.
1331 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001332 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001333 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
1336 return true;
1337 }
1338
Michael Jurka0e260592010-06-30 17:07:39 -07001339 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1340 // using the settings menu to add an item, something similar happens in showAddDialog
1341 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001342 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001343 mAddItemCellInfo = mMenuAddInfo;
1344 }
1345
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 @Override
1347 public boolean onOptionsItemSelected(MenuItem item) {
1348 switch (item.getItemId()) {
1349 case MENU_ADD:
1350 addItems();
1351 return true;
1352 case MENU_WALLPAPER_SETTINGS:
1353 startWallpaper();
1354 return true;
1355 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001356 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 return true;
1358 case MENU_NOTIFICATIONS:
1359 showNotifications();
1360 return true;
1361 }
1362
1363 return super.onOptionsItemSelected(item);
1364 }
Romain Guycbb89e42009-06-08 15:52:54 -07001365
Karl Rosaen138a0412009-04-23 19:00:21 -07001366 /**
1367 * Indicates that we want global search for this activity by setting the globalSearch
1368 * argument for {@link #startSearch} to true.
1369 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001371 @Override
1372 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001373 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001374 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001375 }
1376
Joe Onorato9c1289c2009-08-17 11:03:03 -04001377 public boolean isWorkspaceLocked() {
1378 return mWorkspaceLoading || mWaitingForResult;
1379 }
1380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001382 if (LauncherApplication.isScreenXLarge()) {
1383 // Animate the widget chooser up from the bottom of the screen
1384 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001385 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001386 }
1387 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001388 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001389 showAddDialog(mMenuAddInfo);
1390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392
Michael Jurkaa63c4522010-08-19 13:52:27 -07001393 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1394 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001395 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001396
1397 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1398 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001399 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1400 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1401 addAppWidgetImpl(appWidgetId);
1402 }
1403
1404 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001405 // TODO: catch bad widget exception when sent
1406 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001407 // TODO: Is this log message meaningful?
1408 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1409 addAppWidgetImpl(appWidgetId);
1410 }
1411
1412 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001413 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414
Bjorn Bringert7984c942009-12-09 15:38:25 +00001415 if (appWidget.configure != null) {
1416 // Launch over to configure widget, if needed
1417 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1418 intent.setComponent(appWidget.configure);
1419 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1420
Romain Guy8e633c52010-03-04 12:51:36 -08001421 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001423 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001424 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426 }
Romain Guycbb89e42009-06-08 15:52:54 -07001427
Joe Onoratodeb98af2010-02-19 14:59:39 -08001428 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001429 // Handle case where user selected "Applications"
1430 String applicationName = getResources().getString(R.string.group_applications);
1431 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001432
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001433 if (applicationName != null && applicationName.equals(shortcutName)) {
1434 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1435 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001436
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001437 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1438 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001439 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001440 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001441 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001442 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444
1445 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001446 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001447 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001448 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001449
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001450 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001451 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001452 } else {
1453 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456
Joe Onorato9c1289c2009-08-17 11:03:03 -04001457 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 UserFolderInfo folderInfo = new UserFolderInfo();
1459 folderInfo.title = getText(R.string.folder_name);
1460
1461 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001462 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 if (!findSingleSlot(cellInfo)) return;
1464
1465 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 LauncherModel.addItemToDatabase(this, folderInfo,
1467 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001468 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001469 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470
1471 // Create the view
1472 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001473 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
Romain Guycbb89e42009-06-08 15:52:54 -07001477
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 void removeFolder(FolderInfo folder) {
1479 mFolders.remove(folder.id);
1480 }
1481
1482 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001483 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 if (!findSingleSlot(cellInfo)) return;
1485
1486 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1487
1488 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001490 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1492 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494 }
1495
1496 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1497 CellLayout.CellInfo cellInfo, boolean notify) {
1498
1499 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1500 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1501
1502 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 Intent.ShortcutIconResource iconResource = null;
1504
1505 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1506 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1507 try {
1508 iconResource = (Intent.ShortcutIconResource) extra;
1509 final PackageManager packageManager = context.getPackageManager();
1510 Resources resources = packageManager.getResourcesForApplication(
1511 iconResource.packageName);
1512 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1513 icon = resources.getDrawable(id);
1514 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001515 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517 }
1518
1519 if (icon == null) {
1520 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1521 }
1522
1523 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001524 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 info.title = name;
1526 info.iconResource = iconResource;
1527 info.uri = data.getData();
1528 info.baseIntent = baseIntent;
1529 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1530 LiveFolders.DISPLAY_MODE_GRID);
1531
1532 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1533 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 return info;
1537 }
1538
1539 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1540 final int[] xy = new int[2];
1541 if (findSlot(cellInfo, xy, 1, 1)) {
1542 cellInfo.cellX = xy[0];
1543 cellInfo.cellY = xy[1];
1544 return true;
1545 }
1546 return false;
1547 }
1548
1549 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1550 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001551 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1552 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1554 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1555 return false;
1556 }
1557 }
1558 return true;
1559 }
1560
1561 private void showNotifications() {
1562 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1563 if (statusBar != null) {
1564 statusBar.expand();
1565 }
1566 }
1567
1568 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001569 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001571 Intent chooser = Intent.createChooser(pickWallpaper,
1572 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001573 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1574 // Removed in Eclair MR1
1575// WallpaperManager wm = (WallpaperManager)
1576// getSystemService(Context.WALLPAPER_SERVICE);
1577// WallpaperInfo wi = wm.getWallpaperInfo();
1578// if (wi != null && wi.getSettingsActivity() != null) {
1579// LabeledIntent li = new LabeledIntent(getPackageName(),
1580// R.string.configure_wallpaper, 0);
1581// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1582// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1583// }
Mike Clerona0618e42009-10-22 13:55:21 -07001584 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001587 /**
1588 * Registers various content observers. The current implementation registers
1589 * only a favorites observer to keep track of the favorites applications.
1590 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001591 private void registerContentObservers() {
1592 ContentResolver resolver = getContentResolver();
1593 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1594 true, mWidgetObserver);
1595 }
1596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 @Override
1598 public boolean dispatchKeyEvent(KeyEvent event) {
1599 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1600 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001602 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001603 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001604 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001605 dumpState();
1606 return true;
1607 }
1608 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001609 }
1610 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1611 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001612 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 return true;
1614 }
1615 }
1616
1617 return super.dispatchKeyEvent(event);
1618 }
1619
Joe Onorato88ec0992009-11-19 13:16:06 -08001620 @Override
1621 public void onBackPressed() {
1622 if (isAllAppsVisible()) {
1623 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001624 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001625 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001626 } else {
1627 closeFolder();
1628 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001629 // Some launcher layouts don't have a previous and next view
1630 if (mPreviousView != null) {
1631 dismissPreview(mPreviousView);
1632 dismissPreview(mNextView);
1633 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001634 }
1635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 private void closeFolder() {
1637 Folder folder = mWorkspace.getOpenFolder();
1638 if (folder != null) {
1639 closeFolder(folder);
1640 }
1641 }
1642
1643 void closeFolder(Folder folder) {
1644 folder.getInfo().opened = false;
1645 ViewGroup parent = (ViewGroup) folder.getParent();
1646 if (parent != null) {
1647 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001648 if (folder instanceof DropTarget) {
1649 // Live folders aren't DropTargets.
1650 mDragController.removeDropTarget((DropTarget)folder);
1651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
1653 folder.onClose();
1654 }
1655
1656 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001657 * Re-listen when widgets are reset.
1658 */
1659 private void onAppWidgetReset() {
1660 mAppWidgetHost.startListening();
1661 }
1662
1663 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1665 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001667 private void unbindDesktopItems() {
1668 for (ItemInfo item: mDesktopItems) {
1669 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
1671 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 /**
1674 * Launches the intent referred by the clicked shortcut.
1675 *
1676 * @param v The view representing the clicked shortcut.
1677 */
1678 public void onClick(View v) {
1679 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001680 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001682 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001683 int[] pos = new int[2];
1684 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001685 intent.setSourceBounds(new Rect(pos[0], pos[1],
1686 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001687 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 } else if (tag instanceof FolderInfo) {
1689 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001690 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001691 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001692 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001693 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001694 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
1697 }
1698
Michael Jurka0e260592010-06-30 17:07:39 -07001699 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001700 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001701 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001702 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001703 return false;
1704 }
1705
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001707 * Event handler for the search button
1708 *
1709 * @param v The view that was clicked.
1710 */
1711 public void onClickSearchButton(View v) {
1712 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1713 View button = findViewById(R.id.search_button);
1714 i.setSourceBounds(
1715 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1716 startActivity(i);
1717 }
1718
1719 /**
1720 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 * enters home screen customization mode.
1722 *
1723 * @param v The view that was clicked.
1724 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001725 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001726 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 }
1728
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001729 /**
1730 * Event handler for the "grid" button that appears on the home screen, which
1731 * enters all apps mode.
1732 *
1733 * @param v The view that was clicked.
1734 */
1735 public void onClickAllAppsButton(View v) {
1736 showAllApps(true);
1737 }
1738
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001739 public void onClickAppMarketButton(View v) {
1740 if (mAppMarketIntent != null) {
1741 startActivitySafely(mAppMarketIntent, "app market");
1742 }
1743 }
1744
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001745 void startApplicationDetailsActivity(String packageName) {
1746 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1747 Uri.fromParts("package", packageName, null));
1748 startActivity(intent);
1749 }
1750
Joe Onoratof984e852010-03-25 09:47:45 -07001751 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1753 try {
1754 startActivity(intent);
1755 } catch (ActivityNotFoundException e) {
1756 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001757 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 } catch (SecurityException e) {
1759 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001760 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001762 "or use the exported attribute for this activity. "
1763 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
1765 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001766
Romain Guy8e633c52010-03-04 12:51:36 -08001767 void startActivityForResultSafely(Intent intent, int requestCode) {
1768 try {
1769 startActivityForResult(intent, requestCode);
1770 } catch (ActivityNotFoundException e) {
1771 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1772 } catch (SecurityException e) {
1773 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1774 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1775 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1776 "or use the exported attribute for this activity.", e);
1777 }
1778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779
1780 private void handleFolderClick(FolderInfo folderInfo) {
1781 if (!folderInfo.opened) {
1782 // Close any open folder
1783 closeFolder();
1784 // Open the requested folder
1785 openFolder(folderInfo);
1786 } else {
1787 // Find the open folder...
1788 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1789 int folderScreen;
1790 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001791 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 // .. and close it
1793 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001794 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 // Close any folder open on the current screen
1796 closeFolder();
1797 // Pull the folder onto this screen
1798 openFolder(folderInfo);
1799 }
1800 }
1801 }
1802 }
1803
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 /**
1805 * Opens the user fodler described by the specified tag. The opening of the folder
1806 * is animated relative to the specified View. If the View is null, no animation
1807 * is played.
1808 *
1809 * @param folderInfo The FolderInfo describing the folder to open.
1810 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001811 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 Folder openFolder;
1813
1814 if (folderInfo instanceof UserFolderInfo) {
1815 openFolder = UserFolder.fromXml(this);
1816 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001817 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 } else {
1819 return;
1820 }
1821
Joe Onorato00acb122009-08-04 16:04:30 -04001822 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 openFolder.setLauncher(this);
1824
1825 openFolder.bind(folderInfo);
1826 folderInfo.opened = true;
1827
Winson Chungaafa03c2010-06-11 17:34:16 -07001828 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 openFolder.onOpen();
1831 }
1832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001834 switch (v.getId()) {
1835 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001836 if (!isAllAppsVisible()) {
1837 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1838 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001839 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001840 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001841 return true;
1842 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001843 if (!isAllAppsVisible()) {
1844 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1845 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001846 showPreviews(v);
1847 }
1848 return true;
1849 case R.id.all_apps_button:
1850 if (!isAllAppsVisible()) {
1851 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1852 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1853 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001854 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001855 return true;
1856 }
1857
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 return false;
1860 }
1861
1862 if (!(v instanceof CellLayout)) {
1863 v = (View) v.getParent();
1864 }
1865
1866 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1867
1868 // This happens when long clicking an item with the dpad/trackball
1869 if (cellInfo == null) {
1870 return true;
1871 }
1872
1873 if (mWorkspace.allowLongPress()) {
1874 if (cellInfo.cell == null) {
1875 if (cellInfo.valid) {
1876 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001877 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001878 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1879 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 showAddDialog(cellInfo);
1881 }
1882 } else {
1883 if (!(cellInfo.cell instanceof Folder)) {
1884 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001885 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1886 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 mWorkspace.startDrag(cellInfo);
1888 }
1889 }
1890 }
1891 return true;
1892 }
1893
Romain Guye6b8e2f2009-11-10 11:56:55 -08001894 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001895 private void dismissPreview(final View v) {
1896 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001897 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001898 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1899 public void onDismiss() {
1900 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1901 int count = group.getChildCount();
1902 for (int i = 0; i < count; i++) {
1903 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1904 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001905 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1906 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1907
1908 v.setTag(R.id.workspace, null);
1909 v.setTag(R.id.icon, null);
1910 window.setOnDismissListener(null);
1911 }
1912 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001913 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001914 }
1915 v.setTag(null);
1916 }
1917
Romain Guyf8e6a802009-12-07 17:48:02 -08001918 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001919 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001920 }
1921
Romain Guya6abce82009-11-10 02:54:41 -08001922 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001923 final Resources resources = getResources();
1924 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001925
Romain Guya6abce82009-11-10 02:54:41 -08001926 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001927
Romain Guy9d31e9f2009-11-11 18:54:28 -08001928 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001929
Romain Guyf8e6a802009-12-07 17:48:02 -08001930 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001931 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001932 int extraW = (int) ((r.left + r.right) * max);
1933 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001934
1935 int aW = cell.getWidth() - extraW;
1936 float w = aW / max;
1937
1938 int width = cell.getWidth();
1939 int height = cell.getHeight();
1940 int x = cell.getLeftPadding();
1941 int y = cell.getTopPadding();
1942 width -= (x + cell.getRightPadding());
1943 height -= (y + cell.getBottomPadding());
1944
1945 float scale = w / width;
1946
1947 int count = end - start;
1948
1949 final float sWidth = width * scale;
1950 float sHeight = height * scale;
1951
Romain Guye6b8e2f2009-11-10 11:56:55 -08001952 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001953
Romain Guye6b8e2f2009-11-10 11:56:55 -08001954 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1955 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001956
1957 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001958 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001959 cell = (CellLayout) workspace.getChildAt(i);
1960
Romain Guyf8e6a802009-12-07 17:48:02 -08001961 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001962 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001963
1964 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001965 c.scale(scale, scale);
1966 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1967 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001968
Romain Guy9d31e9f2009-11-11 18:54:28 -08001969 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001970 image.setImageBitmap(bitmap);
1971 image.setTag(i);
1972 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001973 image.setOnFocusChangeListener(handler);
1974 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07001975 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001976
1977 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001978 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1979
Winson Chungaafa03c2010-06-11 17:34:16 -07001980 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001981 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001982
1983 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001984 p.setContentView(preview);
1985 p.setWidth((int) (sWidth * count + extraW));
1986 p.setHeight((int) (sHeight + extraH));
1987 p.setAnimationStyle(R.style.AnimationPreview);
1988 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001989 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001990 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001991 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001992
Romain Guye6b8e2f2009-11-10 11:56:55 -08001993 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1994 public void onDismiss() {
1995 dismissPreview(anchor);
1996 }
1997 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001998
1999 anchor.setTag(p);
2000 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002001 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002002 }
2003
Romain Guy9d31e9f2009-11-11 18:54:28 -08002004 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002005 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002006
Romain Guye6b8e2f2009-11-10 11:56:55 -08002007 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002008 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002009 }
2010
2011 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002012 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002013 v.post(this);
2014 }
2015
2016 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002017 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002018 }
2019
2020 public void onFocusChange(View v, boolean hasFocus) {
2021 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002022 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002023 }
Romain Guya6abce82009-11-10 02:54:41 -08002024 }
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 Workspace getWorkspace() {
2028 return mWorkspace;
2029 }
2030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 @Override
2032 protected Dialog onCreateDialog(int id) {
2033 switch (id) {
2034 case DIALOG_CREATE_SHORTCUT:
2035 return new CreateShortcut().createDialog();
2036 case DIALOG_RENAME_FOLDER:
2037 return new RenameFolder().createDialog();
2038 }
2039
2040 return super.onCreateDialog(id);
2041 }
2042
2043 @Override
2044 protected void onPrepareDialog(int id, Dialog dialog) {
2045 switch (id) {
2046 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 break;
2048 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002049 if (mFolderInfo != null) {
2050 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2051 final CharSequence text = mFolderInfo.title;
2052 input.setText(text);
2053 input.setSelection(0, text.length());
2054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 break;
2056 }
2057 }
2058
2059 void showRenameDialog(FolderInfo info) {
2060 mFolderInfo = info;
2061 mWaitingForResult = true;
2062 showDialog(DIALOG_RENAME_FOLDER);
2063 }
2064
2065 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2066 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002067 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 mWaitingForResult = true;
2069 showDialog(DIALOG_CREATE_SHORTCUT);
2070 }
2071
Joe Onoratodeb98af2010-02-19 14:59:39 -08002072 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002073 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002074 Bundle bundle = new Bundle();
2075
2076 ArrayList<String> shortcutNames = new ArrayList<String>();
2077 shortcutNames.add(getString(R.string.group_applications));
2078 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2079
2080 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2081 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2082 R.drawable.ic_launcher_application));
2083 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2084
2085 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2086 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002087 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002088 pickIntent.putExtras(bundle);
2089
Joe Onoratodeb98af2010-02-19 14:59:39 -08002090 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002091 }
2092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 private class RenameFolder {
2094 private EditText mInput;
2095
2096 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2098 mInput = (EditText) layout.findViewById(R.id.folder_name);
2099
2100 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2101 builder.setIcon(0);
2102 builder.setTitle(getString(R.string.rename_folder_title));
2103 builder.setCancelable(true);
2104 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2105 public void onCancel(DialogInterface dialog) {
2106 cleanup();
2107 }
2108 });
2109 builder.setNegativeButton(getString(R.string.cancel_action),
2110 new Dialog.OnClickListener() {
2111 public void onClick(DialogInterface dialog, int which) {
2112 cleanup();
2113 }
2114 }
2115 );
2116 builder.setPositiveButton(getString(R.string.rename_action),
2117 new Dialog.OnClickListener() {
2118 public void onClick(DialogInterface dialog, int which) {
2119 changeFolderName();
2120 }
2121 }
2122 );
2123 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002124
2125 final AlertDialog dialog = builder.create();
2126 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2127 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002128 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002129 mInput.requestFocus();
2130 InputMethodManager inputManager = (InputMethodManager)
2131 getSystemService(Context.INPUT_METHOD_SERVICE);
2132 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002133 }
2134 });
2135
2136 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 }
2138
2139 private void changeFolderName() {
2140 final String name = mInput.getText().toString();
2141 if (!TextUtils.isEmpty(name)) {
2142 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 mFolderInfo.title = name;
2145 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2146
Joe Onorato9c1289c2009-08-17 11:03:03 -04002147 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002148 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 } else {
2151 final FolderIcon folderIcon = (FolderIcon)
2152 mWorkspace.getViewForTag(mFolderInfo);
2153 if (folderIcon != null) {
2154 folderIcon.setText(name);
2155 getWorkspace().requestLayout();
2156 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002157 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 mWorkspaceLoading = true;
2159 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 }
2161 }
2162 }
2163 cleanup();
2164 }
2165
2166 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 dismissDialog(DIALOG_RENAME_FOLDER);
2168 mWaitingForResult = false;
2169 mFolderInfo = null;
2170 }
2171 }
2172
Daniel Sandler843e8602010-06-07 14:59:01 -04002173 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2174 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002175 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002176 }
2177
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002178 // AllAppsView.Watcher
2179 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002180 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2181 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002182 mWorkspace.setVisibility(View.GONE);
2183 }
2184 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002185
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002186 private void showToolbarButton(View button) {
2187 button.setAlpha(1.0f);
2188 button.setVisibility(View.VISIBLE);
2189 button.setFocusable(true);
2190 button.setClickable(true);
2191 }
2192
2193 private void hideToolbarButton(View button) {
2194 button.setAlpha(0.0f);
2195 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2196 button.setVisibility(View.INVISIBLE);
2197 button.setFocusable(false);
2198 button.setClickable(false);
2199 }
2200
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002201 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002202 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002203 *
2204 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2205 * @param view The toolbar button to be animated
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002206 * @param seq A Sequencer that will be used to animate the transition. If null, the
2207 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002208 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002209 private void hideOrShowToolbarButton(boolean show, final View view, Sequencer seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002210 final boolean showing = show;
2211 final boolean hiding = !show;
2212
2213 final int duration = show ?
2214 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2215 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2216
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002217 if (seq != null) {
2218 Animatable anim = new PropertyAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2219 anim.addListener(new AnimatableListenerAdapter() {
2220 public void onAnimationStart(Animatable animation) {
2221 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002222 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002223 public void onAnimationEnd(Animatable animation) {
2224 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002225 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002226 });
2227 seq.play(anim);
2228 } else {
2229 if (showing) {
2230 showToolbarButton(view);
2231 } else {
2232 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002233 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002234 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002235 }
2236
2237 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002238 * Show/hide the appropriate toolbar buttons for newState.
2239 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002240 *
2241 * @param newState The state that is being switched to
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002242 * @param showSeq Sequencer in which to put "show" animations, or null.
2243 * @param hideSeq Sequencer in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002244 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002245 private void hideAndShowToolbarButtons(State newState, Sequencer showSeq, Sequencer hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002246 final View searchButton = findViewById(R.id.search_button);
2247 final View allAppsButton = findViewById(R.id.all_apps_button);
2248 final View marketButton = findViewById(R.id.market_button);
2249 final View configureButton = findViewById(R.id.configure_button);
2250
2251 switch (newState) {
2252 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002253 hideOrShowToolbarButton(true, searchButton, showSeq);
2254 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2255 hideOrShowToolbarButton(true, configureButton, showSeq);
2256 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002257 break;
2258 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259 hideOrShowToolbarButton(true, configureButton, showSeq);
2260 hideOrShowToolbarButton(true, marketButton, showSeq);
2261 hideOrShowToolbarButton(false, searchButton, hideSeq);
2262 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002263 break;
2264 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002265 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2266 hideOrShowToolbarButton(false, searchButton, hideSeq);
2267 hideOrShowToolbarButton(false, marketButton, hideSeq);
2268 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002269 break;
2270 }
2271 }
2272
2273 /**
2274 * Helper method for the cameraZoomIn/cameraZoomOut animations
2275 * @param view The view being animated
2276 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2277 * @param scaleFactor The scale factor used for the zoom
2278 */
2279 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2280 final int height = view.getHeight();
2281 view.setPivotX(view.getWidth() / 2.0f);
2282 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2283 // Assumes that the view is normally anchored to either the top or bottom of the screen
2284 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2285 if (state == State.ALL_APPS) {
2286 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2287 } else {
2288 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2289 }
2290 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002291
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 /**
2293 * Zoom the camera out from the workspace to reveal 'toView'.
2294 * Assumes that the view to show is anchored at either the very top or very bottom
2295 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002296 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002297 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002298 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002299 final Resources res = getResources();
2300 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2301 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002302 final boolean toAllApps = (toState == State.ALL_APPS);
2303 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002304
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002305 setPivotsForZoom(toView, toState, scale);
2306
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002307 if (toState == State.ALL_APPS) {
2308 mWorkspace.shrinkToBottom(animated);
2309 } else {
2310 mWorkspace.shrinkToTop(animated);
2311 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002312
2313 if (animated) {
2314 Animator scaleAnim = new PropertyAnimator(duration, toView,
2315 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2316 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2317 scaleAnim.setInterpolator(new DecelerateInterpolator());
2318 scaleAnim.addListener(new AnimatableListenerAdapter() {
2319 public void onAnimationStart(Animatable animation) {
2320 // Prepare the position
2321 toView.setTranslationX(0.0f);
2322 toView.setTranslationY(0.0f);
2323 toView.setVisibility(View.VISIBLE);
2324 }
2325 });
2326
2327 Sequencer toolbarHideAnim = new Sequencer();
2328 Sequencer toolbarShowAnim = new Sequencer();
2329 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2330
2331 // toView should appear right at the end of the workspace shrink animation
2332 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2333
2334 Sequencer s = new Sequencer();
2335 s.playTogether(scaleAnim, toolbarHideAnim);
2336 s.play(scaleAnim).after(startDelay);
2337
2338 // Show the new toolbar buttons just as the main animation is ending
2339 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2340 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2341 s.start();
2342 } else {
2343 toView.setTranslationX(0.0f);
2344 toView.setTranslationY(0.0f);
2345 toView.setScaleX(1.0f);
2346 toView.setScaleY(1.0f);
2347 toView.setVisibility(View.VISIBLE);
2348 hideAndShowToolbarButtons(toState, null, null);
2349 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002350 }
2351
2352 /**
2353 * Zoom the camera back into the workspace, hiding 'fromView'.
2354 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002355 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2356 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002357 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002358 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002359 Resources res = getResources();
2360 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2361 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002362 final View fromView =
2363 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2364
2365 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002366
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002367 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002368
2369 if (animated) {
2370 Sequencer s = new Sequencer();
2371 Animator scaleAnim = new PropertyAnimator(duration, fromView,
2372 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2373 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2374 scaleAnim.setInterpolator(new AccelerateInterpolator());
2375 s.addListener(new AnimatableListenerAdapter() {
2376 public void onAnimationEnd(Animatable animation) {
2377 fromView.setVisibility(View.GONE);
2378 fromView.setScaleX(1.0f);
2379 fromView.setScaleY(1.0f);
2380 }
2381 });
2382
2383 Sequencer toolbarHideAnim = new Sequencer();
2384 Sequencer toolbarShowAnim = new Sequencer();
2385 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2386
2387 s.playTogether(scaleAnim, toolbarHideAnim);
2388
2389 // Show the new toolbar buttons at the very end of the whole animation
2390 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2391 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2392 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2393 s.start();
2394 } else {
2395 fromView.setVisibility(View.GONE);
2396 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2397 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002398 }
2399
2400 /**
2401 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2402 * This is the transition used on xlarge screens to go between all apps and
2403 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002404 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2405 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2406 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002407 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002408 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002409 final Resources res = getResources();
2410 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002411 final int workspaceHeight = mWorkspace.getHeight();
2412
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002413 final boolean fromAllApps = (fromState == State.ALL_APPS);
2414 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2415 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002416
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002417 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2418 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2419 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2420 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002421
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002422 if (toState == State.ALL_APPS) {
2423 mWorkspace.shrinkToBottom(animated);
2424 } else {
2425 mWorkspace.shrinkToTop(animated);
2426 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002427
2428 if (animated) {
2429 Sequencer s = new Sequencer();
2430 s.addListener(new AnimatableListenerAdapter() {
2431 public void onAnimationStart(Animatable animation) {
2432 toView.setVisibility(View.VISIBLE);
2433 toView.setY(toViewStartY);
2434 }
2435 public void onAnimationEnd(Animatable animation) {
2436 fromView.setVisibility(View.GONE);
2437 }
2438 });
2439
2440 Sequencer toolbarHideAnim = new Sequencer();
2441 Sequencer toolbarShowAnim = new Sequencer();
2442 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2443
2444 s.playTogether(
2445 toolbarHideAnim,
2446 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2447 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2448
2449 // Show the new toolbar buttons just as the main animation is ending
2450 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2451 s.play(toolbarShowAnim).after(duration - fadeInTime);
2452 s.start();
2453 } else {
2454 fromView.setY(fromViewEndY);
2455 fromView.setVisibility(View.GONE);
2456 toView.setY(toViewEndY);
2457 toView.setVisibility(View.VISIBLE);
2458 hideAndShowToolbarButtons(toState, null, null);
2459 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002460 }
2461
2462 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002463 if (mAllAppsGrid.isVisible())
2464 return;
2465
Michael Jurka79212d82010-07-30 16:36:20 -07002466 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002467 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002468 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002469 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002470 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002471 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002472 } else {
2473 mAllAppsGrid.zoom(1.0f, animated);
2474 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002475
Romain Guyc16fea72010-03-12 17:17:56 -08002476 ((View) mAllAppsGrid).setFocusable(true);
2477 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002478
Joe Onorato7c312c12009-08-13 21:36:53 -07002479 // TODO: fade these two too
2480 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002481 }
2482
Joe Onoratob2061212009-11-24 16:13:54 -05002483 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002484 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002485 * - Home from workspace
2486 * - from center screen
2487 * - from other screens
2488 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002489 * - from center screen
2490 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002491 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002492 * - from center screen
2493 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002494 * - Launch app from workspace and quit
2495 * - with back
2496 * - with home
2497 * - Launch app from all apps and quit
2498 * - with back
2499 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002500 * - Go to a screen that's not the default, then all
2501 * apps, and launch and app, and go back
2502 * - with back
2503 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002504 * - On workspace, long press power and go back
2505 * - with back
2506 * - with home
2507 * - On all apps, long press power and go back
2508 * - with back
2509 * - with home
2510 * - On workspace, power off
2511 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002512 * - Launch an app and turn off the screen while in that app
2513 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002514 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002515 * - From all apps
2516 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002517 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2518 * - From all apps
2519 * - From the center workspace
2520 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002521 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002522 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002523 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002524 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002525 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002526 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002527 } else {
2528 mAllAppsGrid.zoom(0.0f, animated);
2529 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002530 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002531 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002532 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002533 }
2534
Joe Onorato7c312c12009-08-13 21:36:53 -07002535 void lockAllApps() {
2536 // TODO
2537 }
2538
2539 void unlockAllApps() {
2540 // TODO
2541 }
2542
Patrick Dubroy558654c2010-07-23 16:48:11 -07002543 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002544 return mHomeCustomizationDrawer != null &&
2545 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002546 }
2547
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002548 // Show the customization drawer (only exists in x-large configuration)
2549 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002550 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002552 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002553 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002554 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002555 }
2556
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 // Hide the customization drawer (only exists in x-large configuration)
2558 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002559 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002560 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002561 }
2562 }
2563
Michael Jurka213d9632010-07-28 11:29:25 -07002564 void onWorkspaceUnshrink() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 final boolean animated = true;
Michael Jurka213d9632010-07-28 11:29:25 -07002566 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002567 closeAllApps(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002568 }
2569 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002570 hideCustomizationDrawer(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002571 }
2572 }
2573
Joe Onorato7404ee42009-07-31 11:54:44 -07002574 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002575 * Sets the app market icon (shown when all apps is visible on x-large screens)
2576 */
2577 private void updateAppMarketIcon() {
2578 if (LauncherApplication.isScreenXLarge()) {
2579 // Find the app market activity by resolving an intent.
2580 // (If multiple app markets are installed, it will return the ResolverActivity.)
2581 PackageManager packageManager = getPackageManager();
2582 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2583 ComponentName activityName = intent.resolveActivity(getPackageManager());
2584 if (activityName != null) {
2585 mAppMarketIntent = intent;
2586 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2587 Drawable toolbarIcon = null;
2588 try {
2589 // Look for the toolbar icon specified in the activity meta-data
2590 Bundle metaData = packageManager.getActivityInfo(
2591 activityName, PackageManager.GET_META_DATA).metaData;
2592 if (metaData != null) {
2593 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2594 if (iconResId != 0) {
2595 Resources res = packageManager.getResourcesForActivity(activityName);
2596 toolbarIcon = res.getDrawable(iconResId);
2597 }
2598 }
2599 } catch (NameNotFoundException e) {
2600 // Do nothing
2601 }
2602 // If we were unable to find the icon via the meta-data, use a generic one
2603 if (toolbarIcon == null) {
2604 marketButton.setImageResource(R.drawable.app_market_generic);
2605 } else {
2606 marketButton.setImageDrawable(toolbarIcon);
2607 }
2608 } else {
2609 mAppMarketIntent = null;
2610 }
2611 }
2612 }
2613
2614 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 * Displays the shortcut creation dialog and launches, if necessary, the
2616 * appropriate activity.
2617 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002618 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002619 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2620 DialogInterface.OnShowListener {
2621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2628 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002629 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 builder.setInverseBackgroundForced(true);
2632
2633 AlertDialog dialog = builder.create();
2634 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002635 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002636 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 return dialog;
2639 }
2640
2641 public void onCancel(DialogInterface dialog) {
2642 mWaitingForResult = false;
2643 cleanup();
2644 }
2645
Romain Guycbb89e42009-06-08 15:52:54 -07002646 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002647 }
2648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002650 try {
2651 dismissDialog(DIALOG_CREATE_SHORTCUT);
2652 } catch (Exception e) {
2653 // An exception is thrown if the dialog is not visible, which is fine
2654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 }
2656
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002657 /**
2658 * Handle the action clicked in the "Add to home" dialog.
2659 */
2660 public void onClick(DialogInterface dialog, int which) {
2661 Resources res = getResources();
2662 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002663
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002664 switch (which) {
2665 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002666 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002667 break;
2668 }
Romain Guycbb89e42009-06-08 15:52:54 -07002669
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002670 case AddAdapter.ITEM_APPWIDGET: {
2671 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002672
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002673 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2674 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002675 // start the pick activity
2676 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2677 break;
2678 }
Romain Guycbb89e42009-06-08 15:52:54 -07002679
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002680 case AddAdapter.ITEM_LIVE_FOLDER: {
2681 // Insert extra item to handle inserting folder
2682 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002683
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002684 ArrayList<String> shortcutNames = new ArrayList<String>();
2685 shortcutNames.add(res.getString(R.string.group_folder));
2686 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002687
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002688 ArrayList<ShortcutIconResource> shortcutIcons =
2689 new ArrayList<ShortcutIconResource>();
2690 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2691 R.drawable.ic_launcher_folder));
2692 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2693
2694 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2695 pickIntent.putExtra(Intent.EXTRA_INTENT,
2696 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2697 pickIntent.putExtra(Intent.EXTRA_TITLE,
2698 getText(R.string.title_select_live_folder));
2699 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002700
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002701 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2702 break;
2703 }
2704
2705 case AddAdapter.ITEM_WALLPAPER: {
2706 startWallpaper();
2707 break;
2708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 }
2710 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002711
2712 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002713 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715 }
2716
2717 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002718 * Receives notifications when applications are added/removed.
2719 */
2720 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2721 @Override
2722 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002723 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002724 String reason = intent.getStringExtra("reason");
2725 if (!"homekey".equals(reason)) {
2726 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002727 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002728 animate = false;
2729 }
Joe Onoratob2061212009-11-24 16:13:54 -05002730 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002732 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002733 }
2734 }
2735
2736 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002737 * Receives notifications whenever the appwidgets are reset.
2738 */
2739 private class AppWidgetResetObserver extends ContentObserver {
2740 public AppWidgetResetObserver() {
2741 super(new Handler());
2742 }
2743
2744 @Override
2745 public void onChange(boolean selfChange) {
2746 onAppWidgetReset();
2747 }
2748 }
2749
2750 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002751 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002754 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002755 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002756 } else {
2757 return SCREEN_COUNT / 2;
2758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002759 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002760
Joe Onorato9c1289c2009-08-17 11:03:03 -04002761 /**
2762 * Refreshes the shortcuts shown on the workspace.
2763 *
2764 * Implementation of the method from LauncherModel.Callbacks.
2765 */
2766 public void startBinding() {
2767 final Workspace workspace = mWorkspace;
2768 int count = workspace.getChildCount();
2769 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002770 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002771 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2772 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002773
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 if (DEBUG_USER_INTERFACE) {
2775 android.widget.Button finishButton = new android.widget.Button(this);
2776 finishButton.setText("Finish");
2777 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2778
2779 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2780 public void onClick(View v) {
2781 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002783 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 }
2785 }
2786
2787 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002788 * Bind the items start-end from the list.
2789 *
2790 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002792 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2793
2794 final Workspace workspace = mWorkspace;
2795
2796 for (int i=start; i<end; i++) {
2797 final ItemInfo item = shortcuts.get(i);
2798 mDesktopItems.add(item);
2799 switch (item.itemType) {
2800 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2801 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002802 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002803 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2804 false);
2805 break;
2806 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2807 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002808 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002809 (UserFolderInfo) item);
2810 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2811 false);
2812 break;
2813 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2814 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2815 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002816 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 (LiveFolderInfo) item);
2818 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2819 false);
2820 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822 }
2823
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 }
2826
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 /**
2828 * Implementation of the method from LauncherModel.Callbacks.
2829 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002830 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2831 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834
2835 /**
2836 * Add the views for a widget to the workspace.
2837 *
2838 * Implementation of the method from LauncherModel.Callbacks.
2839 */
2840 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002841 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2842 if (DEBUG_WIDGETS) {
2843 Log.d(TAG, "bindAppWidget: " + item);
2844 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 final Workspace workspace = mWorkspace;
2846
2847 final int appWidgetId = item.appWidgetId;
2848 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002849 if (DEBUG_WIDGETS) {
2850 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2851 }
2852
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2854
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2856 item.hostView.setTag(item);
2857
2858 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2859 item.cellY, item.spanX, item.spanY, false);
2860
2861 workspace.requestLayout();
2862
2863 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002864
2865 if (DEBUG_WIDGETS) {
2866 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2867 + (SystemClock.uptimeMillis()-start) + "ms");
2868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 }
2870
2871 /**
2872 * Callback saying that there aren't any more items to bind.
2873 *
2874 * Implementation of the method from LauncherModel.Callbacks.
2875 */
2876 public void finishBindingItems() {
2877 if (mSavedState != null) {
2878 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002879 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 }
2881
2882 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2883 if (userFolders != null) {
2884 for (long folderId : userFolders) {
2885 final FolderInfo info = mFolders.get(folderId);
2886 if (info != null) {
2887 openFolder(info);
2888 }
2889 }
2890 final Folder openFolder = mWorkspace.getOpenFolder();
2891 if (openFolder != null) {
2892 openFolder.requestFocus();
2893 }
2894 }
2895
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 mSavedState = null;
2897 }
2898
2899 if (mSavedInstanceState != null) {
2900 super.onRestoreInstanceState(mSavedInstanceState);
2901 mSavedInstanceState = null;
2902 }
2903
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 mWorkspaceLoading = false;
2905 }
2906
2907 /**
2908 * Add the icons for all apps.
2909 *
2910 * Implementation of the method from LauncherModel.Callbacks.
2911 */
2912 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002913 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002914 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 }
2916
2917 /**
2918 * A package was installed.
2919 *
2920 * Implementation of the method from LauncherModel.Callbacks.
2921 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002922 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002924 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002925 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002926 }
2927
2928 /**
2929 * A package was updated.
2930 *
2931 * Implementation of the method from LauncherModel.Callbacks.
2932 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002933 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002934 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002935 mWorkspace.updateShortcuts(apps);
2936 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002937 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 }
2939
2940 /**
2941 * A package was uninstalled.
2942 *
2943 * Implementation of the method from LauncherModel.Callbacks.
2944 */
Joe Onorato36115782010-06-17 13:28:48 -04002945 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002947 if (permanent) {
2948 mWorkspace.removeItems(apps);
2949 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002950 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002951 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002952 }
Joe Onoratobe386092009-11-17 17:32:16 -08002953
2954 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002955 * A number of packages were updated.
2956 */
2957 public void bindPackagesUpdated() {
2958 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002959 if (mCustomizePagedView != null)
2960 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002961 }
2962
2963 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002964 * Prints out out state for debugging.
2965 */
2966 public void dumpState() {
2967 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002968 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002969 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2970 Log.d(TAG, "mRestoring=" + mRestoring);
2971 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2972 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2973 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2974 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002975 mModel.dumpState();
2976 mAllAppsGrid.dumpState();
2977 Log.d(TAG, "END launcher2 dump state");
2978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979}