blob: 07e8834c0af488312f7445afef42a9cd758c06d5 [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;
Michael Jurkaaf442092010-06-10 17:01:57 -070020import com.android.launcher.R;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000021
The Android Open Source Project31dd5032009-03-03 19:32:27 -080022import android.app.Activity;
23import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.app.Dialog;
25import android.app.SearchManager;
26import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070027import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080031import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040032import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080033import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.Context;
35import android.content.DialogInterface;
36import android.content.Intent;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.IntentFilter;
Michael Jurkaaf442092010-06-10 17:01:57 -070038import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040040import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070042import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040043import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080046import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070047import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080048import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040050import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040054import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080055import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070056import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.text.Selection;
58import android.text.SpannableStringBuilder;
59import android.text.TextUtils;
60import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070061import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080063import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.KeyEvent;
65import android.view.LayoutInflater;
66import android.view.Menu;
67import android.view.MenuItem;
68import android.view.View;
69import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.View.OnLongClickListener;
Michael Jurkaaf442092010-06-10 17:01:57 -070071import android.view.animation.AnimationUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070074import android.widget.ImageView;
75import android.widget.LinearLayout;
76import android.widget.PopupWindow;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.widget.TextView;
78import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
Michael Jurkaaf442092010-06-10 17:01:57 -070080import java.io.DataInputStream;
Romain Guy98d01652009-06-30 16:21:04 -070081import java.io.DataOutputStream;
82import java.io.FileNotFoundException;
83import java.io.IOException;
Michael Jurkaaf442092010-06-10 17:01:57 -070084import java.util.ArrayList;
85import java.util.HashMap;
86import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080087
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";
Joe Onoratocc67f472010-06-08 10:54:30 -070094 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095
Joe Onorato9c1289c2009-08-17 11:03:03 -040096 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -040097 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 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;
Michael Jurkaaf442092010-06-10 17:01:57 -0700184 private WidgetChooser mWidgetChooser;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private Bundle mSavedState;
187
188 private SpannableStringBuilder mDefaultKeySsb = null;
189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 private boolean mWorkspaceLoading = true;
191
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800192 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private boolean mRestoring;
194 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
196 private Bundle mSavedInstanceState;
197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800199 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200
Romain Guy84f296c2009-11-04 15:00:44 -0800201 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
202 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700203
Romain Guy1fbc1c82009-11-09 20:43:08 -0800204 private ImageView mPreviousView;
205 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500206
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400207 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400208 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
250 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 }
253
254 // For handling default keys
255 mDefaultKeySsb = new SpannableStringBuilder();
256 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800257
258 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
259 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 }
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700263 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
264 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final Configuration configuration = getResources().getConfiguration();
267
Romain Guy98d01652009-06-30 16:21:04 -0700268 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 final String locale = configuration.locale.toString();
270
Romain Guy98d01652009-06-30 16:21:04 -0700271 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 final int mcc = configuration.mcc;
273
Romain Guy98d01652009-06-30 16:21:04 -0700274 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 final int mnc = configuration.mnc;
276
Romain Guy84f296c2009-11-04 15:00:44 -0800277 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Romain Guy84f296c2009-11-04 15:00:44 -0800279 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700280 localeConfiguration.locale = locale;
281 localeConfiguration.mcc = mcc;
282 localeConfiguration.mnc = mnc;
283
284 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400286
287 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700288 }
289 }
290
291 private static class LocaleConfiguration {
292 public String locale;
293 public int mcc = -1;
294 public int mnc = -1;
295 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700296
Romain Guy98d01652009-06-30 16:21:04 -0700297 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
298 DataInputStream in = null;
299 try {
300 in = new DataInputStream(context.openFileInput(PREFERENCES));
301 configuration.locale = in.readUTF();
302 configuration.mcc = in.readInt();
303 configuration.mnc = in.readInt();
304 } catch (FileNotFoundException e) {
305 // Ignore
306 } catch (IOException e) {
307 // Ignore
308 } finally {
309 if (in != null) {
310 try {
311 in.close();
312 } catch (IOException e) {
313 // Ignore
314 }
315 }
316 }
317 }
318
319 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
320 DataOutputStream out = null;
321 try {
322 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
323 out.writeUTF(configuration.locale);
324 out.writeInt(configuration.mcc);
325 out.writeInt(configuration.mnc);
326 out.flush();
327 } catch (FileNotFoundException e) {
328 // Ignore
329 } catch (IOException e) {
330 //noinspection ResultOfMethodCallIgnored
331 context.getFileStreamPath(PREFERENCES).delete();
332 } finally {
333 if (out != null) {
334 try {
335 out.close();
336 } catch (IOException e) {
337 // Ignore
338 }
339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341 }
342
343 static int getScreen() {
344 synchronized (sLock) {
345 return sScreen;
346 }
347 }
348
349 static void setScreen(int screen) {
350 synchronized (sLock) {
351 sScreen = screen;
352 }
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700356 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357
358 Display display = getWindowManager().getDefaultDisplay();
359 boolean isPortrait = display.getWidth() < display.getHeight();
360
361 final int width = isPortrait ? display.getWidth() : display.getHeight();
362 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700363 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
365
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400366 // Note: This doesn't do all the client-id magic that BrowserProvider does
367 // in Browser. (http://b/2425179)
368 private Uri getDefaultBrowserUri() {
369 String url = getString(R.string.default_browser_url);
370 if (url.indexOf("{CID}") != -1) {
371 url = url.replace("{CID}", "android-google");
372 }
373 return Uri.parse(url);
374 }
375
376 // Load the Intent templates from arrays.xml to populate the hotseats. For
377 // each Intent, if it resolves to a single app, use that as the launch
378 // intent & use that app's label as the contentDescription. Otherwise,
379 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400380 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400381 if (mHotseatConfig == null) {
382 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
383 if (mHotseatConfig.length > 0) {
384 mHotseats = new Intent[mHotseatConfig.length];
385 mHotseatLabels = new CharSequence[mHotseatConfig.length];
386 mHotseatIcons = new Drawable[mHotseatConfig.length];
387 } else {
388 mHotseats = null;
389 mHotseatIcons = null;
390 mHotseatLabels = null;
391 }
392
393 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
394 for (int i=0; i<mHotseatConfig.length; i++) {
395 // load icon for this slot; currently unrelated to the actual activity
396 try {
397 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
398 } catch (ArrayIndexOutOfBoundsException ex) {
399 Log.w(TAG, "Missing hotseat_icons array item #" + i);
400 mHotseatIcons[i] = null;
401 }
402 }
403 hotseatIconDrawables.recycle();
404 }
405
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400406 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400407 for (int i=0; i<mHotseatConfig.length; i++) {
408 Intent intent = null;
409 if (mHotseatConfig[i].equals("*BROWSER*")) {
410 // magic value meaning "launch user's default web browser"
411 // replace it with a generic web request so we can see if there is indeed a default
412 String defaultUri = getString(R.string.default_browser_url);
413 intent = new Intent(
414 Intent.ACTION_VIEW,
415 ((defaultUri != null)
416 ? Uri.parse(defaultUri)
417 : getDefaultBrowserUri())
418 ).addCategory(Intent.CATEGORY_BROWSABLE);
419 // note: if the user launches this without a default set, she
420 // will always be taken to the default URL above; this is
421 // unavoidable as we must specify a valid URL in order for the
422 // chooser to appear, and once the user selects something, that
423 // URL is unavoidably sent to the chosen app.
424 } else {
425 try {
426 intent = Intent.parseUri(mHotseatConfig[i], 0);
427 } catch (java.net.URISyntaxException ex) {
428 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
429 // bogus; leave intent=null
430 }
431 }
432
433 if (intent == null) {
434 mHotseats[i] = null;
435 mHotseatLabels[i] = getText(R.string.activity_not_found);
436 continue;
437 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400438
439 if (LOGD) {
440 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400441 + " initial intent=["
442 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400443 + "]");
444 }
445
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400446 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
447 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
448 if (LOGD) {
449 Log.d(TAG, "Best match for intent: " + bestMatch);
450 Log.d(TAG, "All matches: ");
451 for (ResolveInfo ri : allMatches) {
452 Log.d(TAG, " --> " + ri);
453 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400454 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400455 // did this resolve to a single app, or the resolver?
456 if (allMatches.size() == 0 || bestMatch == null) {
457 // can't find any activity to handle this. let's leave the
458 // intent as-is and let Launcher show a toast when it fails
459 // to launch.
460 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400461
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400462 // set accessibility text to "Not installed"
463 mHotseatLabels[i] = getText(R.string.activity_not_found);
464 } else {
465 boolean found = false;
466 for (ResolveInfo ri : allMatches) {
467 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
468 && bestMatch.activityInfo.applicationInfo.packageName
469 .equals(ri.activityInfo.applicationInfo.packageName)) {
470 found = true;
471 break;
472 }
473 }
474
475 if (!found) {
476 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
477 // the bestMatch is probably the ResolveActivity, meaning the
478 // user has not yet selected a default
479 // so: we'll keep the original intent for now
480 mHotseats[i] = intent;
481
482 // set the accessibility text to "Select shortcut"
483 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
484 } else {
485 // we have an app!
486 // now reconstruct the intent to launch it through the front
487 // door
488 ComponentName com = new ComponentName(
489 bestMatch.activityInfo.applicationInfo.packageName,
490 bestMatch.activityInfo.name);
491 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
492
493 // load the app label for accessibility
494 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
495 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400496 }
497
498 if (LOGD) {
499 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400500 + " final intent=["
501 + ((mHotseats[i] == null)
502 ? "null"
503 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400504 + "] label=[" + mHotseatLabels[i]
505 + "]"
506 );
507 }
508 }
509 }
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 @Override
512 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700513 mWaitingForResult = false;
514
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 // The pattern used here is that a user PICKs a specific application,
516 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
519 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700520
Romain Guy94dabf12009-07-21 10:55:43 -0700521 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 switch (requestCode) {
523 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 break;
526 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800527 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 break;
529 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 break;
532 case REQUEST_PICK_LIVE_FOLDER:
533 addLiveFolder(data);
534 break;
535 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700538 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700539 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700541 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700542 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
543 // TODO: Is this log message meaningful?
544 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
545 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700547 case REQUEST_PICK_WALLPAPER:
548 // We just wanted the activity result here so we can clear mWaitingForResult
549 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 }
Romain Guy18042c82009-11-06 11:44:55 -0800551 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
552 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
553 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 // Clean up the appWidgetId if we canceled
555 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
556 if (appWidgetId != -1) {
557 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 }
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561
562 @Override
563 protected void onResume() {
564 super.onResume();
565
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800566 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569 mWorkspaceLoading = true;
570 mModel.startLoader(this, true);
571 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
573 }
574
575 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700576 protected void onPause() {
577 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700578 // Some launcher layouts don't have a previous and next view
579 if (mPreviousView != null) {
580 dismissPreview(mPreviousView);
581 dismissPreview(mNextView);
582 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800583 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700584 }
Romain Guycbb89e42009-06-08 15:52:54 -0700585
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700586 @Override
587 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400588 // Flag the loader to stop early before switching
589 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800590 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800591 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700592 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700593
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800594 // We can't hide the IME if it was forced open. So don't bother
595 /*
596 @Override
597 public void onWindowFocusChanged(boolean hasFocus) {
598 super.onWindowFocusChanged(hasFocus);
599
600 if (hasFocus) {
601 final InputMethodManager inputManager = (InputMethodManager)
602 getSystemService(Context.INPUT_METHOD_SERVICE);
603 WindowManager.LayoutParams lp = getWindow().getAttributes();
604 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
605 android.os.Handler()) {
606 protected void onReceiveResult(int resultCode, Bundle resultData) {
607 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
608 }
609 });
610 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
611 }
612 }
613 */
614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 private boolean acceptFilter() {
616 final InputMethodManager inputManager = (InputMethodManager)
617 getSystemService(Context.INPUT_METHOD_SERVICE);
618 return !inputManager.isFullscreenMode();
619 }
620
621 @Override
622 public boolean onKeyDown(int keyCode, KeyEvent event) {
623 boolean handled = super.onKeyDown(keyCode, event);
624 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
625 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
626 keyCode, event);
627 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700628 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700629 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700630 // showSearchDialog()
631 // If there are multiple keystrokes before the search dialog takes focus,
632 // onSearchRequested() will be called for every keystroke,
633 // but it is idempotent, so it's fine.
634 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 }
636 }
637
Joe Onorato8a9625e2010-01-28 15:55:35 -0800638 // Eat the long press event so the keyboard doesn't come up.
639 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
640 return true;
641 }
642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 return handled;
644 }
645
Karl Rosaen138a0412009-04-23 19:00:21 -0700646 private String getTypedText() {
647 return mDefaultKeySsb.toString();
648 }
649
650 private void clearTypedText() {
651 mDefaultKeySsb.clear();
652 mDefaultKeySsb.clearSpans();
653 Selection.setSelection(mDefaultKeySsb, 0);
654 }
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 /**
657 * Restores the previous state, if it exists.
658 *
659 * @param savedState The previous state.
660 */
661 private void restoreState(Bundle savedState) {
662 if (savedState == null) {
663 return;
664 }
665
Joe Onorato3a8820b2009-11-10 15:06:42 -0800666 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
667 if (allApps) {
668 showAllApps(false);
669 }
670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
672 if (currentScreen > -1) {
673 mWorkspace.setCurrentScreen(currentScreen);
674 }
675
676 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
677 if (addScreen > -1) {
678 mAddItemCellInfo = new CellLayout.CellInfo();
679 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
680 addItemCellInfo.valid = true;
681 addItemCellInfo.screen = addScreen;
682 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
683 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
684 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
685 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
686 addItemCellInfo.findVacantCellsFromOccupied(
687 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
688 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
689 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
690 mRestoring = true;
691 }
692
693 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
694 if (renameFolder) {
695 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 mRestoring = true;
698 }
699 }
700
701 /**
702 * Finds all the views we need and configure them properly.
703 */
704 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400705 DragController dragController = mDragController;
706
Romain Guyb1b69f52009-08-10 15:10:15 -0700707 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400708 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709
Joe Onorato7c312c12009-08-13 21:36:53 -0700710 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700711 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700712 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800713 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700714 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800715 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
718 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500719 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720
Joe Onorato7c312c12009-08-13 21:36:53 -0700721 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
722 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723
Joe Onorato7c312c12009-08-13 21:36:53 -0700724 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700726 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800727 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800728
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 mWidgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
730 if (mWidgetChooser != null) {
731 mWidgetChooser.setDragController(dragController);
732 mWidgetChooser.setLauncher(this);
733 } else {
734 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
735 hotseatLeft.setContentDescription(mHotseatLabels[0]);
736 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
737 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
738 hotseatRight.setContentDescription(mHotseatLabels[1]);
739 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400740
Michael Jurkaaf442092010-06-10 17:01:57 -0700741 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
742 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800743
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 Drawable previous = mPreviousView.getDrawable();
745 Drawable next = mNextView.getDrawable();
746 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747
Michael Jurkaaf442092010-06-10 17:01:57 -0700748 mPreviousView.setHapticFeedbackEnabled(false);
749 mPreviousView.setOnLongClickListener(this);
750 mNextView.setHapticFeedbackEnabled(false);
751 mNextView.setOnLongClickListener(this);
752 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400755 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
758 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400759 deleteZone.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700760 int deleteZoneHandleId = isScreenXLarge() ? R.id.add_button : R.id.all_apps_button_cluster;
761 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762
Joe Onorato00acb122009-08-04 16:04:30 -0400763 dragController.setDragScoller(workspace);
764 dragController.setDragListener(deleteZone);
765 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800766 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700767
Joe Onorato00acb122009-08-04 16:04:30 -0400768 // The order here is bottom to top.
769 dragController.addDropTarget(workspace);
770 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 }
772
Romain Guy8a73c512009-11-09 19:19:59 -0800773 @SuppressWarnings({"UnusedDeclaration"})
774 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800775 if (!isAllAppsVisible()) {
776 mWorkspace.scrollLeft();
777 }
Romain Guy8a73c512009-11-09 19:19:59 -0800778 }
779
780 @SuppressWarnings({"UnusedDeclaration"})
781 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800782 if (!isAllAppsVisible()) {
783 mWorkspace.scrollRight();
784 }
Romain Guy8a73c512009-11-09 19:19:59 -0800785 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400786
787 @SuppressWarnings({"UnusedDeclaration"})
788 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400789 if (isAllAppsVisible()) return;
790
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400791 int index = -1;
792 if (v.getId() == R.id.hotseat_left) {
793 index = 0;
794 } else if (v.getId() == R.id.hotseat_right) {
795 index = 1;
796 }
797
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400798 // reload these every tap; you never know when they might change
799 loadHotseats();
800 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
801 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400802 startActivitySafely(
803 mHotseats[index],
804 "hotseat"
805 );
806 }
807 }
Romain Guy8a73c512009-11-09 19:19:59 -0800808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 /**
810 * Creates a view representing a shortcut.
811 *
812 * @param info The data structure describing the shortcut.
813 *
814 * @return A View inflated from R.layout.application.
815 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800816 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 return createShortcut(R.layout.application,
818 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
819 }
820
821 /**
822 * Creates a view representing a shortcut inflated from the specified resource.
823 *
824 * @param layoutResId The id of the XML layout used to create the shortcut.
825 * @param parent The group the shortcut belongs to.
826 * @param info The data structure describing the shortcut.
827 *
828 * @return A View inflated from layoutResId.
829 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800830 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
832
Joe Onorato0589f0f2010-02-08 13:44:00 -0800833 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
834 new FastBitmapDrawable(info.getIcon(mIconCache)),
835 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 favorite.setText(info.title);
837 favorite.setTag(info);
838 favorite.setOnClickListener(this);
839
840 return favorite;
841 }
842
843 /**
844 * Add an application shortcut to the workspace.
845 *
846 * @param data The intent describing the application.
847 * @param cellInfo The position on screen where to create the shortcut.
848 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 cellInfo.screen = mWorkspace.getCurrentScreen();
851 if (!findSingleSlot(cellInfo)) return;
852
Joe Onorato0589f0f2010-02-08 13:44:00 -0800853 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
854 data, context);
855
Romain Guy73b979d2009-06-09 12:57:21 -0700856 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800857 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800859 info.container = ItemInfo.NO_ID;
860 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
861 } else {
862 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864 }
Romain Guycbb89e42009-06-08 15:52:54 -0700865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 /**
867 * Add a shortcut to the workspace.
868 *
869 * @param data The intent describing the shortcut.
870 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 cellInfo.screen = mWorkspace.getCurrentScreen();
874 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700875
Joe Onorato0589f0f2010-02-08 13:44:00 -0800876 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877
878 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
881 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
883 }
884
Romain Guycbb89e42009-06-08 15:52:54 -0700885
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700887 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 * @param data The intent describing the appWidgetId.
890 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700892 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700893 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700894
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700895 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700897 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 // Try finding open space on Launcher screen
900 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800901 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
902 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
903 return;
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700906 // Build Launcher-specific widget info and save to database
907 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 launcherInfo.spanX = spans[0];
909 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 LauncherModel.addItemToDatabase(this, launcherInfo,
912 LauncherSettings.Favorites.CONTAINER_DESKTOP,
913 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
914
915 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700919 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700920
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700921 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 }
927 }
Romain Guycbb89e42009-06-08 15:52:54 -0700928
Joe Onorato9c1289c2009-08-17 11:03:03 -0400929 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
930 mDesktopItems.remove(launcherInfo);
931 launcherInfo.hostView = null;
932 }
933
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934 public LauncherAppWidgetHost getAppWidgetHost() {
935 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800938 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800939 getWindow().closeAllPanels();
940
941 try {
942 dismissDialog(DIALOG_CREATE_SHORTCUT);
943 // Unlock the workspace if the dialog was showing
944 } catch (Exception e) {
945 // An exception is thrown if the dialog is not visible, which is fine
946 }
947
948 try {
949 dismissDialog(DIALOG_RENAME_FOLDER);
950 // Unlock the workspace if the dialog was showing
951 } catch (Exception e) {
952 // An exception is thrown if the dialog is not visible, which is fine
953 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800954
955 // Whatever we were doing is hereby canceled.
956 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800957 }
958
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 @Override
960 protected void onNewIntent(Intent intent) {
961 super.onNewIntent(intent);
962
963 // Close the menu
964 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800965 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800966 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700967
Joe Onorato14f122b2009-11-19 14:06:36 -0800968 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
969 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
970 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800971 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800972 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800973 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800974 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700975
Joe Onorato3a8820b2009-11-10 15:06:42 -0800976 final View v = getWindow().peekDecorView();
977 if (v != null && v.getWindowToken() != null) {
978 InputMethodManager imm = (InputMethodManager)getSystemService(
979 INPUT_METHOD_SERVICE);
980 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982 }
983 }
984
985 @Override
986 protected void onRestoreInstanceState(Bundle savedInstanceState) {
987 // Do not call super here
988 mSavedInstanceState = savedInstanceState;
989 }
990
991 @Override
992 protected void onSaveInstanceState(Bundle outState) {
993 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
994
995 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
996 if (folders.size() > 0) {
997 final int count = folders.size();
998 long[] ids = new long[count];
999 for (int i = 0; i < count; i++) {
1000 final FolderInfo info = folders.get(i).getInfo();
1001 ids[i] = info.id;
1002 }
1003 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1004 } else {
1005 super.onSaveInstanceState(outState);
1006 }
1007
Joe Onoratofb0ca672009-09-14 17:55:46 -04001008 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001009 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012
1013 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1014 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1015 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1016
1017 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1018 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1019 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1020 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1021 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1022 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1023 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1024 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1025 layout.getOccupiedCells());
1026 }
1027
1028 if (mFolderInfo != null && mWaitingForResult) {
1029 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1030 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1031 }
1032 }
1033
1034 @Override
1035 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001041 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
1044 TextKeyListener.getInstance().release();
1045
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001049
1050 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001051
1052 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001053 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001054
1055 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057
1058 @Override
1059 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001060 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 super.startActivityForResult(intent, requestCode);
1062 }
1063
1064 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001065 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001067
Joe Onorato7bb17492009-09-24 17:51:01 -07001068 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Karl Rosaen138a0412009-04-23 19:00:21 -07001070 if (initialQuery == null) {
1071 // Use any text typed in the launcher as the initial query
1072 initialQuery = getTypedText();
1073 clearTypedText();
1074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 if (appSearchData == null) {
1076 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001077 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Karl Rosaen138a0412009-04-23 19:00:21 -07001080 final SearchManager searchManager =
1081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001082 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001083 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085
1086 @Override
1087 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 if (isWorkspaceLocked()) {
1089 return false;
1090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091
1092 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1095 .setIcon(android.R.drawable.ic_menu_add)
1096 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 .setIcon(android.R.drawable.ic_menu_gallery)
1099 .setAlphabeticShortcut('W');
1100 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1101 .setIcon(android.R.drawable.ic_search_category_default)
1102 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1103 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1104 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1105 .setAlphabeticShortcut('N');
1106
1107 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001108 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1109 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110
1111 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1112 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1113 .setIntent(settings);
1114
1115 return true;
1116 }
1117
1118 @Override
1119 public boolean onPrepareOptionsMenu(Menu menu) {
1120 super.onPrepareOptionsMenu(menu);
1121
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001122 // If all apps is animating, don't show the menu, because we don't know
1123 // which one to show.
1124 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1125 return false;
1126 }
1127
1128 // Only show the add and wallpaper options when we're not in all apps.
1129 boolean visible = !mAllAppsGrid.isOpaque();
1130 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1131 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1132
1133 // Disable add if the workspace is full.
1134 if (visible) {
1135 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1136 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138
1139 return true;
1140 }
1141
1142 @Override
1143 public boolean onOptionsItemSelected(MenuItem item) {
1144 switch (item.getItemId()) {
1145 case MENU_ADD:
1146 addItems();
1147 return true;
1148 case MENU_WALLPAPER_SETTINGS:
1149 startWallpaper();
1150 return true;
1151 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001152 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 return true;
1154 case MENU_NOTIFICATIONS:
1155 showNotifications();
1156 return true;
1157 }
1158
1159 return super.onOptionsItemSelected(item);
1160 }
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Karl Rosaen138a0412009-04-23 19:00:21 -07001162 /**
1163 * Indicates that we want global search for this activity by setting the globalSearch
1164 * argument for {@link #startSearch} to true.
1165 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001167 @Override
1168 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001169 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001171 }
1172
Joe Onorato9c1289c2009-08-17 11:03:03 -04001173 public boolean isWorkspaceLocked() {
1174 return mWorkspaceLoading || mWaitingForResult;
1175 }
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001178 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 showAddDialog(mMenuAddInfo);
1180 }
1181
Michael Jurkaaf442092010-06-10 17:01:57 -07001182 boolean isScreenXLarge() {
1183 int screenLayout = getResources().getConfiguration().screenLayout;
1184 return (screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE;
1185 }
1186
1187 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1188 mAddItemCellInfo = cellInfo;
1189 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1190 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1191 addAppWidgetImpl(appWidgetId);
1192 }
1193
1194 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 // TODO: catch bad widget exception when sent
1196 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001197 // TODO: Is this log message meaningful?
1198 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1199 addAppWidgetImpl(appWidgetId);
1200 }
1201
1202 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001203 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204
Bjorn Bringert7984c942009-12-09 15:38:25 +00001205 if (appWidget.configure != null) {
1206 // Launch over to configure widget, if needed
1207 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1208 intent.setComponent(appWidget.configure);
1209 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1210
Romain Guy8e633c52010-03-04 12:51:36 -08001211 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001213 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001214 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216 }
Romain Guycbb89e42009-06-08 15:52:54 -07001217
Joe Onoratodeb98af2010-02-19 14:59:39 -08001218 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001219 // Handle case where user selected "Applications"
1220 String applicationName = getResources().getString(R.string.group_applications);
1221 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001222
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001223 if (applicationName != null && applicationName.equals(shortcutName)) {
1224 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1225 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001226
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001227 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1228 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001229 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001230 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001231 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234
1235 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001236 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001237 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001238 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001240 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001242 } else {
1243 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1244 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 UserFolderInfo folderInfo = new UserFolderInfo();
1249 folderInfo.title = getText(R.string.folder_name);
1250
1251 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1252 cellInfo.screen = mWorkspace.getCurrentScreen();
1253 if (!findSingleSlot(cellInfo)) return;
1254
1255 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001256 LauncherModel.addItemToDatabase(this, folderInfo,
1257 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001259 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260
1261 // Create the view
1262 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1263 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1264 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
Romain Guycbb89e42009-06-08 15:52:54 -07001267
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 void removeFolder(FolderInfo folder) {
1269 mFolders.remove(folder.id);
1270 }
1271
1272 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 cellInfo.screen = mWorkspace.getCurrentScreen();
1274 if (!findSingleSlot(cellInfo)) return;
1275
1276 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1277
1278 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1280 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001281 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1282 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284 }
1285
1286 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1287 CellLayout.CellInfo cellInfo, boolean notify) {
1288
1289 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1290 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1291
1292 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 Intent.ShortcutIconResource iconResource = null;
1294
1295 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1296 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1297 try {
1298 iconResource = (Intent.ShortcutIconResource) extra;
1299 final PackageManager packageManager = context.getPackageManager();
1300 Resources resources = packageManager.getResourcesForApplication(
1301 iconResource.packageName);
1302 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1303 icon = resources.getDrawable(id);
1304 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001305 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307 }
1308
1309 if (icon == null) {
1310 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1311 }
1312
1313 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001314 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 info.title = name;
1316 info.iconResource = iconResource;
1317 info.uri = data.getData();
1318 info.baseIntent = baseIntent;
1319 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1320 LiveFolders.DISPLAY_MODE_GRID);
1321
1322 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1323 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
1326 return info;
1327 }
1328
1329 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1330 final int[] xy = new int[2];
1331 if (findSlot(cellInfo, xy, 1, 1)) {
1332 cellInfo.cellX = xy[0];
1333 cellInfo.cellY = xy[1];
1334 return true;
1335 }
1336 return false;
1337 }
1338
1339 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1340 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1341 boolean[] occupied = mSavedState != null ?
1342 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1343 cellInfo = mWorkspace.findAllVacantCells(occupied);
1344 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1345 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1346 return false;
1347 }
1348 }
1349 return true;
1350 }
1351
1352 private void showNotifications() {
1353 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1354 if (statusBar != null) {
1355 statusBar.expand();
1356 }
1357 }
1358
1359 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001360 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001362 Intent chooser = Intent.createChooser(pickWallpaper,
1363 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001364 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1365 // Removed in Eclair MR1
1366// WallpaperManager wm = (WallpaperManager)
1367// getSystemService(Context.WALLPAPER_SERVICE);
1368// WallpaperInfo wi = wm.getWallpaperInfo();
1369// if (wi != null && wi.getSettingsActivity() != null) {
1370// LabeledIntent li = new LabeledIntent(getPackageName(),
1371// R.string.configure_wallpaper, 0);
1372// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1373// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1374// }
Mike Clerona0618e42009-10-22 13:55:21 -07001375 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001378 /**
1379 * Registers various content observers. The current implementation registers
1380 * only a favorites observer to keep track of the favorites applications.
1381 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001382 private void registerContentObservers() {
1383 ContentResolver resolver = getContentResolver();
1384 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1385 true, mWidgetObserver);
1386 }
1387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 @Override
1389 public boolean dispatchKeyEvent(KeyEvent event) {
1390 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1391 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001393 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001394 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001395 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001396 dumpState();
1397 return true;
1398 }
1399 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001400 }
1401 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1402 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001403 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 return true;
1405 }
1406 }
1407
1408 return super.dispatchKeyEvent(event);
1409 }
1410
Joe Onorato88ec0992009-11-19 13:16:06 -08001411 @Override
1412 public void onBackPressed() {
1413 if (isAllAppsVisible()) {
1414 closeAllApps(true);
1415 } else {
1416 closeFolder();
1417 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001418 // Some launcher layouts don't have a previous and next view
1419 if (mPreviousView != null) {
1420 dismissPreview(mPreviousView);
1421 dismissPreview(mNextView);
1422 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001423 }
1424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 private void closeFolder() {
1426 Folder folder = mWorkspace.getOpenFolder();
1427 if (folder != null) {
1428 closeFolder(folder);
1429 }
1430 }
1431
1432 void closeFolder(Folder folder) {
1433 folder.getInfo().opened = false;
1434 ViewGroup parent = (ViewGroup) folder.getParent();
1435 if (parent != null) {
1436 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001437 if (folder instanceof DropTarget) {
1438 // Live folders aren't DropTargets.
1439 mDragController.removeDropTarget((DropTarget)folder);
1440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442 folder.onClose();
1443 }
1444
1445 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001446 * Re-listen when widgets are reset.
1447 */
1448 private void onAppWidgetReset() {
1449 mAppWidgetHost.startListening();
1450 }
1451
1452 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1454 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001456 private void unbindDesktopItems() {
1457 for (ItemInfo item: mDesktopItems) {
1458 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 /**
1463 * Launches the intent referred by the clicked shortcut.
1464 *
1465 * @param v The view representing the clicked shortcut.
1466 */
1467 public void onClick(View v) {
1468 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001469 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001471 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001472 int[] pos = new int[2];
1473 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001474 intent.setSourceBounds(new Rect(pos[0], pos[1],
1475 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001476 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 } else if (tag instanceof FolderInfo) {
1478 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001479 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001480 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001481 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001482 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001483 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486 }
1487
Michael Jurkaaf442092010-06-10 17:01:57 -07001488 /**
1489 * Event handler for the "plus" button that appears on the home screen, which
1490 * enters home screen customization mode.
1491 *
1492 * @param v The view that was clicked.
1493 */
1494 public void onClickAddButton(View v) {
1495 View widgetChooser = findViewById(R.id.widget_chooser);
1496 widgetChooser.setVisibility(View.VISIBLE);
1497
1498 // Animate the widget chooser up from the bottom of the screen
1499 widgetChooser.startAnimation(AnimationUtils.loadAnimation(this, R.anim.widget_chooser_slide_up));
1500 }
1501
1502 public void onClickAllAppsButton(View w) {
1503 showAllApps(true);
1504 }
1505
Joe Onoratof984e852010-03-25 09:47:45 -07001506 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1508 try {
1509 startActivity(intent);
1510 } catch (ActivityNotFoundException e) {
1511 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001512 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 } catch (SecurityException e) {
1514 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001515 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001517 "or use the exported attribute for this activity. "
1518 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520 }
Romain Guy8e633c52010-03-04 12:51:36 -08001521
1522 void startActivityForResultSafely(Intent intent, int requestCode) {
1523 try {
1524 startActivityForResult(intent, requestCode);
1525 } catch (ActivityNotFoundException e) {
1526 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1527 } catch (SecurityException e) {
1528 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1529 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1530 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1531 "or use the exported attribute for this activity.", e);
1532 }
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
1535 private void handleFolderClick(FolderInfo folderInfo) {
1536 if (!folderInfo.opened) {
1537 // Close any open folder
1538 closeFolder();
1539 // Open the requested folder
1540 openFolder(folderInfo);
1541 } else {
1542 // Find the open folder...
1543 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1544 int folderScreen;
1545 if (openFolder != null) {
1546 folderScreen = mWorkspace.getScreenForView(openFolder);
1547 // .. and close it
1548 closeFolder(openFolder);
1549 if (folderScreen != mWorkspace.getCurrentScreen()) {
1550 // Close any folder open on the current screen
1551 closeFolder();
1552 // Pull the folder onto this screen
1553 openFolder(folderInfo);
1554 }
1555 }
1556 }
1557 }
1558
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 /**
1560 * Opens the user fodler described by the specified tag. The opening of the folder
1561 * is animated relative to the specified View. If the View is null, no animation
1562 * is played.
1563 *
1564 * @param folderInfo The FolderInfo describing the folder to open.
1565 */
1566 private void openFolder(FolderInfo folderInfo) {
1567 Folder openFolder;
1568
1569 if (folderInfo instanceof UserFolderInfo) {
1570 openFolder = UserFolder.fromXml(this);
1571 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001572 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 } else {
1574 return;
1575 }
1576
Joe Onorato00acb122009-08-04 16:04:30 -04001577 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 openFolder.setLauncher(this);
1579
1580 openFolder.bind(folderInfo);
1581 folderInfo.opened = true;
1582
1583 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1584 openFolder.onOpen();
1585 }
1586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001588 switch (v.getId()) {
1589 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001590 if (!isAllAppsVisible()) {
1591 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1592 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001593 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001594 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001595 return true;
1596 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001597 if (!isAllAppsVisible()) {
1598 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1599 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001600 showPreviews(v);
1601 }
1602 return true;
1603 case R.id.all_apps_button:
1604 if (!isAllAppsVisible()) {
1605 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1606 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1607 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001608 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001609 return true;
1610 }
1611
Joe Onorato9c1289c2009-08-17 11:03:03 -04001612 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 return false;
1614 }
1615
1616 if (!(v instanceof CellLayout)) {
1617 v = (View) v.getParent();
1618 }
1619
1620 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1621
1622 // This happens when long clicking an item with the dpad/trackball
1623 if (cellInfo == null) {
1624 return true;
1625 }
1626
1627 if (mWorkspace.allowLongPress()) {
1628 if (cellInfo.cell == null) {
1629 if (cellInfo.valid) {
1630 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001631 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001632 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1633 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 showAddDialog(cellInfo);
1635 }
1636 } else {
1637 if (!(cellInfo.cell instanceof Folder)) {
1638 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001639 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1640 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 mWorkspace.startDrag(cellInfo);
1642 }
1643 }
1644 }
1645 return true;
1646 }
1647
Romain Guye6b8e2f2009-11-10 11:56:55 -08001648 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001649 private void dismissPreview(final View v) {
1650 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001651 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001652 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1653 public void onDismiss() {
1654 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1655 int count = group.getChildCount();
1656 for (int i = 0; i < count; i++) {
1657 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1658 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001659 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1660 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1661
1662 v.setTag(R.id.workspace, null);
1663 v.setTag(R.id.icon, null);
1664 window.setOnDismissListener(null);
1665 }
1666 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001667 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001668 }
1669 v.setTag(null);
1670 }
1671
Romain Guyf8e6a802009-12-07 17:48:02 -08001672 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001673 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001674 }
1675
Romain Guya6abce82009-11-10 02:54:41 -08001676 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001677 final Resources resources = getResources();
1678 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001679
Romain Guya6abce82009-11-10 02:54:41 -08001680 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001681
1682 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001683
Romain Guyf8e6a802009-12-07 17:48:02 -08001684 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001685 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001686 int extraW = (int) ((r.left + r.right) * max);
1687 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001688
1689 int aW = cell.getWidth() - extraW;
1690 float w = aW / max;
1691
1692 int width = cell.getWidth();
1693 int height = cell.getHeight();
1694 int x = cell.getLeftPadding();
1695 int y = cell.getTopPadding();
1696 width -= (x + cell.getRightPadding());
1697 height -= (y + cell.getBottomPadding());
1698
1699 float scale = w / width;
1700
1701 int count = end - start;
1702
1703 final float sWidth = width * scale;
1704 float sHeight = height * scale;
1705
Romain Guye6b8e2f2009-11-10 11:56:55 -08001706 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001707
Romain Guye6b8e2f2009-11-10 11:56:55 -08001708 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1709 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001710
1711 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001712 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001713 cell = (CellLayout) workspace.getChildAt(i);
1714
Romain Guyf8e6a802009-12-07 17:48:02 -08001715 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001716 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001717
1718 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001719 c.scale(scale, scale);
1720 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1721 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001722
Romain Guy9d31e9f2009-11-11 18:54:28 -08001723 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001724 image.setImageBitmap(bitmap);
1725 image.setTag(i);
1726 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001727 image.setOnFocusChangeListener(handler);
1728 image.setFocusable(true);
1729 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001730
1731 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001732 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1733
1734 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001735 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001736
1737 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001738 p.setContentView(preview);
1739 p.setWidth((int) (sWidth * count + extraW));
1740 p.setHeight((int) (sHeight + extraH));
1741 p.setAnimationStyle(R.style.AnimationPreview);
1742 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001743 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001744 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001745 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001746
Romain Guye6b8e2f2009-11-10 11:56:55 -08001747 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1748 public void onDismiss() {
1749 dismissPreview(anchor);
1750 }
1751 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001752
1753 anchor.setTag(p);
1754 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001755 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001756 }
1757
Romain Guy9d31e9f2009-11-11 18:54:28 -08001758 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001759 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001760
Romain Guye6b8e2f2009-11-10 11:56:55 -08001761 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001762 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001763 }
1764
1765 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001766 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001767 v.post(this);
1768 }
1769
1770 public void run() {
1771 dismissPreview(mAnchor);
1772 }
1773
1774 public void onFocusChange(View v, boolean hasFocus) {
1775 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001776 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001777 }
Romain Guya6abce82009-11-10 02:54:41 -08001778 }
1779 }
1780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 Workspace getWorkspace() {
1782 return mWorkspace;
1783 }
1784
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 @Override
1786 protected Dialog onCreateDialog(int id) {
1787 switch (id) {
1788 case DIALOG_CREATE_SHORTCUT:
1789 return new CreateShortcut().createDialog();
1790 case DIALOG_RENAME_FOLDER:
1791 return new RenameFolder().createDialog();
1792 }
1793
1794 return super.onCreateDialog(id);
1795 }
1796
1797 @Override
1798 protected void onPrepareDialog(int id, Dialog dialog) {
1799 switch (id) {
1800 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 break;
1802 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001803 if (mFolderInfo != null) {
1804 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1805 final CharSequence text = mFolderInfo.title;
1806 input.setText(text);
1807 input.setSelection(0, text.length());
1808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 break;
1810 }
1811 }
1812
1813 void showRenameDialog(FolderInfo info) {
1814 mFolderInfo = info;
1815 mWaitingForResult = true;
1816 showDialog(DIALOG_RENAME_FOLDER);
1817 }
1818
1819 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1820 mAddItemCellInfo = cellInfo;
1821 mWaitingForResult = true;
1822 showDialog(DIALOG_CREATE_SHORTCUT);
1823 }
1824
Joe Onoratodeb98af2010-02-19 14:59:39 -08001825 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001826 Bundle bundle = new Bundle();
1827
1828 ArrayList<String> shortcutNames = new ArrayList<String>();
1829 shortcutNames.add(getString(R.string.group_applications));
1830 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1831
1832 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1833 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1834 R.drawable.ic_launcher_application));
1835 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1836
1837 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1838 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001839 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001840 pickIntent.putExtras(bundle);
1841
Joe Onoratodeb98af2010-02-19 14:59:39 -08001842 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001843 }
1844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 private class RenameFolder {
1846 private EditText mInput;
1847
1848 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1850 mInput = (EditText) layout.findViewById(R.id.folder_name);
1851
1852 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1853 builder.setIcon(0);
1854 builder.setTitle(getString(R.string.rename_folder_title));
1855 builder.setCancelable(true);
1856 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1857 public void onCancel(DialogInterface dialog) {
1858 cleanup();
1859 }
1860 });
1861 builder.setNegativeButton(getString(R.string.cancel_action),
1862 new Dialog.OnClickListener() {
1863 public void onClick(DialogInterface dialog, int which) {
1864 cleanup();
1865 }
1866 }
1867 );
1868 builder.setPositiveButton(getString(R.string.rename_action),
1869 new Dialog.OnClickListener() {
1870 public void onClick(DialogInterface dialog, int which) {
1871 changeFolderName();
1872 }
1873 }
1874 );
1875 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001876
1877 final AlertDialog dialog = builder.create();
1878 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1879 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001880 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001881 mInput.requestFocus();
1882 InputMethodManager inputManager = (InputMethodManager)
1883 getSystemService(Context.INPUT_METHOD_SERVICE);
1884 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001885 }
1886 });
1887
1888 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
1891 private void changeFolderName() {
1892 final String name = mInput.getText().toString();
1893 if (!TextUtils.isEmpty(name)) {
1894 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001895 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 mFolderInfo.title = name;
1897 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1898
Joe Onorato9c1289c2009-08-17 11:03:03 -04001899 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001900 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001901 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 } else {
1903 final FolderIcon folderIcon = (FolderIcon)
1904 mWorkspace.getViewForTag(mFolderInfo);
1905 if (folderIcon != null) {
1906 folderIcon.setText(name);
1907 getWorkspace().requestLayout();
1908 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001909 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910 mWorkspaceLoading = true;
1911 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913 }
1914 }
1915 cleanup();
1916 }
1917
1918 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 dismissDialog(DIALOG_RENAME_FOLDER);
1920 mWaitingForResult = false;
1921 mFolderInfo = null;
1922 }
1923 }
1924
Daniel Sandler843e8602010-06-07 14:59:01 -04001925 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1926 public boolean isAllAppsVisible() {
1927 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001928 }
1929
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001930 // AllAppsView.Watcher
1931 public void zoomed(float zoom) {
1932 if (zoom == 1.0f) {
1933 mWorkspace.setVisibility(View.GONE);
1934 }
1935 }
1936
Joe Onorato3a8820b2009-11-10 15:06:42 -08001937 void showAllApps(boolean animated) {
1938 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001939
Romain Guyc16fea72010-03-12 17:17:56 -08001940 ((View) mAllAppsGrid).setFocusable(true);
1941 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001942
Joe Onorato7c312c12009-08-13 21:36:53 -07001943 // TODO: fade these two too
1944 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001945 }
1946
Joe Onoratob2061212009-11-24 16:13:54 -05001947 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001948 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001949 * - Home from workspace
1950 * - from center screen
1951 * - from other screens
1952 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001953 * - from center screen
1954 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001955 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001956 * - from center screen
1957 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001958 * - Launch app from workspace and quit
1959 * - with back
1960 * - with home
1961 * - Launch app from all apps and quit
1962 * - with back
1963 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001964 * - Go to a screen that's not the default, then all
1965 * apps, and launch and app, and go back
1966 * - with back
1967 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001968 * - On workspace, long press power and go back
1969 * - with back
1970 * - with home
1971 * - On all apps, long press power and go back
1972 * - with back
1973 * - with home
1974 * - On workspace, power off
1975 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001976 * - Launch an app and turn off the screen while in that app
1977 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001978 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001979 * - From all apps
1980 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001981 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1982 * - From all apps
1983 * - From the center workspace
1984 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001985 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001986 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001987 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001988 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001989 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001990 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001991 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001992 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001993 }
1994
Joe Onorato7c312c12009-08-13 21:36:53 -07001995 void lockAllApps() {
1996 // TODO
1997 }
1998
1999 void unlockAllApps() {
2000 // TODO
2001 }
2002
Joe Onorato7404ee42009-07-31 11:54:44 -07002003 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 * Displays the shortcut creation dialog and launches, if necessary, the
2005 * appropriate activity.
2006 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002008 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2009 DialogInterface.OnShowListener {
2010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2017 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002018 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 builder.setInverseBackgroundForced(true);
2021
2022 AlertDialog dialog = builder.create();
2023 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002024 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002025 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 return dialog;
2028 }
2029
2030 public void onCancel(DialogInterface dialog) {
2031 mWaitingForResult = false;
2032 cleanup();
2033 }
2034
Romain Guycbb89e42009-06-08 15:52:54 -07002035 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002036 }
2037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002039 try {
2040 dismissDialog(DIALOG_CREATE_SHORTCUT);
2041 } catch (Exception e) {
2042 // An exception is thrown if the dialog is not visible, which is fine
2043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
2045
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002046 /**
2047 * Handle the action clicked in the "Add to home" dialog.
2048 */
2049 public void onClick(DialogInterface dialog, int which) {
2050 Resources res = getResources();
2051 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002052
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002053 switch (which) {
2054 case AddAdapter.ITEM_SHORTCUT: {
2055 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002056 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002057 break;
2058 }
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002060 case AddAdapter.ITEM_APPWIDGET: {
2061 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002062
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002063 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2064 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002065 // start the pick activity
2066 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2067 break;
2068 }
Romain Guycbb89e42009-06-08 15:52:54 -07002069
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002070 case AddAdapter.ITEM_LIVE_FOLDER: {
2071 // Insert extra item to handle inserting folder
2072 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002073
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002074 ArrayList<String> shortcutNames = new ArrayList<String>();
2075 shortcutNames.add(res.getString(R.string.group_folder));
2076 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002077
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002078 ArrayList<ShortcutIconResource> shortcutIcons =
2079 new ArrayList<ShortcutIconResource>();
2080 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2081 R.drawable.ic_launcher_folder));
2082 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2083
2084 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2085 pickIntent.putExtra(Intent.EXTRA_INTENT,
2086 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2087 pickIntent.putExtra(Intent.EXTRA_TITLE,
2088 getText(R.string.title_select_live_folder));
2089 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002090
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002091 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2092 break;
2093 }
2094
2095 case AddAdapter.ITEM_WALLPAPER: {
2096 startWallpaper();
2097 break;
2098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002101
2102 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002103 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
2106
2107 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002108 * Receives notifications when applications are added/removed.
2109 */
2110 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2111 @Override
2112 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002113 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002114 String reason = intent.getStringExtra("reason");
2115 if (!"homekey".equals(reason)) {
2116 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002117 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002118 animate = false;
2119 }
Joe Onoratob2061212009-11-24 16:13:54 -05002120 closeAllApps(animate);
2121 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002122 }
2123 }
2124
2125 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002126 * Receives notifications whenever the appwidgets are reset.
2127 */
2128 private class AppWidgetResetObserver extends ContentObserver {
2129 public AppWidgetResetObserver() {
2130 super(new Handler());
2131 }
2132
2133 @Override
2134 public void onChange(boolean selfChange) {
2135 onAppWidgetReset();
2136 }
2137 }
2138
2139 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002140 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002143 if (mWorkspace != null) {
2144 return mWorkspace.getCurrentScreen();
2145 } else {
2146 return SCREEN_COUNT / 2;
2147 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002149
Joe Onorato9c1289c2009-08-17 11:03:03 -04002150 /**
2151 * Refreshes the shortcuts shown on the workspace.
2152 *
2153 * Implementation of the method from LauncherModel.Callbacks.
2154 */
2155 public void startBinding() {
2156 final Workspace workspace = mWorkspace;
2157 int count = workspace.getChildCount();
2158 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002159 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2161 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002162
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 if (DEBUG_USER_INTERFACE) {
2164 android.widget.Button finishButton = new android.widget.Button(this);
2165 finishButton.setText("Finish");
2166 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2167
2168 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2169 public void onClick(View v) {
2170 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002172 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 }
2174 }
2175
2176 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 * Bind the items start-end from the list.
2178 *
2179 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2182
2183 final Workspace workspace = mWorkspace;
2184
2185 for (int i=start; i<end; i++) {
2186 final ItemInfo item = shortcuts.get(i);
2187 mDesktopItems.add(item);
2188 switch (item.itemType) {
2189 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2190 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002191 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002192 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2193 false);
2194 break;
2195 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2196 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2197 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2198 (UserFolderInfo) item);
2199 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2200 false);
2201 break;
2202 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2203 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2204 R.layout.live_folder_icon, this,
2205 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2206 (LiveFolderInfo) item);
2207 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2208 false);
2209 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 }
2212
Joe Onorato9c1289c2009-08-17 11:03:03 -04002213 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 /**
2217 * Implementation of the method from LauncherModel.Callbacks.
2218 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002219 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2220 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002223
2224 /**
2225 * Add the views for a widget to the workspace.
2226 *
2227 * Implementation of the method from LauncherModel.Callbacks.
2228 */
2229 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002230 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2231 if (DEBUG_WIDGETS) {
2232 Log.d(TAG, "bindAppWidget: " + item);
2233 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002234 final Workspace workspace = mWorkspace;
2235
2236 final int appWidgetId = item.appWidgetId;
2237 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002238 if (DEBUG_WIDGETS) {
2239 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2240 }
2241
Joe Onorato9c1289c2009-08-17 11:03:03 -04002242 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2243
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2245 item.hostView.setTag(item);
2246
2247 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2248 item.cellY, item.spanX, item.spanY, false);
2249
2250 workspace.requestLayout();
2251
2252 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002253
2254 if (DEBUG_WIDGETS) {
2255 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2256 + (SystemClock.uptimeMillis()-start) + "ms");
2257 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002258 }
2259
2260 /**
2261 * Callback saying that there aren't any more items to bind.
2262 *
2263 * Implementation of the method from LauncherModel.Callbacks.
2264 */
2265 public void finishBindingItems() {
2266 if (mSavedState != null) {
2267 if (!mWorkspace.hasFocus()) {
2268 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2269 }
2270
2271 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2272 if (userFolders != null) {
2273 for (long folderId : userFolders) {
2274 final FolderInfo info = mFolders.get(folderId);
2275 if (info != null) {
2276 openFolder(info);
2277 }
2278 }
2279 final Folder openFolder = mWorkspace.getOpenFolder();
2280 if (openFolder != null) {
2281 openFolder.requestFocus();
2282 }
2283 }
2284
Joe Onorato9c1289c2009-08-17 11:03:03 -04002285 mSavedState = null;
2286 }
2287
2288 if (mSavedInstanceState != null) {
2289 super.onRestoreInstanceState(mSavedInstanceState);
2290 mSavedInstanceState = null;
2291 }
2292
Joe Onorato9c1289c2009-08-17 11:03:03 -04002293 mWorkspaceLoading = false;
2294 }
2295
2296 /**
2297 * Add the icons for all apps.
2298 *
2299 * Implementation of the method from LauncherModel.Callbacks.
2300 */
2301 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002302 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002303 }
2304
2305 /**
2306 * A package was installed.
2307 *
2308 * Implementation of the method from LauncherModel.Callbacks.
2309 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002310 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002312 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 }
2314
2315 /**
2316 * A package was updated.
2317 *
2318 * Implementation of the method from LauncherModel.Callbacks.
2319 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002320 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002321 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002322 mWorkspace.updateShortcuts(apps);
2323 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002324 }
2325
2326 /**
2327 * A package was uninstalled.
2328 *
2329 * Implementation of the method from LauncherModel.Callbacks.
2330 */
Joe Onorato36115782010-06-17 13:28:48 -04002331 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002332 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002333 if (permanent) {
2334 mWorkspace.removeItems(apps);
2335 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002336 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002337 }
Joe Onoratobe386092009-11-17 17:32:16 -08002338
2339 /**
2340 * Prints out out state for debugging.
2341 */
2342 public void dumpState() {
2343 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002344 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002345 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2346 Log.d(TAG, "mRestoring=" + mRestoring);
2347 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2348 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2349 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2350 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002351 mModel.dumpState();
2352 mAllAppsGrid.dumpState();
2353 Log.d(TAG, "END launcher2 dump state");
2354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355}