blob: 132f0e907b341470dba8e2a1cad2cf5920a03dcf [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040036import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040038import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080044import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080045import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080047import android.graphics.drawable.ColorDrawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040048import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080052import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.text.Selection;
55import android.text.SpannableStringBuilder;
56import android.text.TextUtils;
57import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070058import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080060import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.view.KeyEvent;
62import android.view.LayoutInflater;
63import android.view.Menu;
64import android.view.MenuItem;
65import android.view.View;
66import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.view.View.OnLongClickListener;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.widget.TextView;
71import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080072import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080073import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080074import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070075import android.appwidget.AppWidgetManager;
76import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import java.util.ArrayList;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040079import java.util.List;
Joe Onorato9c1289c2009-08-17 11:03:03 -040080import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070081import java.io.DataOutputStream;
82import java.io.FileNotFoundException;
83import java.io.IOException;
84import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085
Romain Guyedcce092010-03-04 13:03:17 -080086import com.android.launcher.R;
87
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088/**
89 * Default launcher application.
90 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040091public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050092 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080093 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 static final boolean LOGD = false;
95
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final boolean PROFILE_STARTUP = false;
97 static final boolean PROFILE_ROTATE = false;
98 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100 private static final int WALLPAPER_SCREENS_SPAN = 2;
101
102 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800103 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 private static final int MENU_ADD = Menu.FIRST + 1;
106 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
107 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
108 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700109 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
111 private static final int REQUEST_CREATE_SHORTCUT = 1;
112 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700113 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 private static final int REQUEST_PICK_APPLICATION = 6;
115 private static final int REQUEST_PICK_SHORTCUT = 7;
116 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700117 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700118 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
121
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800122 static final int SCREEN_COUNT = 5;
123 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700125 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato7c312c12009-08-13 21:36:53 -0700127 static final int DIALOG_CREATE_SHORTCUT = 1;
128 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Romain Guy98d01652009-06-30 16:21:04 -0700130 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 // Type: int
133 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
134 // Type: boolean
135 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
136 // Type: long
137 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
152 // Type: int[]
153 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
154 // Type: boolean
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
156 // Type: long
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800162 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800164 private final BroadcastReceiver mCloseSystemDialogsReceiver
165 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800166 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private LayoutInflater mInflater;
169
Joe Onorato00acb122009-08-04 16:04:30 -0400170 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private AppWidgetManager mAppWidgetManager;
174 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private CellLayout.CellInfo mAddItemCellInfo;
177 private CellLayout.CellInfo mMenuAddInfo;
178 private final int[] mCellCoordinates = new int[2];
179 private FolderInfo mFolderInfo;
180
Joe Onorato7c312c12009-08-13 21:36:53 -0700181 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700182 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private Bundle mSavedState;
186
187 private SpannableStringBuilder mDefaultKeySsb = null;
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 private boolean mWorkspaceLoading = true;
190
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800191 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private boolean mRestoring;
193 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
195 private Bundle mSavedInstanceState;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800198 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199
Romain Guy84f296c2009-11-04 15:00:44 -0800200 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
201 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Romain Guy1fbc1c82009-11-09 20:43:08 -0800203 private ImageView mPreviousView;
204 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500205
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400206 // Hotseats (quick-launch icons next to AllApps)
207 // TODO: move these intial intents out to Uris in an XML resource
208 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400209 private String[] mHotseatConfig = null;
210 private Intent[] mHotseats = null;
211 private Drawable[] mHotseatIcons = null;
212 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 @Override
215 protected void onCreate(Bundle savedInstanceState) {
216 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700217
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 LauncherApplication app = ((LauncherApplication)getApplication());
219 mModel = app.setLauncher(this);
220 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400221 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700223
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700224 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
226 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 if (PROFILE_STARTUP) {
229 android.os.Debug.startMethodTracing("/sdcard/launcher");
230 }
231
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400232 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 checkForLocaleChange();
234 setWallpaperDimension();
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 setContentView(R.layout.launcher);
237 setupViews();
238
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800239 registerContentObservers();
240
Joe Onorato7c312c12009-08-13 21:36:53 -0700241 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 mSavedState = savedInstanceState;
244 restoreState(mSavedState);
245
246 if (PROFILE_STARTUP) {
247 android.os.Debug.stopMethodTracing();
248 }
249
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700250 // We have a new AllAppsView, we need to re-bind everything, and it could have
251 // changed in our absence.
252 mModel.setAllAppsDirty();
253 mModel.setWorkspaceDirty();
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 }
258
259 // For handling default keys
260 mDefaultKeySsb = new SpannableStringBuilder();
261 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800262
263 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
264 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
Romain Guycbb89e42009-06-08 15:52:54 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700268 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
269 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 final Configuration configuration = getResources().getConfiguration();
272
Romain Guy98d01652009-06-30 16:21:04 -0700273 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 final String locale = configuration.locale.toString();
275
Romain Guy98d01652009-06-30 16:21:04 -0700276 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 final int mcc = configuration.mcc;
278
Romain Guy98d01652009-06-30 16:21:04 -0700279 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 final int mnc = configuration.mnc;
281
Romain Guy84f296c2009-11-04 15:00:44 -0800282 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Romain Guy84f296c2009-11-04 15:00:44 -0800284 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700285 localeConfiguration.locale = locale;
286 localeConfiguration.mcc = mcc;
287 localeConfiguration.mnc = mnc;
288
289 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800290 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400291
292 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700293 }
294 }
295
296 private static class LocaleConfiguration {
297 public String locale;
298 public int mcc = -1;
299 public int mnc = -1;
300 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700301
Romain Guy98d01652009-06-30 16:21:04 -0700302 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
303 DataInputStream in = null;
304 try {
305 in = new DataInputStream(context.openFileInput(PREFERENCES));
306 configuration.locale = in.readUTF();
307 configuration.mcc = in.readInt();
308 configuration.mnc = in.readInt();
309 } catch (FileNotFoundException e) {
310 // Ignore
311 } catch (IOException e) {
312 // Ignore
313 } finally {
314 if (in != null) {
315 try {
316 in.close();
317 } catch (IOException e) {
318 // Ignore
319 }
320 }
321 }
322 }
323
324 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
325 DataOutputStream out = null;
326 try {
327 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
328 out.writeUTF(configuration.locale);
329 out.writeInt(configuration.mcc);
330 out.writeInt(configuration.mnc);
331 out.flush();
332 } catch (FileNotFoundException e) {
333 // Ignore
334 } catch (IOException e) {
335 //noinspection ResultOfMethodCallIgnored
336 context.getFileStreamPath(PREFERENCES).delete();
337 } finally {
338 if (out != null) {
339 try {
340 out.close();
341 } catch (IOException e) {
342 // Ignore
343 }
344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
346 }
347
348 static int getScreen() {
349 synchronized (sLock) {
350 return sScreen;
351 }
352 }
353
354 static void setScreen(int screen) {
355 synchronized (sLock) {
356 sScreen = screen;
357 }
358 }
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700361 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
363 Display display = getWindowManager().getDefaultDisplay();
364 boolean isPortrait = display.getWidth() < display.getHeight();
365
366 final int width = isPortrait ? display.getWidth() : display.getHeight();
367 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700368 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
370
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400371 // Note: This doesn't do all the client-id magic that BrowserProvider does
372 // in Browser. (http://b/2425179)
373 private Uri getDefaultBrowserUri() {
374 String url = getString(R.string.default_browser_url);
375 if (url.indexOf("{CID}") != -1) {
376 url = url.replace("{CID}", "android-google");
377 }
378 return Uri.parse(url);
379 }
380
381 // Load the Intent templates from arrays.xml to populate the hotseats. For
382 // each Intent, if it resolves to a single app, use that as the launch
383 // intent & use that app's label as the contentDescription. Otherwise,
384 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400385 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400386 if (mHotseatConfig == null) {
387 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
388 if (mHotseatConfig.length > 0) {
389 mHotseats = new Intent[mHotseatConfig.length];
390 mHotseatLabels = new CharSequence[mHotseatConfig.length];
391 mHotseatIcons = new Drawable[mHotseatConfig.length];
392 } else {
393 mHotseats = null;
394 mHotseatIcons = null;
395 mHotseatLabels = null;
396 }
397
398 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
399 for (int i=0; i<mHotseatConfig.length; i++) {
400 // load icon for this slot; currently unrelated to the actual activity
401 try {
402 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
403 } catch (ArrayIndexOutOfBoundsException ex) {
404 Log.w(TAG, "Missing hotseat_icons array item #" + i);
405 mHotseatIcons[i] = null;
406 }
407 }
408 hotseatIconDrawables.recycle();
409 }
410
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400411 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400412 for (int i=0; i<mHotseatConfig.length; i++) {
413 Intent intent = null;
414 if (mHotseatConfig[i].equals("*BROWSER*")) {
415 // magic value meaning "launch user's default web browser"
416 // replace it with a generic web request so we can see if there is indeed a default
417 String defaultUri = getString(R.string.default_browser_url);
418 intent = new Intent(
419 Intent.ACTION_VIEW,
420 ((defaultUri != null)
421 ? Uri.parse(defaultUri)
422 : getDefaultBrowserUri())
423 ).addCategory(Intent.CATEGORY_BROWSABLE);
424 // note: if the user launches this without a default set, she
425 // will always be taken to the default URL above; this is
426 // unavoidable as we must specify a valid URL in order for the
427 // chooser to appear, and once the user selects something, that
428 // URL is unavoidably sent to the chosen app.
429 } else {
430 try {
431 intent = Intent.parseUri(mHotseatConfig[i], 0);
432 } catch (java.net.URISyntaxException ex) {
433 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
434 // bogus; leave intent=null
435 }
436 }
437
438 if (intent == null) {
439 mHotseats[i] = null;
440 mHotseatLabels[i] = getText(R.string.activity_not_found);
441 continue;
442 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400443
444 if (LOGD) {
445 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400446 + " initial intent=["
447 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400448 + "]");
449 }
450
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400451 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
452 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
453 if (LOGD) {
454 Log.d(TAG, "Best match for intent: " + bestMatch);
455 Log.d(TAG, "All matches: ");
456 for (ResolveInfo ri : allMatches) {
457 Log.d(TAG, " --> " + ri);
458 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400459 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400460 // did this resolve to a single app, or the resolver?
461 if (allMatches.size() == 0 || bestMatch == null) {
462 // can't find any activity to handle this. let's leave the
463 // intent as-is and let Launcher show a toast when it fails
464 // to launch.
465 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400466
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400467 // set accessibility text to "Not installed"
468 mHotseatLabels[i] = getText(R.string.activity_not_found);
469 } else {
470 boolean found = false;
471 for (ResolveInfo ri : allMatches) {
472 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
473 && bestMatch.activityInfo.applicationInfo.packageName
474 .equals(ri.activityInfo.applicationInfo.packageName)) {
475 found = true;
476 break;
477 }
478 }
479
480 if (!found) {
481 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
482 // the bestMatch is probably the ResolveActivity, meaning the
483 // user has not yet selected a default
484 // so: we'll keep the original intent for now
485 mHotseats[i] = intent;
486
487 // set the accessibility text to "Select shortcut"
488 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
489 } else {
490 // we have an app!
491 // now reconstruct the intent to launch it through the front
492 // door
493 ComponentName com = new ComponentName(
494 bestMatch.activityInfo.applicationInfo.packageName,
495 bestMatch.activityInfo.name);
496 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
497
498 // load the app label for accessibility
499 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
500 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400501 }
502
503 if (LOGD) {
504 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400505 + " final intent=["
506 + ((mHotseats[i] == null)
507 ? "null"
508 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400509 + "] label=[" + mHotseatLabels[i]
510 + "]"
511 );
512 }
513 }
514 }
515
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 @Override
517 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700518 mWaitingForResult = false;
519
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 // The pattern used here is that a user PICKs a specific application,
521 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700522
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
524 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700525
Romain Guy94dabf12009-07-21 10:55:43 -0700526 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 switch (requestCode) {
528 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400529 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 break;
531 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800532 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 break;
534 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 break;
537 case REQUEST_PICK_LIVE_FOLDER:
538 addLiveFolder(data);
539 break;
540 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 case REQUEST_PICK_APPWIDGET:
544 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700549 case REQUEST_PICK_WALLPAPER:
550 // We just wanted the activity result here so we can clear mWaitingForResult
551 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 }
Romain Guy18042c82009-11-06 11:44:55 -0800553 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
554 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
555 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 // Clean up the appWidgetId if we canceled
557 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
558 if (appWidgetId != -1) {
559 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
563
564 @Override
565 protected void onResume() {
566 super.onResume();
567
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800568 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 mWorkspaceLoading = true;
572 mModel.startLoader(this, true);
573 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 }
575 }
576
577 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700578 protected void onPause() {
579 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800580 dismissPreview(mPreviousView);
581 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800582 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 }
Romain Guycbb89e42009-06-08 15:52:54 -0700584
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700585 @Override
586 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400587 // Flag the loader to stop early before switching
588 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800589 mAllAppsGrid.surrender();
Romain Guycbb89e42009-06-08 15:52:54 -0700590
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700591 if (PROFILE_ROTATE) {
592 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
593 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800594 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700595 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700596
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800597 // We can't hide the IME if it was forced open. So don't bother
598 /*
599 @Override
600 public void onWindowFocusChanged(boolean hasFocus) {
601 super.onWindowFocusChanged(hasFocus);
602
603 if (hasFocus) {
604 final InputMethodManager inputManager = (InputMethodManager)
605 getSystemService(Context.INPUT_METHOD_SERVICE);
606 WindowManager.LayoutParams lp = getWindow().getAttributes();
607 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
608 android.os.Handler()) {
609 protected void onReceiveResult(int resultCode, Bundle resultData) {
610 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
611 }
612 });
613 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
614 }
615 }
616 */
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 private boolean acceptFilter() {
619 final InputMethodManager inputManager = (InputMethodManager)
620 getSystemService(Context.INPUT_METHOD_SERVICE);
621 return !inputManager.isFullscreenMode();
622 }
623
624 @Override
625 public boolean onKeyDown(int keyCode, KeyEvent event) {
626 boolean handled = super.onKeyDown(keyCode, event);
627 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
628 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
629 keyCode, event);
630 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700631 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700632 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700633 // showSearchDialog()
634 // If there are multiple keystrokes before the search dialog takes focus,
635 // onSearchRequested() will be called for every keystroke,
636 // but it is idempotent, so it's fine.
637 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
639 }
640
Joe Onorato8a9625e2010-01-28 15:55:35 -0800641 // Eat the long press event so the keyboard doesn't come up.
642 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
643 return true;
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 return handled;
647 }
648
Karl Rosaen138a0412009-04-23 19:00:21 -0700649 private String getTypedText() {
650 return mDefaultKeySsb.toString();
651 }
652
653 private void clearTypedText() {
654 mDefaultKeySsb.clear();
655 mDefaultKeySsb.clearSpans();
656 Selection.setSelection(mDefaultKeySsb, 0);
657 }
658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 /**
660 * Restores the previous state, if it exists.
661 *
662 * @param savedState The previous state.
663 */
664 private void restoreState(Bundle savedState) {
665 if (savedState == null) {
666 return;
667 }
668
Joe Onorato3a8820b2009-11-10 15:06:42 -0800669 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
670 if (allApps) {
671 showAllApps(false);
672 }
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
675 if (currentScreen > -1) {
676 mWorkspace.setCurrentScreen(currentScreen);
677 }
678
679 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
680 if (addScreen > -1) {
681 mAddItemCellInfo = new CellLayout.CellInfo();
682 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
683 addItemCellInfo.valid = true;
684 addItemCellInfo.screen = addScreen;
685 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
686 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
687 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
688 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
689 addItemCellInfo.findVacantCellsFromOccupied(
690 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
691 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
692 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
693 mRestoring = true;
694 }
695
696 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
697 if (renameFolder) {
698 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 mRestoring = true;
701 }
702 }
703
704 /**
705 * Finds all the views we need and configure them properly.
706 */
707 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400708 DragController dragController = mDragController;
709
Romain Guyb1b69f52009-08-10 15:10:15 -0700710 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400711 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712
Joe Onorato7c312c12009-08-13 21:36:53 -0700713 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700714 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700715 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800716 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700717 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800718 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
721 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500722 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723
Joe Onorato7c312c12009-08-13 21:36:53 -0700724 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
725 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
Joe Onorato7c312c12009-08-13 21:36:53 -0700727 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700729 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800730 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800731
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400732 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
733 hotseatLeft.setContentDescription(mHotseatLabels[0]);
734 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
735 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
736 hotseatRight.setContentDescription(mHotseatLabels[1]);
737 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400738
Romain Guy1fbc1c82009-11-09 20:43:08 -0800739 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
740 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
741
742 Drawable previous = mPreviousView.getDrawable();
743 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800744 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
Joe Onorato0d44e942009-11-16 18:20:51 -0800746 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800747 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800748 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800749 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400752 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754
755 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400756 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400757 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758
Joe Onorato00acb122009-08-04 16:04:30 -0400759 dragController.setDragScoller(workspace);
760 dragController.setDragListener(deleteZone);
761 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800762 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700763
Joe Onorato00acb122009-08-04 16:04:30 -0400764 // The order here is bottom to top.
765 dragController.addDropTarget(workspace);
766 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
Romain Guy8a73c512009-11-09 19:19:59 -0800769 @SuppressWarnings({"UnusedDeclaration"})
770 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800771 if (!isAllAppsVisible()) {
772 mWorkspace.scrollLeft();
773 }
Romain Guy8a73c512009-11-09 19:19:59 -0800774 }
775
776 @SuppressWarnings({"UnusedDeclaration"})
777 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800778 if (!isAllAppsVisible()) {
779 mWorkspace.scrollRight();
780 }
Romain Guy8a73c512009-11-09 19:19:59 -0800781 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400782
783 @SuppressWarnings({"UnusedDeclaration"})
784 public void launchHotSeat(View v) {
785 int index = -1;
786 if (v.getId() == R.id.hotseat_left) {
787 index = 0;
788 } else if (v.getId() == R.id.hotseat_right) {
789 index = 1;
790 }
791
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400792 // reload these every tap; you never know when they might change
793 loadHotseats();
794 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
795 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400796 startActivitySafely(
797 mHotseats[index],
798 "hotseat"
799 );
800 }
801 }
Romain Guy8a73c512009-11-09 19:19:59 -0800802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 /**
804 * Creates a view representing a shortcut.
805 *
806 * @param info The data structure describing the shortcut.
807 *
808 * @return A View inflated from R.layout.application.
809 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800810 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 return createShortcut(R.layout.application,
812 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
813 }
814
815 /**
816 * Creates a view representing a shortcut inflated from the specified resource.
817 *
818 * @param layoutResId The id of the XML layout used to create the shortcut.
819 * @param parent The group the shortcut belongs to.
820 * @param info The data structure describing the shortcut.
821 *
822 * @return A View inflated from layoutResId.
823 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800824 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
826
Joe Onorato0589f0f2010-02-08 13:44:00 -0800827 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
828 new FastBitmapDrawable(info.getIcon(mIconCache)),
829 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 favorite.setText(info.title);
831 favorite.setTag(info);
832 favorite.setOnClickListener(this);
833
834 return favorite;
835 }
836
837 /**
838 * Add an application shortcut to the workspace.
839 *
840 * @param data The intent describing the application.
841 * @param cellInfo The position on screen where to create the shortcut.
842 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 cellInfo.screen = mWorkspace.getCurrentScreen();
845 if (!findSingleSlot(cellInfo)) return;
846
Joe Onorato0589f0f2010-02-08 13:44:00 -0800847 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
848 data, context);
849
Romain Guy73b979d2009-06-09 12:57:21 -0700850 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800851 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800853 info.container = ItemInfo.NO_ID;
854 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
855 } else {
856 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
858 }
Romain Guycbb89e42009-06-08 15:52:54 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 /**
861 * Add a shortcut to the workspace.
862 *
863 * @param data The intent describing the shortcut.
864 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 cellInfo.screen = mWorkspace.getCurrentScreen();
868 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700869
Joe Onorato0589f0f2010-02-08 13:44:00 -0800870 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
872 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400874 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
875 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
877 }
878
Romain Guycbb89e42009-06-08 15:52:54 -0700879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700883 * @param data The intent describing the appWidgetId.
884 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700888 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700889
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800890 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700891
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700892 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700893
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700896 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 // Try finding open space on Launcher screen
899 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800900 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
901 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
902 return;
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700905 // Build Launcher-specific widget info and save to database
906 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 launcherInfo.spanX = spans[0];
908 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700909
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 LauncherModel.addItemToDatabase(this, launcherInfo,
911 LauncherSettings.Favorites.CONTAINER_DESKTOP,
912 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
913
914 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700918 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700919
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700920 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926 }
Romain Guycbb89e42009-06-08 15:52:54 -0700927
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
929 mDesktopItems.remove(launcherInfo);
930 launcherInfo.hostView = null;
931 }
932
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700933 public LauncherAppWidgetHost getAppWidgetHost() {
934 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
Romain Guycbb89e42009-06-08 15:52:54 -0700936
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800937 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800938 getWindow().closeAllPanels();
939
940 try {
941 dismissDialog(DIALOG_CREATE_SHORTCUT);
942 // Unlock the workspace if the dialog was showing
943 } catch (Exception e) {
944 // An exception is thrown if the dialog is not visible, which is fine
945 }
946
947 try {
948 dismissDialog(DIALOG_RENAME_FOLDER);
949 // Unlock the workspace if the dialog was showing
950 } catch (Exception e) {
951 // An exception is thrown if the dialog is not visible, which is fine
952 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800953
954 // Whatever we were doing is hereby canceled.
955 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800956 }
957
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 @Override
959 protected void onNewIntent(Intent intent) {
960 super.onNewIntent(intent);
961
962 // Close the menu
963 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800964 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800965 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700966
Joe Onorato14f122b2009-11-19 14:06:36 -0800967 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
968 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
969 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800970 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800971 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800972 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800973 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700974
Joe Onorato3a8820b2009-11-10 15:06:42 -0800975 final View v = getWindow().peekDecorView();
976 if (v != null && v.getWindowToken() != null) {
977 InputMethodManager imm = (InputMethodManager)getSystemService(
978 INPUT_METHOD_SERVICE);
979 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981 }
982 }
983
984 @Override
985 protected void onRestoreInstanceState(Bundle savedInstanceState) {
986 // Do not call super here
987 mSavedInstanceState = savedInstanceState;
988 }
989
990 @Override
991 protected void onSaveInstanceState(Bundle outState) {
992 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
993
994 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
995 if (folders.size() > 0) {
996 final int count = folders.size();
997 long[] ids = new long[count];
998 for (int i = 0; i < count; i++) {
999 final FolderInfo info = folders.get(i).getInfo();
1000 ids[i] = info.id;
1001 }
1002 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1003 } else {
1004 super.onSaveInstanceState(outState);
1005 }
1006
Joe Onoratofb0ca672009-09-14 17:55:46 -04001007 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001008 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011
1012 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1013 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1014 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1015
1016 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1017 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1018 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1019 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1020 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1021 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1022 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1023 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1024 layout.getOccupiedCells());
1025 }
1026
1027 if (mFolderInfo != null && mWaitingForResult) {
1028 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1029 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1030 }
1031 }
1032
1033 @Override
1034 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001038 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001040 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
1043 TextKeyListener.getInstance().release();
1044
Joe Onorato9c1289c2009-08-17 11:03:03 -04001045 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001048
1049 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001050
1051 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001052 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001053
1054 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056
1057 @Override
1058 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001059 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 super.startActivityForResult(intent, requestCode);
1061 }
1062
1063 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001064 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001066
Joe Onorato7bb17492009-09-24 17:51:01 -07001067 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001068
Karl Rosaen138a0412009-04-23 19:00:21 -07001069 if (initialQuery == null) {
1070 // Use any text typed in the launcher as the initial query
1071 initialQuery = getTypedText();
1072 clearTypedText();
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 if (appSearchData == null) {
1075 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001076 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
Karl Rosaen138a0412009-04-23 19:00:21 -07001079 final SearchManager searchManager =
1080 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001081 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001082 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
1084
1085 @Override
1086 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 if (isWorkspaceLocked()) {
1088 return false;
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1094 .setIcon(android.R.drawable.ic_menu_add)
1095 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001096 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 .setIcon(android.R.drawable.ic_menu_gallery)
1098 .setAlphabeticShortcut('W');
1099 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1100 .setIcon(android.R.drawable.ic_search_category_default)
1101 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1102 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1103 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1104 .setAlphabeticShortcut('N');
1105
1106 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001107 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1108 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109
1110 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1111 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1112 .setIntent(settings);
1113
1114 return true;
1115 }
1116
1117 @Override
1118 public boolean onPrepareOptionsMenu(Menu menu) {
1119 super.onPrepareOptionsMenu(menu);
1120
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001121 // If all apps is animating, don't show the menu, because we don't know
1122 // which one to show.
1123 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1124 return false;
1125 }
1126
1127 // Only show the add and wallpaper options when we're not in all apps.
1128 boolean visible = !mAllAppsGrid.isOpaque();
1129 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1130 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1131
1132 // Disable add if the workspace is full.
1133 if (visible) {
1134 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1135 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137
1138 return true;
1139 }
1140
1141 @Override
1142 public boolean onOptionsItemSelected(MenuItem item) {
1143 switch (item.getItemId()) {
1144 case MENU_ADD:
1145 addItems();
1146 return true;
1147 case MENU_WALLPAPER_SETTINGS:
1148 startWallpaper();
1149 return true;
1150 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001151 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 return true;
1153 case MENU_NOTIFICATIONS:
1154 showNotifications();
1155 return true;
1156 }
1157
1158 return super.onOptionsItemSelected(item);
1159 }
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Karl Rosaen138a0412009-04-23 19:00:21 -07001161 /**
1162 * Indicates that we want global search for this activity by setting the globalSearch
1163 * argument for {@link #startSearch} to true.
1164 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001166 @Override
1167 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001168 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001169 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001170 }
1171
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 public boolean isWorkspaceLocked() {
1173 return mWorkspaceLoading || mWaitingForResult;
1174 }
1175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001177 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 showAddDialog(mMenuAddInfo);
1179 }
1180
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001181 void addAppWidget(Intent data) {
1182 // TODO: catch bad widget exception when sent
1183 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001184 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185
Bjorn Bringert7984c942009-12-09 15:38:25 +00001186 if (appWidget.configure != null) {
1187 // Launch over to configure widget, if needed
1188 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1189 intent.setComponent(appWidget.configure);
1190 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1191
Romain Guy8e633c52010-03-04 12:51:36 -08001192 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001194 // Otherwise just add it
1195 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
1197 }
Romain Guycbb89e42009-06-08 15:52:54 -07001198
Joe Onoratodeb98af2010-02-19 14:59:39 -08001199 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001200 // Handle case where user selected "Applications"
1201 String applicationName = getResources().getString(R.string.group_applications);
1202 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001203
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001204 if (applicationName != null && applicationName.equals(shortcutName)) {
1205 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1206 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001208 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1209 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001210 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001211 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001212 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 }
1215
1216 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001217 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001218 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001219 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001221 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001223 } else {
1224 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 UserFolderInfo folderInfo = new UserFolderInfo();
1230 folderInfo.title = getText(R.string.folder_name);
1231
1232 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1233 cellInfo.screen = mWorkspace.getCurrentScreen();
1234 if (!findSingleSlot(cellInfo)) return;
1235
1236 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 LauncherModel.addItemToDatabase(this, folderInfo,
1238 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001240 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
1242 // Create the view
1243 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1244 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1245 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001246 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Joe Onorato9c1289c2009-08-17 11:03:03 -04001249 void removeFolder(FolderInfo folder) {
1250 mFolders.remove(folder.id);
1251 }
1252
1253 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 cellInfo.screen = mWorkspace.getCurrentScreen();
1255 if (!findSingleSlot(cellInfo)) return;
1256
1257 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1258
1259 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1261 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1263 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265 }
1266
1267 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1268 CellLayout.CellInfo cellInfo, boolean notify) {
1269
1270 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1271 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1272
1273 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 Intent.ShortcutIconResource iconResource = null;
1275
1276 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1277 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1278 try {
1279 iconResource = (Intent.ShortcutIconResource) extra;
1280 final PackageManager packageManager = context.getPackageManager();
1281 Resources resources = packageManager.getResourcesForApplication(
1282 iconResource.packageName);
1283 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1284 icon = resources.getDrawable(id);
1285 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001286 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288 }
1289
1290 if (icon == null) {
1291 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1292 }
1293
1294 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001295 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 info.title = name;
1297 info.iconResource = iconResource;
1298 info.uri = data.getData();
1299 info.baseIntent = baseIntent;
1300 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1301 LiveFolders.DISPLAY_MODE_GRID);
1302
1303 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1304 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001305 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306
1307 return info;
1308 }
1309
1310 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1311 final int[] xy = new int[2];
1312 if (findSlot(cellInfo, xy, 1, 1)) {
1313 cellInfo.cellX = xy[0];
1314 cellInfo.cellY = xy[1];
1315 return true;
1316 }
1317 return false;
1318 }
1319
1320 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1321 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1322 boolean[] occupied = mSavedState != null ?
1323 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1324 cellInfo = mWorkspace.findAllVacantCells(occupied);
1325 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1326 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1327 return false;
1328 }
1329 }
1330 return true;
1331 }
1332
1333 private void showNotifications() {
1334 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1335 if (statusBar != null) {
1336 statusBar.expand();
1337 }
1338 }
1339
1340 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001341 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001343 Intent chooser = Intent.createChooser(pickWallpaper,
1344 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001345 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1346 // Removed in Eclair MR1
1347// WallpaperManager wm = (WallpaperManager)
1348// getSystemService(Context.WALLPAPER_SERVICE);
1349// WallpaperInfo wi = wm.getWallpaperInfo();
1350// if (wi != null && wi.getSettingsActivity() != null) {
1351// LabeledIntent li = new LabeledIntent(getPackageName(),
1352// R.string.configure_wallpaper, 0);
1353// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1354// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1355// }
Mike Clerona0618e42009-10-22 13:55:21 -07001356 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001359 /**
1360 * Registers various content observers. The current implementation registers
1361 * only a favorites observer to keep track of the favorites applications.
1362 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001363 private void registerContentObservers() {
1364 ContentResolver resolver = getContentResolver();
1365 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1366 true, mWidgetObserver);
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 @Override
1370 public boolean dispatchKeyEvent(KeyEvent event) {
1371 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1372 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001374 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001375 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001376 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001377 dumpState();
1378 return true;
1379 }
1380 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001381 }
1382 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1383 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001384 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 return true;
1386 }
1387 }
1388
1389 return super.dispatchKeyEvent(event);
1390 }
1391
Joe Onorato88ec0992009-11-19 13:16:06 -08001392 @Override
1393 public void onBackPressed() {
1394 if (isAllAppsVisible()) {
1395 closeAllApps(true);
1396 } else {
1397 closeFolder();
1398 }
1399 dismissPreview(mPreviousView);
1400 dismissPreview(mNextView);
1401 }
1402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 private void closeFolder() {
1404 Folder folder = mWorkspace.getOpenFolder();
1405 if (folder != null) {
1406 closeFolder(folder);
1407 }
1408 }
1409
1410 void closeFolder(Folder folder) {
1411 folder.getInfo().opened = false;
1412 ViewGroup parent = (ViewGroup) folder.getParent();
1413 if (parent != null) {
1414 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001415 if (folder instanceof DropTarget) {
1416 // Live folders aren't DropTargets.
1417 mDragController.removeDropTarget((DropTarget)folder);
1418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420 folder.onClose();
1421 }
1422
1423 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001424 * Re-listen when widgets are reset.
1425 */
1426 private void onAppWidgetReset() {
1427 mAppWidgetHost.startListening();
1428 }
1429
1430 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1432 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 private void unbindDesktopItems() {
1435 for (ItemInfo item: mDesktopItems) {
1436 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 }
1438 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 /**
1441 * Launches the intent referred by the clicked shortcut.
1442 *
1443 * @param v The view representing the clicked shortcut.
1444 */
1445 public void onClick(View v) {
1446 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001447 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001449 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001450 int[] pos = new int[2];
1451 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001452 intent.setSourceBounds(new Rect(pos[0], pos[1],
1453 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001454 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 } else if (tag instanceof FolderInfo) {
1456 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001457 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001458 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001459 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001460 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001461 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 }
1464 }
1465
Joe Onoratof984e852010-03-25 09:47:45 -07001466 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1468 try {
1469 startActivity(intent);
1470 } catch (ActivityNotFoundException e) {
1471 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001472 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 } catch (SecurityException e) {
1474 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001475 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001477 "or use the exported attribute for this activity. "
1478 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480 }
Romain Guy8e633c52010-03-04 12:51:36 -08001481
1482 void startActivityForResultSafely(Intent intent, int requestCode) {
1483 try {
1484 startActivityForResult(intent, requestCode);
1485 } catch (ActivityNotFoundException e) {
1486 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1487 } catch (SecurityException e) {
1488 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1489 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1490 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1491 "or use the exported attribute for this activity.", e);
1492 }
1493 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
1495 private void handleFolderClick(FolderInfo folderInfo) {
1496 if (!folderInfo.opened) {
1497 // Close any open folder
1498 closeFolder();
1499 // Open the requested folder
1500 openFolder(folderInfo);
1501 } else {
1502 // Find the open folder...
1503 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1504 int folderScreen;
1505 if (openFolder != null) {
1506 folderScreen = mWorkspace.getScreenForView(openFolder);
1507 // .. and close it
1508 closeFolder(openFolder);
1509 if (folderScreen != mWorkspace.getCurrentScreen()) {
1510 // Close any folder open on the current screen
1511 closeFolder();
1512 // Pull the folder onto this screen
1513 openFolder(folderInfo);
1514 }
1515 }
1516 }
1517 }
1518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 /**
1520 * Opens the user fodler described by the specified tag. The opening of the folder
1521 * is animated relative to the specified View. If the View is null, no animation
1522 * is played.
1523 *
1524 * @param folderInfo The FolderInfo describing the folder to open.
1525 */
1526 private void openFolder(FolderInfo folderInfo) {
1527 Folder openFolder;
1528
1529 if (folderInfo instanceof UserFolderInfo) {
1530 openFolder = UserFolder.fromXml(this);
1531 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001532 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 } else {
1534 return;
1535 }
1536
Joe Onorato00acb122009-08-04 16:04:30 -04001537 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 openFolder.setLauncher(this);
1539
1540 openFolder.bind(folderInfo);
1541 folderInfo.opened = true;
1542
1543 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1544 openFolder.onOpen();
1545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001548 switch (v.getId()) {
1549 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001550 if (!isAllAppsVisible()) {
1551 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1552 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001553 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001554 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001555 return true;
1556 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001557 if (!isAllAppsVisible()) {
1558 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1559 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001560 showPreviews(v);
1561 }
1562 return true;
1563 case R.id.all_apps_button:
1564 if (!isAllAppsVisible()) {
1565 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1566 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1567 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001568 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001569 return true;
1570 }
1571
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 return false;
1574 }
1575
1576 if (!(v instanceof CellLayout)) {
1577 v = (View) v.getParent();
1578 }
1579
1580 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1581
1582 // This happens when long clicking an item with the dpad/trackball
1583 if (cellInfo == null) {
1584 return true;
1585 }
1586
1587 if (mWorkspace.allowLongPress()) {
1588 if (cellInfo.cell == null) {
1589 if (cellInfo.valid) {
1590 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001591 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001592 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1593 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 showAddDialog(cellInfo);
1595 }
1596 } else {
1597 if (!(cellInfo.cell instanceof Folder)) {
1598 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001599 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1600 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 mWorkspace.startDrag(cellInfo);
1602 }
1603 }
1604 }
1605 return true;
1606 }
1607
Romain Guye6b8e2f2009-11-10 11:56:55 -08001608 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001609 private void dismissPreview(final View v) {
1610 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001611 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001612 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1613 public void onDismiss() {
1614 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1615 int count = group.getChildCount();
1616 for (int i = 0; i < count; i++) {
1617 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1618 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001619 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1620 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1621
1622 v.setTag(R.id.workspace, null);
1623 v.setTag(R.id.icon, null);
1624 window.setOnDismissListener(null);
1625 }
1626 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001627 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001628 }
1629 v.setTag(null);
1630 }
1631
Romain Guyf8e6a802009-12-07 17:48:02 -08001632 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001633 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001634 }
1635
Romain Guya6abce82009-11-10 02:54:41 -08001636 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001637 final Resources resources = getResources();
1638 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001639
Romain Guya6abce82009-11-10 02:54:41 -08001640 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001641
1642 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001643
Romain Guyf8e6a802009-12-07 17:48:02 -08001644 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001645 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001646 int extraW = (int) ((r.left + r.right) * max);
1647 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001648
1649 int aW = cell.getWidth() - extraW;
1650 float w = aW / max;
1651
1652 int width = cell.getWidth();
1653 int height = cell.getHeight();
1654 int x = cell.getLeftPadding();
1655 int y = cell.getTopPadding();
1656 width -= (x + cell.getRightPadding());
1657 height -= (y + cell.getBottomPadding());
1658
1659 float scale = w / width;
1660
1661 int count = end - start;
1662
1663 final float sWidth = width * scale;
1664 float sHeight = height * scale;
1665
Romain Guye6b8e2f2009-11-10 11:56:55 -08001666 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001667
Romain Guye6b8e2f2009-11-10 11:56:55 -08001668 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1669 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001670
1671 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001672 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001673 cell = (CellLayout) workspace.getChildAt(i);
1674
Romain Guyf8e6a802009-12-07 17:48:02 -08001675 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001676 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001677
1678 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001679 c.scale(scale, scale);
1680 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1681 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001682
Romain Guy9d31e9f2009-11-11 18:54:28 -08001683 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001684 image.setImageBitmap(bitmap);
1685 image.setTag(i);
1686 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001687 image.setOnFocusChangeListener(handler);
1688 image.setFocusable(true);
1689 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001690
1691 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001692 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1693
1694 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001695 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001696
1697 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001698 p.setContentView(preview);
1699 p.setWidth((int) (sWidth * count + extraW));
1700 p.setHeight((int) (sHeight + extraH));
1701 p.setAnimationStyle(R.style.AnimationPreview);
1702 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001703 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001704 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001705 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001706
Romain Guye6b8e2f2009-11-10 11:56:55 -08001707 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1708 public void onDismiss() {
1709 dismissPreview(anchor);
1710 }
1711 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001712
1713 anchor.setTag(p);
1714 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001715 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001716 }
1717
Romain Guy9d31e9f2009-11-11 18:54:28 -08001718 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001719 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001720
Romain Guye6b8e2f2009-11-10 11:56:55 -08001721 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001722 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001723 }
1724
1725 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001726 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001727 v.post(this);
1728 }
1729
1730 public void run() {
1731 dismissPreview(mAnchor);
1732 }
1733
1734 public void onFocusChange(View v, boolean hasFocus) {
1735 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001736 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001737 }
Romain Guya6abce82009-11-10 02:54:41 -08001738 }
1739 }
1740
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 Workspace getWorkspace() {
1742 return mWorkspace;
1743 }
1744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 @Override
1746 protected Dialog onCreateDialog(int id) {
1747 switch (id) {
1748 case DIALOG_CREATE_SHORTCUT:
1749 return new CreateShortcut().createDialog();
1750 case DIALOG_RENAME_FOLDER:
1751 return new RenameFolder().createDialog();
1752 }
1753
1754 return super.onCreateDialog(id);
1755 }
1756
1757 @Override
1758 protected void onPrepareDialog(int id, Dialog dialog) {
1759 switch (id) {
1760 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 break;
1762 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001763 if (mFolderInfo != null) {
1764 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1765 final CharSequence text = mFolderInfo.title;
1766 input.setText(text);
1767 input.setSelection(0, text.length());
1768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 break;
1770 }
1771 }
1772
1773 void showRenameDialog(FolderInfo info) {
1774 mFolderInfo = info;
1775 mWaitingForResult = true;
1776 showDialog(DIALOG_RENAME_FOLDER);
1777 }
1778
1779 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1780 mAddItemCellInfo = cellInfo;
1781 mWaitingForResult = true;
1782 showDialog(DIALOG_CREATE_SHORTCUT);
1783 }
1784
Joe Onoratodeb98af2010-02-19 14:59:39 -08001785 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001786 Bundle bundle = new Bundle();
1787
1788 ArrayList<String> shortcutNames = new ArrayList<String>();
1789 shortcutNames.add(getString(R.string.group_applications));
1790 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1791
1792 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1793 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1794 R.drawable.ic_launcher_application));
1795 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1796
1797 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1798 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001799 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001800 pickIntent.putExtras(bundle);
1801
Joe Onoratodeb98af2010-02-19 14:59:39 -08001802 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001803 }
1804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 private class RenameFolder {
1806 private EditText mInput;
1807
1808 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1810 mInput = (EditText) layout.findViewById(R.id.folder_name);
1811
1812 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1813 builder.setIcon(0);
1814 builder.setTitle(getString(R.string.rename_folder_title));
1815 builder.setCancelable(true);
1816 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1817 public void onCancel(DialogInterface dialog) {
1818 cleanup();
1819 }
1820 });
1821 builder.setNegativeButton(getString(R.string.cancel_action),
1822 new Dialog.OnClickListener() {
1823 public void onClick(DialogInterface dialog, int which) {
1824 cleanup();
1825 }
1826 }
1827 );
1828 builder.setPositiveButton(getString(R.string.rename_action),
1829 new Dialog.OnClickListener() {
1830 public void onClick(DialogInterface dialog, int which) {
1831 changeFolderName();
1832 }
1833 }
1834 );
1835 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001836
1837 final AlertDialog dialog = builder.create();
1838 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1839 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001840 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001841 mInput.requestFocus();
1842 InputMethodManager inputManager = (InputMethodManager)
1843 getSystemService(Context.INPUT_METHOD_SERVICE);
1844 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001845 }
1846 });
1847
1848 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
1850
1851 private void changeFolderName() {
1852 final String name = mInput.getText().toString();
1853 if (!TextUtils.isEmpty(name)) {
1854 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001855 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 mFolderInfo.title = name;
1857 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1858
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001860 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 mModel.setWorkspaceDirty();
1862 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 } else {
1864 final FolderIcon folderIcon = (FolderIcon)
1865 mWorkspace.getViewForTag(mFolderInfo);
1866 if (folderIcon != null) {
1867 folderIcon.setText(name);
1868 getWorkspace().requestLayout();
1869 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001870 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871 mModel.setWorkspaceDirty();
1872 mWorkspaceLoading = true;
1873 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875 }
1876 }
1877 cleanup();
1878 }
1879
1880 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 dismissDialog(DIALOG_RENAME_FOLDER);
1882 mWaitingForResult = false;
1883 mFolderInfo = null;
1884 }
1885 }
1886
Joe Onoratofb0ca672009-09-14 17:55:46 -04001887 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001888 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001889 }
1890
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001891 // AllAppsView.Watcher
1892 public void zoomed(float zoom) {
1893 if (zoom == 1.0f) {
1894 mWorkspace.setVisibility(View.GONE);
1895 }
1896 }
1897
Joe Onorato3a8820b2009-11-10 15:06:42 -08001898 void showAllApps(boolean animated) {
1899 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001900
Romain Guyc16fea72010-03-12 17:17:56 -08001901 ((View) mAllAppsGrid).setFocusable(true);
1902 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001903
Joe Onorato7c312c12009-08-13 21:36:53 -07001904 // TODO: fade these two too
1905 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001906 }
1907
Joe Onoratob2061212009-11-24 16:13:54 -05001908 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001909 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001910 * - Home from workspace
1911 * - from center screen
1912 * - from other screens
1913 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001914 * - from center screen
1915 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001916 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001917 * - from center screen
1918 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001919 * - Launch app from workspace and quit
1920 * - with back
1921 * - with home
1922 * - Launch app from all apps and quit
1923 * - with back
1924 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001925 * - Go to a screen that's not the default, then all
1926 * apps, and launch and app, and go back
1927 * - with back
1928 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001929 * - On workspace, long press power and go back
1930 * - with back
1931 * - with home
1932 * - On all apps, long press power and go back
1933 * - with back
1934 * - with home
1935 * - On workspace, power off
1936 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001937 * - Launch an app and turn off the screen while in that app
1938 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001939 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001940 * - From all apps
1941 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001942 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1943 * - From all apps
1944 * - From the center workspace
1945 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001946 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001947 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001948 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001949 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001950 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001951 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001952 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001953 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001954 }
1955
Joe Onorato7c312c12009-08-13 21:36:53 -07001956 void lockAllApps() {
1957 // TODO
1958 }
1959
1960 void unlockAllApps() {
1961 // TODO
1962 }
1963
Joe Onorato7404ee42009-07-31 11:54:44 -07001964 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 * Displays the shortcut creation dialog and launches, if necessary, the
1966 * appropriate activity.
1967 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001968 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001969 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1970 DialogInterface.OnShowListener {
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1978 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001979 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 builder.setInverseBackgroundForced(true);
1982
1983 AlertDialog dialog = builder.create();
1984 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001985 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001986 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 return dialog;
1989 }
1990
1991 public void onCancel(DialogInterface dialog) {
1992 mWaitingForResult = false;
1993 cleanup();
1994 }
1995
Romain Guycbb89e42009-06-08 15:52:54 -07001996 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001997 }
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002000 try {
2001 dismissDialog(DIALOG_CREATE_SHORTCUT);
2002 } catch (Exception e) {
2003 // An exception is thrown if the dialog is not visible, which is fine
2004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 /**
2008 * Handle the action clicked in the "Add to home" dialog.
2009 */
2010 public void onClick(DialogInterface dialog, int which) {
2011 Resources res = getResources();
2012 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002013
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002014 switch (which) {
2015 case AddAdapter.ITEM_SHORTCUT: {
2016 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002017 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002018 break;
2019 }
Romain Guycbb89e42009-06-08 15:52:54 -07002020
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002021 case AddAdapter.ITEM_APPWIDGET: {
2022 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002023
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002024 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2025 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002026 // start the pick activity
2027 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2028 break;
2029 }
Romain Guycbb89e42009-06-08 15:52:54 -07002030
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002031 case AddAdapter.ITEM_LIVE_FOLDER: {
2032 // Insert extra item to handle inserting folder
2033 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002034
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002035 ArrayList<String> shortcutNames = new ArrayList<String>();
2036 shortcutNames.add(res.getString(R.string.group_folder));
2037 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002039 ArrayList<ShortcutIconResource> shortcutIcons =
2040 new ArrayList<ShortcutIconResource>();
2041 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2042 R.drawable.ic_launcher_folder));
2043 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2044
2045 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2046 pickIntent.putExtra(Intent.EXTRA_INTENT,
2047 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2048 pickIntent.putExtra(Intent.EXTRA_TITLE,
2049 getText(R.string.title_select_live_folder));
2050 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002051
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002052 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2053 break;
2054 }
2055
2056 case AddAdapter.ITEM_WALLPAPER: {
2057 startWallpaper();
2058 break;
2059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002062
2063 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002064 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067
2068 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002069 * Receives notifications when applications are added/removed.
2070 */
2071 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2072 @Override
2073 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002074 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002075 String reason = intent.getStringExtra("reason");
2076 if (!"homekey".equals(reason)) {
2077 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002078 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002079 animate = false;
2080 }
Joe Onoratob2061212009-11-24 16:13:54 -05002081 closeAllApps(animate);
2082 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002083 }
2084 }
2085
2086 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002087 * Receives notifications whenever the appwidgets are reset.
2088 */
2089 private class AppWidgetResetObserver extends ContentObserver {
2090 public AppWidgetResetObserver() {
2091 super(new Handler());
2092 }
2093
2094 @Override
2095 public void onChange(boolean selfChange) {
2096 onAppWidgetReset();
2097 }
2098 }
2099
2100 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 public int getCurrentWorkspaceScreen() {
2104 return mWorkspace.getCurrentScreen();
2105 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002106
Joe Onorato9c1289c2009-08-17 11:03:03 -04002107 /**
2108 * Refreshes the shortcuts shown on the workspace.
2109 *
2110 * Implementation of the method from LauncherModel.Callbacks.
2111 */
2112 public void startBinding() {
2113 final Workspace workspace = mWorkspace;
2114 int count = workspace.getChildCount();
2115 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002116 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002117 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2118 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002119
Joe Onorato9c1289c2009-08-17 11:03:03 -04002120 if (DEBUG_USER_INTERFACE) {
2121 android.widget.Button finishButton = new android.widget.Button(this);
2122 finishButton.setText("Finish");
2123 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2124
2125 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2126 public void onClick(View v) {
2127 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002128 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002129 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 }
2131 }
2132
2133 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002134 * Bind the items start-end from the list.
2135 *
2136 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2139
2140 final Workspace workspace = mWorkspace;
2141
2142 for (int i=start; i<end; i++) {
2143 final ItemInfo item = shortcuts.get(i);
2144 mDesktopItems.add(item);
2145 switch (item.itemType) {
2146 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2147 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002148 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2150 false);
2151 break;
2152 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2153 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2154 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2155 (UserFolderInfo) item);
2156 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2157 false);
2158 break;
2159 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2160 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2161 R.layout.live_folder_icon, this,
2162 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2163 (LiveFolderInfo) item);
2164 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2165 false);
2166 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 }
2169
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 }
2172
Joe Onorato9c1289c2009-08-17 11:03:03 -04002173 /**
2174 * Implementation of the method from LauncherModel.Callbacks.
2175 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002176 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2177 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002180
2181 /**
2182 * Add the views for a widget to the workspace.
2183 *
2184 * Implementation of the method from LauncherModel.Callbacks.
2185 */
2186 public void bindAppWidget(LauncherAppWidgetInfo item) {
2187 final Workspace workspace = mWorkspace;
2188
2189 final int appWidgetId = item.appWidgetId;
2190 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2191 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2192
Joe Onorato9c1289c2009-08-17 11:03:03 -04002193 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2194 item.hostView.setTag(item);
2195
2196 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2197 item.cellY, item.spanX, item.spanY, false);
2198
2199 workspace.requestLayout();
2200
2201 mDesktopItems.add(item);
2202 }
2203
2204 /**
2205 * Callback saying that there aren't any more items to bind.
2206 *
2207 * Implementation of the method from LauncherModel.Callbacks.
2208 */
2209 public void finishBindingItems() {
2210 if (mSavedState != null) {
2211 if (!mWorkspace.hasFocus()) {
2212 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2213 }
2214
2215 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2216 if (userFolders != null) {
2217 for (long folderId : userFolders) {
2218 final FolderInfo info = mFolders.get(folderId);
2219 if (info != null) {
2220 openFolder(info);
2221 }
2222 }
2223 final Folder openFolder = mWorkspace.getOpenFolder();
2224 if (openFolder != null) {
2225 openFolder.requestFocus();
2226 }
2227 }
2228
Joe Onorato9c1289c2009-08-17 11:03:03 -04002229 mSavedState = null;
2230 }
2231
2232 if (mSavedInstanceState != null) {
2233 super.onRestoreInstanceState(mSavedInstanceState);
2234 mSavedInstanceState = null;
2235 }
2236
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 mWorkspaceLoading = false;
2238 }
2239
2240 /**
2241 * Add the icons for all apps.
2242 *
2243 * Implementation of the method from LauncherModel.Callbacks.
2244 */
2245 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002246 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002247 }
2248
2249 /**
2250 * A package was installed.
2251 *
2252 * Implementation of the method from LauncherModel.Callbacks.
2253 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002254 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002255 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002256 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002257 }
2258
2259 /**
2260 * A package was updated.
2261 *
2262 * Implementation of the method from LauncherModel.Callbacks.
2263 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002264 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002265 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002266 mWorkspace.updateShortcuts(apps);
2267 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002268 }
2269
2270 /**
2271 * A package was uninstalled.
2272 *
2273 * Implementation of the method from LauncherModel.Callbacks.
2274 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002275 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002277 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002278 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 }
Joe Onoratobe386092009-11-17 17:32:16 -08002280
2281 /**
2282 * Prints out out state for debugging.
2283 */
2284 public void dumpState() {
2285 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002286 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002287 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2288 Log.d(TAG, "mRestoring=" + mRestoring);
2289 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2290 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2291 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2292 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002293 mModel.dumpState();
2294 mAllAppsGrid.dumpState();
2295 Log.d(TAG, "END launcher2 dump state");
2296 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297}