blob: 28a6712635b8442953565e95117162aeffeb1242 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040036import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040038import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070040import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040041import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080044import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080045import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080047import android.graphics.drawable.ColorDrawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040048import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070049import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040053import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080054import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.text.Selection;
57import android.text.SpannableStringBuilder;
58import android.text.TextUtils;
59import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070060import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080062import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.view.KeyEvent;
64import android.view.LayoutInflater;
65import android.view.Menu;
66import android.view.MenuItem;
67import android.view.View;
68import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.view.View.OnLongClickListener;
70import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.widget.TextView;
73import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080074import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080075import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080076import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070077import android.appwidget.AppWidgetManager;
78import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import java.util.ArrayList;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040081import java.util.List;
Joe Onorato9c1289c2009-08-17 11:03:03 -040082import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070083import java.io.DataOutputStream;
84import java.io.FileNotFoundException;
85import java.io.IOException;
86import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Romain Guyedcce092010-03-04 13:03:17 -080088import com.android.launcher.R;
89
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090/**
91 * Default launcher application.
92 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040093public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050094 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080095 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -070096 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -040099 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 private static final int WALLPAPER_SCREENS_SPAN = 2;
103
104 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800105 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int MENU_ADD = Menu.FIRST + 1;
108 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
109 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
110 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700111 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
113 private static final int REQUEST_CREATE_SHORTCUT = 1;
114 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700115 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int REQUEST_PICK_APPLICATION = 6;
117 private static final int REQUEST_PICK_SHORTCUT = 7;
118 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700120 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
123
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800124 static final int SCREEN_COUNT = 5;
125 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700127 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Joe Onorato7c312c12009-08-13 21:36:53 -0700129 static final int DIALOG_CREATE_SHORTCUT = 1;
130 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Romain Guy98d01652009-06-30 16:21:04 -0700132 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 // Type: int
135 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
136 // Type: boolean
137 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
138 // Type: long
139 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
152 // Type: int
153 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
154 // Type: int[]
155 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
156 // Type: boolean
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158 // Type: long
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800166 private final BroadcastReceiver mCloseSystemDialogsReceiver
167 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800168 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private LayoutInflater mInflater;
171
Joe Onorato00acb122009-08-04 16:04:30 -0400172 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private CellLayout.CellInfo mAddItemCellInfo;
179 private CellLayout.CellInfo mMenuAddInfo;
180 private final int[] mCellCoordinates = new int[2];
181 private FolderInfo mFolderInfo;
182
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700184 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700185 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Bundle mSavedState;
188
189 private SpannableStringBuilder mDefaultKeySsb = null;
190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private boolean mWorkspaceLoading = true;
192
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800193 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private boolean mRestoring;
195 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 private Bundle mSavedInstanceState;
198
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800200 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700202 private static LocaleConfiguration sLocaleConfiguration = null;
203
Romain Guy84f296c2009-11-04 15:00:44 -0800204 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700205 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
Romain Guy1fbc1c82009-11-09 20:43:08 -0800207 private ImageView mPreviousView;
208 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500209
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400210 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400211 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400212 private String[] mHotseatConfig = null;
213 private Intent[] mHotseats = null;
214 private Drawable[] mHotseatIcons = null;
215 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 @Override
218 protected void onCreate(Bundle savedInstanceState) {
219 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700220
Joe Onorato0589f0f2010-02-08 13:44:00 -0800221 LauncherApplication app = ((LauncherApplication)getApplication());
222 mModel = app.setLauncher(this);
223 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400224 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700226
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700227 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
229 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 if (PROFILE_STARTUP) {
232 android.os.Debug.startMethodTracing("/sdcard/launcher");
233 }
234
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400235 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 checkForLocaleChange();
237 setWallpaperDimension();
238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 setContentView(R.layout.launcher);
240 setupViews();
241
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800242 registerContentObservers();
243
Joe Onorato7c312c12009-08-13 21:36:53 -0700244 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 mSavedState = savedInstanceState;
247 restoreState(mSavedState);
248
249 if (PROFILE_STARTUP) {
250 android.os.Debug.stopMethodTracing();
251 }
252
253 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 }
256
257 // For handling default keys
258 mDefaultKeySsb = new SpannableStringBuilder();
259 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800260
261 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
262 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 }
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700266 if (sLocaleConfiguration == null) {
267 new AsyncTask<Void, Void, LocaleConfiguration>() {
268 @Override
269 protected LocaleConfiguration doInBackground(Void... unused) {
270 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
271 readConfiguration(Launcher.this, localeConfiguration);
272 return localeConfiguration;
273 }
274
275 @Override
276 protected void onPostExecute(LocaleConfiguration result) {
277 sLocaleConfiguration = result;
278 checkForLocaleChange(); // recursive, but now with a locale configuration
279 }
280 }.execute();
281 return;
282 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 final Configuration configuration = getResources().getConfiguration();
285
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700286 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 final String locale = configuration.locale.toString();
288
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700289 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 final int mcc = configuration.mcc;
291
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700292 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 final int mnc = configuration.mnc;
294
Romain Guy84f296c2009-11-04 15:00:44 -0800295 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Romain Guy84f296c2009-11-04 15:00:44 -0800297 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700298 sLocaleConfiguration.locale = locale;
299 sLocaleConfiguration.mcc = mcc;
300 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700301
Joe Onorato0589f0f2010-02-08 13:44:00 -0800302 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400303 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304
305 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
306 new Thread("WriteLocaleConfiguration") {
307 public void run() {
308 writeConfiguration(Launcher.this, localeConfiguration);
309 }
310 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700311 }
312 }
313
314 private static class LocaleConfiguration {
315 public String locale;
316 public int mcc = -1;
317 public int mnc = -1;
318 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700319
Romain Guy98d01652009-06-30 16:21:04 -0700320 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
321 DataInputStream in = null;
322 try {
323 in = new DataInputStream(context.openFileInput(PREFERENCES));
324 configuration.locale = in.readUTF();
325 configuration.mcc = in.readInt();
326 configuration.mnc = in.readInt();
327 } catch (FileNotFoundException e) {
328 // Ignore
329 } catch (IOException e) {
330 // Ignore
331 } finally {
332 if (in != null) {
333 try {
334 in.close();
335 } catch (IOException e) {
336 // Ignore
337 }
338 }
339 }
340 }
341
342 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
343 DataOutputStream out = null;
344 try {
345 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
346 out.writeUTF(configuration.locale);
347 out.writeInt(configuration.mcc);
348 out.writeInt(configuration.mnc);
349 out.flush();
350 } catch (FileNotFoundException e) {
351 // Ignore
352 } catch (IOException e) {
353 //noinspection ResultOfMethodCallIgnored
354 context.getFileStreamPath(PREFERENCES).delete();
355 } finally {
356 if (out != null) {
357 try {
358 out.close();
359 } catch (IOException e) {
360 // Ignore
361 }
362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 }
364 }
365
366 static int getScreen() {
367 synchronized (sLock) {
368 return sScreen;
369 }
370 }
371
372 static void setScreen(int screen) {
373 synchronized (sLock) {
374 sScreen = screen;
375 }
376 }
377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700379 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
381 Display display = getWindowManager().getDefaultDisplay();
382 boolean isPortrait = display.getWidth() < display.getHeight();
383
384 final int width = isPortrait ? display.getWidth() : display.getHeight();
385 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700386 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 }
388
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400389 // Note: This doesn't do all the client-id magic that BrowserProvider does
390 // in Browser. (http://b/2425179)
391 private Uri getDefaultBrowserUri() {
392 String url = getString(R.string.default_browser_url);
393 if (url.indexOf("{CID}") != -1) {
394 url = url.replace("{CID}", "android-google");
395 }
396 return Uri.parse(url);
397 }
398
399 // Load the Intent templates from arrays.xml to populate the hotseats. For
400 // each Intent, if it resolves to a single app, use that as the launch
401 // intent & use that app's label as the contentDescription. Otherwise,
402 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400403 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400404 if (mHotseatConfig == null) {
405 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
406 if (mHotseatConfig.length > 0) {
407 mHotseats = new Intent[mHotseatConfig.length];
408 mHotseatLabels = new CharSequence[mHotseatConfig.length];
409 mHotseatIcons = new Drawable[mHotseatConfig.length];
410 } else {
411 mHotseats = null;
412 mHotseatIcons = null;
413 mHotseatLabels = null;
414 }
415
416 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
417 for (int i=0; i<mHotseatConfig.length; i++) {
418 // load icon for this slot; currently unrelated to the actual activity
419 try {
420 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
421 } catch (ArrayIndexOutOfBoundsException ex) {
422 Log.w(TAG, "Missing hotseat_icons array item #" + i);
423 mHotseatIcons[i] = null;
424 }
425 }
426 hotseatIconDrawables.recycle();
427 }
428
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400429 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400430 for (int i=0; i<mHotseatConfig.length; i++) {
431 Intent intent = null;
432 if (mHotseatConfig[i].equals("*BROWSER*")) {
433 // magic value meaning "launch user's default web browser"
434 // replace it with a generic web request so we can see if there is indeed a default
435 String defaultUri = getString(R.string.default_browser_url);
436 intent = new Intent(
437 Intent.ACTION_VIEW,
438 ((defaultUri != null)
439 ? Uri.parse(defaultUri)
440 : getDefaultBrowserUri())
441 ).addCategory(Intent.CATEGORY_BROWSABLE);
442 // note: if the user launches this without a default set, she
443 // will always be taken to the default URL above; this is
444 // unavoidable as we must specify a valid URL in order for the
445 // chooser to appear, and once the user selects something, that
446 // URL is unavoidably sent to the chosen app.
447 } else {
448 try {
449 intent = Intent.parseUri(mHotseatConfig[i], 0);
450 } catch (java.net.URISyntaxException ex) {
451 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
452 // bogus; leave intent=null
453 }
454 }
455
456 if (intent == null) {
457 mHotseats[i] = null;
458 mHotseatLabels[i] = getText(R.string.activity_not_found);
459 continue;
460 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400461
462 if (LOGD) {
463 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400464 + " initial intent=["
465 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400466 + "]");
467 }
468
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400469 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
470 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
471 if (LOGD) {
472 Log.d(TAG, "Best match for intent: " + bestMatch);
473 Log.d(TAG, "All matches: ");
474 for (ResolveInfo ri : allMatches) {
475 Log.d(TAG, " --> " + ri);
476 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400477 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400478 // did this resolve to a single app, or the resolver?
479 if (allMatches.size() == 0 || bestMatch == null) {
480 // can't find any activity to handle this. let's leave the
481 // intent as-is and let Launcher show a toast when it fails
482 // to launch.
483 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400484
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400485 // set accessibility text to "Not installed"
486 mHotseatLabels[i] = getText(R.string.activity_not_found);
487 } else {
488 boolean found = false;
489 for (ResolveInfo ri : allMatches) {
490 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
491 && bestMatch.activityInfo.applicationInfo.packageName
492 .equals(ri.activityInfo.applicationInfo.packageName)) {
493 found = true;
494 break;
495 }
496 }
497
498 if (!found) {
499 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
500 // the bestMatch is probably the ResolveActivity, meaning the
501 // user has not yet selected a default
502 // so: we'll keep the original intent for now
503 mHotseats[i] = intent;
504
505 // set the accessibility text to "Select shortcut"
506 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
507 } else {
508 // we have an app!
509 // now reconstruct the intent to launch it through the front
510 // door
511 ComponentName com = new ComponentName(
512 bestMatch.activityInfo.applicationInfo.packageName,
513 bestMatch.activityInfo.name);
514 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
515
516 // load the app label for accessibility
517 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
518 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400519 }
520
521 if (LOGD) {
522 Log.d(TAG, "loadHotseats: hotseat " + i
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400523 + " final intent=["
524 + ((mHotseats[i] == null)
525 ? "null"
526 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400527 + "] label=[" + mHotseatLabels[i]
528 + "]"
529 );
530 }
531 }
532 }
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 @Override
535 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700536 mWaitingForResult = false;
537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 // The pattern used here is that a user PICKs a specific application,
539 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700540
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
542 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700543
Romain Guy94dabf12009-07-21 10:55:43 -0700544 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 switch (requestCode) {
546 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 break;
549 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800550 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 break;
552 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 break;
555 case REQUEST_PICK_LIVE_FOLDER:
556 addLiveFolder(data);
557 break;
558 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400559 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700561 case REQUEST_PICK_APPWIDGET:
562 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700564 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400565 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700567 case REQUEST_PICK_WALLPAPER:
568 // We just wanted the activity result here so we can clear mWaitingForResult
569 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
Romain Guy18042c82009-11-06 11:44:55 -0800571 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
572 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
573 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700574 // Clean up the appWidgetId if we canceled
575 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
576 if (appWidgetId != -1) {
577 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581
582 @Override
583 protected void onResume() {
584 super.onResume();
585
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800586 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800587
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400589 mWorkspaceLoading = true;
590 mModel.startLoader(this, true);
591 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
593 }
594
595 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700596 protected void onPause() {
597 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800598 dismissPreview(mPreviousView);
599 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800600 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700601 }
Romain Guycbb89e42009-06-08 15:52:54 -0700602
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700603 @Override
604 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605 // Flag the loader to stop early before switching
606 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800607 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800608 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700609 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700610
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800611 // We can't hide the IME if it was forced open. So don't bother
612 /*
613 @Override
614 public void onWindowFocusChanged(boolean hasFocus) {
615 super.onWindowFocusChanged(hasFocus);
616
617 if (hasFocus) {
618 final InputMethodManager inputManager = (InputMethodManager)
619 getSystemService(Context.INPUT_METHOD_SERVICE);
620 WindowManager.LayoutParams lp = getWindow().getAttributes();
621 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
622 android.os.Handler()) {
623 protected void onReceiveResult(int resultCode, Bundle resultData) {
624 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
625 }
626 });
627 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
628 }
629 }
630 */
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 private boolean acceptFilter() {
633 final InputMethodManager inputManager = (InputMethodManager)
634 getSystemService(Context.INPUT_METHOD_SERVICE);
635 return !inputManager.isFullscreenMode();
636 }
637
638 @Override
639 public boolean onKeyDown(int keyCode, KeyEvent event) {
640 boolean handled = super.onKeyDown(keyCode, event);
641 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
642 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
643 keyCode, event);
644 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700645 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700646 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700647 // showSearchDialog()
648 // If there are multiple keystrokes before the search dialog takes focus,
649 // onSearchRequested() will be called for every keystroke,
650 // but it is idempotent, so it's fine.
651 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
653 }
654
Joe Onorato8a9625e2010-01-28 15:55:35 -0800655 // Eat the long press event so the keyboard doesn't come up.
656 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
657 return true;
658 }
659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 return handled;
661 }
662
Karl Rosaen138a0412009-04-23 19:00:21 -0700663 private String getTypedText() {
664 return mDefaultKeySsb.toString();
665 }
666
667 private void clearTypedText() {
668 mDefaultKeySsb.clear();
669 mDefaultKeySsb.clearSpans();
670 Selection.setSelection(mDefaultKeySsb, 0);
671 }
672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 /**
674 * Restores the previous state, if it exists.
675 *
676 * @param savedState The previous state.
677 */
678 private void restoreState(Bundle savedState) {
679 if (savedState == null) {
680 return;
681 }
682
Joe Onorato3a8820b2009-11-10 15:06:42 -0800683 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
684 if (allApps) {
685 showAllApps(false);
686 }
687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
689 if (currentScreen > -1) {
690 mWorkspace.setCurrentScreen(currentScreen);
691 }
692
693 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
694 if (addScreen > -1) {
695 mAddItemCellInfo = new CellLayout.CellInfo();
696 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
697 addItemCellInfo.valid = true;
698 addItemCellInfo.screen = addScreen;
699 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
700 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
701 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
702 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
703 addItemCellInfo.findVacantCellsFromOccupied(
704 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
705 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
706 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
707 mRestoring = true;
708 }
709
710 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
711 if (renameFolder) {
712 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700713 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 mRestoring = true;
715 }
716 }
717
718 /**
719 * Finds all the views we need and configure them properly.
720 */
721 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400722 DragController dragController = mDragController;
723
Romain Guyb1b69f52009-08-10 15:10:15 -0700724 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400725 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
Joe Onorato7c312c12009-08-13 21:36:53 -0700727 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700728 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700729 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800730 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700731 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800732 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
735 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500736 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737
Joe Onorato7c312c12009-08-13 21:36:53 -0700738 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
739 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740
Joe Onorato7c312c12009-08-13 21:36:53 -0700741 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700743 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800744 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800745
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400746 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
747 hotseatLeft.setContentDescription(mHotseatLabels[0]);
748 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
749 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
750 hotseatRight.setContentDescription(mHotseatLabels[1]);
751 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400752
Romain Guy1fbc1c82009-11-09 20:43:08 -0800753 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
754 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
755
756 Drawable previous = mPreviousView.getDrawable();
757 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800758 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759
Joe Onorato0d44e942009-11-16 18:20:51 -0800760 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800761 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800762 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800763 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400766 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768
769 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400770 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400771 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772
Joe Onorato00acb122009-08-04 16:04:30 -0400773 dragController.setDragScoller(workspace);
774 dragController.setDragListener(deleteZone);
775 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800776 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700777
Joe Onorato00acb122009-08-04 16:04:30 -0400778 // The order here is bottom to top.
779 dragController.addDropTarget(workspace);
780 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
782
Romain Guy8a73c512009-11-09 19:19:59 -0800783 @SuppressWarnings({"UnusedDeclaration"})
784 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800785 if (!isAllAppsVisible()) {
786 mWorkspace.scrollLeft();
787 }
Romain Guy8a73c512009-11-09 19:19:59 -0800788 }
789
790 @SuppressWarnings({"UnusedDeclaration"})
791 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800792 if (!isAllAppsVisible()) {
793 mWorkspace.scrollRight();
794 }
Romain Guy8a73c512009-11-09 19:19:59 -0800795 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400796
797 @SuppressWarnings({"UnusedDeclaration"})
798 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400799 if (isAllAppsVisible()) return;
800
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400801 int index = -1;
802 if (v.getId() == R.id.hotseat_left) {
803 index = 0;
804 } else if (v.getId() == R.id.hotseat_right) {
805 index = 1;
806 }
807
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400808 // reload these every tap; you never know when they might change
809 loadHotseats();
810 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
811 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400812 startActivitySafely(
813 mHotseats[index],
814 "hotseat"
815 );
816 }
817 }
Romain Guy8a73c512009-11-09 19:19:59 -0800818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 /**
820 * Creates a view representing a shortcut.
821 *
822 * @param info The data structure describing the shortcut.
823 *
824 * @return A View inflated from R.layout.application.
825 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800826 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 return createShortcut(R.layout.application,
828 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
829 }
830
831 /**
832 * Creates a view representing a shortcut inflated from the specified resource.
833 *
834 * @param layoutResId The id of the XML layout used to create the shortcut.
835 * @param parent The group the shortcut belongs to.
836 * @param info The data structure describing the shortcut.
837 *
838 * @return A View inflated from layoutResId.
839 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800840 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
842
Joe Onorato0589f0f2010-02-08 13:44:00 -0800843 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
844 new FastBitmapDrawable(info.getIcon(mIconCache)),
845 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 favorite.setText(info.title);
847 favorite.setTag(info);
848 favorite.setOnClickListener(this);
849
850 return favorite;
851 }
852
853 /**
854 * Add an application shortcut to the workspace.
855 *
856 * @param data The intent describing the application.
857 * @param cellInfo The position on screen where to create the shortcut.
858 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 cellInfo.screen = mWorkspace.getCurrentScreen();
861 if (!findSingleSlot(cellInfo)) return;
862
Joe Onorato0589f0f2010-02-08 13:44:00 -0800863 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
864 data, context);
865
Romain Guy73b979d2009-06-09 12:57:21 -0700866 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800867 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800869 info.container = ItemInfo.NO_ID;
870 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
871 } else {
872 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
874 }
Romain Guycbb89e42009-06-08 15:52:54 -0700875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 /**
877 * Add a shortcut to the workspace.
878 *
879 * @param data The intent describing the shortcut.
880 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 cellInfo.screen = mWorkspace.getCurrentScreen();
884 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700885
Joe Onorato0589f0f2010-02-08 13:44:00 -0800886 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887
888 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400890 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
891 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893 }
894
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700897 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700899 * @param data The intent describing the appWidgetId.
900 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700904 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700905
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800906 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700907
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700908 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700909
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700910 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700912 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700913
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 // Try finding open space on Launcher screen
915 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800916 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
917 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
918 return;
919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700921 // Build Launcher-specific widget info and save to database
922 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 launcherInfo.spanX = spans[0];
924 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700925
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 LauncherModel.addItemToDatabase(this, launcherInfo,
927 LauncherSettings.Favorites.CONTAINER_DESKTOP,
928 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
929
930 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700935
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700936 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942 }
Romain Guycbb89e42009-06-08 15:52:54 -0700943
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
945 mDesktopItems.remove(launcherInfo);
946 launcherInfo.hostView = null;
947 }
948
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700949 public LauncherAppWidgetHost getAppWidgetHost() {
950 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
Romain Guycbb89e42009-06-08 15:52:54 -0700952
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800953 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800954 getWindow().closeAllPanels();
955
956 try {
957 dismissDialog(DIALOG_CREATE_SHORTCUT);
958 // Unlock the workspace if the dialog was showing
959 } catch (Exception e) {
960 // An exception is thrown if the dialog is not visible, which is fine
961 }
962
963 try {
964 dismissDialog(DIALOG_RENAME_FOLDER);
965 // Unlock the workspace if the dialog was showing
966 } catch (Exception e) {
967 // An exception is thrown if the dialog is not visible, which is fine
968 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800969
970 // Whatever we were doing is hereby canceled.
971 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 @Override
975 protected void onNewIntent(Intent intent) {
976 super.onNewIntent(intent);
977
978 // Close the menu
979 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800980 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800981 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700982
Joe Onorato14f122b2009-11-19 14:06:36 -0800983 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
984 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
985 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800986 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800987 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800988 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800989 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700990
Joe Onorato3a8820b2009-11-10 15:06:42 -0800991 final View v = getWindow().peekDecorView();
992 if (v != null && v.getWindowToken() != null) {
993 InputMethodManager imm = (InputMethodManager)getSystemService(
994 INPUT_METHOD_SERVICE);
995 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997 }
998 }
999
1000 @Override
1001 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1002 // Do not call super here
1003 mSavedInstanceState = savedInstanceState;
1004 }
1005
1006 @Override
1007 protected void onSaveInstanceState(Bundle outState) {
1008 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1009
1010 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1011 if (folders.size() > 0) {
1012 final int count = folders.size();
1013 long[] ids = new long[count];
1014 for (int i = 0; i < count; i++) {
1015 final FolderInfo info = folders.get(i).getInfo();
1016 ids[i] = info.id;
1017 }
1018 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1019 } else {
1020 super.onSaveInstanceState(outState);
1021 }
1022
Joe Onoratofb0ca672009-09-14 17:55:46 -04001023 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001024 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027
1028 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1029 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1030 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1031
1032 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1033 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1034 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1035 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1036 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1037 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1038 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1039 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1040 layout.getOccupiedCells());
1041 }
1042
1043 if (mFolderInfo != null && mWaitingForResult) {
1044 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1045 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1046 }
1047 }
1048
1049 @Override
1050 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001056 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058
1059 TextKeyListener.getInstance().release();
1060
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001064
1065 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001066
1067 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001068 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001069
1070 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 }
1072
1073 @Override
1074 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001075 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 super.startActivityForResult(intent, requestCode);
1077 }
1078
1079 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001080 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001082
Joe Onorato7bb17492009-09-24 17:51:01 -07001083 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001084
Karl Rosaen138a0412009-04-23 19:00:21 -07001085 if (initialQuery == null) {
1086 // Use any text typed in the launcher as the initial query
1087 initialQuery = getTypedText();
1088 clearTypedText();
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 if (appSearchData == null) {
1091 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001092 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Karl Rosaen138a0412009-04-23 19:00:21 -07001095 final SearchManager searchManager =
1096 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001097 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001098 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100
1101 @Override
1102 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 if (isWorkspaceLocked()) {
1104 return false;
1105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106
1107 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1110 .setIcon(android.R.drawable.ic_menu_add)
1111 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001112 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 .setIcon(android.R.drawable.ic_menu_gallery)
1114 .setAlphabeticShortcut('W');
1115 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1116 .setIcon(android.R.drawable.ic_search_category_default)
1117 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1118 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1119 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1120 .setAlphabeticShortcut('N');
1121
1122 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001123 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1124 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125
1126 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1127 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1128 .setIntent(settings);
1129
1130 return true;
1131 }
1132
1133 @Override
1134 public boolean onPrepareOptionsMenu(Menu menu) {
1135 super.onPrepareOptionsMenu(menu);
1136
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001137 // If all apps is animating, don't show the menu, because we don't know
1138 // which one to show.
1139 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1140 return false;
1141 }
1142
1143 // Only show the add and wallpaper options when we're not in all apps.
1144 boolean visible = !mAllAppsGrid.isOpaque();
1145 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1146 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1147
1148 // Disable add if the workspace is full.
1149 if (visible) {
1150 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1151 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153
1154 return true;
1155 }
1156
1157 @Override
1158 public boolean onOptionsItemSelected(MenuItem item) {
1159 switch (item.getItemId()) {
1160 case MENU_ADD:
1161 addItems();
1162 return true;
1163 case MENU_WALLPAPER_SETTINGS:
1164 startWallpaper();
1165 return true;
1166 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001167 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 return true;
1169 case MENU_NOTIFICATIONS:
1170 showNotifications();
1171 return true;
1172 }
1173
1174 return super.onOptionsItemSelected(item);
1175 }
Romain Guycbb89e42009-06-08 15:52:54 -07001176
Karl Rosaen138a0412009-04-23 19:00:21 -07001177 /**
1178 * Indicates that we want global search for this activity by setting the globalSearch
1179 * argument for {@link #startSearch} to true.
1180 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001182 @Override
1183 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001184 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001185 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001186 }
1187
Joe Onorato9c1289c2009-08-17 11:03:03 -04001188 public boolean isWorkspaceLocked() {
1189 return mWorkspaceLoading || mWaitingForResult;
1190 }
1191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001193 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 showAddDialog(mMenuAddInfo);
1195 }
1196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 void addAppWidget(Intent data) {
1198 // TODO: catch bad widget exception when sent
1199 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001200 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
Bjorn Bringert7984c942009-12-09 15:38:25 +00001202 if (appWidget.configure != null) {
1203 // Launch over to configure widget, if needed
1204 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1205 intent.setComponent(appWidget.configure);
1206 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1207
Romain Guy8e633c52010-03-04 12:51:36 -08001208 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001210 // Otherwise just add it
1211 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
Romain Guycbb89e42009-06-08 15:52:54 -07001214
Joe Onoratodeb98af2010-02-19 14:59:39 -08001215 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001216 // Handle case where user selected "Applications"
1217 String applicationName = getResources().getString(R.string.group_applications);
1218 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001219
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001220 if (applicationName != null && applicationName.equals(shortcutName)) {
1221 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1222 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001223
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001224 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1225 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001226 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001227 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001228 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231
1232 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001233 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001234 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001235 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001237 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001239 } else {
1240 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 UserFolderInfo folderInfo = new UserFolderInfo();
1246 folderInfo.title = getText(R.string.folder_name);
1247
1248 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1249 cellInfo.screen = mWorkspace.getCurrentScreen();
1250 if (!findSingleSlot(cellInfo)) return;
1251
1252 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 LauncherModel.addItemToDatabase(this, folderInfo,
1254 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001256 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257
1258 // Create the view
1259 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1260 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1261 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 }
Romain Guycbb89e42009-06-08 15:52:54 -07001264
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001266 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 }
1268
1269 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 cellInfo.screen = mWorkspace.getCurrentScreen();
1271 if (!findSingleSlot(cellInfo)) return;
1272
1273 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1274
1275 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1277 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1279 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281 }
1282
1283 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1284 CellLayout.CellInfo cellInfo, boolean notify) {
1285
1286 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1287 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1288
1289 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 Intent.ShortcutIconResource iconResource = null;
1291
1292 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1293 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1294 try {
1295 iconResource = (Intent.ShortcutIconResource) extra;
1296 final PackageManager packageManager = context.getPackageManager();
1297 Resources resources = packageManager.getResourcesForApplication(
1298 iconResource.packageName);
1299 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1300 icon = resources.getDrawable(id);
1301 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001302 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304 }
1305
1306 if (icon == null) {
1307 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1308 }
1309
1310 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001311 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 info.title = name;
1313 info.iconResource = iconResource;
1314 info.uri = data.getData();
1315 info.baseIntent = baseIntent;
1316 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1317 LiveFolders.DISPLAY_MODE_GRID);
1318
1319 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1320 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001321 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
1323 return info;
1324 }
1325
1326 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1327 final int[] xy = new int[2];
1328 if (findSlot(cellInfo, xy, 1, 1)) {
1329 cellInfo.cellX = xy[0];
1330 cellInfo.cellY = xy[1];
1331 return true;
1332 }
1333 return false;
1334 }
1335
1336 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1337 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1338 boolean[] occupied = mSavedState != null ?
1339 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1340 cellInfo = mWorkspace.findAllVacantCells(occupied);
1341 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1342 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1343 return false;
1344 }
1345 }
1346 return true;
1347 }
1348
1349 private void showNotifications() {
1350 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1351 if (statusBar != null) {
1352 statusBar.expand();
1353 }
1354 }
1355
1356 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001357 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001359 Intent chooser = Intent.createChooser(pickWallpaper,
1360 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001361 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1362 // Removed in Eclair MR1
1363// WallpaperManager wm = (WallpaperManager)
1364// getSystemService(Context.WALLPAPER_SERVICE);
1365// WallpaperInfo wi = wm.getWallpaperInfo();
1366// if (wi != null && wi.getSettingsActivity() != null) {
1367// LabeledIntent li = new LabeledIntent(getPackageName(),
1368// R.string.configure_wallpaper, 0);
1369// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1370// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1371// }
Mike Clerona0618e42009-10-22 13:55:21 -07001372 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
1374
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001375 /**
1376 * Registers various content observers. The current implementation registers
1377 * only a favorites observer to keep track of the favorites applications.
1378 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001379 private void registerContentObservers() {
1380 ContentResolver resolver = getContentResolver();
1381 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1382 true, mWidgetObserver);
1383 }
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 @Override
1386 public boolean dispatchKeyEvent(KeyEvent event) {
1387 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1388 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001390 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001391 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001392 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001393 dumpState();
1394 return true;
1395 }
1396 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001397 }
1398 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1399 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001400 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 return true;
1402 }
1403 }
1404
1405 return super.dispatchKeyEvent(event);
1406 }
1407
Joe Onorato88ec0992009-11-19 13:16:06 -08001408 @Override
1409 public void onBackPressed() {
1410 if (isAllAppsVisible()) {
1411 closeAllApps(true);
1412 } else {
1413 closeFolder();
1414 }
1415 dismissPreview(mPreviousView);
1416 dismissPreview(mNextView);
1417 }
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 private void closeFolder() {
1420 Folder folder = mWorkspace.getOpenFolder();
1421 if (folder != null) {
1422 closeFolder(folder);
1423 }
1424 }
1425
1426 void closeFolder(Folder folder) {
1427 folder.getInfo().opened = false;
1428 ViewGroup parent = (ViewGroup) folder.getParent();
1429 if (parent != null) {
1430 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001431 if (folder instanceof DropTarget) {
1432 // Live folders aren't DropTargets.
1433 mDragController.removeDropTarget((DropTarget)folder);
1434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436 folder.onClose();
1437 }
1438
1439 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001440 * Re-listen when widgets are reset.
1441 */
1442 private void onAppWidgetReset() {
1443 mAppWidgetHost.startListening();
1444 }
1445
1446 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1448 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 private void unbindDesktopItems() {
1451 for (ItemInfo item: mDesktopItems) {
1452 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 /**
1457 * Launches the intent referred by the clicked shortcut.
1458 *
1459 * @param v The view representing the clicked shortcut.
1460 */
1461 public void onClick(View v) {
1462 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001463 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001465 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001466 int[] pos = new int[2];
1467 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001468 intent.setSourceBounds(new Rect(pos[0], pos[1],
1469 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001470 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 } else if (tag instanceof FolderInfo) {
1472 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001473 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001474 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001475 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001476 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001477 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480 }
1481
Joe Onoratof984e852010-03-25 09:47:45 -07001482 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1484 try {
1485 startActivity(intent);
1486 } catch (ActivityNotFoundException e) {
1487 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001488 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 } catch (SecurityException e) {
1490 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001491 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001493 "or use the exported attribute for this activity. "
1494 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496 }
Romain Guy8e633c52010-03-04 12:51:36 -08001497
1498 void startActivityForResultSafely(Intent intent, int requestCode) {
1499 try {
1500 startActivityForResult(intent, requestCode);
1501 } catch (ActivityNotFoundException e) {
1502 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1503 } catch (SecurityException e) {
1504 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1505 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1506 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1507 "or use the exported attribute for this activity.", e);
1508 }
1509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
1511 private void handleFolderClick(FolderInfo folderInfo) {
1512 if (!folderInfo.opened) {
1513 // Close any open folder
1514 closeFolder();
1515 // Open the requested folder
1516 openFolder(folderInfo);
1517 } else {
1518 // Find the open folder...
1519 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1520 int folderScreen;
1521 if (openFolder != null) {
1522 folderScreen = mWorkspace.getScreenForView(openFolder);
1523 // .. and close it
1524 closeFolder(openFolder);
1525 if (folderScreen != mWorkspace.getCurrentScreen()) {
1526 // Close any folder open on the current screen
1527 closeFolder();
1528 // Pull the folder onto this screen
1529 openFolder(folderInfo);
1530 }
1531 }
1532 }
1533 }
1534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 /**
1536 * Opens the user fodler described by the specified tag. The opening of the folder
1537 * is animated relative to the specified View. If the View is null, no animation
1538 * is played.
1539 *
1540 * @param folderInfo The FolderInfo describing the folder to open.
1541 */
1542 private void openFolder(FolderInfo folderInfo) {
1543 Folder openFolder;
1544
1545 if (folderInfo instanceof UserFolderInfo) {
1546 openFolder = UserFolder.fromXml(this);
1547 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001548 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 } else {
1550 return;
1551 }
1552
Joe Onorato00acb122009-08-04 16:04:30 -04001553 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 openFolder.setLauncher(this);
1555
1556 openFolder.bind(folderInfo);
1557 folderInfo.opened = true;
1558
1559 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1560 openFolder.onOpen();
1561 }
1562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001564 switch (v.getId()) {
1565 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001566 if (!isAllAppsVisible()) {
1567 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1568 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001569 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001570 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001571 return true;
1572 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001573 if (!isAllAppsVisible()) {
1574 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1575 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001576 showPreviews(v);
1577 }
1578 return true;
1579 case R.id.all_apps_button:
1580 if (!isAllAppsVisible()) {
1581 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1582 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1583 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001584 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001585 return true;
1586 }
1587
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 return false;
1590 }
1591
1592 if (!(v instanceof CellLayout)) {
1593 v = (View) v.getParent();
1594 }
1595
1596 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1597
1598 // This happens when long clicking an item with the dpad/trackball
1599 if (cellInfo == null) {
1600 return true;
1601 }
1602
1603 if (mWorkspace.allowLongPress()) {
1604 if (cellInfo.cell == null) {
1605 if (cellInfo.valid) {
1606 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001607 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001608 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1609 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 showAddDialog(cellInfo);
1611 }
1612 } else {
1613 if (!(cellInfo.cell instanceof Folder)) {
1614 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001615 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1616 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 mWorkspace.startDrag(cellInfo);
1618 }
1619 }
1620 }
1621 return true;
1622 }
1623
Romain Guye6b8e2f2009-11-10 11:56:55 -08001624 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001625 private void dismissPreview(final View v) {
1626 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001627 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001628 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1629 public void onDismiss() {
1630 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1631 int count = group.getChildCount();
1632 for (int i = 0; i < count; i++) {
1633 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1634 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001635 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1636 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1637
1638 v.setTag(R.id.workspace, null);
1639 v.setTag(R.id.icon, null);
1640 window.setOnDismissListener(null);
1641 }
1642 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001643 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001644 }
1645 v.setTag(null);
1646 }
1647
Romain Guyf8e6a802009-12-07 17:48:02 -08001648 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001649 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001650 }
1651
Romain Guya6abce82009-11-10 02:54:41 -08001652 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001653 final Resources resources = getResources();
1654 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001655
Romain Guya6abce82009-11-10 02:54:41 -08001656 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001657
1658 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001659
Romain Guyf8e6a802009-12-07 17:48:02 -08001660 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001661 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001662 int extraW = (int) ((r.left + r.right) * max);
1663 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001664
1665 int aW = cell.getWidth() - extraW;
1666 float w = aW / max;
1667
1668 int width = cell.getWidth();
1669 int height = cell.getHeight();
1670 int x = cell.getLeftPadding();
1671 int y = cell.getTopPadding();
1672 width -= (x + cell.getRightPadding());
1673 height -= (y + cell.getBottomPadding());
1674
1675 float scale = w / width;
1676
1677 int count = end - start;
1678
1679 final float sWidth = width * scale;
1680 float sHeight = height * scale;
1681
Romain Guye6b8e2f2009-11-10 11:56:55 -08001682 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001683
Romain Guye6b8e2f2009-11-10 11:56:55 -08001684 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1685 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001686
1687 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001688 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001689 cell = (CellLayout) workspace.getChildAt(i);
1690
Romain Guyf8e6a802009-12-07 17:48:02 -08001691 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001692 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001693
1694 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001695 c.scale(scale, scale);
1696 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1697 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001698
Romain Guy9d31e9f2009-11-11 18:54:28 -08001699 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001700 image.setImageBitmap(bitmap);
1701 image.setTag(i);
1702 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001703 image.setOnFocusChangeListener(handler);
1704 image.setFocusable(true);
1705 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001706
1707 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001708 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1709
1710 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001711 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001712
1713 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001714 p.setContentView(preview);
1715 p.setWidth((int) (sWidth * count + extraW));
1716 p.setHeight((int) (sHeight + extraH));
1717 p.setAnimationStyle(R.style.AnimationPreview);
1718 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001719 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001720 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001721 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001722
Romain Guye6b8e2f2009-11-10 11:56:55 -08001723 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1724 public void onDismiss() {
1725 dismissPreview(anchor);
1726 }
1727 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001728
1729 anchor.setTag(p);
1730 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001731 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001732 }
1733
Romain Guy9d31e9f2009-11-11 18:54:28 -08001734 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001735 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001736
Romain Guye6b8e2f2009-11-10 11:56:55 -08001737 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001738 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001739 }
1740
1741 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001742 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001743 v.post(this);
1744 }
1745
1746 public void run() {
1747 dismissPreview(mAnchor);
1748 }
1749
1750 public void onFocusChange(View v, boolean hasFocus) {
1751 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001752 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001753 }
Romain Guya6abce82009-11-10 02:54:41 -08001754 }
1755 }
1756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 Workspace getWorkspace() {
1758 return mWorkspace;
1759 }
1760
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 @Override
1762 protected Dialog onCreateDialog(int id) {
1763 switch (id) {
1764 case DIALOG_CREATE_SHORTCUT:
1765 return new CreateShortcut().createDialog();
1766 case DIALOG_RENAME_FOLDER:
1767 return new RenameFolder().createDialog();
1768 }
1769
1770 return super.onCreateDialog(id);
1771 }
1772
1773 @Override
1774 protected void onPrepareDialog(int id, Dialog dialog) {
1775 switch (id) {
1776 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 break;
1778 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001779 if (mFolderInfo != null) {
1780 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1781 final CharSequence text = mFolderInfo.title;
1782 input.setText(text);
1783 input.setSelection(0, text.length());
1784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 break;
1786 }
1787 }
1788
1789 void showRenameDialog(FolderInfo info) {
1790 mFolderInfo = info;
1791 mWaitingForResult = true;
1792 showDialog(DIALOG_RENAME_FOLDER);
1793 }
1794
1795 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1796 mAddItemCellInfo = cellInfo;
1797 mWaitingForResult = true;
1798 showDialog(DIALOG_CREATE_SHORTCUT);
1799 }
1800
Joe Onoratodeb98af2010-02-19 14:59:39 -08001801 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001802 Bundle bundle = new Bundle();
1803
1804 ArrayList<String> shortcutNames = new ArrayList<String>();
1805 shortcutNames.add(getString(R.string.group_applications));
1806 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1807
1808 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1809 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1810 R.drawable.ic_launcher_application));
1811 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1812
1813 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1814 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001815 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001816 pickIntent.putExtras(bundle);
1817
Joe Onoratodeb98af2010-02-19 14:59:39 -08001818 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 private class RenameFolder {
1822 private EditText mInput;
1823
1824 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1826 mInput = (EditText) layout.findViewById(R.id.folder_name);
1827
1828 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1829 builder.setIcon(0);
1830 builder.setTitle(getString(R.string.rename_folder_title));
1831 builder.setCancelable(true);
1832 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1833 public void onCancel(DialogInterface dialog) {
1834 cleanup();
1835 }
1836 });
1837 builder.setNegativeButton(getString(R.string.cancel_action),
1838 new Dialog.OnClickListener() {
1839 public void onClick(DialogInterface dialog, int which) {
1840 cleanup();
1841 }
1842 }
1843 );
1844 builder.setPositiveButton(getString(R.string.rename_action),
1845 new Dialog.OnClickListener() {
1846 public void onClick(DialogInterface dialog, int which) {
1847 changeFolderName();
1848 }
1849 }
1850 );
1851 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001852
1853 final AlertDialog dialog = builder.create();
1854 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1855 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001856 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001857 mInput.requestFocus();
1858 InputMethodManager inputManager = (InputMethodManager)
1859 getSystemService(Context.INPUT_METHOD_SERVICE);
1860 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001861 }
1862 });
1863
1864 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866
1867 private void changeFolderName() {
1868 final String name = mInput.getText().toString();
1869 if (!TextUtils.isEmpty(name)) {
1870 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001871 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 mFolderInfo.title = name;
1873 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1874
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001876 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 } else {
1879 final FolderIcon folderIcon = (FolderIcon)
1880 mWorkspace.getViewForTag(mFolderInfo);
1881 if (folderIcon != null) {
1882 folderIcon.setText(name);
1883 getWorkspace().requestLayout();
1884 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001885 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001886 mWorkspaceLoading = true;
1887 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 }
1889 }
1890 }
1891 cleanup();
1892 }
1893
1894 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 dismissDialog(DIALOG_RENAME_FOLDER);
1896 mWaitingForResult = false;
1897 mFolderInfo = null;
1898 }
1899 }
1900
Daniel Sandler843e8602010-06-07 14:59:01 -04001901 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1902 public boolean isAllAppsVisible() {
1903 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001904 }
1905
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001906 // AllAppsView.Watcher
1907 public void zoomed(float zoom) {
1908 if (zoom == 1.0f) {
1909 mWorkspace.setVisibility(View.GONE);
1910 }
1911 }
1912
Joe Onorato3a8820b2009-11-10 15:06:42 -08001913 void showAllApps(boolean animated) {
1914 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001915
Romain Guyc16fea72010-03-12 17:17:56 -08001916 ((View) mAllAppsGrid).setFocusable(true);
1917 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001918
Joe Onorato7c312c12009-08-13 21:36:53 -07001919 // TODO: fade these two too
1920 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001921 }
1922
Joe Onoratob2061212009-11-24 16:13:54 -05001923 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001924 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001925 * - Home from workspace
1926 * - from center screen
1927 * - from other screens
1928 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001929 * - from center screen
1930 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001931 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001932 * - from center screen
1933 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001934 * - Launch app from workspace and quit
1935 * - with back
1936 * - with home
1937 * - Launch app from all apps and quit
1938 * - with back
1939 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001940 * - Go to a screen that's not the default, then all
1941 * apps, and launch and app, and go back
1942 * - with back
1943 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001944 * - On workspace, long press power and go back
1945 * - with back
1946 * - with home
1947 * - On all apps, long press power and go back
1948 * - with back
1949 * - with home
1950 * - On workspace, power off
1951 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001952 * - Launch an app and turn off the screen while in that app
1953 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001954 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001955 * - From all apps
1956 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001957 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1958 * - From all apps
1959 * - From the center workspace
1960 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001961 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001962 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001963 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001964 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001965 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001966 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001967 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001968 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001969 }
1970
Joe Onorato7c312c12009-08-13 21:36:53 -07001971 void lockAllApps() {
1972 // TODO
1973 }
1974
1975 void unlockAllApps() {
1976 // TODO
1977 }
1978
Joe Onorato7404ee42009-07-31 11:54:44 -07001979 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 * Displays the shortcut creation dialog and launches, if necessary, the
1981 * appropriate activity.
1982 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001983 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001984 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1985 DialogInterface.OnShowListener {
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001988
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1993 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001994 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 builder.setInverseBackgroundForced(true);
1997
1998 AlertDialog dialog = builder.create();
1999 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002000 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002001 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 return dialog;
2004 }
2005
2006 public void onCancel(DialogInterface dialog) {
2007 mWaitingForResult = false;
2008 cleanup();
2009 }
2010
Romain Guycbb89e42009-06-08 15:52:54 -07002011 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002012 }
2013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002015 try {
2016 dismissDialog(DIALOG_CREATE_SHORTCUT);
2017 } catch (Exception e) {
2018 // An exception is thrown if the dialog is not visible, which is fine
2019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
2021
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002022 /**
2023 * Handle the action clicked in the "Add to home" dialog.
2024 */
2025 public void onClick(DialogInterface dialog, int which) {
2026 Resources res = getResources();
2027 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002028
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002029 switch (which) {
2030 case AddAdapter.ITEM_SHORTCUT: {
2031 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002032 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002033 break;
2034 }
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002036 case AddAdapter.ITEM_APPWIDGET: {
2037 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002039 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2040 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002041 // start the pick activity
2042 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2043 break;
2044 }
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002046 case AddAdapter.ITEM_LIVE_FOLDER: {
2047 // Insert extra item to handle inserting folder
2048 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002049
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002050 ArrayList<String> shortcutNames = new ArrayList<String>();
2051 shortcutNames.add(res.getString(R.string.group_folder));
2052 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002053
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002054 ArrayList<ShortcutIconResource> shortcutIcons =
2055 new ArrayList<ShortcutIconResource>();
2056 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2057 R.drawable.ic_launcher_folder));
2058 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2059
2060 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2061 pickIntent.putExtra(Intent.EXTRA_INTENT,
2062 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2063 pickIntent.putExtra(Intent.EXTRA_TITLE,
2064 getText(R.string.title_select_live_folder));
2065 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002066
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002067 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2068 break;
2069 }
2070
2071 case AddAdapter.ITEM_WALLPAPER: {
2072 startWallpaper();
2073 break;
2074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002077
2078 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08002079 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
2082
2083 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002084 * Receives notifications when applications are added/removed.
2085 */
2086 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2087 @Override
2088 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002089 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002090 String reason = intent.getStringExtra("reason");
2091 if (!"homekey".equals(reason)) {
2092 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002093 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002094 animate = false;
2095 }
Joe Onoratob2061212009-11-24 16:13:54 -05002096 closeAllApps(animate);
2097 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002098 }
2099 }
2100
2101 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002102 * Receives notifications whenever the appwidgets are reset.
2103 */
2104 private class AppWidgetResetObserver extends ContentObserver {
2105 public AppWidgetResetObserver() {
2106 super(new Handler());
2107 }
2108
2109 @Override
2110 public void onChange(boolean selfChange) {
2111 onAppWidgetReset();
2112 }
2113 }
2114
2115 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002116 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002118 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002119 if (mWorkspace != null) {
2120 return mWorkspace.getCurrentScreen();
2121 } else {
2122 return SCREEN_COUNT / 2;
2123 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002124 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002125
Joe Onorato9c1289c2009-08-17 11:03:03 -04002126 /**
2127 * Refreshes the shortcuts shown on the workspace.
2128 *
2129 * Implementation of the method from LauncherModel.Callbacks.
2130 */
2131 public void startBinding() {
2132 final Workspace workspace = mWorkspace;
2133 int count = workspace.getChildCount();
2134 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002135 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2137 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002138
Joe Onorato9c1289c2009-08-17 11:03:03 -04002139 if (DEBUG_USER_INTERFACE) {
2140 android.widget.Button finishButton = new android.widget.Button(this);
2141 finishButton.setText("Finish");
2142 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2143
2144 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2145 public void onClick(View v) {
2146 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002147 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002148 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 }
2150 }
2151
2152 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002153 * Bind the items start-end from the list.
2154 *
2155 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002157 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2158
2159 final Workspace workspace = mWorkspace;
2160
2161 for (int i=start; i<end; i++) {
2162 final ItemInfo item = shortcuts.get(i);
2163 mDesktopItems.add(item);
2164 switch (item.itemType) {
2165 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2166 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002167 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2169 false);
2170 break;
2171 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2172 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2173 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2174 (UserFolderInfo) item);
2175 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2176 false);
2177 break;
2178 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2179 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2180 R.layout.live_folder_icon, this,
2181 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2182 (LiveFolderInfo) item);
2183 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2184 false);
2185 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
2188
Joe Onorato9c1289c2009-08-17 11:03:03 -04002189 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 }
2191
Joe Onorato9c1289c2009-08-17 11:03:03 -04002192 /**
2193 * Implementation of the method from LauncherModel.Callbacks.
2194 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002195 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002196 sFolders.clear();
2197 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199
2200 /**
2201 * Add the views for a widget to the workspace.
2202 *
2203 * Implementation of the method from LauncherModel.Callbacks.
2204 */
2205 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002206 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2207 if (DEBUG_WIDGETS) {
2208 Log.d(TAG, "bindAppWidget: " + item);
2209 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 final Workspace workspace = mWorkspace;
2211
2212 final int appWidgetId = item.appWidgetId;
2213 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002214 if (DEBUG_WIDGETS) {
2215 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2216 }
2217
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2219
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2221 item.hostView.setTag(item);
2222
2223 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2224 item.cellY, item.spanX, item.spanY, false);
2225
2226 workspace.requestLayout();
2227
2228 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002229
2230 if (DEBUG_WIDGETS) {
2231 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2232 + (SystemClock.uptimeMillis()-start) + "ms");
2233 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002234 }
2235
2236 /**
2237 * Callback saying that there aren't any more items to bind.
2238 *
2239 * Implementation of the method from LauncherModel.Callbacks.
2240 */
2241 public void finishBindingItems() {
2242 if (mSavedState != null) {
2243 if (!mWorkspace.hasFocus()) {
2244 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2245 }
2246
2247 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2248 if (userFolders != null) {
2249 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002250 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002251 if (info != null) {
2252 openFolder(info);
2253 }
2254 }
2255 final Folder openFolder = mWorkspace.getOpenFolder();
2256 if (openFolder != null) {
2257 openFolder.requestFocus();
2258 }
2259 }
2260
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 mSavedState = null;
2262 }
2263
2264 if (mSavedInstanceState != null) {
2265 super.onRestoreInstanceState(mSavedInstanceState);
2266 mSavedInstanceState = null;
2267 }
2268
Joe Onorato9c1289c2009-08-17 11:03:03 -04002269 mWorkspaceLoading = false;
2270 }
2271
2272 /**
2273 * Add the icons for all apps.
2274 *
2275 * Implementation of the method from LauncherModel.Callbacks.
2276 */
2277 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002278 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 }
2280
2281 /**
2282 * A package was installed.
2283 *
2284 * Implementation of the method from LauncherModel.Callbacks.
2285 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002286 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002287 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002288 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002289 }
2290
2291 /**
2292 * A package was updated.
2293 *
2294 * Implementation of the method from LauncherModel.Callbacks.
2295 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002296 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002298 mWorkspace.updateShortcuts(apps);
2299 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002300 }
2301
2302 /**
2303 * A package was uninstalled.
2304 *
2305 * Implementation of the method from LauncherModel.Callbacks.
2306 */
Joe Onorato36115782010-06-17 13:28:48 -04002307 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002309 if (permanent) {
2310 mWorkspace.removeItems(apps);
2311 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002312 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002313 }
Joe Onoratobe386092009-11-17 17:32:16 -08002314
2315 /**
2316 * Prints out out state for debugging.
2317 */
2318 public void dumpState() {
2319 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002320 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002321 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2322 Log.d(TAG, "mRestoring=" + mRestoring);
2323 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2324 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2325 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002326 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002327 mModel.dumpState();
2328 mAllAppsGrid.dumpState();
2329 Log.d(TAG, "END launcher2 dump state");
2330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331}