blob: 8eece96046be619b4e2227cabe12fda0f86770fe [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
1462 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001463 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001464 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001465 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001466
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001467 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001469 } else {
1470 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 UserFolderInfo folderInfo = new UserFolderInfo();
1476 folderInfo.title = getText(R.string.folder_name);
1477
1478 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001479 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 if (!findSingleSlot(cellInfo)) return;
1481
1482 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001483 LauncherModel.addItemToDatabase(this, folderInfo,
1484 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001485 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001486 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
1488 // Create the view
1489 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001490 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
Romain Guycbb89e42009-06-08 15:52:54 -07001494
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001496 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 }
1498
1499 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001500 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 if (!findSingleSlot(cellInfo)) return;
1502
1503 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1504
1505 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001507 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001508 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1509 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511 }
1512
1513 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1514 CellLayout.CellInfo cellInfo, boolean notify) {
1515
1516 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1517 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1518
1519 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 Intent.ShortcutIconResource iconResource = null;
1521
1522 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1523 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1524 try {
1525 iconResource = (Intent.ShortcutIconResource) extra;
1526 final PackageManager packageManager = context.getPackageManager();
1527 Resources resources = packageManager.getResourcesForApplication(
1528 iconResource.packageName);
1529 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1530 icon = resources.getDrawable(id);
1531 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001532 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
1534 }
1535
1536 if (icon == null) {
1537 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1538 }
1539
1540 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001541 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 info.title = name;
1543 info.iconResource = iconResource;
1544 info.uri = data.getData();
1545 info.baseIntent = baseIntent;
1546 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1547 LiveFolders.DISPLAY_MODE_GRID);
1548
1549 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1550 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001551 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
1553 return info;
1554 }
1555
1556 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1557 final int[] xy = new int[2];
1558 if (findSlot(cellInfo, xy, 1, 1)) {
1559 cellInfo.cellX = xy[0];
1560 cellInfo.cellY = xy[1];
1561 return true;
1562 }
1563 return false;
1564 }
1565
1566 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1567 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001568 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1569 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1571 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1572 return false;
1573 }
1574 }
1575 return true;
1576 }
1577
1578 private void showNotifications() {
1579 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1580 if (statusBar != null) {
1581 statusBar.expand();
1582 }
1583 }
1584
1585 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001586 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001588 Intent chooser = Intent.createChooser(pickWallpaper,
1589 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001590 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1591 // Removed in Eclair MR1
1592// WallpaperManager wm = (WallpaperManager)
1593// getSystemService(Context.WALLPAPER_SERVICE);
1594// WallpaperInfo wi = wm.getWallpaperInfo();
1595// if (wi != null && wi.getSettingsActivity() != null) {
1596// LabeledIntent li = new LabeledIntent(getPackageName(),
1597// R.string.configure_wallpaper, 0);
1598// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1599// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1600// }
Mike Clerona0618e42009-10-22 13:55:21 -07001601 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
1603
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001604 /**
1605 * Registers various content observers. The current implementation registers
1606 * only a favorites observer to keep track of the favorites applications.
1607 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001608 private void registerContentObservers() {
1609 ContentResolver resolver = getContentResolver();
1610 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1611 true, mWidgetObserver);
1612 }
1613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 @Override
1615 public boolean dispatchKeyEvent(KeyEvent event) {
1616 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1617 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001619 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001620 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001621 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001622 dumpState();
1623 return true;
1624 }
1625 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001626 }
1627 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1628 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001629 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 return true;
1631 }
1632 }
1633
1634 return super.dispatchKeyEvent(event);
1635 }
1636
Joe Onorato88ec0992009-11-19 13:16:06 -08001637 @Override
1638 public void onBackPressed() {
1639 if (isAllAppsVisible()) {
1640 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001641 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001642 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001643 } else {
1644 closeFolder();
1645 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001646 // Some launcher layouts don't have a previous and next view
1647 if (mPreviousView != null) {
1648 dismissPreview(mPreviousView);
1649 dismissPreview(mNextView);
1650 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001651 }
1652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 private void closeFolder() {
1654 Folder folder = mWorkspace.getOpenFolder();
1655 if (folder != null) {
1656 closeFolder(folder);
1657 }
1658 }
1659
1660 void closeFolder(Folder folder) {
1661 folder.getInfo().opened = false;
1662 ViewGroup parent = (ViewGroup) folder.getParent();
1663 if (parent != null) {
1664 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001665 if (folder instanceof DropTarget) {
1666 // Live folders aren't DropTargets.
1667 mDragController.removeDropTarget((DropTarget)folder);
1668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 }
1670 folder.onClose();
1671 }
1672
1673 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001674 * Re-listen when widgets are reset.
1675 */
1676 private void onAppWidgetReset() {
1677 mAppWidgetHost.startListening();
1678 }
1679
1680 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001681 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1682 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001684 private void unbindDesktopItems() {
1685 for (ItemInfo item: mDesktopItems) {
1686 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
1688 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001689
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 /**
1691 * Launches the intent referred by the clicked shortcut.
1692 *
1693 * @param v The view representing the clicked shortcut.
1694 */
1695 public void onClick(View v) {
1696 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001697 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001699 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001700 int[] pos = new int[2];
1701 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001702 intent.setSourceBounds(new Rect(pos[0], pos[1],
1703 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001704 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 } else if (tag instanceof FolderInfo) {
1706 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001707 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001708 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001709 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001710 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001711 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714 }
1715
Michael Jurka0e260592010-06-30 17:07:39 -07001716 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001717 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001718 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001719 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001720 return false;
1721 }
1722
Michael Jurkaaf442092010-06-10 17:01:57 -07001723 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001724 * Event handler for the search button
1725 *
1726 * @param v The view that was clicked.
1727 */
1728 public void onClickSearchButton(View v) {
1729 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1730 View button = findViewById(R.id.search_button);
1731 i.setSourceBounds(
1732 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1733 startActivity(i);
1734 }
1735
1736 /**
1737 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001738 * enters home screen customization mode.
1739 *
1740 * @param v The view that was clicked.
1741 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001742 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001743 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001744 }
1745
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001746 /**
1747 * Event handler for the "grid" button that appears on the home screen, which
1748 * enters all apps mode.
1749 *
1750 * @param v The view that was clicked.
1751 */
1752 public void onClickAllAppsButton(View v) {
1753 showAllApps(true);
1754 }
1755
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001756 public void onClickAppMarketButton(View v) {
1757 if (mAppMarketIntent != null) {
1758 startActivitySafely(mAppMarketIntent, "app market");
1759 }
1760 }
1761
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001762 void startApplicationDetailsActivity(ComponentName componentName) {
1763 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001764 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1765 Uri.fromParts("package", packageName, null));
1766 startActivity(intent);
1767 }
1768
Patrick Dubroy5539af72010-09-07 15:22:01 -07001769 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1770 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1771 // System applications cannot be installed. For now, show a toast explaining that.
1772 // We may give them the option of disabling apps this way.
1773 int messageId = R.string.uninstall_system_app_text;
1774 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1775 } else {
1776 String packageName = appInfo.componentName.getPackageName();
1777 String className = appInfo.componentName.getClassName();
1778 Intent intent = new Intent(
1779 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1780 startActivity(intent);
1781 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001782 }
1783
Joe Onoratof984e852010-03-25 09:47:45 -07001784 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1786 try {
1787 startActivity(intent);
1788 } catch (ActivityNotFoundException e) {
1789 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001790 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 } catch (SecurityException e) {
1792 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001793 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001795 "or use the exported attribute for this activity. "
1796 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001799
Romain Guy8e633c52010-03-04 12:51:36 -08001800 void startActivityForResultSafely(Intent intent, int requestCode) {
1801 try {
1802 startActivityForResult(intent, requestCode);
1803 } catch (ActivityNotFoundException e) {
1804 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1805 } catch (SecurityException e) {
1806 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1807 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1808 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1809 "or use the exported attribute for this activity.", e);
1810 }
1811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
1813 private void handleFolderClick(FolderInfo folderInfo) {
1814 if (!folderInfo.opened) {
1815 // Close any open folder
1816 closeFolder();
1817 // Open the requested folder
1818 openFolder(folderInfo);
1819 } else {
1820 // Find the open folder...
1821 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1822 int folderScreen;
1823 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001824 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 // .. and close it
1826 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001827 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 // Close any folder open on the current screen
1829 closeFolder();
1830 // Pull the folder onto this screen
1831 openFolder(folderInfo);
1832 }
1833 }
1834 }
1835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 /**
1838 * Opens the user fodler described by the specified tag. The opening of the folder
1839 * is animated relative to the specified View. If the View is null, no animation
1840 * is played.
1841 *
1842 * @param folderInfo The FolderInfo describing the folder to open.
1843 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001844 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 Folder openFolder;
1846
1847 if (folderInfo instanceof UserFolderInfo) {
1848 openFolder = UserFolder.fromXml(this);
1849 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001850 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 } else {
1852 return;
1853 }
1854
Joe Onorato00acb122009-08-04 16:04:30 -04001855 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 openFolder.setLauncher(this);
1857
1858 openFolder.bind(folderInfo);
1859 folderInfo.opened = true;
1860
Winson Chungaafa03c2010-06-11 17:34:16 -07001861 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 openFolder.onOpen();
1864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001867 switch (v.getId()) {
1868 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001869 if (!isAllAppsVisible()) {
1870 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1871 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001872 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001873 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001874 return true;
1875 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001876 if (!isAllAppsVisible()) {
1877 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1878 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001879 showPreviews(v);
1880 }
1881 return true;
1882 case R.id.all_apps_button:
1883 if (!isAllAppsVisible()) {
1884 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1885 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1886 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001887 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001888 return true;
1889 }
1890
Joe Onorato9c1289c2009-08-17 11:03:03 -04001891 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 return false;
1893 }
1894
1895 if (!(v instanceof CellLayout)) {
1896 v = (View) v.getParent();
1897 }
1898
1899 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1900
1901 // This happens when long clicking an item with the dpad/trackball
1902 if (cellInfo == null) {
1903 return true;
1904 }
1905
1906 if (mWorkspace.allowLongPress()) {
1907 if (cellInfo.cell == null) {
1908 if (cellInfo.valid) {
1909 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001910 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001911 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1912 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 showAddDialog(cellInfo);
1914 }
1915 } else {
1916 if (!(cellInfo.cell instanceof Folder)) {
1917 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001918 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1919 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 mWorkspace.startDrag(cellInfo);
1921 }
1922 }
1923 }
1924 return true;
1925 }
1926
Romain Guye6b8e2f2009-11-10 11:56:55 -08001927 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001928 private void dismissPreview(final View v) {
1929 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001930 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001931 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1932 public void onDismiss() {
1933 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1934 int count = group.getChildCount();
1935 for (int i = 0; i < count; i++) {
1936 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1937 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001938 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1939 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1940
1941 v.setTag(R.id.workspace, null);
1942 v.setTag(R.id.icon, null);
1943 window.setOnDismissListener(null);
1944 }
1945 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001946 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001947 }
1948 v.setTag(null);
1949 }
1950
Romain Guyf8e6a802009-12-07 17:48:02 -08001951 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001952 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001953 }
1954
Romain Guya6abce82009-11-10 02:54:41 -08001955 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001956 final Resources resources = getResources();
1957 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001958
Romain Guya6abce82009-11-10 02:54:41 -08001959 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001960
Romain Guy9d31e9f2009-11-11 18:54:28 -08001961 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001962
Romain Guyf8e6a802009-12-07 17:48:02 -08001963 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001964 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001965 int extraW = (int) ((r.left + r.right) * max);
1966 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001967
1968 int aW = cell.getWidth() - extraW;
1969 float w = aW / max;
1970
1971 int width = cell.getWidth();
1972 int height = cell.getHeight();
1973 int x = cell.getLeftPadding();
1974 int y = cell.getTopPadding();
1975 width -= (x + cell.getRightPadding());
1976 height -= (y + cell.getBottomPadding());
1977
1978 float scale = w / width;
1979
1980 int count = end - start;
1981
1982 final float sWidth = width * scale;
1983 float sHeight = height * scale;
1984
Romain Guye6b8e2f2009-11-10 11:56:55 -08001985 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001986
Romain Guye6b8e2f2009-11-10 11:56:55 -08001987 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1988 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001989
1990 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001991 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001992 cell = (CellLayout) workspace.getChildAt(i);
1993
Romain Guyf8e6a802009-12-07 17:48:02 -08001994 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001995 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001996
1997 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001998 c.scale(scale, scale);
1999 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
2000 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08002001
Romain Guy9d31e9f2009-11-11 18:54:28 -08002002 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002003 image.setImageBitmap(bitmap);
2004 image.setTag(i);
2005 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002006 image.setOnFocusChangeListener(handler);
2007 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002008 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002009
2010 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002011 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2012
Winson Chungaafa03c2010-06-11 17:34:16 -07002013 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002014 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002015
2016 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002017 p.setContentView(preview);
2018 p.setWidth((int) (sWidth * count + extraW));
2019 p.setHeight((int) (sHeight + extraH));
2020 p.setAnimationStyle(R.style.AnimationPreview);
2021 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002022 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002023 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002024 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002025
Romain Guye6b8e2f2009-11-10 11:56:55 -08002026 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2027 public void onDismiss() {
2028 dismissPreview(anchor);
2029 }
2030 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002031
2032 anchor.setTag(p);
2033 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002034 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002035 }
2036
Romain Guy9d31e9f2009-11-11 18:54:28 -08002037 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002038 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002039
Romain Guye6b8e2f2009-11-10 11:56:55 -08002040 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002041 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002042 }
2043
2044 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002045 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002046 v.post(this);
2047 }
2048
2049 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002050 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002051 }
2052
2053 public void onFocusChange(View v, boolean hasFocus) {
2054 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002055 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002056 }
Romain Guya6abce82009-11-10 02:54:41 -08002057 }
2058 }
2059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 Workspace getWorkspace() {
2061 return mWorkspace;
2062 }
2063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 @Override
2065 protected Dialog onCreateDialog(int id) {
2066 switch (id) {
2067 case DIALOG_CREATE_SHORTCUT:
2068 return new CreateShortcut().createDialog();
2069 case DIALOG_RENAME_FOLDER:
2070 return new RenameFolder().createDialog();
2071 }
2072
2073 return super.onCreateDialog(id);
2074 }
2075
2076 @Override
2077 protected void onPrepareDialog(int id, Dialog dialog) {
2078 switch (id) {
2079 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 break;
2081 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002082 if (mFolderInfo != null) {
2083 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2084 final CharSequence text = mFolderInfo.title;
2085 input.setText(text);
2086 input.setSelection(0, text.length());
2087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 break;
2089 }
2090 }
2091
2092 void showRenameDialog(FolderInfo info) {
2093 mFolderInfo = info;
2094 mWaitingForResult = true;
2095 showDialog(DIALOG_RENAME_FOLDER);
2096 }
2097
2098 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2099 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002100 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 mWaitingForResult = true;
2102 showDialog(DIALOG_CREATE_SHORTCUT);
2103 }
2104
Joe Onoratodeb98af2010-02-19 14:59:39 -08002105 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002106 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002107 Bundle bundle = new Bundle();
2108
2109 ArrayList<String> shortcutNames = new ArrayList<String>();
2110 shortcutNames.add(getString(R.string.group_applications));
2111 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2112
2113 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2114 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2115 R.drawable.ic_launcher_application));
2116 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2117
2118 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2119 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002120 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002121 pickIntent.putExtras(bundle);
2122
Joe Onoratodeb98af2010-02-19 14:59:39 -08002123 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002124 }
2125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 private class RenameFolder {
2127 private EditText mInput;
2128
2129 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2131 mInput = (EditText) layout.findViewById(R.id.folder_name);
2132
2133 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2134 builder.setIcon(0);
2135 builder.setTitle(getString(R.string.rename_folder_title));
2136 builder.setCancelable(true);
2137 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2138 public void onCancel(DialogInterface dialog) {
2139 cleanup();
2140 }
2141 });
2142 builder.setNegativeButton(getString(R.string.cancel_action),
2143 new Dialog.OnClickListener() {
2144 public void onClick(DialogInterface dialog, int which) {
2145 cleanup();
2146 }
2147 }
2148 );
2149 builder.setPositiveButton(getString(R.string.rename_action),
2150 new Dialog.OnClickListener() {
2151 public void onClick(DialogInterface dialog, int which) {
2152 changeFolderName();
2153 }
2154 }
2155 );
2156 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002157
2158 final AlertDialog dialog = builder.create();
2159 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2160 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002161 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002162 mInput.requestFocus();
2163 InputMethodManager inputManager = (InputMethodManager)
2164 getSystemService(Context.INPUT_METHOD_SERVICE);
2165 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002166 }
2167 });
2168
2169 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 }
2171
2172 private void changeFolderName() {
2173 final String name = mInput.getText().toString();
2174 if (!TextUtils.isEmpty(name)) {
2175 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002176 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 mFolderInfo.title = name;
2178 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2179
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002181 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 } else {
2184 final FolderIcon folderIcon = (FolderIcon)
2185 mWorkspace.getViewForTag(mFolderInfo);
2186 if (folderIcon != null) {
2187 folderIcon.setText(name);
2188 getWorkspace().requestLayout();
2189 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002190 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002191 mWorkspaceLoading = true;
2192 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 }
2194 }
2195 }
2196 cleanup();
2197 }
2198
2199 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 dismissDialog(DIALOG_RENAME_FOLDER);
2201 mWaitingForResult = false;
2202 mFolderInfo = null;
2203 }
2204 }
2205
Daniel Sandler843e8602010-06-07 14:59:01 -04002206 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2207 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002208 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002209 }
2210
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002211 // AllAppsView.Watcher
2212 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002213 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2214 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002215 mWorkspace.setVisibility(View.GONE);
2216 }
2217 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002218
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002219 private void showToolbarButton(View button) {
2220 button.setAlpha(1.0f);
2221 button.setVisibility(View.VISIBLE);
2222 button.setFocusable(true);
2223 button.setClickable(true);
2224 }
2225
2226 private void hideToolbarButton(View button) {
2227 button.setAlpha(0.0f);
2228 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2229 button.setVisibility(View.INVISIBLE);
2230 button.setFocusable(false);
2231 button.setClickable(false);
2232 }
2233
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002234 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002235 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002236 *
2237 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2238 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002239 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002240 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002241 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002242 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002243 final boolean showing = show;
2244 final boolean hiding = !show;
2245
2246 final int duration = show ?
2247 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2248 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2249
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002250 if (seq != null) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002251 Animator anim = new ObjectAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2252 anim.addListener(new AnimatorListenerAdapter() {
2253 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002254 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002255 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002256 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002257 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002258 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259 });
2260 seq.play(anim);
2261 } else {
2262 if (showing) {
2263 showToolbarButton(view);
2264 } else {
2265 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002266 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002267 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002268 }
2269
2270 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002271 * Show/hide the appropriate toolbar buttons for newState.
2272 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273 *
2274 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002275 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2276 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002277 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002278 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002279 final View searchButton = findViewById(R.id.search_button);
2280 final View allAppsButton = findViewById(R.id.all_apps_button);
2281 final View marketButton = findViewById(R.id.market_button);
2282 final View configureButton = findViewById(R.id.configure_button);
2283
2284 switch (newState) {
2285 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002286 hideOrShowToolbarButton(true, searchButton, showSeq);
2287 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2288 hideOrShowToolbarButton(true, configureButton, showSeq);
2289 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002290 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002291 break;
2292 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002293 hideOrShowToolbarButton(true, configureButton, showSeq);
2294 hideOrShowToolbarButton(true, marketButton, showSeq);
2295 hideOrShowToolbarButton(false, searchButton, hideSeq);
2296 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002297 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002298 break;
2299 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002300 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2301 hideOrShowToolbarButton(false, searchButton, hideSeq);
2302 hideOrShowToolbarButton(false, marketButton, hideSeq);
2303 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002304 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002305 break;
2306 }
2307 }
2308
2309 /**
2310 * Helper method for the cameraZoomIn/cameraZoomOut animations
2311 * @param view The view being animated
2312 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2313 * @param scaleFactor The scale factor used for the zoom
2314 */
2315 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2316 final int height = view.getHeight();
2317 view.setPivotX(view.getWidth() / 2.0f);
2318 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2319 // Assumes that the view is normally anchored to either the top or bottom of the screen
2320 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2321 if (state == State.ALL_APPS) {
2322 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2323 } else {
2324 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2325 }
2326 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002327
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002328 /**
2329 * Zoom the camera out from the workspace to reveal 'toView'.
2330 * Assumes that the view to show is anchored at either the very top or very bottom
2331 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002332 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002333 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002334 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002335 final Resources res = getResources();
2336 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2337 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002338 final boolean toAllApps = (toState == State.ALL_APPS);
2339 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002340
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002341 setPivotsForZoom(toView, toState, scale);
2342
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002343 if (toState == State.ALL_APPS) {
2344 mWorkspace.shrinkToBottom(animated);
2345 } else {
2346 mWorkspace.shrinkToTop(animated);
2347 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002348
2349 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002350 ValueAnimator scaleAnim = new ObjectAnimator(duration, toView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002351 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2352 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2353 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002354 scaleAnim.addListener(new AnimatorListenerAdapter() {
2355 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002356 // Prepare the position
2357 toView.setTranslationX(0.0f);
2358 toView.setTranslationY(0.0f);
2359 toView.setVisibility(View.VISIBLE);
2360 }
2361 });
2362
Chet Haaseb1254a62010-09-07 13:35:00 -07002363 AnimatorSet toolbarHideAnim = new AnimatorSet();
2364 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002365 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2366
2367 // toView should appear right at the end of the workspace shrink animation
2368 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2369
Chet Haaseb1254a62010-09-07 13:35:00 -07002370 AnimatorSet s = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002371 s.playTogether(scaleAnim, toolbarHideAnim);
2372 s.play(scaleAnim).after(startDelay);
2373
2374 // Show the new toolbar buttons just as the main animation is ending
2375 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2376 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2377 s.start();
2378 } else {
2379 toView.setTranslationX(0.0f);
2380 toView.setTranslationY(0.0f);
2381 toView.setScaleX(1.0f);
2382 toView.setScaleY(1.0f);
2383 toView.setVisibility(View.VISIBLE);
2384 hideAndShowToolbarButtons(toState, null, null);
2385 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002386 }
2387
2388 /**
2389 * Zoom the camera back into the workspace, hiding 'fromView'.
2390 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002391 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2392 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002393 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002394 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002395 Resources res = getResources();
2396 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2397 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002398 final View fromView =
2399 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2400
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002401 mCustomizePagedView.endChoiceMode();
2402 mAllAppsPagedView.endChoiceMode();
2403
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002404 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002405
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002406 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002407
2408 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002409 AnimatorSet s = new AnimatorSet();
2410 ValueAnimator scaleAnim = new ObjectAnimator(duration, fromView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002411 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2412 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2413 scaleAnim.setInterpolator(new AccelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002414 s.addListener(new AnimatorListenerAdapter() {
2415 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002416 fromView.setVisibility(View.GONE);
2417 fromView.setScaleX(1.0f);
2418 fromView.setScaleY(1.0f);
2419 }
2420 });
2421
Chet Haaseb1254a62010-09-07 13:35:00 -07002422 AnimatorSet toolbarHideAnim = new AnimatorSet();
2423 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002424 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2425
2426 s.playTogether(scaleAnim, toolbarHideAnim);
2427
2428 // Show the new toolbar buttons at the very end of the whole animation
2429 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2430 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2431 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2432 s.start();
2433 } else {
2434 fromView.setVisibility(View.GONE);
2435 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2436 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002437 }
2438
2439 /**
2440 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2441 * This is the transition used on xlarge screens to go between all apps and
2442 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002443 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2444 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2445 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002446 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002447 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002448 final Resources res = getResources();
2449 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002450 final int workspaceHeight = mWorkspace.getHeight();
2451
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002452 final boolean fromAllApps = (fromState == State.ALL_APPS);
2453 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2454 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002455
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002456 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2457 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2458 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2459 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002460
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002461 mCustomizePagedView.endChoiceMode();
2462 mAllAppsPagedView.endChoiceMode();
2463
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 if (toState == State.ALL_APPS) {
2465 mWorkspace.shrinkToBottom(animated);
2466 } else {
2467 mWorkspace.shrinkToTop(animated);
2468 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469
2470 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002471 AnimatorSet s = new AnimatorSet();
2472 s.addListener(new AnimatorListenerAdapter() {
2473 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002474 toView.setVisibility(View.VISIBLE);
2475 toView.setY(toViewStartY);
2476 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002477 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002478 fromView.setVisibility(View.GONE);
2479 }
2480 });
2481
Chet Haaseb1254a62010-09-07 13:35:00 -07002482 AnimatorSet toolbarHideAnim = new AnimatorSet();
2483 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002484 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2485
2486 s.playTogether(
2487 toolbarHideAnim,
Chet Haaseb1254a62010-09-07 13:35:00 -07002488 new ObjectAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2489 new ObjectAnimator(duration, toView, "y", toViewStartY, toViewEndY));
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002490
2491 // Show the new toolbar buttons just as the main animation is ending
2492 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2493 s.play(toolbarShowAnim).after(duration - fadeInTime);
2494 s.start();
2495 } else {
2496 fromView.setY(fromViewEndY);
2497 fromView.setVisibility(View.GONE);
2498 toView.setY(toViewEndY);
2499 toView.setVisibility(View.VISIBLE);
2500 hideAndShowToolbarButtons(toState, null, null);
2501 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002502 }
2503
2504 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002505 if (mAllAppsGrid.isVisible())
2506 return;
2507
Michael Jurka79212d82010-07-30 16:36:20 -07002508 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002509 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002511 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002512 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002513 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002514 } else {
2515 mAllAppsGrid.zoom(1.0f, animated);
2516 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002517
Romain Guyc16fea72010-03-12 17:17:56 -08002518 ((View) mAllAppsGrid).setFocusable(true);
2519 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002520
Joe Onorato7c312c12009-08-13 21:36:53 -07002521 // TODO: fade these two too
2522 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002523 }
2524
Joe Onoratob2061212009-11-24 16:13:54 -05002525 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002526 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002527 * - Home from workspace
2528 * - from center screen
2529 * - from other screens
2530 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002531 * - from center screen
2532 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002533 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002534 * - from center screen
2535 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002536 * - Launch app from workspace and quit
2537 * - with back
2538 * - with home
2539 * - Launch app from all apps and quit
2540 * - with back
2541 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002542 * - Go to a screen that's not the default, then all
2543 * apps, and launch and app, and go back
2544 * - with back
2545 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002546 * - On workspace, long press power and go back
2547 * - with back
2548 * - with home
2549 * - On all apps, long press power and go back
2550 * - with back
2551 * - with home
2552 * - On workspace, power off
2553 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002554 * - Launch an app and turn off the screen while in that app
2555 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002556 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002557 * - From all apps
2558 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002559 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2560 * - From all apps
2561 * - From the center workspace
2562 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002563 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002564 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002565 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002566 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002569 } else {
2570 mAllAppsGrid.zoom(0.0f, animated);
2571 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002572 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002573 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002574 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002575 }
2576
Joe Onorato7c312c12009-08-13 21:36:53 -07002577 void lockAllApps() {
2578 // TODO
2579 }
2580
2581 void unlockAllApps() {
2582 // TODO
2583 }
2584
Patrick Dubroy558654c2010-07-23 16:48:11 -07002585 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002586 return mHomeCustomizationDrawer != null &&
2587 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002588 }
2589
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 // Show the customization drawer (only exists in x-large configuration)
2591 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002592 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002594 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002596 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002597 }
2598
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 // Hide the customization drawer (only exists in x-large configuration)
2600 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002601 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002603 }
2604 }
2605
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002606 void onWorkspaceClick(CellLayout layout) {
2607 Object itemInfo = mAllAppsPagedView.getChosenItem();
2608 if (itemInfo == null) {
2609 itemInfo = mCustomizePagedView.getChosenItem();
Michael Jurka213d9632010-07-28 11:29:25 -07002610 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002611
2612 if (itemInfo == null) {
2613 // No items are chosen in All Apps or Customize, so just zoom into the workspace
2614 mWorkspace.unshrink(layout);
2615 if (isAllAppsVisible()) {
2616 closeAllApps(true);
2617 } else if (isCustomizationDrawerVisible()) {
2618 hideCustomizationDrawer(true);
2619 }
2620 } else {
2621 // Act as if the chosen item was dropped on the given CellLayout
2622 if (mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2623 mAllAppsPagedView.endChoiceMode();
2624 mCustomizePagedView.endChoiceMode();
2625 } else {
2626 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
2627 }
Michael Jurka213d9632010-07-28 11:29:25 -07002628 }
2629 }
2630
Joe Onorato7404ee42009-07-31 11:54:44 -07002631 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002632 * Sets the app market icon (shown when all apps is visible on x-large screens)
2633 */
2634 private void updateAppMarketIcon() {
2635 if (LauncherApplication.isScreenXLarge()) {
2636 // Find the app market activity by resolving an intent.
2637 // (If multiple app markets are installed, it will return the ResolverActivity.)
2638 PackageManager packageManager = getPackageManager();
2639 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2640 ComponentName activityName = intent.resolveActivity(getPackageManager());
2641 if (activityName != null) {
2642 mAppMarketIntent = intent;
2643 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2644 Drawable toolbarIcon = null;
2645 try {
2646 // Look for the toolbar icon specified in the activity meta-data
2647 Bundle metaData = packageManager.getActivityInfo(
2648 activityName, PackageManager.GET_META_DATA).metaData;
2649 if (metaData != null) {
2650 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2651 if (iconResId != 0) {
2652 Resources res = packageManager.getResourcesForActivity(activityName);
2653 toolbarIcon = res.getDrawable(iconResId);
2654 }
2655 }
2656 } catch (NameNotFoundException e) {
2657 // Do nothing
2658 }
2659 // If we were unable to find the icon via the meta-data, use a generic one
2660 if (toolbarIcon == null) {
2661 marketButton.setImageResource(R.drawable.app_market_generic);
2662 } else {
2663 marketButton.setImageDrawable(toolbarIcon);
2664 }
2665 } else {
2666 mAppMarketIntent = null;
2667 }
2668 }
2669 }
2670
2671 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 * Displays the shortcut creation dialog and launches, if necessary, the
2673 * appropriate activity.
2674 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002675 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002676 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2677 DialogInterface.OnShowListener {
2678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2685 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002686 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688 builder.setInverseBackgroundForced(true);
2689
2690 AlertDialog dialog = builder.create();
2691 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002692 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002693 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 return dialog;
2696 }
2697
2698 public void onCancel(DialogInterface dialog) {
2699 mWaitingForResult = false;
2700 cleanup();
2701 }
2702
Romain Guycbb89e42009-06-08 15:52:54 -07002703 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002704 }
2705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002707 try {
2708 dismissDialog(DIALOG_CREATE_SHORTCUT);
2709 } catch (Exception e) {
2710 // An exception is thrown if the dialog is not visible, which is fine
2711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 }
2713
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002714 /**
2715 * Handle the action clicked in the "Add to home" dialog.
2716 */
2717 public void onClick(DialogInterface dialog, int which) {
2718 Resources res = getResources();
2719 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002720
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002721 switch (which) {
2722 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002723 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002724 break;
2725 }
Romain Guycbb89e42009-06-08 15:52:54 -07002726
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002727 case AddAdapter.ITEM_APPWIDGET: {
2728 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002729
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002730 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2731 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002732 // start the pick activity
2733 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2734 break;
2735 }
Romain Guycbb89e42009-06-08 15:52:54 -07002736
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002737 case AddAdapter.ITEM_LIVE_FOLDER: {
2738 // Insert extra item to handle inserting folder
2739 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002740
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002741 ArrayList<String> shortcutNames = new ArrayList<String>();
2742 shortcutNames.add(res.getString(R.string.group_folder));
2743 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002744
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002745 ArrayList<ShortcutIconResource> shortcutIcons =
2746 new ArrayList<ShortcutIconResource>();
2747 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2748 R.drawable.ic_launcher_folder));
2749 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2750
2751 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2752 pickIntent.putExtra(Intent.EXTRA_INTENT,
2753 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2754 pickIntent.putExtra(Intent.EXTRA_TITLE,
2755 getText(R.string.title_select_live_folder));
2756 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002757
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002758 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2759 break;
2760 }
2761
2762 case AddAdapter.ITEM_WALLPAPER: {
2763 startWallpaper();
2764 break;
2765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 }
2767 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002768
2769 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002770 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002772 }
2773
2774 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002775 * Receives notifications when applications are added/removed.
2776 */
2777 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2778 @Override
2779 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002780 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002781 String reason = intent.getStringExtra("reason");
2782 if (!"homekey".equals(reason)) {
2783 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002784 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002785 animate = false;
2786 }
Joe Onoratob2061212009-11-24 16:13:54 -05002787 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002788 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002789 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002790 }
2791 }
2792
2793 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002794 * Receives notifications whenever the appwidgets are reset.
2795 */
2796 private class AppWidgetResetObserver extends ContentObserver {
2797 public AppWidgetResetObserver() {
2798 super(new Handler());
2799 }
2800
2801 @Override
2802 public void onChange(boolean selfChange) {
2803 onAppWidgetReset();
2804 }
2805 }
2806
2807 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002810 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002811 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002812 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002813 } else {
2814 return SCREEN_COUNT / 2;
2815 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002817
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002818 void setAllAppsPagedView(PagedView view) {
2819 mAllAppsPagedView = view;
2820 }
2821
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 /**
2823 * Refreshes the shortcuts shown on the workspace.
2824 *
2825 * Implementation of the method from LauncherModel.Callbacks.
2826 */
2827 public void startBinding() {
2828 final Workspace workspace = mWorkspace;
2829 int count = workspace.getChildCount();
2830 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002831 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2833 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002834
Joe Onorato9c1289c2009-08-17 11:03:03 -04002835 if (DEBUG_USER_INTERFACE) {
2836 android.widget.Button finishButton = new android.widget.Button(this);
2837 finishButton.setText("Finish");
2838 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2839
2840 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2841 public void onClick(View v) {
2842 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002844 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
2846 }
2847
2848 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002849 * Bind the items start-end from the list.
2850 *
2851 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2854
2855 final Workspace workspace = mWorkspace;
2856
2857 for (int i=start; i<end; i++) {
2858 final ItemInfo item = shortcuts.get(i);
2859 mDesktopItems.add(item);
2860 switch (item.itemType) {
2861 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2862 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002863 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2865 false);
2866 break;
2867 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2868 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002869 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 (UserFolderInfo) item);
2871 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2872 false);
2873 break;
2874 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2875 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2876 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002877 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 (LiveFolderInfo) item);
2879 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2880 false);
2881 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 }
2884
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
2887
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 /**
2889 * Implementation of the method from LauncherModel.Callbacks.
2890 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002891 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002892 sFolders.clear();
2893 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895
2896 /**
2897 * Add the views for a widget to the workspace.
2898 *
2899 * Implementation of the method from LauncherModel.Callbacks.
2900 */
2901 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002902 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2903 if (DEBUG_WIDGETS) {
2904 Log.d(TAG, "bindAppWidget: " + item);
2905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 final Workspace workspace = mWorkspace;
2907
2908 final int appWidgetId = item.appWidgetId;
2909 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002910 if (DEBUG_WIDGETS) {
2911 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2912 }
2913
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2915
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2917 item.hostView.setTag(item);
2918
2919 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2920 item.cellY, item.spanX, item.spanY, false);
2921
2922 workspace.requestLayout();
2923
2924 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002925
2926 if (DEBUG_WIDGETS) {
2927 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2928 + (SystemClock.uptimeMillis()-start) + "ms");
2929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002930 }
2931
2932 /**
2933 * Callback saying that there aren't any more items to bind.
2934 *
2935 * Implementation of the method from LauncherModel.Callbacks.
2936 */
2937 public void finishBindingItems() {
2938 if (mSavedState != null) {
2939 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002940 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002941 }
2942
2943 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2944 if (userFolders != null) {
2945 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002946 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002947 if (info != null) {
2948 openFolder(info);
2949 }
2950 }
2951 final Folder openFolder = mWorkspace.getOpenFolder();
2952 if (openFolder != null) {
2953 openFolder.requestFocus();
2954 }
2955 }
2956
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 mSavedState = null;
2958 }
2959
2960 if (mSavedInstanceState != null) {
2961 super.onRestoreInstanceState(mSavedInstanceState);
2962 mSavedInstanceState = null;
2963 }
2964
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965 mWorkspaceLoading = false;
2966 }
2967
2968 /**
2969 * Add the icons for all apps.
2970 *
2971 * Implementation of the method from LauncherModel.Callbacks.
2972 */
2973 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002974 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002975 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 }
2977
2978 /**
2979 * A package was installed.
2980 *
2981 * Implementation of the method from LauncherModel.Callbacks.
2982 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002983 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002985 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002986 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002987 }
2988
2989 /**
2990 * A package was updated.
2991 *
2992 * Implementation of the method from LauncherModel.Callbacks.
2993 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002994 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002996 mWorkspace.updateShortcuts(apps);
2997 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002998 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 }
3000
3001 /**
3002 * A package was uninstalled.
3003 *
3004 * Implementation of the method from LauncherModel.Callbacks.
3005 */
Joe Onorato36115782010-06-17 13:28:48 -04003006 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003008 if (permanent) {
3009 mWorkspace.removeItems(apps);
3010 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003011 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003012 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 }
Joe Onoratobe386092009-11-17 17:32:16 -08003014
3015 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003016 * A number of packages were updated.
3017 */
3018 public void bindPackagesUpdated() {
3019 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07003020 if (mCustomizePagedView != null)
3021 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07003022 }
3023
3024 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003025 * Prints out out state for debugging.
3026 */
3027 public void dumpState() {
3028 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003029 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003030 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3031 Log.d(TAG, "mRestoring=" + mRestoring);
3032 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3033 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3034 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003035 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003036 mModel.dumpState();
3037 mAllAppsGrid.dumpState();
3038 Log.d(TAG, "END launcher2 dump state");
3039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040}