blob: a6f0a0e01fd41e5ea220300af894b155e50141ff [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
Winson Chungaafa03c2010-06-11 17:34:16 -0700377 @Override
378 public void onConfigurationChanged(Configuration newConfig) {
379 // TODO Auto-generated method stub
380 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700381 }
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700384 if (sLocaleConfiguration == null) {
385 new AsyncTask<Void, Void, LocaleConfiguration>() {
386 @Override
387 protected LocaleConfiguration doInBackground(Void... unused) {
388 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
389 readConfiguration(Launcher.this, localeConfiguration);
390 return localeConfiguration;
391 }
392
393 @Override
394 protected void onPostExecute(LocaleConfiguration result) {
395 sLocaleConfiguration = result;
396 checkForLocaleChange(); // recursive, but now with a locale configuration
397 }
398 }.execute();
399 return;
400 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 final Configuration configuration = getResources().getConfiguration();
403
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 final String locale = configuration.locale.toString();
406
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700407 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 final int mcc = configuration.mcc;
409
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700410 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 final int mnc = configuration.mnc;
412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414
Romain Guy84f296c2009-11-04 15:00:44 -0800415 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416 sLocaleConfiguration.locale = locale;
417 sLocaleConfiguration.mcc = mcc;
418 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700419
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400421 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422
423 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
424 new Thread("WriteLocaleConfiguration") {
425 public void run() {
426 writeConfiguration(Launcher.this, localeConfiguration);
427 }
428 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700429 }
430 }
431
432 private static class LocaleConfiguration {
433 public String locale;
434 public int mcc = -1;
435 public int mnc = -1;
436 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700437
Romain Guy98d01652009-06-30 16:21:04 -0700438 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
439 DataInputStream in = null;
440 try {
441 in = new DataInputStream(context.openFileInput(PREFERENCES));
442 configuration.locale = in.readUTF();
443 configuration.mcc = in.readInt();
444 configuration.mnc = in.readInt();
445 } catch (FileNotFoundException e) {
446 // Ignore
447 } catch (IOException e) {
448 // Ignore
449 } finally {
450 if (in != null) {
451 try {
452 in.close();
453 } catch (IOException e) {
454 // Ignore
455 }
456 }
457 }
458 }
459
460 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
461 DataOutputStream out = null;
462 try {
463 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
464 out.writeUTF(configuration.locale);
465 out.writeInt(configuration.mcc);
466 out.writeInt(configuration.mnc);
467 out.flush();
468 } catch (FileNotFoundException e) {
469 // Ignore
470 } catch (IOException e) {
471 //noinspection ResultOfMethodCallIgnored
472 context.getFileStreamPath(PREFERENCES).delete();
473 } finally {
474 if (out != null) {
475 try {
476 out.close();
477 } catch (IOException e) {
478 // Ignore
479 }
480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482 }
483
484 static int getScreen() {
485 synchronized (sLock) {
486 return sScreen;
487 }
488 }
489
490 static void setScreen(int screen) {
491 synchronized (sLock) {
492 sScreen = screen;
493 }
494 }
495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700497 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
499 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700500 // TODO: Put back when we decide about scrolling the wallpaper
501 // boolean isPortrait = display.getWidth() < display.getHeight();
502 // final int width = isPortrait ? display.getWidth() : display.getHeight();
503 // final int height = isPortrait ? display.getHeight() : display.getWidth();
504 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
505 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 }
507
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 // Note: This doesn't do all the client-id magic that BrowserProvider does
509 // in Browser. (http://b/2425179)
510 private Uri getDefaultBrowserUri() {
511 String url = getString(R.string.default_browser_url);
512 if (url.indexOf("{CID}") != -1) {
513 url = url.replace("{CID}", "android-google");
514 }
515 return Uri.parse(url);
516 }
517
518 // Load the Intent templates from arrays.xml to populate the hotseats. For
519 // each Intent, if it resolves to a single app, use that as the launch
520 // intent & use that app's label as the contentDescription. Otherwise,
521 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400522 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400523 if (mHotseatConfig == null) {
524 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
525 if (mHotseatConfig.length > 0) {
526 mHotseats = new Intent[mHotseatConfig.length];
527 mHotseatLabels = new CharSequence[mHotseatConfig.length];
528 mHotseatIcons = new Drawable[mHotseatConfig.length];
529 } else {
530 mHotseats = null;
531 mHotseatIcons = null;
532 mHotseatLabels = null;
533 }
534
535 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
536 for (int i=0; i<mHotseatConfig.length; i++) {
537 // load icon for this slot; currently unrelated to the actual activity
538 try {
539 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
540 } catch (ArrayIndexOutOfBoundsException ex) {
541 Log.w(TAG, "Missing hotseat_icons array item #" + i);
542 mHotseatIcons[i] = null;
543 }
544 }
545 hotseatIconDrawables.recycle();
546 }
547
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400548 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 for (int i=0; i<mHotseatConfig.length; i++) {
550 Intent intent = null;
551 if (mHotseatConfig[i].equals("*BROWSER*")) {
552 // magic value meaning "launch user's default web browser"
553 // replace it with a generic web request so we can see if there is indeed a default
554 String defaultUri = getString(R.string.default_browser_url);
555 intent = new Intent(
556 Intent.ACTION_VIEW,
557 ((defaultUri != null)
558 ? Uri.parse(defaultUri)
559 : getDefaultBrowserUri())
560 ).addCategory(Intent.CATEGORY_BROWSABLE);
561 // note: if the user launches this without a default set, she
562 // will always be taken to the default URL above; this is
563 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400565 // URL is unavoidably sent to the chosen app.
566 } else {
567 try {
568 intent = Intent.parseUri(mHotseatConfig[i], 0);
569 } catch (java.net.URISyntaxException ex) {
570 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
571 // bogus; leave intent=null
572 }
573 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 if (intent == null) {
576 mHotseats[i] = null;
577 mHotseatLabels[i] = getText(R.string.activity_not_found);
578 continue;
579 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580
581 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700582 Log.d(TAG, "loadHotseats: hotseat " + i
583 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400585 + "]");
586 }
587
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
589 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700590 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 Log.d(TAG, "Best match for intent: " + bestMatch);
592 Log.d(TAG, "All matches: ");
593 for (ResolveInfo ri : allMatches) {
594 Log.d(TAG, " --> " + ri);
595 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400596 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400597 // did this resolve to a single app, or the resolver?
598 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 // can't find any activity to handle this. let's leave the
600 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400601 // to launch.
602 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400603
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400604 // set accessibility text to "Not installed"
605 mHotseatLabels[i] = getText(R.string.activity_not_found);
606 } else {
607 boolean found = false;
608 for (ResolveInfo ri : allMatches) {
609 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
610 && bestMatch.activityInfo.applicationInfo.packageName
611 .equals(ri.activityInfo.applicationInfo.packageName)) {
612 found = true;
613 break;
614 }
615 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700616
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400617 if (!found) {
618 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
619 // the bestMatch is probably the ResolveActivity, meaning the
620 // user has not yet selected a default
621 // so: we'll keep the original intent for now
622 mHotseats[i] = intent;
623
624 // set the accessibility text to "Select shortcut"
625 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
626 } else {
627 // we have an app!
628 // now reconstruct the intent to launch it through the front
629 // door
630 ComponentName com = new ComponentName(
631 bestMatch.activityInfo.applicationInfo.packageName,
632 bestMatch.activityInfo.name);
633 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
634
635 // load the app label for accessibility
636 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
637 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638 }
639
640 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 Log.d(TAG, "loadHotseats: hotseat " + i
642 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400643 + ((mHotseats[i] == null)
644 ? "null"
645 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400646 + "] label=[" + mHotseatLabels[i]
647 + "]"
648 );
649 }
650 }
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 @Override
654 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700655 mWaitingForResult = false;
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // The pattern used here is that a user PICKs a specific application,
658 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
661 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700662
Romain Guy94dabf12009-07-21 10:55:43 -0700663 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 switch (requestCode) {
665 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 break;
668 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800669 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 break;
671 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 break;
674 case REQUEST_PICK_LIVE_FOLDER:
675 addLiveFolder(data);
676 break;
677 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700681 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700685 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700687 case REQUEST_PICK_WALLPAPER:
688 // We just wanted the activity result here so we can clear mWaitingForResult
689 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
Romain Guy18042c82009-11-06 11:44:55 -0800691 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
692 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
693 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700694 // Clean up the appWidgetId if we canceled
695 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
696 if (appWidgetId != -1) {
697 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 }
701
702 @Override
703 protected void onResume() {
704 super.onResume();
705
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800706 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 mWorkspaceLoading = true;
710 mModel.startLoader(this, true);
711 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700713 // When we resume Launcher, a different Activity might be responsible for the app
714 // market intent, so refresh the icon
715 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
718 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 protected void onPause() {
720 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700721 // Some launcher layouts don't have a previous and next view
722 if (mPreviousView != null) {
723 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700724 }
725 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 dismissPreview(mNextView);
727 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800728 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 }
Romain Guycbb89e42009-06-08 15:52:54 -0700730
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700731 @Override
732 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 // Flag the loader to stop early before switching
734 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800735 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800736 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700737 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800739 // We can't hide the IME if it was forced open. So don't bother
740 /*
741 @Override
742 public void onWindowFocusChanged(boolean hasFocus) {
743 super.onWindowFocusChanged(hasFocus);
744
745 if (hasFocus) {
746 final InputMethodManager inputManager = (InputMethodManager)
747 getSystemService(Context.INPUT_METHOD_SERVICE);
748 WindowManager.LayoutParams lp = getWindow().getAttributes();
749 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
750 android.os.Handler()) {
751 protected void onReceiveResult(int resultCode, Bundle resultData) {
752 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
753 }
754 });
755 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
756 }
757 }
758 */
759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 private boolean acceptFilter() {
761 final InputMethodManager inputManager = (InputMethodManager)
762 getSystemService(Context.INPUT_METHOD_SERVICE);
763 return !inputManager.isFullscreenMode();
764 }
765
766 @Override
767 public boolean onKeyDown(int keyCode, KeyEvent event) {
768 boolean handled = super.onKeyDown(keyCode, event);
769 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
770 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
771 keyCode, event);
772 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700773 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700774 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700775 // showSearchDialog()
776 // If there are multiple keystrokes before the search dialog takes focus,
777 // onSearchRequested() will be called for every keystroke,
778 // but it is idempotent, so it's fine.
779 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 }
781 }
782
Joe Onorato8a9625e2010-01-28 15:55:35 -0800783 // Eat the long press event so the keyboard doesn't come up.
784 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
785 return true;
786 }
787
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 return handled;
789 }
790
Karl Rosaen138a0412009-04-23 19:00:21 -0700791 private String getTypedText() {
792 return mDefaultKeySsb.toString();
793 }
794
795 private void clearTypedText() {
796 mDefaultKeySsb.clear();
797 mDefaultKeySsb.clearSpans();
798 Selection.setSelection(mDefaultKeySsb, 0);
799 }
800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 /**
802 * Restores the previous state, if it exists.
803 *
804 * @param savedState The previous state.
805 */
806 private void restoreState(Bundle savedState) {
807 if (savedState == null) {
808 return;
809 }
810
Joe Onorato3a8820b2009-11-10 15:06:42 -0800811 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
812 if (allApps) {
813 showAllApps(false);
814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
817 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700818 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
820
821 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
822 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700823 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 mAddItemCellInfo = new CellLayout.CellInfo();
825 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
826 addItemCellInfo.valid = true;
827 addItemCellInfo.screen = addScreen;
828 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
829 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
830 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
831 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700832 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
834 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
835 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
836 mRestoring = true;
837 }
838
839 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
840 if (renameFolder) {
841 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700842 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 mRestoring = true;
844 }
845 }
846
847 /**
848 * Finds all the views we need and configure them properly.
849 */
850 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700851 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400852
Romain Guyb1b69f52009-08-10 15:10:15 -0700853 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400854 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855
Joe Onorato7c312c12009-08-13 21:36:53 -0700856 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700857 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700858 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800859 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700860 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700861 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700862
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700863 if (LauncherApplication.isScreenXLarge()) {
864 // They need to be INVISIBLE initially so that they will be measured in the layout.
865 // Otherwise the animations are messed up when we show them for the first time.
866 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
867 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
868 }
869
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
871 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500872 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700873
Joe Onorato7c312c12009-08-13 21:36:53 -0700874 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
875 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700877 View handleView = findViewById(R.id.all_apps_button);
878 if (handleView != null && handleView instanceof HandleView) {
879 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700880 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700881 mHandleView.setLauncher(this);
882 mHandleView.setOnClickListener(this);
883 mHandleView.setOnLongClickListener(this);
884 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800885
Winson Chung80baf5a2010-08-09 16:03:15 -0700886 if (mCustomizePagedView != null) {
887 mCustomizePagedView.setLauncher(this);
888 mCustomizePagedView.setDragController(dragController);
889 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700890 } else {
891 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
892 hotseatLeft.setContentDescription(mHotseatLabels[0]);
893 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
894 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
895 hotseatRight.setContentDescription(mHotseatLabels[1]);
896 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400897
Michael Jurkaaf442092010-06-10 17:01:57 -0700898 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
899 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800900
Michael Jurkaaf442092010-06-10 17:01:57 -0700901 Drawable previous = mPreviousView.getDrawable();
902 Drawable next = mNextView.getDrawable();
903 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904
Michael Jurkaaf442092010-06-10 17:01:57 -0700905 mPreviousView.setHapticFeedbackEnabled(false);
906 mPreviousView.setOnLongClickListener(this);
907 mNextView.setHapticFeedbackEnabled(false);
908 mNextView.setOnLongClickListener(this);
909 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400912 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914
915 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400916 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700917 int deleteZoneHandleId;
918 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700919 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700920 } else {
921 deleteZoneHandleId = R.id.all_apps_button_cluster;
922 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700923 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700924 dragController.addDragListener(deleteZone);
925
926 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
927 if (infoButton != null) {
928 infoButton.setLauncher(this);
929 infoButton.setHandle(findViewById(R.id.configure_button));
930 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
931 dragController.addDragListener(infoButton);
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933
Joe Onorato00acb122009-08-04 16:04:30 -0400934 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400935 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800936 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700937
Joe Onorato00acb122009-08-04 16:04:30 -0400938 // The order here is bottom to top.
939 dragController.addDropTarget(workspace);
940 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700941 if (infoButton != null) {
942 dragController.addDropTarget(infoButton);
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
Romain Guy8a73c512009-11-09 19:19:59 -0800946 @SuppressWarnings({"UnusedDeclaration"})
947 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800948 if (!isAllAppsVisible()) {
949 mWorkspace.scrollLeft();
950 }
Romain Guy8a73c512009-11-09 19:19:59 -0800951 }
952
953 @SuppressWarnings({"UnusedDeclaration"})
954 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800955 if (!isAllAppsVisible()) {
956 mWorkspace.scrollRight();
957 }
Romain Guy8a73c512009-11-09 19:19:59 -0800958 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400959
960 @SuppressWarnings({"UnusedDeclaration"})
961 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400962 if (isAllAppsVisible()) return;
963
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400964 int index = -1;
965 if (v.getId() == R.id.hotseat_left) {
966 index = 0;
967 } else if (v.getId() == R.id.hotseat_right) {
968 index = 1;
969 }
970
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400971 // reload these every tap; you never know when they might change
972 loadHotseats();
973 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
974 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400975 startActivitySafely(
976 mHotseats[index],
977 "hotseat"
978 );
979 }
980 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 /**
983 * Creates a view representing a shortcut.
984 *
985 * @param info The data structure describing the shortcut.
986 *
987 * @return A View inflated from R.layout.application.
988 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800989 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700991 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993
994 /**
995 * Creates a view representing a shortcut inflated from the specified resource.
996 *
997 * @param layoutResId The id of the XML layout used to create the shortcut.
998 * @param parent The group the shortcut belongs to.
999 * @param info The data structure describing the shortcut.
1000 *
1001 * @return A View inflated from layoutResId.
1002 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001003 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1005
Joe Onorato0589f0f2010-02-08 13:44:00 -08001006 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1007 new FastBitmapDrawable(info.getIcon(mIconCache)),
1008 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 favorite.setText(info.title);
1010 favorite.setTag(info);
1011 favorite.setOnClickListener(this);
1012
1013 return favorite;
1014 }
1015
1016 /**
1017 * Add an application shortcut to the workspace.
1018 *
1019 * @param data The intent describing the application.
1020 * @param cellInfo The position on screen where to create the shortcut.
1021 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001023 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 if (!findSingleSlot(cellInfo)) return;
1025
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1027 data, context);
1028
Romain Guy73b979d2009-06-09 12:57:21 -07001029 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001030 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032 info.container = ItemInfo.NO_ID;
1033 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1034 } else {
1035 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037 }
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 /**
1040 * Add a shortcut to the workspace.
1041 *
1042 * @param data The intent describing the shortcut.
1043 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001046 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Joe Onorato0589f0f2010-02-08 13:44:00 -08001049 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050
1051 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1054 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056 }
1057
Romain Guycbb89e42009-06-08 15:52:54 -07001058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001060 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001062 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001065 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001067
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001070 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073 // We have saved the position to which the widget was dragged-- this really only matters
1074 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001076
1077 // For now, we don't save the coordinate where we dropped the icon because we're not
1078 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1079 // a widget on the home screen in case we want to add it in the future
1080 final int[] xyTouch = null;
1081 //final int[] xyTouch = mAddItemCoordinates;
1082 boolean findNearestVacantAreaFailed = false;
1083 if (xyTouch != null) {
1084 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1085 int[] result = screen.findNearestVacantArea(
1086 mAddItemCoordinates[0], mAddItemCoordinates[1],
1087 spans[0], spans[1], cellInfo, xy);
1088 findNearestVacantAreaFailed = (result == null);
1089 }
1090
1091 if (findNearestVacantAreaFailed ||
1092 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001093 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1094 return;
1095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 // Build Launcher-specific widget info and save to database
1098 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 launcherInfo.spanX = spans[0];
1100 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 LauncherModel.addItemToDatabase(this, launcherInfo,
1103 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001104 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105
1106 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001107 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Michael Jurkaa63c4522010-08-19 13:52:27 -07001115 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1121 mDesktopItems.remove(launcherInfo);
1122 launcherInfo.hostView = null;
1123 }
1124
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001125 public LauncherAppWidgetHost getAppWidgetHost() {
1126 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001129 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001130 getWindow().closeAllPanels();
1131
1132 try {
1133 dismissDialog(DIALOG_CREATE_SHORTCUT);
1134 // Unlock the workspace if the dialog was showing
1135 } catch (Exception e) {
1136 // An exception is thrown if the dialog is not visible, which is fine
1137 }
1138
1139 try {
1140 dismissDialog(DIALOG_RENAME_FOLDER);
1141 // Unlock the workspace if the dialog was showing
1142 } catch (Exception e) {
1143 // An exception is thrown if the dialog is not visible, which is fine
1144 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001145
1146 // Whatever we were doing is hereby canceled.
1147 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001148 }
1149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 @Override
1151 protected void onNewIntent(Intent intent) {
1152 super.onNewIntent(intent);
1153
1154 // Close the menu
1155 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001156 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001157 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001158
Joe Onorato14f122b2009-11-19 14:06:36 -08001159 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1160 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1161 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001162 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1163
Michael Jurka4cb37242010-08-09 21:05:32 -07001164 // in all these cases, only animate if we're already on home
1165 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001166 if (alreadyOnHome && !mWorkspace.isSmall() &&
1167 !allAppsVisible && !customizationDrawerVisible) {
1168 mWorkspace.shrinkToMiddle();
1169 } else {
1170 mWorkspace.unshrink(alreadyOnHome);
1171 }
Michael Jurka0142d492010-08-25 17:46:15 -07001172 } else if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001173 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001174 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001175 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001176 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001177 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001178
Joe Onorato3a8820b2009-11-10 15:06:42 -08001179 final View v = getWindow().peekDecorView();
1180 if (v != null && v.getWindowToken() != null) {
1181 InputMethodManager imm = (InputMethodManager)getSystemService(
1182 INPUT_METHOD_SERVICE);
1183 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 }
1185 }
1186 }
1187
1188 @Override
1189 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1190 // Do not call super here
1191 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001192
1193 if (mHomeCustomizationDrawer != null) {
1194 String cur = savedInstanceState.getString("currentTab");
1195 if (cur != null) {
1196 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1197 }
1198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200
1201 @Override
1202 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001203 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204
1205 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1206 if (folders.size() > 0) {
1207 final int count = folders.size();
1208 long[] ids = new long[count];
1209 for (int i = 0; i < count; i++) {
1210 final FolderInfo info = folders.get(i).getInfo();
1211 ids[i] = info.id;
1212 }
1213 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1214 } else {
1215 super.onSaveInstanceState(outState);
1216 }
1217
Joe Onoratofb0ca672009-09-14 17:55:46 -04001218 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001219 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222
1223 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1224 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1225 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1226
1227 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1228 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1229 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1230 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1231 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1232 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1233 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1234 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001235 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 }
1237
1238 if (mFolderInfo != null && mWaitingForResult) {
1239 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1240 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1241 }
Michael Jurka946ad472010-07-09 18:05:18 -07001242
1243 if (mHomeCustomizationDrawer != null) {
1244 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1245 if (currentTabTag != null) {
1246 outState.putString("currentTab", currentTabTag);
1247 }
1248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
1251 @Override
1252 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001258 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
1261 TextKeyListener.getInstance().release();
1262
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001266
1267 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001268
Patrick Dubroyab962b72010-07-26 12:10:10 -07001269 // Some launcher layouts don't have a previous and next view
1270 if (mPreviousView != null) {
1271 dismissPreview(mPreviousView);
1272 }
1273 if (mNextView != null) {
1274 dismissPreview(mNextView);
1275 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001276
1277 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279
1280 @Override
1281 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001282 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 super.startActivityForResult(intent, requestCode);
1284 }
1285
1286 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001287 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001289
Joe Onorato7bb17492009-09-24 17:51:01 -07001290 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001291
Karl Rosaen138a0412009-04-23 19:00:21 -07001292 if (initialQuery == null) {
1293 // Use any text typed in the launcher as the initial query
1294 initialQuery = getTypedText();
1295 clearTypedText();
1296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 if (appSearchData == null) {
1298 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001299 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
Romain Guycbb89e42009-06-08 15:52:54 -07001301
Karl Rosaen138a0412009-04-23 19:00:21 -07001302 final SearchManager searchManager =
1303 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001304 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001305 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
1308 @Override
1309 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001310 if (isWorkspaceLocked() || LauncherApplication.isScreenXLarge()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001311 return false;
1312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
1314 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1317 .setIcon(android.R.drawable.ic_menu_add)
1318 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001319 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 .setIcon(android.R.drawable.ic_menu_gallery)
1321 .setAlphabeticShortcut('W');
1322 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1323 .setIcon(android.R.drawable.ic_search_category_default)
1324 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1325 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1326 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1327 .setAlphabeticShortcut('N');
1328
1329 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001330 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1331 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332
1333 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1334 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1335 .setIntent(settings);
1336
1337 return true;
1338 }
1339
1340 @Override
1341 public boolean onPrepareOptionsMenu(Menu menu) {
1342 super.onPrepareOptionsMenu(menu);
1343
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001344 // If all apps is animating, don't show the menu, because we don't know
1345 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001346 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001347 return false;
1348 }
1349
1350 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001351 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001352 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1353 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1354
1355 // Disable add if the workspace is full.
1356 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001357 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001358 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360
1361 return true;
1362 }
1363
Michael Jurka0e260592010-06-30 17:07:39 -07001364 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1365 // using the settings menu to add an item, something similar happens in showAddDialog
1366 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001367 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001368 mAddItemCellInfo = mMenuAddInfo;
1369 }
1370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 @Override
1372 public boolean onOptionsItemSelected(MenuItem item) {
1373 switch (item.getItemId()) {
1374 case MENU_ADD:
1375 addItems();
1376 return true;
1377 case MENU_WALLPAPER_SETTINGS:
1378 startWallpaper();
1379 return true;
1380 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001381 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 return true;
1383 case MENU_NOTIFICATIONS:
1384 showNotifications();
1385 return true;
1386 }
1387
1388 return super.onOptionsItemSelected(item);
1389 }
Romain Guycbb89e42009-06-08 15:52:54 -07001390
Karl Rosaen138a0412009-04-23 19:00:21 -07001391 /**
1392 * Indicates that we want global search for this activity by setting the globalSearch
1393 * argument for {@link #startSearch} to true.
1394 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001396 @Override
1397 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001398 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001399 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001400 }
1401
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 public boolean isWorkspaceLocked() {
1403 return mWorkspaceLoading || mWaitingForResult;
1404 }
1405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001407 if (LauncherApplication.isScreenXLarge()) {
1408 // Animate the widget chooser up from the bottom of the screen
1409 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001410 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001411 }
1412 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001413 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001414 showAddDialog(mMenuAddInfo);
1415 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
Michael Jurkaa63c4522010-08-19 13:52:27 -07001418 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1419 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001420 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001421
1422 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1423 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001424 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1425 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1426 addAppWidgetImpl(appWidgetId);
1427 }
1428
1429 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001430 // TODO: catch bad widget exception when sent
1431 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001432 // TODO: Is this log message meaningful?
1433 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1434 addAppWidgetImpl(appWidgetId);
1435 }
1436
1437 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001438 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439
Bjorn Bringert7984c942009-12-09 15:38:25 +00001440 if (appWidget.configure != null) {
1441 // Launch over to configure widget, if needed
1442 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1443 intent.setComponent(appWidget.configure);
1444 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1445
Romain Guy8e633c52010-03-04 12:51:36 -08001446 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001448 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001449 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451 }
Romain Guycbb89e42009-06-08 15:52:54 -07001452
Joe Onoratodeb98af2010-02-19 14:59:39 -08001453 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001454 // Handle case where user selected "Applications"
1455 String applicationName = getResources().getString(R.string.group_applications);
1456 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001457
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001458 if (applicationName != null && applicationName.equals(shortcutName)) {
1459 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1460 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001461
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001462 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1463 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001464 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001465 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001466 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469
1470 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001471 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001472 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001473 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001474
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001475 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001476 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001477 } else {
1478 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481
Joe Onorato9c1289c2009-08-17 11:03:03 -04001482 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 UserFolderInfo folderInfo = new UserFolderInfo();
1484 folderInfo.title = getText(R.string.folder_name);
1485
1486 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001487 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 if (!findSingleSlot(cellInfo)) return;
1489
1490 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 LauncherModel.addItemToDatabase(this, folderInfo,
1492 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001493 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001494 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
1496 // Create the view
1497 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001498 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001504 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505 }
1506
1507 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001508 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 if (!findSingleSlot(cellInfo)) return;
1510
1511 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1512
1513 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001515 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001516 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1517 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
1519 }
1520
1521 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1522 CellLayout.CellInfo cellInfo, boolean notify) {
1523
1524 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1525 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1526
1527 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 Intent.ShortcutIconResource iconResource = null;
1529
1530 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1531 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1532 try {
1533 iconResource = (Intent.ShortcutIconResource) extra;
1534 final PackageManager packageManager = context.getPackageManager();
1535 Resources resources = packageManager.getResourcesForApplication(
1536 iconResource.packageName);
1537 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1538 icon = resources.getDrawable(id);
1539 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001540 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542 }
1543
1544 if (icon == null) {
1545 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1546 }
1547
1548 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001549 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 info.title = name;
1551 info.iconResource = iconResource;
1552 info.uri = data.getData();
1553 info.baseIntent = baseIntent;
1554 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1555 LiveFolders.DISPLAY_MODE_GRID);
1556
1557 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1558 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001559 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 return info;
1562 }
1563
1564 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1565 final int[] xy = new int[2];
1566 if (findSlot(cellInfo, xy, 1, 1)) {
1567 cellInfo.cellX = xy[0];
1568 cellInfo.cellY = xy[1];
1569 return true;
1570 }
1571 return false;
1572 }
1573
1574 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1575 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001576 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1577 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1579 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1580 return false;
1581 }
1582 }
1583 return true;
1584 }
1585
1586 private void showNotifications() {
1587 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1588 if (statusBar != null) {
1589 statusBar.expand();
1590 }
1591 }
1592
1593 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001594 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001596 Intent chooser = Intent.createChooser(pickWallpaper,
1597 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001598 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1599 // Removed in Eclair MR1
1600// WallpaperManager wm = (WallpaperManager)
1601// getSystemService(Context.WALLPAPER_SERVICE);
1602// WallpaperInfo wi = wm.getWallpaperInfo();
1603// if (wi != null && wi.getSettingsActivity() != null) {
1604// LabeledIntent li = new LabeledIntent(getPackageName(),
1605// R.string.configure_wallpaper, 0);
1606// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1607// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1608// }
Mike Clerona0618e42009-10-22 13:55:21 -07001609 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
1611
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001612 /**
1613 * Registers various content observers. The current implementation registers
1614 * only a favorites observer to keep track of the favorites applications.
1615 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001616 private void registerContentObservers() {
1617 ContentResolver resolver = getContentResolver();
1618 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1619 true, mWidgetObserver);
1620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 @Override
1623 public boolean dispatchKeyEvent(KeyEvent event) {
1624 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1625 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001627 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001628 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001629 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001630 dumpState();
1631 return true;
1632 }
1633 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001634 }
1635 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1636 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001637 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 return true;
1639 }
1640 }
1641
1642 return super.dispatchKeyEvent(event);
1643 }
1644
Joe Onorato88ec0992009-11-19 13:16:06 -08001645 @Override
1646 public void onBackPressed() {
1647 if (isAllAppsVisible()) {
1648 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001649 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001650 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001651 } else {
1652 closeFolder();
1653 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001654 // Some launcher layouts don't have a previous and next view
1655 if (mPreviousView != null) {
1656 dismissPreview(mPreviousView);
1657 dismissPreview(mNextView);
1658 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 private void closeFolder() {
1662 Folder folder = mWorkspace.getOpenFolder();
1663 if (folder != null) {
1664 closeFolder(folder);
1665 }
1666 }
1667
1668 void closeFolder(Folder folder) {
1669 folder.getInfo().opened = false;
1670 ViewGroup parent = (ViewGroup) folder.getParent();
1671 if (parent != null) {
1672 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001673 if (folder instanceof DropTarget) {
1674 // Live folders aren't DropTargets.
1675 mDragController.removeDropTarget((DropTarget)folder);
1676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 }
1678 folder.onClose();
1679 }
1680
1681 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001682 * Re-listen when widgets are reset.
1683 */
1684 private void onAppWidgetReset() {
1685 mAppWidgetHost.startListening();
1686 }
1687
1688 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001689 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1690 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692 private void unbindDesktopItems() {
1693 for (ItemInfo item: mDesktopItems) {
1694 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 }
1696 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001697
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 /**
1699 * Launches the intent referred by the clicked shortcut.
1700 *
1701 * @param v The view representing the clicked shortcut.
1702 */
1703 public void onClick(View v) {
1704 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001705 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001707 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001708 int[] pos = new int[2];
1709 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001710 intent.setSourceBounds(new Rect(pos[0], pos[1],
1711 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001712 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 } else if (tag instanceof FolderInfo) {
1714 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001715 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001716 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001717 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001718 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001719 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001720 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722 }
1723
Michael Jurka0e260592010-06-30 17:07:39 -07001724 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001725 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001726 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001727 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001728 return false;
1729 }
1730
Michael Jurkaaf442092010-06-10 17:01:57 -07001731 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001732 * Event handler for the search button
1733 *
1734 * @param v The view that was clicked.
1735 */
1736 public void onClickSearchButton(View v) {
1737 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1738 View button = findViewById(R.id.search_button);
1739 i.setSourceBounds(
1740 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1741 startActivity(i);
1742 }
1743
1744 /**
1745 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001746 * enters home screen customization mode.
1747 *
1748 * @param v The view that was clicked.
1749 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001750 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001751 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001752 }
1753
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001754 /**
1755 * Event handler for the "grid" button that appears on the home screen, which
1756 * enters all apps mode.
1757 *
1758 * @param v The view that was clicked.
1759 */
1760 public void onClickAllAppsButton(View v) {
1761 showAllApps(true);
1762 }
1763
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001764 public void onClickAppMarketButton(View v) {
1765 if (mAppMarketIntent != null) {
1766 startActivitySafely(mAppMarketIntent, "app market");
1767 }
1768 }
1769
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001770 void startApplicationDetailsActivity(ComponentName componentName) {
1771 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001772 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1773 Uri.fromParts("package", packageName, null));
1774 startActivity(intent);
1775 }
1776
Patrick Dubroy5539af72010-09-07 15:22:01 -07001777 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1778 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1779 // System applications cannot be installed. For now, show a toast explaining that.
1780 // We may give them the option of disabling apps this way.
1781 int messageId = R.string.uninstall_system_app_text;
1782 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1783 } else {
1784 String packageName = appInfo.componentName.getPackageName();
1785 String className = appInfo.componentName.getClassName();
1786 Intent intent = new Intent(
1787 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1788 startActivity(intent);
1789 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001790 }
1791
Joe Onoratof984e852010-03-25 09:47:45 -07001792 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1794 try {
1795 startActivity(intent);
1796 } catch (ActivityNotFoundException e) {
1797 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001798 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 } catch (SecurityException e) {
1800 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001801 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001803 "or use the exported attribute for this activity. "
1804 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001807
Romain Guy8e633c52010-03-04 12:51:36 -08001808 void startActivityForResultSafely(Intent intent, int requestCode) {
1809 try {
1810 startActivityForResult(intent, requestCode);
1811 } catch (ActivityNotFoundException e) {
1812 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1813 } catch (SecurityException e) {
1814 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1815 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1816 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1817 "or use the exported attribute for this activity.", e);
1818 }
1819 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820
1821 private void handleFolderClick(FolderInfo folderInfo) {
1822 if (!folderInfo.opened) {
1823 // Close any open folder
1824 closeFolder();
1825 // Open the requested folder
1826 openFolder(folderInfo);
1827 } else {
1828 // Find the open folder...
1829 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1830 int folderScreen;
1831 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001832 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 // .. and close it
1834 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001835 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 // Close any folder open on the current screen
1837 closeFolder();
1838 // Pull the folder onto this screen
1839 openFolder(folderInfo);
1840 }
1841 }
1842 }
1843 }
1844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 /**
1846 * Opens the user fodler described by the specified tag. The opening of the folder
1847 * is animated relative to the specified View. If the View is null, no animation
1848 * is played.
1849 *
1850 * @param folderInfo The FolderInfo describing the folder to open.
1851 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001852 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 Folder openFolder;
1854
1855 if (folderInfo instanceof UserFolderInfo) {
1856 openFolder = UserFolder.fromXml(this);
1857 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001858 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 } else {
1860 return;
1861 }
1862
Joe Onorato00acb122009-08-04 16:04:30 -04001863 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 openFolder.setLauncher(this);
1865
1866 openFolder.bind(folderInfo);
1867 folderInfo.opened = true;
1868
Winson Chungaafa03c2010-06-11 17:34:16 -07001869 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 openFolder.onOpen();
1872 }
1873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001875 switch (v.getId()) {
1876 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001877 if (!isAllAppsVisible()) {
1878 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1879 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001880 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001881 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001882 return true;
1883 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001884 if (!isAllAppsVisible()) {
1885 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1886 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001887 showPreviews(v);
1888 }
1889 return true;
1890 case R.id.all_apps_button:
1891 if (!isAllAppsVisible()) {
1892 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1893 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1894 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001895 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001896 return true;
1897 }
1898
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 return false;
1901 }
1902
1903 if (!(v instanceof CellLayout)) {
1904 v = (View) v.getParent();
1905 }
1906
1907 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1908
1909 // This happens when long clicking an item with the dpad/trackball
1910 if (cellInfo == null) {
1911 return true;
1912 }
1913
1914 if (mWorkspace.allowLongPress()) {
1915 if (cellInfo.cell == null) {
1916 if (cellInfo.valid) {
1917 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001918 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001919 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1920 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 showAddDialog(cellInfo);
1922 }
1923 } else {
1924 if (!(cellInfo.cell instanceof Folder)) {
1925 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001926 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1927 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 mWorkspace.startDrag(cellInfo);
1929 }
1930 }
1931 }
1932 return true;
1933 }
1934
Romain Guye6b8e2f2009-11-10 11:56:55 -08001935 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001936 private void dismissPreview(final View v) {
1937 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001938 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001939 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1940 public void onDismiss() {
1941 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1942 int count = group.getChildCount();
1943 for (int i = 0; i < count; i++) {
1944 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1945 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001946 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1947 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1948
1949 v.setTag(R.id.workspace, null);
1950 v.setTag(R.id.icon, null);
1951 window.setOnDismissListener(null);
1952 }
1953 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001954 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001955 }
1956 v.setTag(null);
1957 }
1958
Romain Guyf8e6a802009-12-07 17:48:02 -08001959 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001960 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001961 }
1962
Romain Guya6abce82009-11-10 02:54:41 -08001963 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001964 final Resources resources = getResources();
1965 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001966
Romain Guya6abce82009-11-10 02:54:41 -08001967 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001968
Romain Guy9d31e9f2009-11-11 18:54:28 -08001969 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001970
Romain Guyf8e6a802009-12-07 17:48:02 -08001971 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001972 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001973 int extraW = (int) ((r.left + r.right) * max);
1974 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001975
1976 int aW = cell.getWidth() - extraW;
1977 float w = aW / max;
1978
1979 int width = cell.getWidth();
1980 int height = cell.getHeight();
1981 int x = cell.getLeftPadding();
1982 int y = cell.getTopPadding();
1983 width -= (x + cell.getRightPadding());
1984 height -= (y + cell.getBottomPadding());
1985
1986 float scale = w / width;
1987
1988 int count = end - start;
1989
1990 final float sWidth = width * scale;
1991 float sHeight = height * scale;
1992
Romain Guye6b8e2f2009-11-10 11:56:55 -08001993 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001994
Romain Guye6b8e2f2009-11-10 11:56:55 -08001995 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1996 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001997
1998 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001999 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002000 cell = (CellLayout) workspace.getChildAt(i);
2001
Romain Guyf8e6a802009-12-07 17:48:02 -08002002 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002003 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002004
2005 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002006 c.scale(scale, scale);
2007 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
2008 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08002009
Romain Guy9d31e9f2009-11-11 18:54:28 -08002010 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002011 image.setImageBitmap(bitmap);
2012 image.setTag(i);
2013 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002014 image.setOnFocusChangeListener(handler);
2015 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002016 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002017
2018 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002019 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2020
Winson Chungaafa03c2010-06-11 17:34:16 -07002021 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002022 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002023
2024 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002025 p.setContentView(preview);
2026 p.setWidth((int) (sWidth * count + extraW));
2027 p.setHeight((int) (sHeight + extraH));
2028 p.setAnimationStyle(R.style.AnimationPreview);
2029 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002030 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002031 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002032 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002033
Romain Guye6b8e2f2009-11-10 11:56:55 -08002034 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2035 public void onDismiss() {
2036 dismissPreview(anchor);
2037 }
2038 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002039
2040 anchor.setTag(p);
2041 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002042 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002043 }
2044
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002046 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002047
Romain Guye6b8e2f2009-11-10 11:56:55 -08002048 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002049 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002050 }
2051
2052 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002053 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002054 v.post(this);
2055 }
2056
2057 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002058 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002059 }
2060
2061 public void onFocusChange(View v, boolean hasFocus) {
2062 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002063 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002064 }
Romain Guya6abce82009-11-10 02:54:41 -08002065 }
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 Workspace getWorkspace() {
2069 return mWorkspace;
2070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 @Override
2073 protected Dialog onCreateDialog(int id) {
2074 switch (id) {
2075 case DIALOG_CREATE_SHORTCUT:
2076 return new CreateShortcut().createDialog();
2077 case DIALOG_RENAME_FOLDER:
2078 return new RenameFolder().createDialog();
2079 }
2080
2081 return super.onCreateDialog(id);
2082 }
2083
2084 @Override
2085 protected void onPrepareDialog(int id, Dialog dialog) {
2086 switch (id) {
2087 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 break;
2089 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002090 if (mFolderInfo != null) {
2091 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2092 final CharSequence text = mFolderInfo.title;
2093 input.setText(text);
2094 input.setSelection(0, text.length());
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 break;
2097 }
2098 }
2099
2100 void showRenameDialog(FolderInfo info) {
2101 mFolderInfo = info;
2102 mWaitingForResult = true;
2103 showDialog(DIALOG_RENAME_FOLDER);
2104 }
2105
2106 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2107 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002108 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 mWaitingForResult = true;
2110 showDialog(DIALOG_CREATE_SHORTCUT);
2111 }
2112
Joe Onoratodeb98af2010-02-19 14:59:39 -08002113 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002114 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002115 Bundle bundle = new Bundle();
2116
2117 ArrayList<String> shortcutNames = new ArrayList<String>();
2118 shortcutNames.add(getString(R.string.group_applications));
2119 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2120
2121 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2122 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2123 R.drawable.ic_launcher_application));
2124 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2125
2126 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2127 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002128 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002129 pickIntent.putExtras(bundle);
2130
Joe Onoratodeb98af2010-02-19 14:59:39 -08002131 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002132 }
2133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 private class RenameFolder {
2135 private EditText mInput;
2136
2137 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2139 mInput = (EditText) layout.findViewById(R.id.folder_name);
2140
2141 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2142 builder.setIcon(0);
2143 builder.setTitle(getString(R.string.rename_folder_title));
2144 builder.setCancelable(true);
2145 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2146 public void onCancel(DialogInterface dialog) {
2147 cleanup();
2148 }
2149 });
2150 builder.setNegativeButton(getString(R.string.cancel_action),
2151 new Dialog.OnClickListener() {
2152 public void onClick(DialogInterface dialog, int which) {
2153 cleanup();
2154 }
2155 }
2156 );
2157 builder.setPositiveButton(getString(R.string.rename_action),
2158 new Dialog.OnClickListener() {
2159 public void onClick(DialogInterface dialog, int which) {
2160 changeFolderName();
2161 }
2162 }
2163 );
2164 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002165
2166 final AlertDialog dialog = builder.create();
2167 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2168 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002169 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002170 mInput.requestFocus();
2171 InputMethodManager inputManager = (InputMethodManager)
2172 getSystemService(Context.INPUT_METHOD_SERVICE);
2173 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002174 }
2175 });
2176
2177 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 }
2179
2180 private void changeFolderName() {
2181 final String name = mInput.getText().toString();
2182 if (!TextUtils.isEmpty(name)) {
2183 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002184 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 mFolderInfo.title = name;
2186 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2187
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002189 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 } else {
2192 final FolderIcon folderIcon = (FolderIcon)
2193 mWorkspace.getViewForTag(mFolderInfo);
2194 if (folderIcon != null) {
2195 folderIcon.setText(name);
2196 getWorkspace().requestLayout();
2197 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002198 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 mWorkspaceLoading = true;
2200 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 }
2202 }
2203 }
2204 cleanup();
2205 }
2206
2207 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 dismissDialog(DIALOG_RENAME_FOLDER);
2209 mWaitingForResult = false;
2210 mFolderInfo = null;
2211 }
2212 }
2213
Daniel Sandler843e8602010-06-07 14:59:01 -04002214 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2215 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002216 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002217 }
2218
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002219 // AllAppsView.Watcher
2220 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002221 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2222 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002223 mWorkspace.setVisibility(View.GONE);
2224 }
2225 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002226
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227 private void showToolbarButton(View button) {
2228 button.setAlpha(1.0f);
2229 button.setVisibility(View.VISIBLE);
2230 button.setFocusable(true);
2231 button.setClickable(true);
2232 }
2233
2234 private void hideToolbarButton(View button) {
2235 button.setAlpha(0.0f);
2236 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2237 button.setVisibility(View.INVISIBLE);
2238 button.setFocusable(false);
2239 button.setClickable(false);
2240 }
2241
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002242 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002243 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002244 *
2245 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2246 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002247 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002248 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002249 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002250 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002251 final boolean showing = show;
2252 final boolean hiding = !show;
2253
2254 final int duration = show ?
2255 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2256 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2257
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 if (seq != null) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002259 Animator anim = new ObjectAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2260 anim.addListener(new AnimatorListenerAdapter() {
2261 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002262 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002263 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002264 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002265 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002266 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002267 });
2268 seq.play(anim);
2269 } else {
2270 if (showing) {
2271 showToolbarButton(view);
2272 } else {
2273 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002274 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002275 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002276 }
2277
2278 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002279 * Show/hide the appropriate toolbar buttons for newState.
2280 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002281 *
2282 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002283 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2284 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002285 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002286 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002287 final View searchButton = findViewById(R.id.search_button);
2288 final View allAppsButton = findViewById(R.id.all_apps_button);
2289 final View marketButton = findViewById(R.id.market_button);
2290 final View configureButton = findViewById(R.id.configure_button);
2291
2292 switch (newState) {
2293 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002294 hideOrShowToolbarButton(true, searchButton, showSeq);
2295 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2296 hideOrShowToolbarButton(true, configureButton, showSeq);
2297 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002298 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002299 break;
2300 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002301 hideOrShowToolbarButton(true, configureButton, showSeq);
2302 hideOrShowToolbarButton(true, marketButton, showSeq);
2303 hideOrShowToolbarButton(false, searchButton, hideSeq);
2304 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002305 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 break;
2307 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002308 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2309 hideOrShowToolbarButton(false, searchButton, hideSeq);
2310 hideOrShowToolbarButton(false, marketButton, hideSeq);
2311 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002312 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313 break;
2314 }
2315 }
2316
2317 /**
2318 * Helper method for the cameraZoomIn/cameraZoomOut animations
2319 * @param view The view being animated
2320 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2321 * @param scaleFactor The scale factor used for the zoom
2322 */
2323 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2324 final int height = view.getHeight();
2325 view.setPivotX(view.getWidth() / 2.0f);
2326 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2327 // Assumes that the view is normally anchored to either the top or bottom of the screen
2328 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2329 if (state == State.ALL_APPS) {
2330 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2331 } else {
2332 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2333 }
2334 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002335
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002336 /**
2337 * Zoom the camera out from the workspace to reveal 'toView'.
2338 * Assumes that the view to show is anchored at either the very top or very bottom
2339 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002340 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002341 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002342 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002343 final Resources res = getResources();
2344 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2345 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002346 final boolean toAllApps = (toState == State.ALL_APPS);
2347 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002348
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002349 setPivotsForZoom(toView, toState, scale);
2350
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351 if (toState == State.ALL_APPS) {
2352 mWorkspace.shrinkToBottom(animated);
2353 } else {
2354 mWorkspace.shrinkToTop(animated);
2355 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002356
2357 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002358 ValueAnimator scaleAnim = new ObjectAnimator(duration, toView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002359 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2360 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2361 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002362 scaleAnim.addListener(new AnimatorListenerAdapter() {
2363 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002364 // Prepare the position
2365 toView.setTranslationX(0.0f);
2366 toView.setTranslationY(0.0f);
2367 toView.setVisibility(View.VISIBLE);
2368 }
2369 });
2370
Chet Haaseb1254a62010-09-07 13:35:00 -07002371 AnimatorSet toolbarHideAnim = new AnimatorSet();
2372 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002373 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2374
2375 // toView should appear right at the end of the workspace shrink animation
2376 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2377
Chet Haaseb1254a62010-09-07 13:35:00 -07002378 AnimatorSet s = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002379 s.playTogether(scaleAnim, toolbarHideAnim);
2380 s.play(scaleAnim).after(startDelay);
2381
2382 // Show the new toolbar buttons just as the main animation is ending
2383 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2384 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2385 s.start();
2386 } else {
2387 toView.setTranslationX(0.0f);
2388 toView.setTranslationY(0.0f);
2389 toView.setScaleX(1.0f);
2390 toView.setScaleY(1.0f);
2391 toView.setVisibility(View.VISIBLE);
2392 hideAndShowToolbarButtons(toState, null, null);
2393 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002394 }
2395
2396 /**
2397 * Zoom the camera back into the workspace, hiding 'fromView'.
2398 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002399 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2400 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002401 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002402 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002403 Resources res = getResources();
2404 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2405 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002406 final View fromView =
2407 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2408
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002409 mCustomizePagedView.endChoiceMode();
2410 mAllAppsPagedView.endChoiceMode();
2411
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002412 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002413
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002414 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415
2416 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002417 AnimatorSet s = new AnimatorSet();
2418 ValueAnimator scaleAnim = new ObjectAnimator(duration, fromView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002419 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2420 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2421 scaleAnim.setInterpolator(new AccelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002422 s.addListener(new AnimatorListenerAdapter() {
2423 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002424 fromView.setVisibility(View.GONE);
2425 fromView.setScaleX(1.0f);
2426 fromView.setScaleY(1.0f);
2427 }
2428 });
2429
Chet Haaseb1254a62010-09-07 13:35:00 -07002430 AnimatorSet toolbarHideAnim = new AnimatorSet();
2431 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002432 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2433
2434 s.playTogether(scaleAnim, toolbarHideAnim);
2435
2436 // Show the new toolbar buttons at the very end of the whole animation
2437 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2438 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2439 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2440 s.start();
2441 } else {
2442 fromView.setVisibility(View.GONE);
2443 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2444 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002445 }
2446
2447 /**
2448 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2449 * This is the transition used on xlarge screens to go between all apps and
2450 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002451 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2452 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2453 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002455 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002456 final Resources res = getResources();
2457 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002458 final int workspaceHeight = mWorkspace.getHeight();
2459
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002460 final boolean fromAllApps = (fromState == State.ALL_APPS);
2461 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2462 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002463
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2465 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2466 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2467 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002468
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002469 mCustomizePagedView.endChoiceMode();
2470 mAllAppsPagedView.endChoiceMode();
2471
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002472 if (toState == State.ALL_APPS) {
2473 mWorkspace.shrinkToBottom(animated);
2474 } else {
2475 mWorkspace.shrinkToTop(animated);
2476 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477
2478 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002479 AnimatorSet s = new AnimatorSet();
2480 s.addListener(new AnimatorListenerAdapter() {
2481 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002482 toView.setVisibility(View.VISIBLE);
2483 toView.setY(toViewStartY);
2484 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002485 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002486 fromView.setVisibility(View.GONE);
2487 }
2488 });
2489
Chet Haaseb1254a62010-09-07 13:35:00 -07002490 AnimatorSet toolbarHideAnim = new AnimatorSet();
2491 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002492 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2493
2494 s.playTogether(
2495 toolbarHideAnim,
Chet Haaseb1254a62010-09-07 13:35:00 -07002496 new ObjectAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2497 new ObjectAnimator(duration, toView, "y", toViewStartY, toViewEndY));
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002498
2499 // Show the new toolbar buttons just as the main animation is ending
2500 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2501 s.play(toolbarShowAnim).after(duration - fadeInTime);
2502 s.start();
2503 } else {
2504 fromView.setY(fromViewEndY);
2505 fromView.setVisibility(View.GONE);
2506 toView.setY(toViewEndY);
2507 toView.setVisibility(View.VISIBLE);
2508 hideAndShowToolbarButtons(toState, null, null);
2509 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002510 }
2511
2512 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002513 if (mAllAppsGrid.isVisible())
2514 return;
2515
Michael Jurka79212d82010-07-30 16:36:20 -07002516 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002517 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002518 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002519 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002520 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002521 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002522 } else {
2523 mAllAppsGrid.zoom(1.0f, animated);
2524 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002525
Romain Guyc16fea72010-03-12 17:17:56 -08002526 ((View) mAllAppsGrid).setFocusable(true);
2527 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002528
Joe Onorato7c312c12009-08-13 21:36:53 -07002529 // TODO: fade these two too
2530 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002531 }
2532
Joe Onoratob2061212009-11-24 16:13:54 -05002533 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002534 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002535 * - Home from workspace
2536 * - from center screen
2537 * - from other screens
2538 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002539 * - from center screen
2540 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002541 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002542 * - from center screen
2543 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002544 * - Launch app from workspace and quit
2545 * - with back
2546 * - with home
2547 * - Launch app from all apps and quit
2548 * - with back
2549 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002550 * - Go to a screen that's not the default, then all
2551 * apps, and launch and app, and go back
2552 * - with back
2553 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002554 * - On workspace, long press power and go back
2555 * - with back
2556 * - with home
2557 * - On all apps, long press power and go back
2558 * - with back
2559 * - with home
2560 * - On workspace, power off
2561 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002562 * - Launch an app and turn off the screen while in that app
2563 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002564 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002565 * - From all apps
2566 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002567 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2568 * - From all apps
2569 * - From the center workspace
2570 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002571 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002572 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002573 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002574 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002577 } else {
2578 mAllAppsGrid.zoom(0.0f, animated);
2579 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002580 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002581 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002582 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002583 }
2584
Joe Onorato7c312c12009-08-13 21:36:53 -07002585 void lockAllApps() {
2586 // TODO
2587 }
2588
2589 void unlockAllApps() {
2590 // TODO
2591 }
2592
Patrick Dubroy558654c2010-07-23 16:48:11 -07002593 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002594 return mHomeCustomizationDrawer != null &&
2595 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002596 }
2597
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 // Show the customization drawer (only exists in x-large configuration)
2599 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002600 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002602 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002604 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002605 }
2606
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 // Hide the customization drawer (only exists in x-large configuration)
2608 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002609 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002611 }
2612 }
2613
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002614 void onWorkspaceClick(CellLayout layout) {
2615 Object itemInfo = mAllAppsPagedView.getChosenItem();
2616 if (itemInfo == null) {
2617 itemInfo = mCustomizePagedView.getChosenItem();
Michael Jurka213d9632010-07-28 11:29:25 -07002618 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002619
2620 if (itemInfo == null) {
2621 // No items are chosen in All Apps or Customize, so just zoom into the workspace
2622 mWorkspace.unshrink(layout);
2623 if (isAllAppsVisible()) {
2624 closeAllApps(true);
2625 } else if (isCustomizationDrawerVisible()) {
2626 hideCustomizationDrawer(true);
2627 }
2628 } else {
2629 // Act as if the chosen item was dropped on the given CellLayout
2630 if (mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2631 mAllAppsPagedView.endChoiceMode();
2632 mCustomizePagedView.endChoiceMode();
2633 } else {
2634 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
2635 }
Michael Jurka213d9632010-07-28 11:29:25 -07002636 }
2637 }
2638
Joe Onorato7404ee42009-07-31 11:54:44 -07002639 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002640 * Sets the app market icon (shown when all apps is visible on x-large screens)
2641 */
2642 private void updateAppMarketIcon() {
2643 if (LauncherApplication.isScreenXLarge()) {
2644 // Find the app market activity by resolving an intent.
2645 // (If multiple app markets are installed, it will return the ResolverActivity.)
2646 PackageManager packageManager = getPackageManager();
2647 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2648 ComponentName activityName = intent.resolveActivity(getPackageManager());
2649 if (activityName != null) {
2650 mAppMarketIntent = intent;
2651 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2652 Drawable toolbarIcon = null;
2653 try {
2654 // Look for the toolbar icon specified in the activity meta-data
2655 Bundle metaData = packageManager.getActivityInfo(
2656 activityName, PackageManager.GET_META_DATA).metaData;
2657 if (metaData != null) {
2658 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2659 if (iconResId != 0) {
2660 Resources res = packageManager.getResourcesForActivity(activityName);
2661 toolbarIcon = res.getDrawable(iconResId);
2662 }
2663 }
2664 } catch (NameNotFoundException e) {
2665 // Do nothing
2666 }
2667 // If we were unable to find the icon via the meta-data, use a generic one
2668 if (toolbarIcon == null) {
2669 marketButton.setImageResource(R.drawable.app_market_generic);
2670 } else {
2671 marketButton.setImageDrawable(toolbarIcon);
2672 }
2673 } else {
2674 mAppMarketIntent = null;
2675 }
2676 }
2677 }
2678
2679 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 * Displays the shortcut creation dialog and launches, if necessary, the
2681 * appropriate activity.
2682 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002683 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002684 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2685 DialogInterface.OnShowListener {
2686
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002688
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2693 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002694 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 builder.setInverseBackgroundForced(true);
2697
2698 AlertDialog dialog = builder.create();
2699 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002700 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002701 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002703 return dialog;
2704 }
2705
2706 public void onCancel(DialogInterface dialog) {
2707 mWaitingForResult = false;
2708 cleanup();
2709 }
2710
Romain Guycbb89e42009-06-08 15:52:54 -07002711 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002712 }
2713
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002715 try {
2716 dismissDialog(DIALOG_CREATE_SHORTCUT);
2717 } catch (Exception e) {
2718 // An exception is thrown if the dialog is not visible, which is fine
2719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 }
2721
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002722 /**
2723 * Handle the action clicked in the "Add to home" dialog.
2724 */
2725 public void onClick(DialogInterface dialog, int which) {
2726 Resources res = getResources();
2727 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002728
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002729 switch (which) {
2730 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002731 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002732 break;
2733 }
Romain Guycbb89e42009-06-08 15:52:54 -07002734
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002735 case AddAdapter.ITEM_APPWIDGET: {
2736 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002737
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002738 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2739 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002740 // start the pick activity
2741 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2742 break;
2743 }
Romain Guycbb89e42009-06-08 15:52:54 -07002744
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002745 case AddAdapter.ITEM_LIVE_FOLDER: {
2746 // Insert extra item to handle inserting folder
2747 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002748
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002749 ArrayList<String> shortcutNames = new ArrayList<String>();
2750 shortcutNames.add(res.getString(R.string.group_folder));
2751 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002752
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002753 ArrayList<ShortcutIconResource> shortcutIcons =
2754 new ArrayList<ShortcutIconResource>();
2755 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2756 R.drawable.ic_launcher_folder));
2757 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2758
2759 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2760 pickIntent.putExtra(Intent.EXTRA_INTENT,
2761 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2762 pickIntent.putExtra(Intent.EXTRA_TITLE,
2763 getText(R.string.title_select_live_folder));
2764 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002765
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002766 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2767 break;
2768 }
2769
2770 case AddAdapter.ITEM_WALLPAPER: {
2771 startWallpaper();
2772 break;
2773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 }
2775 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002776
2777 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002778 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 }
2781
2782 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002783 * Receives notifications when applications are added/removed.
2784 */
2785 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2786 @Override
2787 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002788 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002789 String reason = intent.getStringExtra("reason");
2790 if (!"homekey".equals(reason)) {
2791 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002792 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002793 animate = false;
2794 }
Joe Onoratob2061212009-11-24 16:13:54 -05002795 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002796 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002797 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002798 }
2799 }
2800
2801 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002802 * Receives notifications whenever the appwidgets are reset.
2803 */
2804 private class AppWidgetResetObserver extends ContentObserver {
2805 public AppWidgetResetObserver() {
2806 super(new Handler());
2807 }
2808
2809 @Override
2810 public void onChange(boolean selfChange) {
2811 onAppWidgetReset();
2812 }
2813 }
2814
2815 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002819 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002820 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002821 } else {
2822 return SCREEN_COUNT / 2;
2823 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002825
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002826 void setAllAppsPagedView(PagedView view) {
2827 mAllAppsPagedView = view;
2828 }
2829
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 /**
2831 * Refreshes the shortcuts shown on the workspace.
2832 *
2833 * Implementation of the method from LauncherModel.Callbacks.
2834 */
2835 public void startBinding() {
2836 final Workspace workspace = mWorkspace;
2837 int count = workspace.getChildCount();
2838 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002839 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2841 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002842
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 if (DEBUG_USER_INTERFACE) {
2844 android.widget.Button finishButton = new android.widget.Button(this);
2845 finishButton.setText("Finish");
2846 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2847
2848 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2849 public void onClick(View v) {
2850 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 }
2854 }
2855
2856 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 * Bind the items start-end from the list.
2858 *
2859 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2862
2863 final Workspace workspace = mWorkspace;
2864
2865 for (int i=start; i<end; i++) {
2866 final ItemInfo item = shortcuts.get(i);
2867 mDesktopItems.add(item);
2868 switch (item.itemType) {
2869 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2870 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002871 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2873 false);
2874 break;
2875 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2876 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002877 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 (UserFolderInfo) item);
2879 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2880 false);
2881 break;
2882 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2883 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2884 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002885 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 (LiveFolderInfo) item);
2887 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2888 false);
2889 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 }
2892
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 }
2895
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 /**
2897 * Implementation of the method from LauncherModel.Callbacks.
2898 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002899 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002900 sFolders.clear();
2901 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002902 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002903
2904 /**
2905 * Add the views for a widget to the workspace.
2906 *
2907 * Implementation of the method from LauncherModel.Callbacks.
2908 */
2909 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002910 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2911 if (DEBUG_WIDGETS) {
2912 Log.d(TAG, "bindAppWidget: " + item);
2913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 final Workspace workspace = mWorkspace;
2915
2916 final int appWidgetId = item.appWidgetId;
2917 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002918 if (DEBUG_WIDGETS) {
2919 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2920 }
2921
Joe Onorato9c1289c2009-08-17 11:03:03 -04002922 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2923
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2925 item.hostView.setTag(item);
2926
2927 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2928 item.cellY, item.spanX, item.spanY, false);
2929
2930 workspace.requestLayout();
2931
2932 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002933
2934 if (DEBUG_WIDGETS) {
2935 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2936 + (SystemClock.uptimeMillis()-start) + "ms");
2937 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 }
2939
2940 /**
2941 * Callback saying that there aren't any more items to bind.
2942 *
2943 * Implementation of the method from LauncherModel.Callbacks.
2944 */
2945 public void finishBindingItems() {
2946 if (mSavedState != null) {
2947 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002948 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 }
2950
2951 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2952 if (userFolders != null) {
2953 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002954 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002955 if (info != null) {
2956 openFolder(info);
2957 }
2958 }
2959 final Folder openFolder = mWorkspace.getOpenFolder();
2960 if (openFolder != null) {
2961 openFolder.requestFocus();
2962 }
2963 }
2964
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965 mSavedState = null;
2966 }
2967
2968 if (mSavedInstanceState != null) {
2969 super.onRestoreInstanceState(mSavedInstanceState);
2970 mSavedInstanceState = null;
2971 }
2972
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 mWorkspaceLoading = false;
2974 }
2975
2976 /**
2977 * Add the icons for all apps.
2978 *
2979 * Implementation of the method from LauncherModel.Callbacks.
2980 */
2981 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002982 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002983 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 }
2985
2986 /**
2987 * A package was installed.
2988 *
2989 * Implementation of the method from LauncherModel.Callbacks.
2990 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002991 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002993 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002994 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 }
2996
2997 /**
2998 * A package was updated.
2999 *
3000 * Implementation of the method from LauncherModel.Callbacks.
3001 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003002 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003004 mWorkspace.updateShortcuts(apps);
3005 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003006 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 }
3008
3009 /**
3010 * A package was uninstalled.
3011 *
3012 * Implementation of the method from LauncherModel.Callbacks.
3013 */
Joe Onorato36115782010-06-17 13:28:48 -04003014 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003016 if (permanent) {
3017 mWorkspace.removeItems(apps);
3018 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003019 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003020 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 }
Joe Onoratobe386092009-11-17 17:32:16 -08003022
3023 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003024 * A number of packages were updated.
3025 */
3026 public void bindPackagesUpdated() {
3027 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07003028 if (mCustomizePagedView != null)
3029 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07003030 }
3031
3032 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003033 * Prints out out state for debugging.
3034 */
3035 public void dumpState() {
3036 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003037 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003038 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3039 Log.d(TAG, "mRestoring=" + mRestoring);
3040 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3041 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3042 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003043 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003044 mModel.dumpState();
3045 mAllAppsGrid.dumpState();
3046 Log.d(TAG, "END launcher2 dump state");
3047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048}