blob: d619aa2cec9afc0e68ab681bef6330c93acea9bf [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
Winson Chunge3193b92010-09-10 11:44:42 -0700284 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
285 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700286 TabContentFactory contentFactory = new TabContentFactory() {
287 public View createTabContent(String tag) {
288 return mCustomizePagedView;
289 }
290 };
291
Michael Jurka946ad472010-07-09 18:05:18 -0700292 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
294 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700295 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700296 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
297 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700298 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700299 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
300 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700301 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700302 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
303 .setIndicator(wallpapersLabel).setContent(contentFactory));
304
305 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
306 // new assets
307 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
308 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
309 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
310 TextView text = (TextView) tab.getChildAt(1);
311 text.setTextSize(20.0f);
312 text.setPadding(20, 0, 20, 0);
313 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
314 tab.setBackgroundDrawable(null);
315 }
316
317 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
318 public void onTabChanged(String tabId) {
319 // animate the changing of the tab content by fading pages in and out
320 final int duration = 150;
321 final float alpha = mCustomizePagedView.getAlpha();
Chet Haaseb1254a62010-09-07 13:35:00 -0700322 ValueAnimator alphaAnim = new ObjectAnimator(duration, mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 "alpha", alpha, 0.0f);
Chet Haaseb1254a62010-09-07 13:35:00 -0700324 alphaAnim.addListener(new AnimatorListenerAdapter() {
325 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700326 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
327 if (tag == WIDGETS_TAG) {
328 mCustomizePagedView.setCustomizationFilter(
329 CustomizePagedView.CustomizationType.WidgetCustomization);
330 } else if (tag == FOLDERS_TAG) {
331 mCustomizePagedView.setCustomizationFilter(
332 CustomizePagedView.CustomizationType.FolderCustomization);
333 } else if (tag == SHORTCUTS_TAG) {
334 mCustomizePagedView.setCustomizationFilter(
335 CustomizePagedView.CustomizationType.ShortcutCustomization);
336 } else if (tag == WALLPAPERS_TAG) {
337 mCustomizePagedView.setCustomizationFilter(
338 CustomizePagedView.CustomizationType.WallpaperCustomization);
339 }
340
341 final float alpha = mCustomizePagedView.getAlpha();
Chet Haaseb1254a62010-09-07 13:35:00 -0700342 ValueAnimator alphaAnim = new ObjectAnimator(duration, mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700343 "alpha", alpha, 1.0f);
344 alphaAnim.start();
345 }
346 });
347 alphaAnim.start();
348 }
349 });
Michael Jurka946ad472010-07-09 18:05:18 -0700350
351 mHomeCustomizationDrawer.setCurrentTab(0);
352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 setupViews();
354
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800355 registerContentObservers();
356
Joe Onorato7c312c12009-08-13 21:36:53 -0700357 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 mSavedState = savedInstanceState;
360 restoreState(mSavedState);
361
362 if (PROFILE_STARTUP) {
363 android.os.Debug.stopMethodTracing();
364 }
365
366 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
369
370 // For handling default keys
371 mDefaultKeySsb = new SpannableStringBuilder();
372 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800373
374 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
375 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
Romain Guycbb89e42009-06-08 15:52:54 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700379 if (sLocaleConfiguration == null) {
380 new AsyncTask<Void, Void, LocaleConfiguration>() {
381 @Override
382 protected LocaleConfiguration doInBackground(Void... unused) {
383 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
384 readConfiguration(Launcher.this, localeConfiguration);
385 return localeConfiguration;
386 }
387
388 @Override
389 protected void onPostExecute(LocaleConfiguration result) {
390 sLocaleConfiguration = result;
391 checkForLocaleChange(); // recursive, but now with a locale configuration
392 }
393 }.execute();
394 return;
395 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 final Configuration configuration = getResources().getConfiguration();
398
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final String locale = configuration.locale.toString();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final int mcc = configuration.mcc;
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mnc = configuration.mnc;
407
Romain Guy84f296c2009-11-04 15:00:44 -0800408 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409
Romain Guy84f296c2009-11-04 15:00:44 -0800410 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700411 sLocaleConfiguration.locale = locale;
412 sLocaleConfiguration.mcc = mcc;
413 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700414
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400416 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700417
418 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
419 new Thread("WriteLocaleConfiguration") {
420 public void run() {
421 writeConfiguration(Launcher.this, localeConfiguration);
422 }
423 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700424 }
425 }
426
427 private static class LocaleConfiguration {
428 public String locale;
429 public int mcc = -1;
430 public int mnc = -1;
431 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700432
Romain Guy98d01652009-06-30 16:21:04 -0700433 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
434 DataInputStream in = null;
435 try {
436 in = new DataInputStream(context.openFileInput(PREFERENCES));
437 configuration.locale = in.readUTF();
438 configuration.mcc = in.readInt();
439 configuration.mnc = in.readInt();
440 } catch (FileNotFoundException e) {
441 // Ignore
442 } catch (IOException e) {
443 // Ignore
444 } finally {
445 if (in != null) {
446 try {
447 in.close();
448 } catch (IOException e) {
449 // Ignore
450 }
451 }
452 }
453 }
454
455 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
456 DataOutputStream out = null;
457 try {
458 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
459 out.writeUTF(configuration.locale);
460 out.writeInt(configuration.mcc);
461 out.writeInt(configuration.mnc);
462 out.flush();
463 } catch (FileNotFoundException e) {
464 // Ignore
465 } catch (IOException e) {
466 //noinspection ResultOfMethodCallIgnored
467 context.getFileStreamPath(PREFERENCES).delete();
468 } finally {
469 if (out != null) {
470 try {
471 out.close();
472 } catch (IOException e) {
473 // Ignore
474 }
475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 }
477 }
478
479 static int getScreen() {
480 synchronized (sLock) {
481 return sScreen;
482 }
483 }
484
485 static void setScreen(int screen) {
486 synchronized (sLock) {
487 sScreen = screen;
488 }
489 }
490
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700492 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
494 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700495 // TODO: Put back when we decide about scrolling the wallpaper
496 // boolean isPortrait = display.getWidth() < display.getHeight();
497 // final int width = isPortrait ? display.getWidth() : display.getHeight();
498 // final int height = isPortrait ? display.getHeight() : display.getWidth();
499 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
500 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 }
502
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400503 // Note: This doesn't do all the client-id magic that BrowserProvider does
504 // in Browser. (http://b/2425179)
505 private Uri getDefaultBrowserUri() {
506 String url = getString(R.string.default_browser_url);
507 if (url.indexOf("{CID}") != -1) {
508 url = url.replace("{CID}", "android-google");
509 }
510 return Uri.parse(url);
511 }
512
513 // Load the Intent templates from arrays.xml to populate the hotseats. For
514 // each Intent, if it resolves to a single app, use that as the launch
515 // intent & use that app's label as the contentDescription. Otherwise,
516 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400517 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400518 if (mHotseatConfig == null) {
519 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
520 if (mHotseatConfig.length > 0) {
521 mHotseats = new Intent[mHotseatConfig.length];
522 mHotseatLabels = new CharSequence[mHotseatConfig.length];
523 mHotseatIcons = new Drawable[mHotseatConfig.length];
524 } else {
525 mHotseats = null;
526 mHotseatIcons = null;
527 mHotseatLabels = null;
528 }
529
530 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
531 for (int i=0; i<mHotseatConfig.length; i++) {
532 // load icon for this slot; currently unrelated to the actual activity
533 try {
534 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
535 } catch (ArrayIndexOutOfBoundsException ex) {
536 Log.w(TAG, "Missing hotseat_icons array item #" + i);
537 mHotseatIcons[i] = null;
538 }
539 }
540 hotseatIconDrawables.recycle();
541 }
542
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400543 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 for (int i=0; i<mHotseatConfig.length; i++) {
545 Intent intent = null;
546 if (mHotseatConfig[i].equals("*BROWSER*")) {
547 // magic value meaning "launch user's default web browser"
548 // replace it with a generic web request so we can see if there is indeed a default
549 String defaultUri = getString(R.string.default_browser_url);
550 intent = new Intent(
551 Intent.ACTION_VIEW,
552 ((defaultUri != null)
553 ? Uri.parse(defaultUri)
554 : getDefaultBrowserUri())
555 ).addCategory(Intent.CATEGORY_BROWSABLE);
556 // note: if the user launches this without a default set, she
557 // will always be taken to the default URL above; this is
558 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700559 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 // URL is unavoidably sent to the chosen app.
561 } else {
562 try {
563 intent = Intent.parseUri(mHotseatConfig[i], 0);
564 } catch (java.net.URISyntaxException ex) {
565 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
566 // bogus; leave intent=null
567 }
568 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700569
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400570 if (intent == null) {
571 mHotseats[i] = null;
572 mHotseatLabels[i] = getText(R.string.activity_not_found);
573 continue;
574 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400575
576 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700577 Log.d(TAG, "loadHotseats: hotseat " + i
578 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400579 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580 + "]");
581 }
582
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400583 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
584 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700585 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 Log.d(TAG, "Best match for intent: " + bestMatch);
587 Log.d(TAG, "All matches: ");
588 for (ResolveInfo ri : allMatches) {
589 Log.d(TAG, " --> " + ri);
590 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400591 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400592 // did this resolve to a single app, or the resolver?
593 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700594 // can't find any activity to handle this. let's leave the
595 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400596 // to launch.
597 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400598
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 // set accessibility text to "Not installed"
600 mHotseatLabels[i] = getText(R.string.activity_not_found);
601 } else {
602 boolean found = false;
603 for (ResolveInfo ri : allMatches) {
604 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
605 && bestMatch.activityInfo.applicationInfo.packageName
606 .equals(ri.activityInfo.applicationInfo.packageName)) {
607 found = true;
608 break;
609 }
610 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700611
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400612 if (!found) {
613 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
614 // the bestMatch is probably the ResolveActivity, meaning the
615 // user has not yet selected a default
616 // so: we'll keep the original intent for now
617 mHotseats[i] = intent;
618
619 // set the accessibility text to "Select shortcut"
620 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
621 } else {
622 // we have an app!
623 // now reconstruct the intent to launch it through the front
624 // door
625 ComponentName com = new ComponentName(
626 bestMatch.activityInfo.applicationInfo.packageName,
627 bestMatch.activityInfo.name);
628 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
629
630 // load the app label for accessibility
631 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
632 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633 }
634
635 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 Log.d(TAG, "loadHotseats: hotseat " + i
637 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400638 + ((mHotseats[i] == null)
639 ? "null"
640 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400641 + "] label=[" + mHotseatLabels[i]
642 + "]"
643 );
644 }
645 }
646 }
647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 @Override
649 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700650 mWaitingForResult = false;
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // The pattern used here is that a user PICKs a specific application,
653 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
656 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700657
Romain Guy94dabf12009-07-21 10:55:43 -0700658 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 switch (requestCode) {
660 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 break;
663 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800664 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 break;
666 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 break;
669 case REQUEST_PICK_LIVE_FOLDER:
670 addLiveFolder(data);
671 break;
672 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400673 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700676 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700680 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700682 case REQUEST_PICK_WALLPAPER:
683 // We just wanted the activity result here so we can clear mWaitingForResult
684 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
Romain Guy18042c82009-11-06 11:44:55 -0800686 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
687 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
688 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 // Clean up the appWidgetId if we canceled
690 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
691 if (appWidgetId != -1) {
692 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
696
697 @Override
698 protected void onResume() {
699 super.onResume();
700
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800701 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 mWorkspaceLoading = true;
705 mModel.startLoader(this, true);
706 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700708 // When we resume Launcher, a different Activity might be responsible for the app
709 // market intent, so refresh the icon
710 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
712
713 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 protected void onPause() {
715 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700716 // Some launcher layouts don't have a previous and next view
717 if (mPreviousView != null) {
718 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700719 }
720 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700721 dismissPreview(mNextView);
722 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800723 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 }
Romain Guycbb89e42009-06-08 15:52:54 -0700725
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700726 @Override
727 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400728 // Flag the loader to stop early before switching
729 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800730 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800731 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700732 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700733
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800734 // We can't hide the IME if it was forced open. So don't bother
735 /*
736 @Override
737 public void onWindowFocusChanged(boolean hasFocus) {
738 super.onWindowFocusChanged(hasFocus);
739
740 if (hasFocus) {
741 final InputMethodManager inputManager = (InputMethodManager)
742 getSystemService(Context.INPUT_METHOD_SERVICE);
743 WindowManager.LayoutParams lp = getWindow().getAttributes();
744 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
745 android.os.Handler()) {
746 protected void onReceiveResult(int resultCode, Bundle resultData) {
747 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
748 }
749 });
750 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
751 }
752 }
753 */
754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 private boolean acceptFilter() {
756 final InputMethodManager inputManager = (InputMethodManager)
757 getSystemService(Context.INPUT_METHOD_SERVICE);
758 return !inputManager.isFullscreenMode();
759 }
760
761 @Override
762 public boolean onKeyDown(int keyCode, KeyEvent event) {
763 boolean handled = super.onKeyDown(keyCode, event);
764 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
765 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
766 keyCode, event);
767 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700768 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700769 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700770 // showSearchDialog()
771 // If there are multiple keystrokes before the search dialog takes focus,
772 // onSearchRequested() will be called for every keystroke,
773 // but it is idempotent, so it's fine.
774 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776 }
777
Joe Onorato8a9625e2010-01-28 15:55:35 -0800778 // Eat the long press event so the keyboard doesn't come up.
779 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
780 return true;
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return handled;
784 }
785
Karl Rosaen138a0412009-04-23 19:00:21 -0700786 private String getTypedText() {
787 return mDefaultKeySsb.toString();
788 }
789
790 private void clearTypedText() {
791 mDefaultKeySsb.clear();
792 mDefaultKeySsb.clearSpans();
793 Selection.setSelection(mDefaultKeySsb, 0);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 /**
797 * Restores the previous state, if it exists.
798 *
799 * @param savedState The previous state.
800 */
801 private void restoreState(Bundle savedState) {
802 if (savedState == null) {
803 return;
804 }
805
Joe Onorato3a8820b2009-11-10 15:06:42 -0800806 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
807 if (allApps) {
808 showAllApps(false);
809 }
810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
812 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700813 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 }
815
816 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
817 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700818 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 mAddItemCellInfo = new CellLayout.CellInfo();
820 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
821 addItemCellInfo.valid = true;
822 addItemCellInfo.screen = addScreen;
823 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
824 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
825 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
826 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700827 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
829 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
830 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
831 mRestoring = true;
832 }
833
834 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
835 if (renameFolder) {
836 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700837 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 mRestoring = true;
839 }
840 }
841
842 /**
843 * Finds all the views we need and configure them properly.
844 */
845 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700846 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400847
Romain Guyb1b69f52009-08-10 15:10:15 -0700848 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400849 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850
Joe Onorato7c312c12009-08-13 21:36:53 -0700851 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700852 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700853 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800854 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700855 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700856 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700857
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700858 if (LauncherApplication.isScreenXLarge()) {
859 // They need to be INVISIBLE initially so that they will be measured in the layout.
860 // Otherwise the animations are messed up when we show them for the first time.
861 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
862 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
866 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500867 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700868
Joe Onorato7c312c12009-08-13 21:36:53 -0700869 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
870 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700872 View handleView = findViewById(R.id.all_apps_button);
873 if (handleView != null && handleView instanceof HandleView) {
874 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700875 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700876 mHandleView.setLauncher(this);
877 mHandleView.setOnClickListener(this);
878 mHandleView.setOnLongClickListener(this);
879 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800880
Winson Chung80baf5a2010-08-09 16:03:15 -0700881 if (mCustomizePagedView != null) {
882 mCustomizePagedView.setLauncher(this);
883 mCustomizePagedView.setDragController(dragController);
884 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700885 } else {
886 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
887 hotseatLeft.setContentDescription(mHotseatLabels[0]);
888 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
889 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
890 hotseatRight.setContentDescription(mHotseatLabels[1]);
891 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400892
Michael Jurkaaf442092010-06-10 17:01:57 -0700893 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
894 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800895
Michael Jurkaaf442092010-06-10 17:01:57 -0700896 Drawable previous = mPreviousView.getDrawable();
897 Drawable next = mNextView.getDrawable();
898 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
Michael Jurkaaf442092010-06-10 17:01:57 -0700900 mPreviousView.setHapticFeedbackEnabled(false);
901 mPreviousView.setOnLongClickListener(this);
902 mNextView.setHapticFeedbackEnabled(false);
903 mNextView.setOnLongClickListener(this);
904 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400907 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909
910 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400911 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700912 int deleteZoneHandleId;
913 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700914 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700915 } else {
916 deleteZoneHandleId = R.id.all_apps_button_cluster;
917 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700918 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700919 dragController.addDragListener(deleteZone);
920
921 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
922 if (infoButton != null) {
923 infoButton.setLauncher(this);
924 infoButton.setHandle(findViewById(R.id.configure_button));
925 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
926 dragController.addDragListener(infoButton);
927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928
Joe Onorato00acb122009-08-04 16:04:30 -0400929 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400930 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800931 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700932
Joe Onorato00acb122009-08-04 16:04:30 -0400933 // The order here is bottom to top.
934 dragController.addDropTarget(workspace);
935 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700936 if (infoButton != null) {
937 dragController.addDropTarget(infoButton);
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
Romain Guy8a73c512009-11-09 19:19:59 -0800941 @SuppressWarnings({"UnusedDeclaration"})
942 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800943 if (!isAllAppsVisible()) {
944 mWorkspace.scrollLeft();
945 }
Romain Guy8a73c512009-11-09 19:19:59 -0800946 }
947
948 @SuppressWarnings({"UnusedDeclaration"})
949 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800950 if (!isAllAppsVisible()) {
951 mWorkspace.scrollRight();
952 }
Romain Guy8a73c512009-11-09 19:19:59 -0800953 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400954
955 @SuppressWarnings({"UnusedDeclaration"})
956 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400957 if (isAllAppsVisible()) return;
958
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400959 int index = -1;
960 if (v.getId() == R.id.hotseat_left) {
961 index = 0;
962 } else if (v.getId() == R.id.hotseat_right) {
963 index = 1;
964 }
965
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400966 // reload these every tap; you never know when they might change
967 loadHotseats();
968 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
969 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400970 startActivitySafely(
971 mHotseats[index],
972 "hotseat"
973 );
974 }
975 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 /**
978 * Creates a view representing a shortcut.
979 *
980 * @param info The data structure describing the shortcut.
981 *
982 * @return A View inflated from R.layout.application.
983 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700986 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 /**
990 * Creates a view representing a shortcut inflated from the specified resource.
991 *
992 * @param layoutResId The id of the XML layout used to create the shortcut.
993 * @param parent The group the shortcut belongs to.
994 * @param info The data structure describing the shortcut.
995 *
996 * @return A View inflated from layoutResId.
997 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1000
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1002 new FastBitmapDrawable(info.getIcon(mIconCache)),
1003 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 favorite.setText(info.title);
1005 favorite.setTag(info);
1006 favorite.setOnClickListener(this);
1007
1008 return favorite;
1009 }
1010
1011 /**
1012 * Add an application shortcut to the workspace.
1013 *
1014 * @param data The intent describing the application.
1015 * @param cellInfo The position on screen where to create the shortcut.
1016 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001017 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001018 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 if (!findSingleSlot(cellInfo)) return;
1020
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1022 data, context);
1023
Romain Guy73b979d2009-06-09 12:57:21 -07001024 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001025 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027 info.container = ItemInfo.NO_ID;
1028 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1029 } else {
1030 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
1032 }
Romain Guycbb89e42009-06-08 15:52:54 -07001033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 /**
1035 * Add a shortcut to the workspace.
1036 *
1037 * @param data The intent describing the shortcut.
1038 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001041 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Joe Onorato0589f0f2010-02-08 13:44:00 -08001044 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045
1046 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1049 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
1051 }
1052
Romain Guycbb89e42009-06-08 15:52:54 -07001053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001057 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001060 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001062
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001065 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001068 // We have saved the position to which the widget was dragged-- this really only matters
1069 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071
1072 // For now, we don't save the coordinate where we dropped the icon because we're not
1073 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1074 // a widget on the home screen in case we want to add it in the future
1075 final int[] xyTouch = null;
1076 //final int[] xyTouch = mAddItemCoordinates;
1077 boolean findNearestVacantAreaFailed = false;
1078 if (xyTouch != null) {
1079 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1080 int[] result = screen.findNearestVacantArea(
1081 mAddItemCoordinates[0], mAddItemCoordinates[1],
1082 spans[0], spans[1], cellInfo, xy);
1083 findNearestVacantAreaFailed = (result == null);
1084 }
1085
1086 if (findNearestVacantAreaFailed ||
1087 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001088 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1089 return;
1090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001092 // Build Launcher-specific widget info and save to database
1093 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 launcherInfo.spanX = spans[0];
1095 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 LauncherModel.addItemToDatabase(this, launcherInfo,
1098 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001099 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100
1101 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001102 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001105 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001107 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
Michael Jurkaa63c4522010-08-19 13:52:27 -07001110 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1116 mDesktopItems.remove(launcherInfo);
1117 launcherInfo.hostView = null;
1118 }
1119
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001120 public LauncherAppWidgetHost getAppWidgetHost() {
1121 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 }
Romain Guycbb89e42009-06-08 15:52:54 -07001123
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001124 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001125 getWindow().closeAllPanels();
1126
1127 try {
1128 dismissDialog(DIALOG_CREATE_SHORTCUT);
1129 // Unlock the workspace if the dialog was showing
1130 } catch (Exception e) {
1131 // An exception is thrown if the dialog is not visible, which is fine
1132 }
1133
1134 try {
1135 dismissDialog(DIALOG_RENAME_FOLDER);
1136 // Unlock the workspace if the dialog was showing
1137 } catch (Exception e) {
1138 // An exception is thrown if the dialog is not visible, which is fine
1139 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001140
1141 // Whatever we were doing is hereby canceled.
1142 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001143 }
1144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 @Override
1146 protected void onNewIntent(Intent intent) {
1147 super.onNewIntent(intent);
1148
1149 // Close the menu
1150 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001151 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001152 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001153
Joe Onorato14f122b2009-11-19 14:06:36 -08001154 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1155 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1156 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001157 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1158
Michael Jurka4cb37242010-08-09 21:05:32 -07001159 // in all these cases, only animate if we're already on home
1160 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001161 mWorkspace.unshrink(alreadyOnHome);
1162 }
1163 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001164 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka99229f62010-09-09 11:49:32 -07001165 mWorkspace.moveToDefaultScreen(
1166 alreadyOnHome && (LauncherApplication.isScreenXLarge() || !allAppsVisible));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001167 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001168 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001169 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001170
Joe Onorato3a8820b2009-11-10 15:06:42 -08001171 final View v = getWindow().peekDecorView();
1172 if (v != null && v.getWindowToken() != null) {
1173 InputMethodManager imm = (InputMethodManager)getSystemService(
1174 INPUT_METHOD_SERVICE);
1175 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178 }
1179
1180 @Override
1181 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1182 // Do not call super here
1183 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001184
1185 if (mHomeCustomizationDrawer != null) {
1186 String cur = savedInstanceState.getString("currentTab");
1187 if (cur != null) {
1188 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1189 }
1190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 }
1192
1193 @Override
1194 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001195 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196
1197 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1198 if (folders.size() > 0) {
1199 final int count = folders.size();
1200 long[] ids = new long[count];
1201 for (int i = 0; i < count; i++) {
1202 final FolderInfo info = folders.get(i).getInfo();
1203 ids[i] = info.id;
1204 }
1205 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1206 } else {
1207 super.onSaveInstanceState(outState);
1208 }
1209
Joe Onoratofb0ca672009-09-14 17:55:46 -04001210 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001211 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214
1215 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1216 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1217 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1218
1219 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1220 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1221 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1222 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1223 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1224 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1225 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1226 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001227 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229
1230 if (mFolderInfo != null && mWaitingForResult) {
1231 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1232 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1233 }
Michael Jurka946ad472010-07-09 18:05:18 -07001234
1235 if (mHomeCustomizationDrawer != null) {
1236 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1237 if (currentTabTag != null) {
1238 outState.putString("currentTab", currentTabTag);
1239 }
1240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242
1243 @Override
1244 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001248 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001250 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
1252
1253 TextKeyListener.getInstance().release();
1254
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001258
1259 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001260
Patrick Dubroyab962b72010-07-26 12:10:10 -07001261 // Some launcher layouts don't have a previous and next view
1262 if (mPreviousView != null) {
1263 dismissPreview(mPreviousView);
1264 }
1265 if (mNextView != null) {
1266 dismissPreview(mNextView);
1267 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001268
1269 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271
1272 @Override
1273 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001274 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 super.startActivityForResult(intent, requestCode);
1276 }
1277
1278 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001279 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001281
Joe Onorato7bb17492009-09-24 17:51:01 -07001282 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001283
Karl Rosaen138a0412009-04-23 19:00:21 -07001284 if (initialQuery == null) {
1285 // Use any text typed in the launcher as the initial query
1286 initialQuery = getTypedText();
1287 clearTypedText();
1288 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 if (appSearchData == null) {
1290 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001291 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
Romain Guycbb89e42009-06-08 15:52:54 -07001293
Karl Rosaen138a0412009-04-23 19:00:21 -07001294 final SearchManager searchManager =
1295 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001296 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001297 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299
1300 @Override
1301 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001302 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001303 return false;
1304 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305
1306 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1309 .setIcon(android.R.drawable.ic_menu_add)
1310 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001311 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 .setIcon(android.R.drawable.ic_menu_gallery)
1313 .setAlphabeticShortcut('W');
1314 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1315 .setIcon(android.R.drawable.ic_search_category_default)
1316 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1317 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1318 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1319 .setAlphabeticShortcut('N');
1320
1321 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001322 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1323 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324
1325 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1326 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1327 .setIntent(settings);
1328
1329 return true;
1330 }
1331
1332 @Override
1333 public boolean onPrepareOptionsMenu(Menu menu) {
1334 super.onPrepareOptionsMenu(menu);
1335
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001336 // If all apps is animating, don't show the menu, because we don't know
1337 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001338 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001339 return false;
1340 }
1341
1342 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001343 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001344 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1345 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1346
1347 // Disable add if the workspace is full.
1348 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001349 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001350 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
1353 return true;
1354 }
1355
Michael Jurka0e260592010-06-30 17:07:39 -07001356 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1357 // using the settings menu to add an item, something similar happens in showAddDialog
1358 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001359 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001360 mAddItemCellInfo = mMenuAddInfo;
1361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 @Override
1364 public boolean onOptionsItemSelected(MenuItem item) {
1365 switch (item.getItemId()) {
1366 case MENU_ADD:
1367 addItems();
1368 return true;
1369 case MENU_WALLPAPER_SETTINGS:
1370 startWallpaper();
1371 return true;
1372 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001373 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 return true;
1375 case MENU_NOTIFICATIONS:
1376 showNotifications();
1377 return true;
1378 }
1379
1380 return super.onOptionsItemSelected(item);
1381 }
Romain Guycbb89e42009-06-08 15:52:54 -07001382
Karl Rosaen138a0412009-04-23 19:00:21 -07001383 /**
1384 * Indicates that we want global search for this activity by setting the globalSearch
1385 * argument for {@link #startSearch} to true.
1386 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001388 @Override
1389 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001390 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001391 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001392 }
1393
Joe Onorato9c1289c2009-08-17 11:03:03 -04001394 public boolean isWorkspaceLocked() {
1395 return mWorkspaceLoading || mWaitingForResult;
1396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001399 if (LauncherApplication.isScreenXLarge()) {
1400 // Animate the widget chooser up from the bottom of the screen
1401 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001402 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001403 }
1404 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001405 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001406 showAddDialog(mMenuAddInfo);
1407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
1409
Michael Jurkaa63c4522010-08-19 13:52:27 -07001410 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1411 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001412 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001413
1414 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1415 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001416 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1417 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1418 addAppWidgetImpl(appWidgetId);
1419 }
1420
1421 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001422 // TODO: catch bad widget exception when sent
1423 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001424 // TODO: Is this log message meaningful?
1425 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1426 addAppWidgetImpl(appWidgetId);
1427 }
1428
1429 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001430 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
Bjorn Bringert7984c942009-12-09 15:38:25 +00001432 if (appWidget.configure != null) {
1433 // Launch over to configure widget, if needed
1434 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1435 intent.setComponent(appWidget.configure);
1436 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1437
Romain Guy8e633c52010-03-04 12:51:36 -08001438 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001440 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001441 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443 }
Romain Guycbb89e42009-06-08 15:52:54 -07001444
Joe Onoratodeb98af2010-02-19 14:59:39 -08001445 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001446 // Handle case where user selected "Applications"
1447 String applicationName = getResources().getString(R.string.group_applications);
1448 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001449
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001450 if (applicationName != null && applicationName.equals(shortcutName)) {
1451 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1452 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001453
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001454 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1455 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001456 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001457 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001458 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
Winson Chung24ab2f12010-09-16 14:10:47 -07001462 void processWallpaper(Intent intent) {
1463 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1464 }
1465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001467 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001468 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001469 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001470
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001471 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001473 } else {
1474 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 UserFolderInfo folderInfo = new UserFolderInfo();
1480 folderInfo.title = getText(R.string.folder_name);
1481
1482 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001483 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 if (!findSingleSlot(cellInfo)) return;
1485
1486 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001487 LauncherModel.addItemToDatabase(this, folderInfo,
1488 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001489 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001490 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491
1492 // Create the view
1493 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001494 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001496 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
Romain Guycbb89e42009-06-08 15:52:54 -07001498
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001500 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 }
1502
1503 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001504 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 if (!findSingleSlot(cellInfo)) return;
1506
1507 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1508
1509 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001511 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1513 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515 }
1516
1517 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1518 CellLayout.CellInfo cellInfo, boolean notify) {
1519
1520 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1521 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1522
1523 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 Intent.ShortcutIconResource iconResource = null;
1525
1526 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1527 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1528 try {
1529 iconResource = (Intent.ShortcutIconResource) extra;
1530 final PackageManager packageManager = context.getPackageManager();
1531 Resources resources = packageManager.getResourcesForApplication(
1532 iconResource.packageName);
1533 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1534 icon = resources.getDrawable(id);
1535 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001536 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538 }
1539
1540 if (icon == null) {
1541 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1542 }
1543
1544 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001545 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 info.title = name;
1547 info.iconResource = iconResource;
1548 info.uri = data.getData();
1549 info.baseIntent = baseIntent;
1550 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1551 LiveFolders.DISPLAY_MODE_GRID);
1552
1553 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1554 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001555 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
1557 return info;
1558 }
1559
1560 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1561 final int[] xy = new int[2];
1562 if (findSlot(cellInfo, xy, 1, 1)) {
1563 cellInfo.cellX = xy[0];
1564 cellInfo.cellY = xy[1];
1565 return true;
1566 }
1567 return false;
1568 }
1569
1570 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1571 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001572 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1573 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1575 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1576 return false;
1577 }
1578 }
1579 return true;
1580 }
1581
1582 private void showNotifications() {
1583 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1584 if (statusBar != null) {
1585 statusBar.expand();
1586 }
1587 }
1588
1589 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001590 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001592 Intent chooser = Intent.createChooser(pickWallpaper,
1593 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001594 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1595 // Removed in Eclair MR1
1596// WallpaperManager wm = (WallpaperManager)
1597// getSystemService(Context.WALLPAPER_SERVICE);
1598// WallpaperInfo wi = wm.getWallpaperInfo();
1599// if (wi != null && wi.getSettingsActivity() != null) {
1600// LabeledIntent li = new LabeledIntent(getPackageName(),
1601// R.string.configure_wallpaper, 0);
1602// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1603// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1604// }
Mike Clerona0618e42009-10-22 13:55:21 -07001605 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
1607
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001608 /**
1609 * Registers various content observers. The current implementation registers
1610 * only a favorites observer to keep track of the favorites applications.
1611 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001612 private void registerContentObservers() {
1613 ContentResolver resolver = getContentResolver();
1614 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1615 true, mWidgetObserver);
1616 }
1617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 @Override
1619 public boolean dispatchKeyEvent(KeyEvent event) {
1620 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1621 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001623 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001624 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001625 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001626 dumpState();
1627 return true;
1628 }
1629 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001630 }
1631 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1632 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001633 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 return true;
1635 }
1636 }
1637
1638 return super.dispatchKeyEvent(event);
1639 }
1640
Joe Onorato88ec0992009-11-19 13:16:06 -08001641 @Override
1642 public void onBackPressed() {
1643 if (isAllAppsVisible()) {
1644 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001645 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001646 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001647 } else {
1648 closeFolder();
1649 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001650 // Some launcher layouts don't have a previous and next view
1651 if (mPreviousView != null) {
1652 dismissPreview(mPreviousView);
1653 dismissPreview(mNextView);
1654 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 private void closeFolder() {
1658 Folder folder = mWorkspace.getOpenFolder();
1659 if (folder != null) {
1660 closeFolder(folder);
1661 }
1662 }
1663
1664 void closeFolder(Folder folder) {
1665 folder.getInfo().opened = false;
1666 ViewGroup parent = (ViewGroup) folder.getParent();
1667 if (parent != null) {
1668 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001669 if (folder instanceof DropTarget) {
1670 // Live folders aren't DropTargets.
1671 mDragController.removeDropTarget((DropTarget)folder);
1672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674 folder.onClose();
1675 }
1676
1677 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001678 * Re-listen when widgets are reset.
1679 */
1680 private void onAppWidgetReset() {
1681 mAppWidgetHost.startListening();
1682 }
1683
1684 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001685 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1686 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001688 private void unbindDesktopItems() {
1689 for (ItemInfo item: mDesktopItems) {
1690 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
1692 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001693
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 /**
1695 * Launches the intent referred by the clicked shortcut.
1696 *
1697 * @param v The view representing the clicked shortcut.
1698 */
1699 public void onClick(View v) {
1700 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001701 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001703 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001704 int[] pos = new int[2];
1705 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001706 intent.setSourceBounds(new Rect(pos[0], pos[1],
1707 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001708 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 } else if (tag instanceof FolderInfo) {
1710 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001711 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001712 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001713 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001714 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001715 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718 }
1719
Michael Jurka0e260592010-06-30 17:07:39 -07001720 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001721 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001722 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001723 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001724 return false;
1725 }
1726
Michael Jurkaaf442092010-06-10 17:01:57 -07001727 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001728 * Event handler for the search button
1729 *
1730 * @param v The view that was clicked.
1731 */
1732 public void onClickSearchButton(View v) {
1733 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1734 View button = findViewById(R.id.search_button);
1735 i.setSourceBounds(
1736 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1737 startActivity(i);
1738 }
1739
1740 /**
1741 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001742 * enters home screen customization mode.
1743 *
1744 * @param v The view that was clicked.
1745 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001746 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001747 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001748 }
1749
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001750 /**
1751 * Event handler for the "grid" button that appears on the home screen, which
1752 * enters all apps mode.
1753 *
1754 * @param v The view that was clicked.
1755 */
1756 public void onClickAllAppsButton(View v) {
1757 showAllApps(true);
1758 }
1759
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001760 public void onClickAppMarketButton(View v) {
1761 if (mAppMarketIntent != null) {
1762 startActivitySafely(mAppMarketIntent, "app market");
1763 }
1764 }
1765
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001766 void startApplicationDetailsActivity(ComponentName componentName) {
1767 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001768 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1769 Uri.fromParts("package", packageName, null));
1770 startActivity(intent);
1771 }
1772
Patrick Dubroy5539af72010-09-07 15:22:01 -07001773 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1774 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1775 // System applications cannot be installed. For now, show a toast explaining that.
1776 // We may give them the option of disabling apps this way.
1777 int messageId = R.string.uninstall_system_app_text;
1778 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1779 } else {
1780 String packageName = appInfo.componentName.getPackageName();
1781 String className = appInfo.componentName.getClassName();
1782 Intent intent = new Intent(
1783 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1784 startActivity(intent);
1785 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001786 }
1787
Joe Onoratof984e852010-03-25 09:47:45 -07001788 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1790 try {
1791 startActivity(intent);
1792 } catch (ActivityNotFoundException e) {
1793 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001794 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 } catch (SecurityException e) {
1796 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001797 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001799 "or use the exported attribute for this activity. "
1800 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 }
1802 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001803
Romain Guy8e633c52010-03-04 12:51:36 -08001804 void startActivityForResultSafely(Intent intent, int requestCode) {
1805 try {
1806 startActivityForResult(intent, requestCode);
1807 } catch (ActivityNotFoundException e) {
1808 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1809 } catch (SecurityException e) {
1810 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1811 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1812 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1813 "or use the exported attribute for this activity.", e);
1814 }
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816
1817 private void handleFolderClick(FolderInfo folderInfo) {
1818 if (!folderInfo.opened) {
1819 // Close any open folder
1820 closeFolder();
1821 // Open the requested folder
1822 openFolder(folderInfo);
1823 } else {
1824 // Find the open folder...
1825 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1826 int folderScreen;
1827 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001828 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 // .. and close it
1830 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001831 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 // Close any folder open on the current screen
1833 closeFolder();
1834 // Pull the folder onto this screen
1835 openFolder(folderInfo);
1836 }
1837 }
1838 }
1839 }
1840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 /**
1842 * Opens the user fodler described by the specified tag. The opening of the folder
1843 * is animated relative to the specified View. If the View is null, no animation
1844 * is played.
1845 *
1846 * @param folderInfo The FolderInfo describing the folder to open.
1847 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001848 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 Folder openFolder;
1850
1851 if (folderInfo instanceof UserFolderInfo) {
1852 openFolder = UserFolder.fromXml(this);
1853 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001854 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 } else {
1856 return;
1857 }
1858
Joe Onorato00acb122009-08-04 16:04:30 -04001859 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 openFolder.setLauncher(this);
1861
1862 openFolder.bind(folderInfo);
1863 folderInfo.opened = true;
1864
Winson Chungaafa03c2010-06-11 17:34:16 -07001865 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 openFolder.onOpen();
1868 }
1869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001871 switch (v.getId()) {
1872 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001873 if (!isAllAppsVisible()) {
1874 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1875 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001876 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001877 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001878 return true;
1879 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001880 if (!isAllAppsVisible()) {
1881 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1882 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001883 showPreviews(v);
1884 }
1885 return true;
1886 case R.id.all_apps_button:
1887 if (!isAllAppsVisible()) {
1888 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1889 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1890 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001891 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001892 return true;
1893 }
1894
Joe Onorato9c1289c2009-08-17 11:03:03 -04001895 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 return false;
1897 }
1898
1899 if (!(v instanceof CellLayout)) {
1900 v = (View) v.getParent();
1901 }
1902
1903 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1904
1905 // This happens when long clicking an item with the dpad/trackball
1906 if (cellInfo == null) {
1907 return true;
1908 }
1909
1910 if (mWorkspace.allowLongPress()) {
1911 if (cellInfo.cell == null) {
1912 if (cellInfo.valid) {
1913 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001914 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001915 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1916 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 showAddDialog(cellInfo);
1918 }
1919 } else {
1920 if (!(cellInfo.cell instanceof Folder)) {
1921 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001922 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1923 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 mWorkspace.startDrag(cellInfo);
1925 }
1926 }
1927 }
1928 return true;
1929 }
1930
Romain Guye6b8e2f2009-11-10 11:56:55 -08001931 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001932 private void dismissPreview(final View v) {
1933 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001934 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001935 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1936 public void onDismiss() {
1937 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1938 int count = group.getChildCount();
1939 for (int i = 0; i < count; i++) {
1940 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1941 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001942 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1943 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1944
1945 v.setTag(R.id.workspace, null);
1946 v.setTag(R.id.icon, null);
1947 window.setOnDismissListener(null);
1948 }
1949 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001950 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001951 }
1952 v.setTag(null);
1953 }
1954
Romain Guyf8e6a802009-12-07 17:48:02 -08001955 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001956 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001957 }
1958
Romain Guya6abce82009-11-10 02:54:41 -08001959 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001960 final Resources resources = getResources();
1961 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001962
Romain Guya6abce82009-11-10 02:54:41 -08001963 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001964
Romain Guy9d31e9f2009-11-11 18:54:28 -08001965 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001966
Romain Guyf8e6a802009-12-07 17:48:02 -08001967 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001968 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001969 int extraW = (int) ((r.left + r.right) * max);
1970 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001971
1972 int aW = cell.getWidth() - extraW;
1973 float w = aW / max;
1974
1975 int width = cell.getWidth();
1976 int height = cell.getHeight();
1977 int x = cell.getLeftPadding();
1978 int y = cell.getTopPadding();
1979 width -= (x + cell.getRightPadding());
1980 height -= (y + cell.getBottomPadding());
1981
1982 float scale = w / width;
1983
1984 int count = end - start;
1985
1986 final float sWidth = width * scale;
1987 float sHeight = height * scale;
1988
Romain Guye6b8e2f2009-11-10 11:56:55 -08001989 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001990
Romain Guye6b8e2f2009-11-10 11:56:55 -08001991 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1992 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001993
1994 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001995 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001996 cell = (CellLayout) workspace.getChildAt(i);
1997
Romain Guyf8e6a802009-12-07 17:48:02 -08001998 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001999 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002000
2001 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002002 c.scale(scale, scale);
2003 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
2004 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08002005
Romain Guy9d31e9f2009-11-11 18:54:28 -08002006 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002007 image.setImageBitmap(bitmap);
2008 image.setTag(i);
2009 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002010 image.setOnFocusChangeListener(handler);
2011 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002012 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002013
2014 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002015 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2016
Winson Chungaafa03c2010-06-11 17:34:16 -07002017 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002018 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002019
2020 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002021 p.setContentView(preview);
2022 p.setWidth((int) (sWidth * count + extraW));
2023 p.setHeight((int) (sHeight + extraH));
2024 p.setAnimationStyle(R.style.AnimationPreview);
2025 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002026 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002027 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002028 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002029
Romain Guye6b8e2f2009-11-10 11:56:55 -08002030 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2031 public void onDismiss() {
2032 dismissPreview(anchor);
2033 }
2034 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002035
2036 anchor.setTag(p);
2037 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002038 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002039 }
2040
Romain Guy9d31e9f2009-11-11 18:54:28 -08002041 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002042 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002043
Romain Guye6b8e2f2009-11-10 11:56:55 -08002044 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002045 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002046 }
2047
2048 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002049 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002050 v.post(this);
2051 }
2052
2053 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002054 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002055 }
2056
2057 public void onFocusChange(View v, boolean hasFocus) {
2058 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002059 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002060 }
Romain Guya6abce82009-11-10 02:54:41 -08002061 }
2062 }
2063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 Workspace getWorkspace() {
2065 return mWorkspace;
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 @Override
2069 protected Dialog onCreateDialog(int id) {
2070 switch (id) {
2071 case DIALOG_CREATE_SHORTCUT:
2072 return new CreateShortcut().createDialog();
2073 case DIALOG_RENAME_FOLDER:
2074 return new RenameFolder().createDialog();
2075 }
2076
2077 return super.onCreateDialog(id);
2078 }
2079
2080 @Override
2081 protected void onPrepareDialog(int id, Dialog dialog) {
2082 switch (id) {
2083 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 break;
2085 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002086 if (mFolderInfo != null) {
2087 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2088 final CharSequence text = mFolderInfo.title;
2089 input.setText(text);
2090 input.setSelection(0, text.length());
2091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 break;
2093 }
2094 }
2095
2096 void showRenameDialog(FolderInfo info) {
2097 mFolderInfo = info;
2098 mWaitingForResult = true;
2099 showDialog(DIALOG_RENAME_FOLDER);
2100 }
2101
2102 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2103 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002104 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 mWaitingForResult = true;
2106 showDialog(DIALOG_CREATE_SHORTCUT);
2107 }
2108
Joe Onoratodeb98af2010-02-19 14:59:39 -08002109 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002110 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002111 Bundle bundle = new Bundle();
2112
2113 ArrayList<String> shortcutNames = new ArrayList<String>();
2114 shortcutNames.add(getString(R.string.group_applications));
2115 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2116
2117 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2118 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2119 R.drawable.ic_launcher_application));
2120 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2121
2122 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2123 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002124 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002125 pickIntent.putExtras(bundle);
2126
Joe Onoratodeb98af2010-02-19 14:59:39 -08002127 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002128 }
2129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 private class RenameFolder {
2131 private EditText mInput;
2132
2133 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2135 mInput = (EditText) layout.findViewById(R.id.folder_name);
2136
2137 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2138 builder.setIcon(0);
2139 builder.setTitle(getString(R.string.rename_folder_title));
2140 builder.setCancelable(true);
2141 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2142 public void onCancel(DialogInterface dialog) {
2143 cleanup();
2144 }
2145 });
2146 builder.setNegativeButton(getString(R.string.cancel_action),
2147 new Dialog.OnClickListener() {
2148 public void onClick(DialogInterface dialog, int which) {
2149 cleanup();
2150 }
2151 }
2152 );
2153 builder.setPositiveButton(getString(R.string.rename_action),
2154 new Dialog.OnClickListener() {
2155 public void onClick(DialogInterface dialog, int which) {
2156 changeFolderName();
2157 }
2158 }
2159 );
2160 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002161
2162 final AlertDialog dialog = builder.create();
2163 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2164 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002165 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002166 mInput.requestFocus();
2167 InputMethodManager inputManager = (InputMethodManager)
2168 getSystemService(Context.INPUT_METHOD_SERVICE);
2169 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002170 }
2171 });
2172
2173 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 }
2175
2176 private void changeFolderName() {
2177 final String name = mInput.getText().toString();
2178 if (!TextUtils.isEmpty(name)) {
2179 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002180 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 mFolderInfo.title = name;
2182 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2183
Joe Onorato9c1289c2009-08-17 11:03:03 -04002184 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002185 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 } else {
2188 final FolderIcon folderIcon = (FolderIcon)
2189 mWorkspace.getViewForTag(mFolderInfo);
2190 if (folderIcon != null) {
2191 folderIcon.setText(name);
2192 getWorkspace().requestLayout();
2193 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002194 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002195 mWorkspaceLoading = true;
2196 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198 }
2199 }
2200 cleanup();
2201 }
2202
2203 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 dismissDialog(DIALOG_RENAME_FOLDER);
2205 mWaitingForResult = false;
2206 mFolderInfo = null;
2207 }
2208 }
2209
Daniel Sandler843e8602010-06-07 14:59:01 -04002210 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2211 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002212 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002213 }
2214
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002215 // AllAppsView.Watcher
2216 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002217 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2218 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002219 mWorkspace.setVisibility(View.GONE);
2220 }
2221 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002222
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002223 private void showToolbarButton(View button) {
2224 button.setAlpha(1.0f);
2225 button.setVisibility(View.VISIBLE);
2226 button.setFocusable(true);
2227 button.setClickable(true);
2228 }
2229
2230 private void hideToolbarButton(View button) {
2231 button.setAlpha(0.0f);
2232 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2233 button.setVisibility(View.INVISIBLE);
2234 button.setFocusable(false);
2235 button.setClickable(false);
2236 }
2237
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002238 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002239 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002240 *
2241 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2242 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002243 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002244 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002245 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002246 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002247 final boolean showing = show;
2248 final boolean hiding = !show;
2249
2250 final int duration = show ?
2251 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2252 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2253
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002254 if (seq != null) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002255 Animator anim = new ObjectAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2256 anim.addListener(new AnimatorListenerAdapter() {
2257 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002259 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002260 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002261 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002262 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002263 });
2264 seq.play(anim);
2265 } else {
2266 if (showing) {
2267 showToolbarButton(view);
2268 } else {
2269 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002270 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002271 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002272 }
2273
2274 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002275 * Show/hide the appropriate toolbar buttons for newState.
2276 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002277 *
2278 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002279 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2280 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002281 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002282 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002283 final View searchButton = findViewById(R.id.search_button);
2284 final View allAppsButton = findViewById(R.id.all_apps_button);
2285 final View marketButton = findViewById(R.id.market_button);
2286 final View configureButton = findViewById(R.id.configure_button);
2287
2288 switch (newState) {
2289 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002290 hideOrShowToolbarButton(true, searchButton, showSeq);
2291 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2292 hideOrShowToolbarButton(true, configureButton, showSeq);
2293 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002294 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002295 break;
2296 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002297 hideOrShowToolbarButton(true, configureButton, showSeq);
2298 hideOrShowToolbarButton(true, marketButton, showSeq);
2299 hideOrShowToolbarButton(false, searchButton, hideSeq);
2300 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002301 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002302 break;
2303 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002304 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2305 hideOrShowToolbarButton(false, searchButton, hideSeq);
2306 hideOrShowToolbarButton(false, marketButton, hideSeq);
2307 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002308 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002309 break;
2310 }
2311 }
2312
2313 /**
2314 * Helper method for the cameraZoomIn/cameraZoomOut animations
2315 * @param view The view being animated
2316 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2317 * @param scaleFactor The scale factor used for the zoom
2318 */
2319 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2320 final int height = view.getHeight();
2321 view.setPivotX(view.getWidth() / 2.0f);
2322 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2323 // Assumes that the view is normally anchored to either the top or bottom of the screen
2324 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2325 if (state == State.ALL_APPS) {
2326 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2327 } else {
2328 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2329 }
2330 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002331
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002332 /**
2333 * Zoom the camera out from the workspace to reveal 'toView'.
2334 * Assumes that the view to show is anchored at either the very top or very bottom
2335 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002336 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002337 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002338 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339 final Resources res = getResources();
2340 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2341 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 final boolean toAllApps = (toState == State.ALL_APPS);
2343 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002344
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002345 setPivotsForZoom(toView, toState, scale);
2346
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 if (toState == State.ALL_APPS) {
2348 mWorkspace.shrinkToBottom(animated);
2349 } else {
2350 mWorkspace.shrinkToTop(animated);
2351 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002352
2353 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002354 ValueAnimator scaleAnim = new ObjectAnimator(duration, toView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2356 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2357 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002358 scaleAnim.addListener(new AnimatorListenerAdapter() {
2359 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002360 // Prepare the position
2361 toView.setTranslationX(0.0f);
2362 toView.setTranslationY(0.0f);
2363 toView.setVisibility(View.VISIBLE);
2364 }
2365 });
2366
Chet Haaseb1254a62010-09-07 13:35:00 -07002367 AnimatorSet toolbarHideAnim = new AnimatorSet();
2368 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002369 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2370
2371 // toView should appear right at the end of the workspace shrink animation
2372 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2373
Chet Haaseb1254a62010-09-07 13:35:00 -07002374 AnimatorSet s = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002375 s.playTogether(scaleAnim, toolbarHideAnim);
2376 s.play(scaleAnim).after(startDelay);
2377
2378 // Show the new toolbar buttons just as the main animation is ending
2379 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2380 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2381 s.start();
2382 } else {
2383 toView.setTranslationX(0.0f);
2384 toView.setTranslationY(0.0f);
2385 toView.setScaleX(1.0f);
2386 toView.setScaleY(1.0f);
2387 toView.setVisibility(View.VISIBLE);
2388 hideAndShowToolbarButtons(toState, null, null);
2389 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002390 }
2391
2392 /**
2393 * Zoom the camera back into the workspace, hiding 'fromView'.
2394 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002395 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2396 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002397 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002398 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002399 Resources res = getResources();
2400 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2401 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002402 final View fromView =
2403 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2404
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002405 mCustomizePagedView.endChoiceMode();
2406 mAllAppsPagedView.endChoiceMode();
2407
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002408 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002409
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002410 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002411
2412 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002413 AnimatorSet s = new AnimatorSet();
2414 ValueAnimator scaleAnim = new ObjectAnimator(duration, fromView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2416 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2417 scaleAnim.setInterpolator(new AccelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002418 s.addListener(new AnimatorListenerAdapter() {
2419 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002420 fromView.setVisibility(View.GONE);
2421 fromView.setScaleX(1.0f);
2422 fromView.setScaleY(1.0f);
2423 }
2424 });
2425
Chet Haaseb1254a62010-09-07 13:35:00 -07002426 AnimatorSet toolbarHideAnim = new AnimatorSet();
2427 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002428 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2429
2430 s.playTogether(scaleAnim, toolbarHideAnim);
2431
2432 // Show the new toolbar buttons at the very end of the whole animation
2433 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2434 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2435 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2436 s.start();
2437 } else {
2438 fromView.setVisibility(View.GONE);
2439 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2440 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002441 }
2442
2443 /**
2444 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2445 * This is the transition used on xlarge screens to go between all apps and
2446 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002447 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2448 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2449 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002450 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002451 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002452 final Resources res = getResources();
2453 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454 final int workspaceHeight = mWorkspace.getHeight();
2455
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002456 final boolean fromAllApps = (fromState == State.ALL_APPS);
2457 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2458 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002459
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002460 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2461 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2462 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2463 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002464
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002465 mCustomizePagedView.endChoiceMode();
2466 mAllAppsPagedView.endChoiceMode();
2467
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002468 if (toState == State.ALL_APPS) {
2469 mWorkspace.shrinkToBottom(animated);
2470 } else {
2471 mWorkspace.shrinkToTop(animated);
2472 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473
2474 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002475 AnimatorSet s = new AnimatorSet();
2476 s.addListener(new AnimatorListenerAdapter() {
2477 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002478 toView.setVisibility(View.VISIBLE);
2479 toView.setY(toViewStartY);
2480 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002481 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002482 fromView.setVisibility(View.GONE);
2483 }
2484 });
2485
Chet Haaseb1254a62010-09-07 13:35:00 -07002486 AnimatorSet toolbarHideAnim = new AnimatorSet();
2487 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002488 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2489
2490 s.playTogether(
2491 toolbarHideAnim,
Chet Haaseb1254a62010-09-07 13:35:00 -07002492 new ObjectAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2493 new ObjectAnimator(duration, toView, "y", toViewStartY, toViewEndY));
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002494
2495 // Show the new toolbar buttons just as the main animation is ending
2496 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2497 s.play(toolbarShowAnim).after(duration - fadeInTime);
2498 s.start();
2499 } else {
2500 fromView.setY(fromViewEndY);
2501 fromView.setVisibility(View.GONE);
2502 toView.setY(toViewEndY);
2503 toView.setVisibility(View.VISIBLE);
2504 hideAndShowToolbarButtons(toState, null, null);
2505 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002506 }
2507
2508 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002509 if (mAllAppsGrid.isVisible())
2510 return;
2511
Michael Jurka79212d82010-07-30 16:36:20 -07002512 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002513 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002514 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002515 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002516 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002517 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002518 } else {
2519 mAllAppsGrid.zoom(1.0f, animated);
2520 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002521
Romain Guyc16fea72010-03-12 17:17:56 -08002522 ((View) mAllAppsGrid).setFocusable(true);
2523 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002524
Joe Onorato7c312c12009-08-13 21:36:53 -07002525 // TODO: fade these two too
2526 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002527 }
2528
Joe Onoratob2061212009-11-24 16:13:54 -05002529 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002530 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002531 * - Home from workspace
2532 * - from center screen
2533 * - from other screens
2534 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002535 * - from center screen
2536 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002537 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002538 * - from center screen
2539 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002540 * - Launch app from workspace and quit
2541 * - with back
2542 * - with home
2543 * - Launch app from all apps and quit
2544 * - with back
2545 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002546 * - Go to a screen that's not the default, then all
2547 * apps, and launch and app, and go back
2548 * - with back
2549 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002550 * - On workspace, long press power and go back
2551 * - with back
2552 * - with home
2553 * - On all apps, long press power and go back
2554 * - with back
2555 * - with home
2556 * - On workspace, power off
2557 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002558 * - Launch an app and turn off the screen while in that app
2559 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002560 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002561 * - From all apps
2562 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002563 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2564 * - From all apps
2565 * - From the center workspace
2566 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002567 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002568 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002569 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002570 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002571 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002572 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002573 } else {
2574 mAllAppsGrid.zoom(0.0f, animated);
2575 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002576 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002577 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002578 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002579 }
2580
Joe Onorato7c312c12009-08-13 21:36:53 -07002581 void lockAllApps() {
2582 // TODO
2583 }
2584
2585 void unlockAllApps() {
2586 // TODO
2587 }
2588
Patrick Dubroy558654c2010-07-23 16:48:11 -07002589 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002590 return mHomeCustomizationDrawer != null &&
2591 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002592 }
2593
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594 // Show the customization drawer (only exists in x-large configuration)
2595 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002596 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002597 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002600 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002601 }
2602
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 // Hide the customization drawer (only exists in x-large configuration)
2604 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002605 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002607 }
2608 }
2609
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002610 void onWorkspaceClick(CellLayout layout) {
2611 Object itemInfo = mAllAppsPagedView.getChosenItem();
2612 if (itemInfo == null) {
2613 itemInfo = mCustomizePagedView.getChosenItem();
Michael Jurka213d9632010-07-28 11:29:25 -07002614 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002615
2616 if (itemInfo == null) {
2617 // No items are chosen in All Apps or Customize, so just zoom into the workspace
2618 mWorkspace.unshrink(layout);
2619 if (isAllAppsVisible()) {
2620 closeAllApps(true);
2621 } else if (isCustomizationDrawerVisible()) {
2622 hideCustomizationDrawer(true);
2623 }
2624 } else {
2625 // Act as if the chosen item was dropped on the given CellLayout
2626 if (mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2627 mAllAppsPagedView.endChoiceMode();
2628 mCustomizePagedView.endChoiceMode();
2629 } else {
2630 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
2631 }
Michael Jurka213d9632010-07-28 11:29:25 -07002632 }
2633 }
2634
Joe Onorato7404ee42009-07-31 11:54:44 -07002635 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002636 * Sets the app market icon (shown when all apps is visible on x-large screens)
2637 */
2638 private void updateAppMarketIcon() {
2639 if (LauncherApplication.isScreenXLarge()) {
2640 // Find the app market activity by resolving an intent.
2641 // (If multiple app markets are installed, it will return the ResolverActivity.)
2642 PackageManager packageManager = getPackageManager();
2643 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2644 ComponentName activityName = intent.resolveActivity(getPackageManager());
2645 if (activityName != null) {
2646 mAppMarketIntent = intent;
2647 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2648 Drawable toolbarIcon = null;
2649 try {
2650 // Look for the toolbar icon specified in the activity meta-data
2651 Bundle metaData = packageManager.getActivityInfo(
2652 activityName, PackageManager.GET_META_DATA).metaData;
2653 if (metaData != null) {
2654 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2655 if (iconResId != 0) {
2656 Resources res = packageManager.getResourcesForActivity(activityName);
2657 toolbarIcon = res.getDrawable(iconResId);
2658 }
2659 }
2660 } catch (NameNotFoundException e) {
2661 // Do nothing
2662 }
2663 // If we were unable to find the icon via the meta-data, use a generic one
2664 if (toolbarIcon == null) {
2665 marketButton.setImageResource(R.drawable.app_market_generic);
2666 } else {
2667 marketButton.setImageDrawable(toolbarIcon);
2668 }
2669 } else {
2670 mAppMarketIntent = null;
2671 }
2672 }
2673 }
2674
2675 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 * Displays the shortcut creation dialog and launches, if necessary, the
2677 * appropriate activity.
2678 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002679 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002680 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2681 DialogInterface.OnShowListener {
2682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002686 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2689 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002690 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 builder.setInverseBackgroundForced(true);
2693
2694 AlertDialog dialog = builder.create();
2695 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002696 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002697 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002699 return dialog;
2700 }
2701
2702 public void onCancel(DialogInterface dialog) {
2703 mWaitingForResult = false;
2704 cleanup();
2705 }
2706
Romain Guycbb89e42009-06-08 15:52:54 -07002707 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002708 }
2709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002711 try {
2712 dismissDialog(DIALOG_CREATE_SHORTCUT);
2713 } catch (Exception e) {
2714 // An exception is thrown if the dialog is not visible, which is fine
2715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 }
2717
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002718 /**
2719 * Handle the action clicked in the "Add to home" dialog.
2720 */
2721 public void onClick(DialogInterface dialog, int which) {
2722 Resources res = getResources();
2723 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002724
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002725 switch (which) {
2726 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002727 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002728 break;
2729 }
Romain Guycbb89e42009-06-08 15:52:54 -07002730
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002731 case AddAdapter.ITEM_APPWIDGET: {
2732 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002733
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002734 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2735 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002736 // start the pick activity
2737 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2738 break;
2739 }
Romain Guycbb89e42009-06-08 15:52:54 -07002740
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002741 case AddAdapter.ITEM_LIVE_FOLDER: {
2742 // Insert extra item to handle inserting folder
2743 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002744
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002745 ArrayList<String> shortcutNames = new ArrayList<String>();
2746 shortcutNames.add(res.getString(R.string.group_folder));
2747 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002748
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002749 ArrayList<ShortcutIconResource> shortcutIcons =
2750 new ArrayList<ShortcutIconResource>();
2751 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2752 R.drawable.ic_launcher_folder));
2753 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2754
2755 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2756 pickIntent.putExtra(Intent.EXTRA_INTENT,
2757 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2758 pickIntent.putExtra(Intent.EXTRA_TITLE,
2759 getText(R.string.title_select_live_folder));
2760 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002761
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002762 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2763 break;
2764 }
2765
2766 case AddAdapter.ITEM_WALLPAPER: {
2767 startWallpaper();
2768 break;
2769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770 }
2771 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002772
2773 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002774 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002776 }
2777
2778 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002779 * Receives notifications when applications are added/removed.
2780 */
2781 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2782 @Override
2783 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002784 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002785 String reason = intent.getStringExtra("reason");
2786 if (!"homekey".equals(reason)) {
2787 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002788 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002789 animate = false;
2790 }
Joe Onoratob2061212009-11-24 16:13:54 -05002791 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002792 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002793 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002794 }
2795 }
2796
2797 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002798 * Receives notifications whenever the appwidgets are reset.
2799 */
2800 private class AppWidgetResetObserver extends ContentObserver {
2801 public AppWidgetResetObserver() {
2802 super(new Handler());
2803 }
2804
2805 @Override
2806 public void onChange(boolean selfChange) {
2807 onAppWidgetReset();
2808 }
2809 }
2810
2811 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002812 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002813 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002815 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002816 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002817 } else {
2818 return SCREEN_COUNT / 2;
2819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002821
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002822 void setAllAppsPagedView(PagedView view) {
2823 mAllAppsPagedView = view;
2824 }
2825
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 /**
2827 * Refreshes the shortcuts shown on the workspace.
2828 *
2829 * Implementation of the method from LauncherModel.Callbacks.
2830 */
2831 public void startBinding() {
2832 final Workspace workspace = mWorkspace;
2833 int count = workspace.getChildCount();
2834 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002835 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002836 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2837 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002838
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839 if (DEBUG_USER_INTERFACE) {
2840 android.widget.Button finishButton = new android.widget.Button(this);
2841 finishButton.setText("Finish");
2842 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2843
2844 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2845 public void onClick(View v) {
2846 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002847 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 }
2850 }
2851
2852 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 * Bind the items start-end from the list.
2854 *
2855 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2858
2859 final Workspace workspace = mWorkspace;
2860
2861 for (int i=start; i<end; i++) {
2862 final ItemInfo item = shortcuts.get(i);
2863 mDesktopItems.add(item);
2864 switch (item.itemType) {
2865 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2866 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002867 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2869 false);
2870 break;
2871 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2872 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002873 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002874 (UserFolderInfo) item);
2875 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2876 false);
2877 break;
2878 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2879 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2880 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002881 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 (LiveFolderInfo) item);
2883 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2884 false);
2885 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887 }
2888
Joe Onorato9c1289c2009-08-17 11:03:03 -04002889 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 }
2891
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 /**
2893 * Implementation of the method from LauncherModel.Callbacks.
2894 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002895 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002896 sFolders.clear();
2897 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899
2900 /**
2901 * Add the views for a widget to the workspace.
2902 *
2903 * Implementation of the method from LauncherModel.Callbacks.
2904 */
2905 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002906 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2907 if (DEBUG_WIDGETS) {
2908 Log.d(TAG, "bindAppWidget: " + item);
2909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 final Workspace workspace = mWorkspace;
2911
2912 final int appWidgetId = item.appWidgetId;
2913 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002914 if (DEBUG_WIDGETS) {
2915 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2916 }
2917
Joe Onorato9c1289c2009-08-17 11:03:03 -04002918 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2919
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2921 item.hostView.setTag(item);
2922
2923 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2924 item.cellY, item.spanX, item.spanY, false);
2925
2926 workspace.requestLayout();
2927
2928 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002929
2930 if (DEBUG_WIDGETS) {
2931 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2932 + (SystemClock.uptimeMillis()-start) + "ms");
2933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002934 }
2935
2936 /**
2937 * Callback saying that there aren't any more items to bind.
2938 *
2939 * Implementation of the method from LauncherModel.Callbacks.
2940 */
2941 public void finishBindingItems() {
2942 if (mSavedState != null) {
2943 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002944 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 }
2946
2947 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2948 if (userFolders != null) {
2949 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002950 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 if (info != null) {
2952 openFolder(info);
2953 }
2954 }
2955 final Folder openFolder = mWorkspace.getOpenFolder();
2956 if (openFolder != null) {
2957 openFolder.requestFocus();
2958 }
2959 }
2960
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 mSavedState = null;
2962 }
2963
2964 if (mSavedInstanceState != null) {
2965 super.onRestoreInstanceState(mSavedInstanceState);
2966 mSavedInstanceState = null;
2967 }
2968
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969 mWorkspaceLoading = false;
2970 }
2971
2972 /**
2973 * Add the icons for all apps.
2974 *
2975 * Implementation of the method from LauncherModel.Callbacks.
2976 */
2977 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002978 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002979 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002980 }
2981
2982 /**
2983 * A package was installed.
2984 *
2985 * Implementation of the method from LauncherModel.Callbacks.
2986 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002987 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002989 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002990 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002991 }
2992
2993 /**
2994 * A package was updated.
2995 *
2996 * Implementation of the method from LauncherModel.Callbacks.
2997 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002998 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003000 mWorkspace.updateShortcuts(apps);
3001 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003002 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 }
3004
3005 /**
3006 * A package was uninstalled.
3007 *
3008 * Implementation of the method from LauncherModel.Callbacks.
3009 */
Joe Onorato36115782010-06-17 13:28:48 -04003010 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003011 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003012 if (permanent) {
3013 mWorkspace.removeItems(apps);
3014 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003015 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003016 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003017 }
Joe Onoratobe386092009-11-17 17:32:16 -08003018
3019 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003020 * A number of packages were updated.
3021 */
3022 public void bindPackagesUpdated() {
3023 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07003024 if (mCustomizePagedView != null)
3025 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07003026 }
3027
3028 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003029 * Prints out out state for debugging.
3030 */
3031 public void dumpState() {
3032 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003033 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003034 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3035 Log.d(TAG, "mRestoring=" + mRestoring);
3036 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3037 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3038 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003039 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003040 mModel.dumpState();
3041 mAllAppsGrid.dumpState();
3042 Log.d(TAG, "END launcher2 dump state");
3043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044}