blob: c5450f34ed454d03dcc1c005936514c52d2f2cac [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
Chet Haaseb1254a62010-09-07 13:35:00 -070020import android.animation.Animator;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ValueAnimator;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023import com.android.common.Search;
24import com.android.launcher.R;
25
Chet Haaseb1254a62010-09-07 13:35:00 -070026import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.AnimatorSet;
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;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080066import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070067import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
71import android.text.TextUtils;
72import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070082import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070084import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070085import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070086import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070092import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070093import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070094import android.widget.TabHost.OnTabChangeListener;
95import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070096import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
100import java.io.DataInputStream;
101import java.io.DataOutputStream;
102import java.io.FileNotFoundException;
103import java.io.IOException;
104import java.util.ArrayList;
105import java.util.HashMap;
106import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108/**
109 * Default launcher application.
110 */
Michael Jurka946ad472010-07-09 18:05:18 -0700111public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700112 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
113 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800114 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400118 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int WALLPAPER_SCREENS_SPAN = 2;
122
123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
127 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
128 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
129 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700130 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 private static final int REQUEST_CREATE_SHORTCUT = 1;
133 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
137 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
142
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800143 static final int SCREEN_COUNT = 5;
144 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Joe Onorato7c312c12009-08-13 21:36:53 -0700146 static final int DIALOG_CREATE_SHORTCUT = 1;
147 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
153 // Type: boolean
154 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
171 // Type: int[]
172 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
173 // Type: boolean
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
175 // Type: long
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
177
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 // tags for the customization tabs
179 private static final String WIDGETS_TAG = "widgets";
180 private static final String FOLDERS_TAG = "folders";
181 private static final String SHORTCUTS_TAG = "shortcuts";
182 private static final String WALLPAPERS_TAG = "wallpapers";
183
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700184 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
185
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186 /** The different states that Launcher can be in. */
187 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700207 private int[] mAddItemCoordinates;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private CellLayout.CellInfo mMenuAddInfo;
209 private final int[] mCellCoordinates = new int[2];
210 private FolderInfo mFolderInfo;
211
Joe Onorato7c312c12009-08-13 21:36:53 -0700212 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700215 private TabHost mHomeCustomizationDrawer;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700216
217 private PagedView mAllAppsPagedView = null;
218 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private Bundle mSavedState;
221
222 private SpannableStringBuilder mDefaultKeySsb = null;
223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private boolean mWorkspaceLoading = true;
225
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800226 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private boolean mRestoring;
228 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
230 private Bundle mSavedInstanceState;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800233 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700238 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700239
Romain Guy1fbc1c82009-11-09 20:43:08 -0800240 private ImageView mPreviousView;
241 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500242
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400243 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400244 private String[] mHotseatConfig = null;
245 private Intent[] mHotseats = null;
246 private Drawable[] mHotseatIcons = null;
247 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400248
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700249 private Intent mAppMarketIntent = null;
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 @Override
252 protected void onCreate(Bundle savedInstanceState) {
253 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700254
Winson Chungaafa03c2010-06-11 17:34:16 -0700255 if (LauncherApplication.isInPlaceRotationEnabled()) {
256 // hide the status bar (temporary until we get the status bar design figured out)
257 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
258 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
259 }
260
Joe Onorato0589f0f2010-02-08 13:44:00 -0800261 LauncherApplication app = ((LauncherApplication)getApplication());
262 mModel = app.setLauncher(this);
263 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400264 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700266
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700267 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700268 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
269 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 if (PROFILE_STARTUP) {
272 android.os.Debug.startMethodTracing("/sdcard/launcher");
273 }
274
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400275 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 checkForLocaleChange();
277 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700279 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
280 if (mHomeCustomizationDrawer != null) {
281 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700282
Winson Chung80baf5a2010-08-09 16:03:15 -0700283 // share the same customization workspace across all the tabs
284 mCustomizePagedView = new CustomizePagedView(this);
285 TabContentFactory contentFactory = new TabContentFactory() {
286 public View createTabContent(String tag) {
287 return mCustomizePagedView;
288 }
289 };
290
Michael Jurka946ad472010-07-09 18:05:18 -0700291 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700292 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
293 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700294 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
296 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700297 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700298 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
299 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700300 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700301 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
302 .setIndicator(wallpapersLabel).setContent(contentFactory));
303
304 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
305 // new assets
306 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
307 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
308 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
309 TextView text = (TextView) tab.getChildAt(1);
310 text.setTextSize(20.0f);
311 text.setPadding(20, 0, 20, 0);
312 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
313 tab.setBackgroundDrawable(null);
314 }
315
316 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
317 public void onTabChanged(String tabId) {
318 // animate the changing of the tab content by fading pages in and out
319 final int duration = 150;
320 final float alpha = mCustomizePagedView.getAlpha();
Chet Haaseb1254a62010-09-07 13:35:00 -0700321 ValueAnimator alphaAnim = new ObjectAnimator(duration, mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 "alpha", alpha, 0.0f);
Chet Haaseb1254a62010-09-07 13:35:00 -0700323 alphaAnim.addListener(new AnimatorListenerAdapter() {
324 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
326 if (tag == WIDGETS_TAG) {
327 mCustomizePagedView.setCustomizationFilter(
328 CustomizePagedView.CustomizationType.WidgetCustomization);
329 } else if (tag == FOLDERS_TAG) {
330 mCustomizePagedView.setCustomizationFilter(
331 CustomizePagedView.CustomizationType.FolderCustomization);
332 } else if (tag == SHORTCUTS_TAG) {
333 mCustomizePagedView.setCustomizationFilter(
334 CustomizePagedView.CustomizationType.ShortcutCustomization);
335 } else if (tag == WALLPAPERS_TAG) {
336 mCustomizePagedView.setCustomizationFilter(
337 CustomizePagedView.CustomizationType.WallpaperCustomization);
338 }
339
340 final float alpha = mCustomizePagedView.getAlpha();
Chet Haaseb1254a62010-09-07 13:35:00 -0700341 ValueAnimator alphaAnim = new ObjectAnimator(duration, mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700342 "alpha", alpha, 1.0f);
343 alphaAnim.start();
344 }
345 });
346 alphaAnim.start();
347 }
348 });
Michael Jurka946ad472010-07-09 18:05:18 -0700349
350 mHomeCustomizationDrawer.setCurrentTab(0);
351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 setupViews();
353
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800354 registerContentObservers();
355
Joe Onorato7c312c12009-08-13 21:36:53 -0700356 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 mSavedState = savedInstanceState;
359 restoreState(mSavedState);
360
361 if (PROFILE_STARTUP) {
362 android.os.Debug.stopMethodTracing();
363 }
364
365 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 }
368
369 // For handling default keys
370 mDefaultKeySsb = new SpannableStringBuilder();
371 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800372
373 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
374 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 }
Romain Guycbb89e42009-06-08 15:52:54 -0700376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700378 if (sLocaleConfiguration == null) {
379 new AsyncTask<Void, Void, LocaleConfiguration>() {
380 @Override
381 protected LocaleConfiguration doInBackground(Void... unused) {
382 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
383 readConfiguration(Launcher.this, localeConfiguration);
384 return localeConfiguration;
385 }
386
387 @Override
388 protected void onPostExecute(LocaleConfiguration result) {
389 sLocaleConfiguration = result;
390 checkForLocaleChange(); // recursive, but now with a locale configuration
391 }
392 }.execute();
393 return;
394 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 final Configuration configuration = getResources().getConfiguration();
397
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700398 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 final String locale = configuration.locale.toString();
400
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700401 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 final int mcc = configuration.mcc;
403
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 final int mnc = configuration.mnc;
406
Romain Guy84f296c2009-11-04 15:00:44 -0800407 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700410 sLocaleConfiguration.locale = locale;
411 sLocaleConfiguration.mcc = mcc;
412 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700413
Joe Onorato0589f0f2010-02-08 13:44:00 -0800414 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400415 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416
417 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
418 new Thread("WriteLocaleConfiguration") {
419 public void run() {
420 writeConfiguration(Launcher.this, localeConfiguration);
421 }
422 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700423 }
424 }
425
426 private static class LocaleConfiguration {
427 public String locale;
428 public int mcc = -1;
429 public int mnc = -1;
430 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700431
Romain Guy98d01652009-06-30 16:21:04 -0700432 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
433 DataInputStream in = null;
434 try {
435 in = new DataInputStream(context.openFileInput(PREFERENCES));
436 configuration.locale = in.readUTF();
437 configuration.mcc = in.readInt();
438 configuration.mnc = in.readInt();
439 } catch (FileNotFoundException e) {
440 // Ignore
441 } catch (IOException e) {
442 // Ignore
443 } finally {
444 if (in != null) {
445 try {
446 in.close();
447 } catch (IOException e) {
448 // Ignore
449 }
450 }
451 }
452 }
453
454 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
455 DataOutputStream out = null;
456 try {
457 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
458 out.writeUTF(configuration.locale);
459 out.writeInt(configuration.mcc);
460 out.writeInt(configuration.mnc);
461 out.flush();
462 } catch (FileNotFoundException e) {
463 // Ignore
464 } catch (IOException e) {
465 //noinspection ResultOfMethodCallIgnored
466 context.getFileStreamPath(PREFERENCES).delete();
467 } finally {
468 if (out != null) {
469 try {
470 out.close();
471 } catch (IOException e) {
472 // Ignore
473 }
474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 }
476 }
477
478 static int getScreen() {
479 synchronized (sLock) {
480 return sScreen;
481 }
482 }
483
484 static void setScreen(int screen) {
485 synchronized (sLock) {
486 sScreen = screen;
487 }
488 }
489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700491 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492
493 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700494 // TODO: Put back when we decide about scrolling the wallpaper
495 // boolean isPortrait = display.getWidth() < display.getHeight();
496 // final int width = isPortrait ? display.getWidth() : display.getHeight();
497 // final int height = isPortrait ? display.getHeight() : display.getWidth();
498 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
499 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 }
501
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400502 // Note: This doesn't do all the client-id magic that BrowserProvider does
503 // in Browser. (http://b/2425179)
504 private Uri getDefaultBrowserUri() {
505 String url = getString(R.string.default_browser_url);
506 if (url.indexOf("{CID}") != -1) {
507 url = url.replace("{CID}", "android-google");
508 }
509 return Uri.parse(url);
510 }
511
512 // Load the Intent templates from arrays.xml to populate the hotseats. For
513 // each Intent, if it resolves to a single app, use that as the launch
514 // intent & use that app's label as the contentDescription. Otherwise,
515 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400516 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400517 if (mHotseatConfig == null) {
518 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
519 if (mHotseatConfig.length > 0) {
520 mHotseats = new Intent[mHotseatConfig.length];
521 mHotseatLabels = new CharSequence[mHotseatConfig.length];
522 mHotseatIcons = new Drawable[mHotseatConfig.length];
523 } else {
524 mHotseats = null;
525 mHotseatIcons = null;
526 mHotseatLabels = null;
527 }
528
529 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
530 for (int i=0; i<mHotseatConfig.length; i++) {
531 // load icon for this slot; currently unrelated to the actual activity
532 try {
533 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
534 } catch (ArrayIndexOutOfBoundsException ex) {
535 Log.w(TAG, "Missing hotseat_icons array item #" + i);
536 mHotseatIcons[i] = null;
537 }
538 }
539 hotseatIconDrawables.recycle();
540 }
541
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400542 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 for (int i=0; i<mHotseatConfig.length; i++) {
544 Intent intent = null;
545 if (mHotseatConfig[i].equals("*BROWSER*")) {
546 // magic value meaning "launch user's default web browser"
547 // replace it with a generic web request so we can see if there is indeed a default
548 String defaultUri = getString(R.string.default_browser_url);
549 intent = new Intent(
550 Intent.ACTION_VIEW,
551 ((defaultUri != null)
552 ? Uri.parse(defaultUri)
553 : getDefaultBrowserUri())
554 ).addCategory(Intent.CATEGORY_BROWSABLE);
555 // note: if the user launches this without a default set, she
556 // will always be taken to the default URL above; this is
557 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700558 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 // URL is unavoidably sent to the chosen app.
560 } else {
561 try {
562 intent = Intent.parseUri(mHotseatConfig[i], 0);
563 } catch (java.net.URISyntaxException ex) {
564 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
565 // bogus; leave intent=null
566 }
567 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700568
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400569 if (intent == null) {
570 mHotseats[i] = null;
571 mHotseatLabels[i] = getText(R.string.activity_not_found);
572 continue;
573 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400574
575 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700576 Log.d(TAG, "loadHotseats: hotseat " + i
577 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400578 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400579 + "]");
580 }
581
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
583 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 Log.d(TAG, "Best match for intent: " + bestMatch);
586 Log.d(TAG, "All matches: ");
587 for (ResolveInfo ri : allMatches) {
588 Log.d(TAG, " --> " + ri);
589 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400590 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 // did this resolve to a single app, or the resolver?
592 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700593 // can't find any activity to handle this. let's leave the
594 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400595 // to launch.
596 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400597
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400598 // set accessibility text to "Not installed"
599 mHotseatLabels[i] = getText(R.string.activity_not_found);
600 } else {
601 boolean found = false;
602 for (ResolveInfo ri : allMatches) {
603 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
604 && bestMatch.activityInfo.applicationInfo.packageName
605 .equals(ri.activityInfo.applicationInfo.packageName)) {
606 found = true;
607 break;
608 }
609 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700610
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400611 if (!found) {
612 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
613 // the bestMatch is probably the ResolveActivity, meaning the
614 // user has not yet selected a default
615 // so: we'll keep the original intent for now
616 mHotseats[i] = intent;
617
618 // set the accessibility text to "Select shortcut"
619 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
620 } else {
621 // we have an app!
622 // now reconstruct the intent to launch it through the front
623 // door
624 ComponentName com = new ComponentName(
625 bestMatch.activityInfo.applicationInfo.packageName,
626 bestMatch.activityInfo.name);
627 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
628
629 // load the app label for accessibility
630 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
631 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400632 }
633
634 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 Log.d(TAG, "loadHotseats: hotseat " + i
636 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400637 + ((mHotseats[i] == null)
638 ? "null"
639 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400640 + "] label=[" + mHotseatLabels[i]
641 + "]"
642 );
643 }
644 }
645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 @Override
648 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700649 mWaitingForResult = false;
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 // The pattern used here is that a user PICKs a specific application,
652 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
655 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700656
Romain Guy94dabf12009-07-21 10:55:43 -0700657 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 switch (requestCode) {
659 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 break;
662 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800663 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 break;
665 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 break;
668 case REQUEST_PICK_LIVE_FOLDER:
669 addLiveFolder(data);
670 break;
671 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700675 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700681 case REQUEST_PICK_WALLPAPER:
682 // We just wanted the activity result here so we can clear mWaitingForResult
683 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
Romain Guy18042c82009-11-06 11:44:55 -0800685 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
686 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
687 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 // Clean up the appWidgetId if we canceled
689 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
690 if (appWidgetId != -1) {
691 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695
696 @Override
697 protected void onResume() {
698 super.onResume();
699
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800700 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 mWorkspaceLoading = true;
704 mModel.startLoader(this, true);
705 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700707 // When we resume Launcher, a different Activity might be responsible for the app
708 // market intent, so refresh the icon
709 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
711
712 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700713 protected void onPause() {
714 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700715 // Some launcher layouts don't have a previous and next view
716 if (mPreviousView != null) {
717 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700718 }
719 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700720 dismissPreview(mNextView);
721 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800722 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 }
Romain Guycbb89e42009-06-08 15:52:54 -0700724
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700725 @Override
726 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 // Flag the loader to stop early before switching
728 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800729 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800730 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700731 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800733 // We can't hide the IME if it was forced open. So don't bother
734 /*
735 @Override
736 public void onWindowFocusChanged(boolean hasFocus) {
737 super.onWindowFocusChanged(hasFocus);
738
739 if (hasFocus) {
740 final InputMethodManager inputManager = (InputMethodManager)
741 getSystemService(Context.INPUT_METHOD_SERVICE);
742 WindowManager.LayoutParams lp = getWindow().getAttributes();
743 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
744 android.os.Handler()) {
745 protected void onReceiveResult(int resultCode, Bundle resultData) {
746 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
747 }
748 });
749 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
750 }
751 }
752 */
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 private boolean acceptFilter() {
755 final InputMethodManager inputManager = (InputMethodManager)
756 getSystemService(Context.INPUT_METHOD_SERVICE);
757 return !inputManager.isFullscreenMode();
758 }
759
760 @Override
761 public boolean onKeyDown(int keyCode, KeyEvent event) {
762 boolean handled = super.onKeyDown(keyCode, event);
763 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
764 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
765 keyCode, event);
766 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700767 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700768 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700769 // showSearchDialog()
770 // If there are multiple keystrokes before the search dialog takes focus,
771 // onSearchRequested() will be called for every keystroke,
772 // but it is idempotent, so it's fine.
773 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775 }
776
Joe Onorato8a9625e2010-01-28 15:55:35 -0800777 // Eat the long press event so the keyboard doesn't come up.
778 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
779 return true;
780 }
781
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 return handled;
783 }
784
Karl Rosaen138a0412009-04-23 19:00:21 -0700785 private String getTypedText() {
786 return mDefaultKeySsb.toString();
787 }
788
789 private void clearTypedText() {
790 mDefaultKeySsb.clear();
791 mDefaultKeySsb.clearSpans();
792 Selection.setSelection(mDefaultKeySsb, 0);
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 /**
796 * Restores the previous state, if it exists.
797 *
798 * @param savedState The previous state.
799 */
800 private void restoreState(Bundle savedState) {
801 if (savedState == null) {
802 return;
803 }
804
Joe Onorato3a8820b2009-11-10 15:06:42 -0800805 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
806 if (allApps) {
807 showAllApps(false);
808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
811 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700812 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 }
814
815 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
816 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700817 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 mAddItemCellInfo = new CellLayout.CellInfo();
819 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
820 addItemCellInfo.valid = true;
821 addItemCellInfo.screen = addScreen;
822 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
823 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
824 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
825 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700826 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
828 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
829 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
830 mRestoring = true;
831 }
832
833 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
834 if (renameFolder) {
835 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700836 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 mRestoring = true;
838 }
839 }
840
841 /**
842 * Finds all the views we need and configure them properly.
843 */
844 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700845 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400846
Romain Guyb1b69f52009-08-10 15:10:15 -0700847 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400848 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
Joe Onorato7c312c12009-08-13 21:36:53 -0700850 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700851 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700852 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800853 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700854 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700855 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700856
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700857 if (LauncherApplication.isScreenXLarge()) {
858 // They need to be INVISIBLE initially so that they will be measured in the layout.
859 // Otherwise the animations are messed up when we show them for the first time.
860 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
861 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
862 }
863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
865 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500866 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700867
Joe Onorato7c312c12009-08-13 21:36:53 -0700868 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
869 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700871 View handleView = findViewById(R.id.all_apps_button);
872 if (handleView != null && handleView instanceof HandleView) {
873 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700874 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700875 mHandleView.setLauncher(this);
876 mHandleView.setOnClickListener(this);
877 mHandleView.setOnLongClickListener(this);
878 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800879
Winson Chung80baf5a2010-08-09 16:03:15 -0700880 if (mCustomizePagedView != null) {
881 mCustomizePagedView.setLauncher(this);
882 mCustomizePagedView.setDragController(dragController);
883 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700884 } else {
885 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
886 hotseatLeft.setContentDescription(mHotseatLabels[0]);
887 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
888 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
889 hotseatRight.setContentDescription(mHotseatLabels[1]);
890 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400891
Michael Jurkaaf442092010-06-10 17:01:57 -0700892 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
893 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800894
Michael Jurkaaf442092010-06-10 17:01:57 -0700895 Drawable previous = mPreviousView.getDrawable();
896 Drawable next = mNextView.getDrawable();
897 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
Michael Jurkaaf442092010-06-10 17:01:57 -0700899 mPreviousView.setHapticFeedbackEnabled(false);
900 mPreviousView.setOnLongClickListener(this);
901 mNextView.setHapticFeedbackEnabled(false);
902 mNextView.setOnLongClickListener(this);
903 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400906 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908
909 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400910 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700911 int deleteZoneHandleId;
912 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700913 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700914 } else {
915 deleteZoneHandleId = R.id.all_apps_button_cluster;
916 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700917 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700918 dragController.addDragListener(deleteZone);
919
920 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
921 if (infoButton != null) {
922 infoButton.setLauncher(this);
923 infoButton.setHandle(findViewById(R.id.configure_button));
924 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
925 dragController.addDragListener(infoButton);
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Joe Onorato00acb122009-08-04 16:04:30 -0400928 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400929 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800930 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700931
Joe Onorato00acb122009-08-04 16:04:30 -0400932 // The order here is bottom to top.
933 dragController.addDropTarget(workspace);
934 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700935 if (infoButton != null) {
936 dragController.addDropTarget(infoButton);
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
Romain Guy8a73c512009-11-09 19:19:59 -0800940 @SuppressWarnings({"UnusedDeclaration"})
941 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800942 if (!isAllAppsVisible()) {
943 mWorkspace.scrollLeft();
944 }
Romain Guy8a73c512009-11-09 19:19:59 -0800945 }
946
947 @SuppressWarnings({"UnusedDeclaration"})
948 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800949 if (!isAllAppsVisible()) {
950 mWorkspace.scrollRight();
951 }
Romain Guy8a73c512009-11-09 19:19:59 -0800952 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400953
954 @SuppressWarnings({"UnusedDeclaration"})
955 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400956 if (isAllAppsVisible()) return;
957
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400958 int index = -1;
959 if (v.getId() == R.id.hotseat_left) {
960 index = 0;
961 } else if (v.getId() == R.id.hotseat_right) {
962 index = 1;
963 }
964
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400965 // reload these every tap; you never know when they might change
966 loadHotseats();
967 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
968 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400969 startActivitySafely(
970 mHotseats[index],
971 "hotseat"
972 );
973 }
974 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 /**
977 * Creates a view representing a shortcut.
978 *
979 * @param info The data structure describing the shortcut.
980 *
981 * @return A View inflated from R.layout.application.
982 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800983 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700985 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
987
988 /**
989 * Creates a view representing a shortcut inflated from the specified resource.
990 *
991 * @param layoutResId The id of the XML layout used to create the shortcut.
992 * @param parent The group the shortcut belongs to.
993 * @param info The data structure describing the shortcut.
994 *
995 * @return A View inflated from layoutResId.
996 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
999
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1001 new FastBitmapDrawable(info.getIcon(mIconCache)),
1002 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 favorite.setText(info.title);
1004 favorite.setTag(info);
1005 favorite.setOnClickListener(this);
1006
1007 return favorite;
1008 }
1009
1010 /**
1011 * Add an application shortcut to the workspace.
1012 *
1013 * @param data The intent describing the application.
1014 * @param cellInfo The position on screen where to create the shortcut.
1015 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001016 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001017 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 if (!findSingleSlot(cellInfo)) return;
1019
Joe Onorato0589f0f2010-02-08 13:44:00 -08001020 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1021 data, context);
1022
Romain Guy73b979d2009-06-09 12:57:21 -07001023 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026 info.container = ItemInfo.NO_ID;
1027 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1028 } else {
1029 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 }
1031 }
Romain Guycbb89e42009-06-08 15:52:54 -07001032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 /**
1034 * Add a shortcut to the workspace.
1035 *
1036 * @param data The intent describing the shortcut.
1037 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001040 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001042
Joe Onorato0589f0f2010-02-08 13:44:00 -08001043 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
1045 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1048 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050 }
1051
Romain Guycbb89e42009-06-08 15:52:54 -07001052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001056 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001059 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001060 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001061
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001064 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001067 // We have saved the position to which the widget was dragged-- this really only matters
1068 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001070
1071 // For now, we don't save the coordinate where we dropped the icon because we're not
1072 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1073 // a widget on the home screen in case we want to add it in the future
1074 final int[] xyTouch = null;
1075 //final int[] xyTouch = mAddItemCoordinates;
1076 boolean findNearestVacantAreaFailed = false;
1077 if (xyTouch != null) {
1078 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1079 int[] result = screen.findNearestVacantArea(
1080 mAddItemCoordinates[0], mAddItemCoordinates[1],
1081 spans[0], spans[1], cellInfo, xy);
1082 findNearestVacantAreaFailed = (result == null);
1083 }
1084
1085 if (findNearestVacantAreaFailed ||
1086 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001087 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1088 return;
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 // Build Launcher-specific widget info and save to database
1092 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 launcherInfo.spanX = spans[0];
1094 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 LauncherModel.addItemToDatabase(this, launcherInfo,
1097 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001098 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099
1100 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001104 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001105
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001106 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Michael Jurkaa63c4522010-08-19 13:52:27 -07001109 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1115 mDesktopItems.remove(launcherInfo);
1116 launcherInfo.hostView = null;
1117 }
1118
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001119 public LauncherAppWidgetHost getAppWidgetHost() {
1120 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001123 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001124 getWindow().closeAllPanels();
1125
1126 try {
1127 dismissDialog(DIALOG_CREATE_SHORTCUT);
1128 // Unlock the workspace if the dialog was showing
1129 } catch (Exception e) {
1130 // An exception is thrown if the dialog is not visible, which is fine
1131 }
1132
1133 try {
1134 dismissDialog(DIALOG_RENAME_FOLDER);
1135 // Unlock the workspace if the dialog was showing
1136 } catch (Exception e) {
1137 // An exception is thrown if the dialog is not visible, which is fine
1138 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001139
1140 // Whatever we were doing is hereby canceled.
1141 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001142 }
1143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 @Override
1145 protected void onNewIntent(Intent intent) {
1146 super.onNewIntent(intent);
1147
1148 // Close the menu
1149 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001150 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001151 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001152
Joe Onorato14f122b2009-11-19 14:06:36 -08001153 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1154 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1155 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001156 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1157
Michael Jurka4cb37242010-08-09 21:05:32 -07001158 // in all these cases, only animate if we're already on home
1159 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001160 mWorkspace.unshrink(alreadyOnHome);
1161 }
1162 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001163 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka99229f62010-09-09 11:49:32 -07001164 mWorkspace.moveToDefaultScreen(
1165 alreadyOnHome && (LauncherApplication.isScreenXLarge() || !allAppsVisible));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001166 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001167 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001168 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001169
Joe Onorato3a8820b2009-11-10 15:06:42 -08001170 final View v = getWindow().peekDecorView();
1171 if (v != null && v.getWindowToken() != null) {
1172 InputMethodManager imm = (InputMethodManager)getSystemService(
1173 INPUT_METHOD_SERVICE);
1174 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176 }
1177 }
1178
1179 @Override
1180 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1181 // Do not call super here
1182 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001183
1184 if (mHomeCustomizationDrawer != null) {
1185 String cur = savedInstanceState.getString("currentTab");
1186 if (cur != null) {
1187 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1188 }
1189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
1191
1192 @Override
1193 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001194 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195
1196 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1197 if (folders.size() > 0) {
1198 final int count = folders.size();
1199 long[] ids = new long[count];
1200 for (int i = 0; i < count; i++) {
1201 final FolderInfo info = folders.get(i).getInfo();
1202 ids[i] = info.id;
1203 }
1204 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1205 } else {
1206 super.onSaveInstanceState(outState);
1207 }
1208
Joe Onoratofb0ca672009-09-14 17:55:46 -04001209 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001210 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
1214 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1215 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1216 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1217
1218 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1219 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1220 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1221 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1222 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1223 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1224 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1225 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001226 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
1229 if (mFolderInfo != null && mWaitingForResult) {
1230 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1231 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1232 }
Michael Jurka946ad472010-07-09 18:05:18 -07001233
1234 if (mHomeCustomizationDrawer != null) {
1235 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1236 if (currentTabTag != null) {
1237 outState.putString("currentTab", currentTabTag);
1238 }
1239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
1241
1242 @Override
1243 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001247 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001249 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251
1252 TextKeyListener.getInstance().release();
1253
Joe Onorato9c1289c2009-08-17 11:03:03 -04001254 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001257
1258 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001259
Patrick Dubroyab962b72010-07-26 12:10:10 -07001260 // Some launcher layouts don't have a previous and next view
1261 if (mPreviousView != null) {
1262 dismissPreview(mPreviousView);
1263 }
1264 if (mNextView != null) {
1265 dismissPreview(mNextView);
1266 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001267
1268 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 @Override
1272 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001273 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 super.startActivityForResult(intent, requestCode);
1275 }
1276
1277 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001278 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001280
Joe Onorato7bb17492009-09-24 17:51:01 -07001281 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001282
Karl Rosaen138a0412009-04-23 19:00:21 -07001283 if (initialQuery == null) {
1284 // Use any text typed in the launcher as the initial query
1285 initialQuery = getTypedText();
1286 clearTypedText();
1287 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 if (appSearchData == null) {
1289 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001290 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Karl Rosaen138a0412009-04-23 19:00:21 -07001293 final SearchManager searchManager =
1294 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001295 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001296 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298
1299 @Override
1300 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001301 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001302 return false;
1303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304
1305 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1308 .setIcon(android.R.drawable.ic_menu_add)
1309 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001310 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 .setIcon(android.R.drawable.ic_menu_gallery)
1312 .setAlphabeticShortcut('W');
1313 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1314 .setIcon(android.R.drawable.ic_search_category_default)
1315 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1316 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1317 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1318 .setAlphabeticShortcut('N');
1319
1320 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001321 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1322 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323
1324 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1325 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1326 .setIntent(settings);
1327
1328 return true;
1329 }
1330
1331 @Override
1332 public boolean onPrepareOptionsMenu(Menu menu) {
1333 super.onPrepareOptionsMenu(menu);
1334
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001335 // If all apps is animating, don't show the menu, because we don't know
1336 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001337 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001338 return false;
1339 }
1340
1341 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001342 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001343 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1344 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1345
1346 // Disable add if the workspace is full.
1347 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001348 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001349 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1350 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351
1352 return true;
1353 }
1354
Michael Jurka0e260592010-06-30 17:07:39 -07001355 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1356 // using the settings menu to add an item, something similar happens in showAddDialog
1357 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001358 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001359 mAddItemCellInfo = mMenuAddInfo;
1360 }
1361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 @Override
1363 public boolean onOptionsItemSelected(MenuItem item) {
1364 switch (item.getItemId()) {
1365 case MENU_ADD:
1366 addItems();
1367 return true;
1368 case MENU_WALLPAPER_SETTINGS:
1369 startWallpaper();
1370 return true;
1371 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001372 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 return true;
1374 case MENU_NOTIFICATIONS:
1375 showNotifications();
1376 return true;
1377 }
1378
1379 return super.onOptionsItemSelected(item);
1380 }
Romain Guycbb89e42009-06-08 15:52:54 -07001381
Karl Rosaen138a0412009-04-23 19:00:21 -07001382 /**
1383 * Indicates that we want global search for this activity by setting the globalSearch
1384 * argument for {@link #startSearch} to true.
1385 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001387 @Override
1388 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001389 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001390 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001391 }
1392
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 public boolean isWorkspaceLocked() {
1394 return mWorkspaceLoading || mWaitingForResult;
1395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001398 if (LauncherApplication.isScreenXLarge()) {
1399 // Animate the widget chooser up from the bottom of the screen
1400 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001401 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001402 }
1403 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001404 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001405 showAddDialog(mMenuAddInfo);
1406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
Michael Jurkaa63c4522010-08-19 13:52:27 -07001409 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1410 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001411 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001412
1413 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1414 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001415 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1416 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1417 addAppWidgetImpl(appWidgetId);
1418 }
1419
1420 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001421 // TODO: catch bad widget exception when sent
1422 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001423 // TODO: Is this log message meaningful?
1424 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1425 addAppWidgetImpl(appWidgetId);
1426 }
1427
1428 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001429 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430
Bjorn Bringert7984c942009-12-09 15:38:25 +00001431 if (appWidget.configure != null) {
1432 // Launch over to configure widget, if needed
1433 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1434 intent.setComponent(appWidget.configure);
1435 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1436
Romain Guy8e633c52010-03-04 12:51:36 -08001437 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001439 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001440 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Joe Onoratodeb98af2010-02-19 14:59:39 -08001444 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001445 // Handle case where user selected "Applications"
1446 String applicationName = getResources().getString(R.string.group_applications);
1447 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001448
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001449 if (applicationName != null && applicationName.equals(shortcutName)) {
1450 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1451 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001452
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001453 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1454 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001455 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001456 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001457 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001458 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460
1461 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001462 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001463 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001464 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001466 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001468 } else {
1469 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 UserFolderInfo folderInfo = new UserFolderInfo();
1475 folderInfo.title = getText(R.string.folder_name);
1476
1477 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001478 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 if (!findSingleSlot(cellInfo)) return;
1480
1481 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001482 LauncherModel.addItemToDatabase(this, folderInfo,
1483 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001484 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001485 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
1487 // Create the view
1488 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001489 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
Romain Guycbb89e42009-06-08 15:52:54 -07001493
Joe Onorato9c1289c2009-08-17 11:03:03 -04001494 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001495 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 }
1497
1498 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001499 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 if (!findSingleSlot(cellInfo)) return;
1501
1502 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1503
1504 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001506 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1508 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
1510 }
1511
1512 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1513 CellLayout.CellInfo cellInfo, boolean notify) {
1514
1515 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1516 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1517
1518 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 Intent.ShortcutIconResource iconResource = null;
1520
1521 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1522 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1523 try {
1524 iconResource = (Intent.ShortcutIconResource) extra;
1525 final PackageManager packageManager = context.getPackageManager();
1526 Resources resources = packageManager.getResourcesForApplication(
1527 iconResource.packageName);
1528 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1529 icon = resources.getDrawable(id);
1530 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001531 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533 }
1534
1535 if (icon == null) {
1536 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1537 }
1538
1539 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001540 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 info.title = name;
1542 info.iconResource = iconResource;
1543 info.uri = data.getData();
1544 info.baseIntent = baseIntent;
1545 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1546 LiveFolders.DISPLAY_MODE_GRID);
1547
1548 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1549 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001550 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 return info;
1553 }
1554
1555 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1556 final int[] xy = new int[2];
1557 if (findSlot(cellInfo, xy, 1, 1)) {
1558 cellInfo.cellX = xy[0];
1559 cellInfo.cellY = xy[1];
1560 return true;
1561 }
1562 return false;
1563 }
1564
1565 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1566 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001567 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1568 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1570 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1571 return false;
1572 }
1573 }
1574 return true;
1575 }
1576
1577 private void showNotifications() {
1578 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1579 if (statusBar != null) {
1580 statusBar.expand();
1581 }
1582 }
1583
1584 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001585 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001587 Intent chooser = Intent.createChooser(pickWallpaper,
1588 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001589 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1590 // Removed in Eclair MR1
1591// WallpaperManager wm = (WallpaperManager)
1592// getSystemService(Context.WALLPAPER_SERVICE);
1593// WallpaperInfo wi = wm.getWallpaperInfo();
1594// if (wi != null && wi.getSettingsActivity() != null) {
1595// LabeledIntent li = new LabeledIntent(getPackageName(),
1596// R.string.configure_wallpaper, 0);
1597// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1598// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1599// }
Mike Clerona0618e42009-10-22 13:55:21 -07001600 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001603 /**
1604 * Registers various content observers. The current implementation registers
1605 * only a favorites observer to keep track of the favorites applications.
1606 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001607 private void registerContentObservers() {
1608 ContentResolver resolver = getContentResolver();
1609 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1610 true, mWidgetObserver);
1611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 @Override
1614 public boolean dispatchKeyEvent(KeyEvent event) {
1615 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1616 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001618 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001619 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001620 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001621 dumpState();
1622 return true;
1623 }
1624 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001625 }
1626 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1627 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001628 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 return true;
1630 }
1631 }
1632
1633 return super.dispatchKeyEvent(event);
1634 }
1635
Joe Onorato88ec0992009-11-19 13:16:06 -08001636 @Override
1637 public void onBackPressed() {
1638 if (isAllAppsVisible()) {
1639 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001640 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001641 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001642 } else {
1643 closeFolder();
1644 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001645 // Some launcher layouts don't have a previous and next view
1646 if (mPreviousView != null) {
1647 dismissPreview(mPreviousView);
1648 dismissPreview(mNextView);
1649 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001650 }
1651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 private void closeFolder() {
1653 Folder folder = mWorkspace.getOpenFolder();
1654 if (folder != null) {
1655 closeFolder(folder);
1656 }
1657 }
1658
1659 void closeFolder(Folder folder) {
1660 folder.getInfo().opened = false;
1661 ViewGroup parent = (ViewGroup) folder.getParent();
1662 if (parent != null) {
1663 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001664 if (folder instanceof DropTarget) {
1665 // Live folders aren't DropTargets.
1666 mDragController.removeDropTarget((DropTarget)folder);
1667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 }
1669 folder.onClose();
1670 }
1671
1672 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001673 * Re-listen when widgets are reset.
1674 */
1675 private void onAppWidgetReset() {
1676 mAppWidgetHost.startListening();
1677 }
1678
1679 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001680 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1681 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683 private void unbindDesktopItems() {
1684 for (ItemInfo item: mDesktopItems) {
1685 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 }
1687 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 /**
1690 * Launches the intent referred by the clicked shortcut.
1691 *
1692 * @param v The view representing the clicked shortcut.
1693 */
1694 public void onClick(View v) {
1695 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001696 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001698 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001699 int[] pos = new int[2];
1700 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001701 intent.setSourceBounds(new Rect(pos[0], pos[1],
1702 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001703 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 } else if (tag instanceof FolderInfo) {
1705 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001706 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001707 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001708 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001709 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001710 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 }
1713 }
1714
Michael Jurka0e260592010-06-30 17:07:39 -07001715 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001716 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001717 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001718 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001719 return false;
1720 }
1721
Michael Jurkaaf442092010-06-10 17:01:57 -07001722 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001723 * Event handler for the search button
1724 *
1725 * @param v The view that was clicked.
1726 */
1727 public void onClickSearchButton(View v) {
1728 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1729 View button = findViewById(R.id.search_button);
1730 i.setSourceBounds(
1731 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1732 startActivity(i);
1733 }
1734
1735 /**
1736 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001737 * enters home screen customization mode.
1738 *
1739 * @param v The view that was clicked.
1740 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001741 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001742 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001743 }
1744
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001745 /**
1746 * Event handler for the "grid" button that appears on the home screen, which
1747 * enters all apps mode.
1748 *
1749 * @param v The view that was clicked.
1750 */
1751 public void onClickAllAppsButton(View v) {
1752 showAllApps(true);
1753 }
1754
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001755 public void onClickAppMarketButton(View v) {
1756 if (mAppMarketIntent != null) {
1757 startActivitySafely(mAppMarketIntent, "app market");
1758 }
1759 }
1760
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001761 void startApplicationDetailsActivity(ComponentName componentName) {
1762 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001763 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1764 Uri.fromParts("package", packageName, null));
1765 startActivity(intent);
1766 }
1767
Patrick Dubroy5539af72010-09-07 15:22:01 -07001768 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1769 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1770 // System applications cannot be installed. For now, show a toast explaining that.
1771 // We may give them the option of disabling apps this way.
1772 int messageId = R.string.uninstall_system_app_text;
1773 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1774 } else {
1775 String packageName = appInfo.componentName.getPackageName();
1776 String className = appInfo.componentName.getClassName();
1777 Intent intent = new Intent(
1778 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1779 startActivity(intent);
1780 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001781 }
1782
Joe Onoratof984e852010-03-25 09:47:45 -07001783 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1785 try {
1786 startActivity(intent);
1787 } catch (ActivityNotFoundException e) {
1788 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001789 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 } catch (SecurityException e) {
1791 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001792 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001794 "or use the exported attribute for this activity. "
1795 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001798
Romain Guy8e633c52010-03-04 12:51:36 -08001799 void startActivityForResultSafely(Intent intent, int requestCode) {
1800 try {
1801 startActivityForResult(intent, requestCode);
1802 } catch (ActivityNotFoundException e) {
1803 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1804 } catch (SecurityException e) {
1805 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1806 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1807 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1808 "or use the exported attribute for this activity.", e);
1809 }
1810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811
1812 private void handleFolderClick(FolderInfo folderInfo) {
1813 if (!folderInfo.opened) {
1814 // Close any open folder
1815 closeFolder();
1816 // Open the requested folder
1817 openFolder(folderInfo);
1818 } else {
1819 // Find the open folder...
1820 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1821 int folderScreen;
1822 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001823 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 // .. and close it
1825 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001826 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 // Close any folder open on the current screen
1828 closeFolder();
1829 // Pull the folder onto this screen
1830 openFolder(folderInfo);
1831 }
1832 }
1833 }
1834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 /**
1837 * Opens the user fodler described by the specified tag. The opening of the folder
1838 * is animated relative to the specified View. If the View is null, no animation
1839 * is played.
1840 *
1841 * @param folderInfo The FolderInfo describing the folder to open.
1842 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001843 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 Folder openFolder;
1845
1846 if (folderInfo instanceof UserFolderInfo) {
1847 openFolder = UserFolder.fromXml(this);
1848 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001849 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 } else {
1851 return;
1852 }
1853
Joe Onorato00acb122009-08-04 16:04:30 -04001854 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 openFolder.setLauncher(this);
1856
1857 openFolder.bind(folderInfo);
1858 folderInfo.opened = true;
1859
Winson Chungaafa03c2010-06-11 17:34:16 -07001860 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 openFolder.onOpen();
1863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001866 switch (v.getId()) {
1867 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001868 if (!isAllAppsVisible()) {
1869 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1870 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001871 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001872 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001873 return true;
1874 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001875 if (!isAllAppsVisible()) {
1876 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1877 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001878 showPreviews(v);
1879 }
1880 return true;
1881 case R.id.all_apps_button:
1882 if (!isAllAppsVisible()) {
1883 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1884 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1885 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001886 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001887 return true;
1888 }
1889
Joe Onorato9c1289c2009-08-17 11:03:03 -04001890 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 return false;
1892 }
1893
1894 if (!(v instanceof CellLayout)) {
1895 v = (View) v.getParent();
1896 }
1897
1898 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1899
1900 // This happens when long clicking an item with the dpad/trackball
1901 if (cellInfo == null) {
1902 return true;
1903 }
1904
1905 if (mWorkspace.allowLongPress()) {
1906 if (cellInfo.cell == null) {
1907 if (cellInfo.valid) {
1908 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001909 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001910 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1911 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 showAddDialog(cellInfo);
1913 }
1914 } else {
1915 if (!(cellInfo.cell instanceof Folder)) {
1916 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001917 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1918 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 mWorkspace.startDrag(cellInfo);
1920 }
1921 }
1922 }
1923 return true;
1924 }
1925
Romain Guye6b8e2f2009-11-10 11:56:55 -08001926 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001927 private void dismissPreview(final View v) {
1928 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001929 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001930 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1931 public void onDismiss() {
1932 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1933 int count = group.getChildCount();
1934 for (int i = 0; i < count; i++) {
1935 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1936 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001937 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1938 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1939
1940 v.setTag(R.id.workspace, null);
1941 v.setTag(R.id.icon, null);
1942 window.setOnDismissListener(null);
1943 }
1944 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001945 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001946 }
1947 v.setTag(null);
1948 }
1949
Romain Guyf8e6a802009-12-07 17:48:02 -08001950 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001951 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001952 }
1953
Romain Guya6abce82009-11-10 02:54:41 -08001954 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001955 final Resources resources = getResources();
1956 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001957
Romain Guya6abce82009-11-10 02:54:41 -08001958 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001959
Romain Guy9d31e9f2009-11-11 18:54:28 -08001960 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001961
Romain Guyf8e6a802009-12-07 17:48:02 -08001962 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001963 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001964 int extraW = (int) ((r.left + r.right) * max);
1965 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001966
1967 int aW = cell.getWidth() - extraW;
1968 float w = aW / max;
1969
1970 int width = cell.getWidth();
1971 int height = cell.getHeight();
1972 int x = cell.getLeftPadding();
1973 int y = cell.getTopPadding();
1974 width -= (x + cell.getRightPadding());
1975 height -= (y + cell.getBottomPadding());
1976
1977 float scale = w / width;
1978
1979 int count = end - start;
1980
1981 final float sWidth = width * scale;
1982 float sHeight = height * scale;
1983
Romain Guye6b8e2f2009-11-10 11:56:55 -08001984 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001985
Romain Guye6b8e2f2009-11-10 11:56:55 -08001986 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1987 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001988
1989 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001990 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001991 cell = (CellLayout) workspace.getChildAt(i);
1992
Romain Guyf8e6a802009-12-07 17:48:02 -08001993 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001994 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001995
1996 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001997 c.scale(scale, scale);
1998 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1999 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08002000
Romain Guy9d31e9f2009-11-11 18:54:28 -08002001 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002002 image.setImageBitmap(bitmap);
2003 image.setTag(i);
2004 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002005 image.setOnFocusChangeListener(handler);
2006 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002007 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002008
2009 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002010 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2011
Winson Chungaafa03c2010-06-11 17:34:16 -07002012 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002013 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002014
2015 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002016 p.setContentView(preview);
2017 p.setWidth((int) (sWidth * count + extraW));
2018 p.setHeight((int) (sHeight + extraH));
2019 p.setAnimationStyle(R.style.AnimationPreview);
2020 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002021 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002022 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002023 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002024
Romain Guye6b8e2f2009-11-10 11:56:55 -08002025 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2026 public void onDismiss() {
2027 dismissPreview(anchor);
2028 }
2029 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002030
2031 anchor.setTag(p);
2032 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002033 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002034 }
2035
Romain Guy9d31e9f2009-11-11 18:54:28 -08002036 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002037 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002038
Romain Guye6b8e2f2009-11-10 11:56:55 -08002039 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002040 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002041 }
2042
2043 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002044 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 v.post(this);
2046 }
2047
2048 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002049 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002050 }
2051
2052 public void onFocusChange(View v, boolean hasFocus) {
2053 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002054 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002055 }
Romain Guya6abce82009-11-10 02:54:41 -08002056 }
2057 }
2058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 Workspace getWorkspace() {
2060 return mWorkspace;
2061 }
2062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 @Override
2064 protected Dialog onCreateDialog(int id) {
2065 switch (id) {
2066 case DIALOG_CREATE_SHORTCUT:
2067 return new CreateShortcut().createDialog();
2068 case DIALOG_RENAME_FOLDER:
2069 return new RenameFolder().createDialog();
2070 }
2071
2072 return super.onCreateDialog(id);
2073 }
2074
2075 @Override
2076 protected void onPrepareDialog(int id, Dialog dialog) {
2077 switch (id) {
2078 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 break;
2080 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002081 if (mFolderInfo != null) {
2082 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2083 final CharSequence text = mFolderInfo.title;
2084 input.setText(text);
2085 input.setSelection(0, text.length());
2086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 break;
2088 }
2089 }
2090
2091 void showRenameDialog(FolderInfo info) {
2092 mFolderInfo = info;
2093 mWaitingForResult = true;
2094 showDialog(DIALOG_RENAME_FOLDER);
2095 }
2096
2097 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2098 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002099 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 mWaitingForResult = true;
2101 showDialog(DIALOG_CREATE_SHORTCUT);
2102 }
2103
Joe Onoratodeb98af2010-02-19 14:59:39 -08002104 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002105 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002106 Bundle bundle = new Bundle();
2107
2108 ArrayList<String> shortcutNames = new ArrayList<String>();
2109 shortcutNames.add(getString(R.string.group_applications));
2110 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2111
2112 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2113 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2114 R.drawable.ic_launcher_application));
2115 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2116
2117 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2118 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002119 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002120 pickIntent.putExtras(bundle);
2121
Joe Onoratodeb98af2010-02-19 14:59:39 -08002122 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002123 }
2124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 private class RenameFolder {
2126 private EditText mInput;
2127
2128 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2130 mInput = (EditText) layout.findViewById(R.id.folder_name);
2131
2132 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2133 builder.setIcon(0);
2134 builder.setTitle(getString(R.string.rename_folder_title));
2135 builder.setCancelable(true);
2136 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2137 public void onCancel(DialogInterface dialog) {
2138 cleanup();
2139 }
2140 });
2141 builder.setNegativeButton(getString(R.string.cancel_action),
2142 new Dialog.OnClickListener() {
2143 public void onClick(DialogInterface dialog, int which) {
2144 cleanup();
2145 }
2146 }
2147 );
2148 builder.setPositiveButton(getString(R.string.rename_action),
2149 new Dialog.OnClickListener() {
2150 public void onClick(DialogInterface dialog, int which) {
2151 changeFolderName();
2152 }
2153 }
2154 );
2155 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002156
2157 final AlertDialog dialog = builder.create();
2158 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2159 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002160 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002161 mInput.requestFocus();
2162 InputMethodManager inputManager = (InputMethodManager)
2163 getSystemService(Context.INPUT_METHOD_SERVICE);
2164 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002165 }
2166 });
2167
2168 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
2170
2171 private void changeFolderName() {
2172 final String name = mInput.getText().toString();
2173 if (!TextUtils.isEmpty(name)) {
2174 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002175 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 mFolderInfo.title = name;
2177 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2178
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002180 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 } else {
2183 final FolderIcon folderIcon = (FolderIcon)
2184 mWorkspace.getViewForTag(mFolderInfo);
2185 if (folderIcon != null) {
2186 folderIcon.setText(name);
2187 getWorkspace().requestLayout();
2188 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002189 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 mWorkspaceLoading = true;
2191 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 }
2193 }
2194 }
2195 cleanup();
2196 }
2197
2198 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 dismissDialog(DIALOG_RENAME_FOLDER);
2200 mWaitingForResult = false;
2201 mFolderInfo = null;
2202 }
2203 }
2204
Daniel Sandler843e8602010-06-07 14:59:01 -04002205 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2206 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002207 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002208 }
2209
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002210 // AllAppsView.Watcher
2211 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002212 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2213 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002214 mWorkspace.setVisibility(View.GONE);
2215 }
2216 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002217
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002218 private void showToolbarButton(View button) {
2219 button.setAlpha(1.0f);
2220 button.setVisibility(View.VISIBLE);
2221 button.setFocusable(true);
2222 button.setClickable(true);
2223 }
2224
2225 private void hideToolbarButton(View button) {
2226 button.setAlpha(0.0f);
2227 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2228 button.setVisibility(View.INVISIBLE);
2229 button.setFocusable(false);
2230 button.setClickable(false);
2231 }
2232
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002233 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002234 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002235 *
2236 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2237 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002238 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002239 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002240 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002241 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002242 final boolean showing = show;
2243 final boolean hiding = !show;
2244
2245 final int duration = show ?
2246 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2247 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2248
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002249 if (seq != null) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002250 Animator anim = new ObjectAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2251 anim.addListener(new AnimatorListenerAdapter() {
2252 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002253 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002255 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002256 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002257 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 });
2259 seq.play(anim);
2260 } else {
2261 if (showing) {
2262 showToolbarButton(view);
2263 } else {
2264 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002265 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002266 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002267 }
2268
2269 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002270 * Show/hide the appropriate toolbar buttons for newState.
2271 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002272 *
2273 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002274 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2275 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002276 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002277 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002278 final View searchButton = findViewById(R.id.search_button);
2279 final View allAppsButton = findViewById(R.id.all_apps_button);
2280 final View marketButton = findViewById(R.id.market_button);
2281 final View configureButton = findViewById(R.id.configure_button);
2282
2283 switch (newState) {
2284 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002285 hideOrShowToolbarButton(true, searchButton, showSeq);
2286 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2287 hideOrShowToolbarButton(true, configureButton, showSeq);
2288 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002289 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002290 break;
2291 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002292 hideOrShowToolbarButton(true, configureButton, showSeq);
2293 hideOrShowToolbarButton(true, marketButton, showSeq);
2294 hideOrShowToolbarButton(false, searchButton, hideSeq);
2295 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002296 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002297 break;
2298 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002299 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2300 hideOrShowToolbarButton(false, searchButton, hideSeq);
2301 hideOrShowToolbarButton(false, marketButton, hideSeq);
2302 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002303 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002304 break;
2305 }
2306 }
2307
2308 /**
2309 * Helper method for the cameraZoomIn/cameraZoomOut animations
2310 * @param view The view being animated
2311 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2312 * @param scaleFactor The scale factor used for the zoom
2313 */
2314 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2315 final int height = view.getHeight();
2316 view.setPivotX(view.getWidth() / 2.0f);
2317 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2318 // Assumes that the view is normally anchored to either the top or bottom of the screen
2319 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2320 if (state == State.ALL_APPS) {
2321 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2322 } else {
2323 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2324 }
2325 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002326
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002327 /**
2328 * Zoom the camera out from the workspace to reveal 'toView'.
2329 * Assumes that the view to show is anchored at either the very top or very bottom
2330 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002331 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002332 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002333 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002334 final Resources res = getResources();
2335 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2336 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002337 final boolean toAllApps = (toState == State.ALL_APPS);
2338 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002339
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002340 setPivotsForZoom(toView, toState, scale);
2341
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 if (toState == State.ALL_APPS) {
2343 mWorkspace.shrinkToBottom(animated);
2344 } else {
2345 mWorkspace.shrinkToTop(animated);
2346 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002347
2348 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002349 ValueAnimator scaleAnim = new ObjectAnimator(duration, toView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002350 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2351 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2352 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002353 scaleAnim.addListener(new AnimatorListenerAdapter() {
2354 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355 // Prepare the position
2356 toView.setTranslationX(0.0f);
2357 toView.setTranslationY(0.0f);
2358 toView.setVisibility(View.VISIBLE);
2359 }
2360 });
2361
Chet Haaseb1254a62010-09-07 13:35:00 -07002362 AnimatorSet toolbarHideAnim = new AnimatorSet();
2363 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002364 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2365
2366 // toView should appear right at the end of the workspace shrink animation
2367 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2368
Chet Haaseb1254a62010-09-07 13:35:00 -07002369 AnimatorSet s = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002370 s.playTogether(scaleAnim, toolbarHideAnim);
2371 s.play(scaleAnim).after(startDelay);
2372
2373 // Show the new toolbar buttons just as the main animation is ending
2374 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2375 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2376 s.start();
2377 } else {
2378 toView.setTranslationX(0.0f);
2379 toView.setTranslationY(0.0f);
2380 toView.setScaleX(1.0f);
2381 toView.setScaleY(1.0f);
2382 toView.setVisibility(View.VISIBLE);
2383 hideAndShowToolbarButtons(toState, null, null);
2384 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002385 }
2386
2387 /**
2388 * Zoom the camera back into the workspace, hiding 'fromView'.
2389 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002390 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2391 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002392 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002393 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002394 Resources res = getResources();
2395 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2396 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002397 final View fromView =
2398 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2399
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002400 mCustomizePagedView.endChoiceMode();
2401 mAllAppsPagedView.endChoiceMode();
2402
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002403 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002404
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002405 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406
2407 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002408 AnimatorSet s = new AnimatorSet();
2409 ValueAnimator scaleAnim = new ObjectAnimator(duration, fromView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002410 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2411 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2412 scaleAnim.setInterpolator(new AccelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002413 s.addListener(new AnimatorListenerAdapter() {
2414 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 fromView.setVisibility(View.GONE);
2416 fromView.setScaleX(1.0f);
2417 fromView.setScaleY(1.0f);
2418 }
2419 });
2420
Chet Haaseb1254a62010-09-07 13:35:00 -07002421 AnimatorSet toolbarHideAnim = new AnimatorSet();
2422 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002423 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2424
2425 s.playTogether(scaleAnim, toolbarHideAnim);
2426
2427 // Show the new toolbar buttons at the very end of the whole animation
2428 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2429 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2430 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2431 s.start();
2432 } else {
2433 fromView.setVisibility(View.GONE);
2434 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2435 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002436 }
2437
2438 /**
2439 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2440 * This is the transition used on xlarge screens to go between all apps and
2441 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002442 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2443 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2444 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002445 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002446 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002447 final Resources res = getResources();
2448 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002449 final int workspaceHeight = mWorkspace.getHeight();
2450
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002451 final boolean fromAllApps = (fromState == State.ALL_APPS);
2452 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2453 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002455 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2456 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2457 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2458 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002459
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002460 mCustomizePagedView.endChoiceMode();
2461 mAllAppsPagedView.endChoiceMode();
2462
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002463 if (toState == State.ALL_APPS) {
2464 mWorkspace.shrinkToBottom(animated);
2465 } else {
2466 mWorkspace.shrinkToTop(animated);
2467 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002468
2469 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002470 AnimatorSet s = new AnimatorSet();
2471 s.addListener(new AnimatorListenerAdapter() {
2472 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473 toView.setVisibility(View.VISIBLE);
2474 toView.setY(toViewStartY);
2475 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002476 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477 fromView.setVisibility(View.GONE);
2478 }
2479 });
2480
Chet Haaseb1254a62010-09-07 13:35:00 -07002481 AnimatorSet toolbarHideAnim = new AnimatorSet();
2482 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002483 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2484
2485 s.playTogether(
2486 toolbarHideAnim,
Chet Haaseb1254a62010-09-07 13:35:00 -07002487 new ObjectAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2488 new ObjectAnimator(duration, toView, "y", toViewStartY, toViewEndY));
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002489
2490 // Show the new toolbar buttons just as the main animation is ending
2491 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2492 s.play(toolbarShowAnim).after(duration - fadeInTime);
2493 s.start();
2494 } else {
2495 fromView.setY(fromViewEndY);
2496 fromView.setVisibility(View.GONE);
2497 toView.setY(toViewEndY);
2498 toView.setVisibility(View.VISIBLE);
2499 hideAndShowToolbarButtons(toState, null, null);
2500 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002501 }
2502
2503 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002504 if (mAllAppsGrid.isVisible())
2505 return;
2506
Michael Jurka79212d82010-07-30 16:36:20 -07002507 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002508 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002509 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002510 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002511 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002512 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002513 } else {
2514 mAllAppsGrid.zoom(1.0f, animated);
2515 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002516
Romain Guyc16fea72010-03-12 17:17:56 -08002517 ((View) mAllAppsGrid).setFocusable(true);
2518 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002519
Joe Onorato7c312c12009-08-13 21:36:53 -07002520 // TODO: fade these two too
2521 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002522 }
2523
Joe Onoratob2061212009-11-24 16:13:54 -05002524 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002525 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002526 * - Home from workspace
2527 * - from center screen
2528 * - from other screens
2529 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002530 * - from center screen
2531 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002532 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002533 * - from center screen
2534 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002535 * - Launch app from workspace and quit
2536 * - with back
2537 * - with home
2538 * - Launch app from all apps and quit
2539 * - with back
2540 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002541 * - Go to a screen that's not the default, then all
2542 * apps, and launch and app, and go back
2543 * - with back
2544 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002545 * - On workspace, long press power and go back
2546 * - with back
2547 * - with home
2548 * - On all apps, long press power and go back
2549 * - with back
2550 * - with home
2551 * - On workspace, power off
2552 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002553 * - Launch an app and turn off the screen while in that app
2554 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002555 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002556 * - From all apps
2557 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002558 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2559 * - From all apps
2560 * - From the center workspace
2561 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002562 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002563 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002564 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002565 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002567 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002568 } else {
2569 mAllAppsGrid.zoom(0.0f, animated);
2570 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002571 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002572 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002573 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002574 }
2575
Joe Onorato7c312c12009-08-13 21:36:53 -07002576 void lockAllApps() {
2577 // TODO
2578 }
2579
2580 void unlockAllApps() {
2581 // TODO
2582 }
2583
Patrick Dubroy558654c2010-07-23 16:48:11 -07002584 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002585 return mHomeCustomizationDrawer != null &&
2586 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002587 }
2588
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002589 // Show the customization drawer (only exists in x-large configuration)
2590 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002591 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002592 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002593 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002595 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002596 }
2597
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 // Hide the customization drawer (only exists in x-large configuration)
2599 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002600 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002602 }
2603 }
2604
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002605 void onWorkspaceClick(CellLayout layout) {
2606 Object itemInfo = mAllAppsPagedView.getChosenItem();
2607 if (itemInfo == null) {
2608 itemInfo = mCustomizePagedView.getChosenItem();
Michael Jurka213d9632010-07-28 11:29:25 -07002609 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002610
2611 if (itemInfo == null) {
2612 // No items are chosen in All Apps or Customize, so just zoom into the workspace
2613 mWorkspace.unshrink(layout);
2614 if (isAllAppsVisible()) {
2615 closeAllApps(true);
2616 } else if (isCustomizationDrawerVisible()) {
2617 hideCustomizationDrawer(true);
2618 }
2619 } else {
2620 // Act as if the chosen item was dropped on the given CellLayout
2621 if (mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2622 mAllAppsPagedView.endChoiceMode();
2623 mCustomizePagedView.endChoiceMode();
2624 } else {
2625 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
2626 }
Michael Jurka213d9632010-07-28 11:29:25 -07002627 }
2628 }
2629
Joe Onorato7404ee42009-07-31 11:54:44 -07002630 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002631 * Sets the app market icon (shown when all apps is visible on x-large screens)
2632 */
2633 private void updateAppMarketIcon() {
2634 if (LauncherApplication.isScreenXLarge()) {
2635 // Find the app market activity by resolving an intent.
2636 // (If multiple app markets are installed, it will return the ResolverActivity.)
2637 PackageManager packageManager = getPackageManager();
2638 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2639 ComponentName activityName = intent.resolveActivity(getPackageManager());
2640 if (activityName != null) {
2641 mAppMarketIntent = intent;
2642 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2643 Drawable toolbarIcon = null;
2644 try {
2645 // Look for the toolbar icon specified in the activity meta-data
2646 Bundle metaData = packageManager.getActivityInfo(
2647 activityName, PackageManager.GET_META_DATA).metaData;
2648 if (metaData != null) {
2649 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2650 if (iconResId != 0) {
2651 Resources res = packageManager.getResourcesForActivity(activityName);
2652 toolbarIcon = res.getDrawable(iconResId);
2653 }
2654 }
2655 } catch (NameNotFoundException e) {
2656 // Do nothing
2657 }
2658 // If we were unable to find the icon via the meta-data, use a generic one
2659 if (toolbarIcon == null) {
2660 marketButton.setImageResource(R.drawable.app_market_generic);
2661 } else {
2662 marketButton.setImageDrawable(toolbarIcon);
2663 }
2664 } else {
2665 mAppMarketIntent = null;
2666 }
2667 }
2668 }
2669
2670 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 * Displays the shortcut creation dialog and launches, if necessary, the
2672 * appropriate activity.
2673 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002674 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002675 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2676 DialogInterface.OnShowListener {
2677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2684 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002685 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 builder.setInverseBackgroundForced(true);
2688
2689 AlertDialog dialog = builder.create();
2690 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002691 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002692 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 return dialog;
2695 }
2696
2697 public void onCancel(DialogInterface dialog) {
2698 mWaitingForResult = false;
2699 cleanup();
2700 }
2701
Romain Guycbb89e42009-06-08 15:52:54 -07002702 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002703 }
2704
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002706 try {
2707 dismissDialog(DIALOG_CREATE_SHORTCUT);
2708 } catch (Exception e) {
2709 // An exception is thrown if the dialog is not visible, which is fine
2710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 }
2712
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002713 /**
2714 * Handle the action clicked in the "Add to home" dialog.
2715 */
2716 public void onClick(DialogInterface dialog, int which) {
2717 Resources res = getResources();
2718 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002719
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002720 switch (which) {
2721 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002722 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002723 break;
2724 }
Romain Guycbb89e42009-06-08 15:52:54 -07002725
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002726 case AddAdapter.ITEM_APPWIDGET: {
2727 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002728
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002729 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2730 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002731 // start the pick activity
2732 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2733 break;
2734 }
Romain Guycbb89e42009-06-08 15:52:54 -07002735
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002736 case AddAdapter.ITEM_LIVE_FOLDER: {
2737 // Insert extra item to handle inserting folder
2738 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002739
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002740 ArrayList<String> shortcutNames = new ArrayList<String>();
2741 shortcutNames.add(res.getString(R.string.group_folder));
2742 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002743
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002744 ArrayList<ShortcutIconResource> shortcutIcons =
2745 new ArrayList<ShortcutIconResource>();
2746 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2747 R.drawable.ic_launcher_folder));
2748 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2749
2750 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2751 pickIntent.putExtra(Intent.EXTRA_INTENT,
2752 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2753 pickIntent.putExtra(Intent.EXTRA_TITLE,
2754 getText(R.string.title_select_live_folder));
2755 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002756
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002757 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2758 break;
2759 }
2760
2761 case AddAdapter.ITEM_WALLPAPER: {
2762 startWallpaper();
2763 break;
2764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 }
2766 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002767
2768 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002769 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 }
2772
2773 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002774 * Receives notifications when applications are added/removed.
2775 */
2776 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2777 @Override
2778 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002779 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002780 String reason = intent.getStringExtra("reason");
2781 if (!"homekey".equals(reason)) {
2782 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002783 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002784 animate = false;
2785 }
Joe Onoratob2061212009-11-24 16:13:54 -05002786 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002787 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002788 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002789 }
2790 }
2791
2792 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002793 * Receives notifications whenever the appwidgets are reset.
2794 */
2795 private class AppWidgetResetObserver extends ContentObserver {
2796 public AppWidgetResetObserver() {
2797 super(new Handler());
2798 }
2799
2800 @Override
2801 public void onChange(boolean selfChange) {
2802 onAppWidgetReset();
2803 }
2804 }
2805
2806 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002807 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002808 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002809 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002810 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002811 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002812 } else {
2813 return SCREEN_COUNT / 2;
2814 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002815 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002816
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002817 void setAllAppsPagedView(PagedView view) {
2818 mAllAppsPagedView = view;
2819 }
2820
Joe Onorato9c1289c2009-08-17 11:03:03 -04002821 /**
2822 * Refreshes the shortcuts shown on the workspace.
2823 *
2824 * Implementation of the method from LauncherModel.Callbacks.
2825 */
2826 public void startBinding() {
2827 final Workspace workspace = mWorkspace;
2828 int count = workspace.getChildCount();
2829 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002830 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002831 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2832 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002833
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 if (DEBUG_USER_INTERFACE) {
2835 android.widget.Button finishButton = new android.widget.Button(this);
2836 finishButton.setText("Finish");
2837 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2838
2839 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2840 public void onClick(View v) {
2841 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844 }
2845 }
2846
2847 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 * Bind the items start-end from the list.
2849 *
2850 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2853
2854 final Workspace workspace = mWorkspace;
2855
2856 for (int i=start; i<end; i++) {
2857 final ItemInfo item = shortcuts.get(i);
2858 mDesktopItems.add(item);
2859 switch (item.itemType) {
2860 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2861 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002862 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002863 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2864 false);
2865 break;
2866 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2867 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002868 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 (UserFolderInfo) item);
2870 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2871 false);
2872 break;
2873 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2874 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2875 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002876 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 (LiveFolderInfo) item);
2878 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2879 false);
2880 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 }
2883
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885 }
2886
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 /**
2888 * Implementation of the method from LauncherModel.Callbacks.
2889 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002890 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002891 sFolders.clear();
2892 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894
2895 /**
2896 * Add the views for a widget to the workspace.
2897 *
2898 * Implementation of the method from LauncherModel.Callbacks.
2899 */
2900 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002901 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2902 if (DEBUG_WIDGETS) {
2903 Log.d(TAG, "bindAppWidget: " + item);
2904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002905 final Workspace workspace = mWorkspace;
2906
2907 final int appWidgetId = item.appWidgetId;
2908 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002909 if (DEBUG_WIDGETS) {
2910 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2911 }
2912
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2914
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2916 item.hostView.setTag(item);
2917
2918 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2919 item.cellY, item.spanX, item.spanY, false);
2920
2921 workspace.requestLayout();
2922
2923 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002924
2925 if (DEBUG_WIDGETS) {
2926 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2927 + (SystemClock.uptimeMillis()-start) + "ms");
2928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 }
2930
2931 /**
2932 * Callback saying that there aren't any more items to bind.
2933 *
2934 * Implementation of the method from LauncherModel.Callbacks.
2935 */
2936 public void finishBindingItems() {
2937 if (mSavedState != null) {
2938 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002939 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 }
2941
2942 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2943 if (userFolders != null) {
2944 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002945 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 if (info != null) {
2947 openFolder(info);
2948 }
2949 }
2950 final Folder openFolder = mWorkspace.getOpenFolder();
2951 if (openFolder != null) {
2952 openFolder.requestFocus();
2953 }
2954 }
2955
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 mSavedState = null;
2957 }
2958
2959 if (mSavedInstanceState != null) {
2960 super.onRestoreInstanceState(mSavedInstanceState);
2961 mSavedInstanceState = null;
2962 }
2963
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 mWorkspaceLoading = false;
2965 }
2966
2967 /**
2968 * Add the icons for all apps.
2969 *
2970 * Implementation of the method from LauncherModel.Callbacks.
2971 */
2972 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002973 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002974 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 }
2976
2977 /**
2978 * A package was installed.
2979 *
2980 * Implementation of the method from LauncherModel.Callbacks.
2981 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002982 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002984 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002985 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 }
2987
2988 /**
2989 * A package was updated.
2990 *
2991 * Implementation of the method from LauncherModel.Callbacks.
2992 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002993 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002995 mWorkspace.updateShortcuts(apps);
2996 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002997 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 }
2999
3000 /**
3001 * A package was uninstalled.
3002 *
3003 * Implementation of the method from LauncherModel.Callbacks.
3004 */
Joe Onorato36115782010-06-17 13:28:48 -04003005 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003006 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003007 if (permanent) {
3008 mWorkspace.removeItems(apps);
3009 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003010 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003011 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003012 }
Joe Onoratobe386092009-11-17 17:32:16 -08003013
3014 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003015 * A number of packages were updated.
3016 */
3017 public void bindPackagesUpdated() {
3018 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07003019 if (mCustomizePagedView != null)
3020 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07003021 }
3022
3023 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003024 * Prints out out state for debugging.
3025 */
3026 public void dumpState() {
3027 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003028 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003029 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3030 Log.d(TAG, "mRestoring=" + mRestoring);
3031 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3032 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3033 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003034 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003035 mModel.dumpState();
3036 mAllAppsGrid.dumpState();
3037 Log.d(TAG, "END launcher2 dump state");
3038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039}