blob: 0e1ae7d7b3131cc6f0db397d77124bf60a8f8f57 [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
19import android.app.Activity;
20import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Dialog;
Mike LeBeau736cf282009-07-02 17:46:59 -070022import android.app.ISearchManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.SearchManager;
24import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070025import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080027import android.content.BroadcastReceiver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080029import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070033import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.IntentFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.ActivityInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080045import android.graphics.drawable.ColorDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080047import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.os.Parcelable;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Joe Onoratobe386092009-11-17 17:32:16 -080051import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.text.Selection;
54import android.text.SpannableStringBuilder;
55import android.text.TextUtils;
56import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070057import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080059import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.KeyEvent;
61import android.view.LayoutInflater;
62import android.view.Menu;
63import android.view.MenuItem;
64import android.view.View;
65import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.view.View.OnLongClickListener;
67import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.widget.TextView;
70import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080071import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080072import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080073import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070074import android.appwidget.AppWidgetManager;
75import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040078import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070079import java.io.DataOutputStream;
80import java.io.FileNotFoundException;
81import java.io.IOException;
82import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083
84/**
85 * Default launcher application.
86 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040087public final class Launcher extends Activity
Romain Guya6abce82009-11-10 02:54:41 -080088 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080089 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 static final boolean LOGD = false;
91
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 static final boolean PROFILE_STARTUP = false;
93 static final boolean PROFILE_ROTATE = false;
94 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070095
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096 private static final int WALLPAPER_SCREENS_SPAN = 2;
97
98 private static final int MENU_GROUP_ADD = 1;
99 private static final int MENU_ADD = Menu.FIRST + 1;
100 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
101 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
102 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700103 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
105 private static final int REQUEST_CREATE_SHORTCUT = 1;
106 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700107 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108 private static final int REQUEST_PICK_APPLICATION = 6;
109 private static final int REQUEST_PICK_SHORTCUT = 7;
110 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700111 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700112 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
115
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700116 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
117 static final String SEARCH_WIDGET = "search_widget";
118
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800119 static final int SCREEN_COUNT = 5;
120 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700122 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato7c312c12009-08-13 21:36:53 -0700124 static final int DIALOG_CREATE_SHORTCUT = 1;
125 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Romain Guy98d01652009-06-30 16:21:04 -0700127 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 // Type: int
130 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
131 // Type: boolean
132 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
133 // Type: long
134 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
149 // Type: int[]
150 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
151 // Type: boolean
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800161 private final BroadcastReceiver mCloseSystemDialogsReceiver
162 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800163 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private LayoutInflater mInflater;
166
Joe Onorato00acb122009-08-04 16:04:30 -0400167 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private AppWidgetManager mAppWidgetManager;
171 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private CellLayout.CellInfo mAddItemCellInfo;
174 private CellLayout.CellInfo mMenuAddInfo;
175 private final int[] mCellCoordinates = new int[2];
176 private FolderInfo mFolderInfo;
177
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700180 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
Mike LeBeau736cf282009-07-02 17:46:59 -0700186 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato9c1289c2009-08-17 11:03:03 -0400188 private boolean mWorkspaceLoading = true;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private boolean mRestoring;
191 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
193 private Bundle mSavedInstanceState;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private LauncherModel mModel;
196
Romain Guy84f296c2009-11-04 15:00:44 -0800197 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
198 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Romain Guy1fbc1c82009-11-09 20:43:08 -0800200 private ImageView mPreviousView;
201 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 @Override
204 protected void onCreate(Bundle savedInstanceState) {
205 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400208 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800211 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
212 registerReceiver(mCloseSystemDialogsReceiver, filter);
213
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
216 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 if (PROFILE_STARTUP) {
219 android.os.Debug.startMethodTracing("/sdcard/launcher");
220 }
221
222 checkForLocaleChange();
223 setWallpaperDimension();
224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 setContentView(R.layout.launcher);
226 setupViews();
227
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228 registerContentObservers();
229
Joe Onorato7c312c12009-08-13 21:36:53 -0700230 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 mSavedState = savedInstanceState;
233 restoreState(mSavedState);
234
235 if (PROFILE_STARTUP) {
236 android.os.Debug.stopMethodTracing();
237 }
238
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700239 // We have a new AllAppsView, we need to re-bind everything, and it could have
240 // changed in our absence.
241 mModel.setAllAppsDirty();
242 mModel.setWorkspaceDirty();
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 }
247
248 // For handling default keys
249 mDefaultKeySsb = new SpannableStringBuilder();
250 Selection.setSelection(mDefaultKeySsb, 0);
251 }
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700254 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
255 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 final Configuration configuration = getResources().getConfiguration();
258
Romain Guy98d01652009-06-30 16:21:04 -0700259 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 final String locale = configuration.locale.toString();
261
Romain Guy98d01652009-06-30 16:21:04 -0700262 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 final int mcc = configuration.mcc;
264
Romain Guy98d01652009-06-30 16:21:04 -0700265 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 final int mnc = configuration.mnc;
267
Romain Guy84f296c2009-11-04 15:00:44 -0800268 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
Romain Guy84f296c2009-11-04 15:00:44 -0800270 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700271 localeConfiguration.locale = locale;
272 localeConfiguration.mcc = mcc;
273 localeConfiguration.mnc = mnc;
274
275 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700277 }
278 }
279
280 private static class LocaleConfiguration {
281 public String locale;
282 public int mcc = -1;
283 public int mnc = -1;
284 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700285
Romain Guy98d01652009-06-30 16:21:04 -0700286 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
287 DataInputStream in = null;
288 try {
289 in = new DataInputStream(context.openFileInput(PREFERENCES));
290 configuration.locale = in.readUTF();
291 configuration.mcc = in.readInt();
292 configuration.mnc = in.readInt();
293 } catch (FileNotFoundException e) {
294 // Ignore
295 } catch (IOException e) {
296 // Ignore
297 } finally {
298 if (in != null) {
299 try {
300 in.close();
301 } catch (IOException e) {
302 // Ignore
303 }
304 }
305 }
306 }
307
308 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
309 DataOutputStream out = null;
310 try {
311 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
312 out.writeUTF(configuration.locale);
313 out.writeInt(configuration.mcc);
314 out.writeInt(configuration.mnc);
315 out.flush();
316 } catch (FileNotFoundException e) {
317 // Ignore
318 } catch (IOException e) {
319 //noinspection ResultOfMethodCallIgnored
320 context.getFileStreamPath(PREFERENCES).delete();
321 } finally {
322 if (out != null) {
323 try {
324 out.close();
325 } catch (IOException e) {
326 // Ignore
327 }
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
330 }
331
332 static int getScreen() {
333 synchronized (sLock) {
334 return sScreen;
335 }
336 }
337
338 static void setScreen(int screen) {
339 synchronized (sLock) {
340 sScreen = screen;
341 }
342 }
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700345 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346
347 Display display = getWindowManager().getDefaultDisplay();
348 boolean isPortrait = display.getWidth() < display.getHeight();
349
350 final int width = isPortrait ? display.getWidth() : display.getHeight();
351 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700352 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 }
354
355 @Override
356 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700357 mWaitingForResult = false;
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 // The pattern used here is that a user PICKs a specific application,
360 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
363 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700364
Romain Guy94dabf12009-07-21 10:55:43 -0700365 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 switch (requestCode) {
367 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 break;
370 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700371 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 break;
373 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 break;
376 case REQUEST_PICK_LIVE_FOLDER:
377 addLiveFolder(data);
378 break;
379 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 case REQUEST_PICK_APPWIDGET:
383 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400386 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700388 case REQUEST_PICK_WALLPAPER:
389 // We just wanted the activity result here so we can clear mWaitingForResult
390 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
Romain Guy18042c82009-11-06 11:44:55 -0800392 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
393 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
394 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700395 // Clean up the appWidgetId if we canceled
396 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
397 if (appWidgetId != -1) {
398 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
403 @Override
404 protected void onResume() {
405 super.onResume();
406
407 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400408 mWorkspaceLoading = true;
409 mModel.startLoader(this, true);
410 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700412
Mike LeBeau736cf282009-07-02 17:46:59 -0700413 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
414 // onNewIntent), then close the search dialog if needed, because it probably
415 // came from the user pressing 'home' (rather than, for example, pressing 'back').
416 if (mIsNewIntent) {
417 // Post to a handler so that this happens after the search dialog tries to open
418 // itself again.
419 mWorkspace.post(new Runnable() {
420 public void run() {
Bjorn Bringert98047dc2009-11-21 22:08:04 +0000421 stopSearch();
Mike LeBeau736cf282009-07-02 17:46:59 -0700422 }
423 });
424 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700425
Mike LeBeau736cf282009-07-02 17:46:59 -0700426 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
428
429 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 protected void onPause() {
431 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800432 dismissPreview(mPreviousView);
433 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800434 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 }
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700437 @Override
438 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400439 // Flag the loader to stop early before switching
440 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700442 if (PROFILE_ROTATE) {
443 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
444 }
445 return null;
446 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 private boolean acceptFilter() {
449 final InputMethodManager inputManager = (InputMethodManager)
450 getSystemService(Context.INPUT_METHOD_SERVICE);
451 return !inputManager.isFullscreenMode();
452 }
453
454 @Override
455 public boolean onKeyDown(int keyCode, KeyEvent event) {
456 boolean handled = super.onKeyDown(keyCode, event);
457 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
458 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
459 keyCode, event);
460 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700461 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700462 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700463 // showSearchDialog()
464 // If there are multiple keystrokes before the search dialog takes focus,
465 // onSearchRequested() will be called for every keystroke,
466 // but it is idempotent, so it's fine.
467 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
469 }
470
471 return handled;
472 }
473
Karl Rosaen138a0412009-04-23 19:00:21 -0700474 private String getTypedText() {
475 return mDefaultKeySsb.toString();
476 }
477
478 private void clearTypedText() {
479 mDefaultKeySsb.clear();
480 mDefaultKeySsb.clearSpans();
481 Selection.setSelection(mDefaultKeySsb, 0);
482 }
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 /**
485 * Restores the previous state, if it exists.
486 *
487 * @param savedState The previous state.
488 */
489 private void restoreState(Bundle savedState) {
490 if (savedState == null) {
491 return;
492 }
493
Joe Onorato3a8820b2009-11-10 15:06:42 -0800494 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
495 if (allApps) {
496 showAllApps(false);
497 }
498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
500 if (currentScreen > -1) {
501 mWorkspace.setCurrentScreen(currentScreen);
502 }
503
504 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
505 if (addScreen > -1) {
506 mAddItemCellInfo = new CellLayout.CellInfo();
507 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
508 addItemCellInfo.valid = true;
509 addItemCellInfo.screen = addScreen;
510 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
511 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
512 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
513 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
514 addItemCellInfo.findVacantCellsFromOccupied(
515 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
516 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
517 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
518 mRestoring = true;
519 }
520
521 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
522 if (renameFolder) {
523 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 mRestoring = true;
526 }
527 }
528
529 /**
530 * Finds all the views we need and configure them properly.
531 */
532 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400533 DragController dragController = mDragController;
534
Romain Guyb1b69f52009-08-10 15:10:15 -0700535 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400536 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537
Joe Onorato7c312c12009-08-13 21:36:53 -0700538 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700539 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700540 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700541 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700542 // Manage focusability manually since this thing is always visible
543 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700544
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
546 final Workspace workspace = mWorkspace;
547
Joe Onorato7c312c12009-08-13 21:36:53 -0700548 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
549 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550
Joe Onorato7c312c12009-08-13 21:36:53 -0700551 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700552 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700553 mHandleView.setOnClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800554
555 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
556 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
557
558 Drawable previous = mPreviousView.getDrawable();
559 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800560 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561
Joe Onorato0d44e942009-11-16 18:20:51 -0800562 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800563 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800564 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800565 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400568 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570
571 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400572 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700573 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574
Joe Onorato00acb122009-08-04 16:04:30 -0400575 dragController.setDragScoller(workspace);
576 dragController.setDragListener(deleteZone);
577 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700578
Joe Onorato00acb122009-08-04 16:04:30 -0400579 // The order here is bottom to top.
580 dragController.addDropTarget(workspace);
581 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 }
583
Romain Guy8a73c512009-11-09 19:19:59 -0800584 @SuppressWarnings({"UnusedDeclaration"})
585 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800586 if (!isAllAppsVisible()) {
587 mWorkspace.scrollLeft();
588 }
Romain Guy8a73c512009-11-09 19:19:59 -0800589 }
590
591 @SuppressWarnings({"UnusedDeclaration"})
592 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800593 if (!isAllAppsVisible()) {
594 mWorkspace.scrollRight();
595 }
Romain Guy8a73c512009-11-09 19:19:59 -0800596 }
597
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 /**
599 * Creates a view representing a shortcut.
600 *
601 * @param info The data structure describing the shortcut.
602 *
603 * @return A View inflated from R.layout.application.
604 */
605 View createShortcut(ApplicationInfo info) {
606 return createShortcut(R.layout.application,
607 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
608 }
609
610 /**
611 * Creates a view representing a shortcut inflated from the specified resource.
612 *
613 * @param layoutResId The id of the XML layout used to create the shortcut.
614 * @param parent The group the shortcut belongs to.
615 * @param info The data structure describing the shortcut.
616 *
617 * @return A View inflated from layoutResId.
618 */
619 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
620 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
621
Joe Onorato5162ea92009-09-03 09:39:42 -0700622 if (info.icon == null) {
623 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
624 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700626 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 info.filtered = true;
628 }
629
630 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
631 favorite.setText(info.title);
632 favorite.setTag(info);
633 favorite.setOnClickListener(this);
634
635 return favorite;
636 }
637
638 /**
639 * Add an application shortcut to the workspace.
640 *
641 * @param data The intent describing the application.
642 * @param cellInfo The position on screen where to create the shortcut.
643 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400644 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 cellInfo.screen = mWorkspace.getCurrentScreen();
646 if (!findSingleSlot(cellInfo)) return;
647
Romain Guy73b979d2009-06-09 12:57:21 -0700648 final ApplicationInfo info = infoFromApplicationIntent(context, data);
649 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400650 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700651 }
652 }
653
654 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 ComponentName component = data.getComponent();
656 PackageManager packageManager = context.getPackageManager();
657 ActivityInfo activityInfo = null;
658 try {
659 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
660 } catch (NameNotFoundException e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800661 Log.e(TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 if (activityInfo != null) {
665 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 itemInfo.title = activityInfo.loadLabel(packageManager);
668 if (itemInfo.title == null) {
669 itemInfo.title = activityInfo.name;
670 }
Romain Guycbb89e42009-06-08 15:52:54 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
673 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
674 itemInfo.icon = activityInfo.loadIcon(packageManager);
675 itemInfo.container = ItemInfo.NO_ID;
676
Romain Guy73b979d2009-06-09 12:57:21 -0700677 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
Romain Guy73b979d2009-06-09 12:57:21 -0700679
680 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 /**
684 * Add a shortcut to the workspace.
685 *
686 * @param data The intent describing the shortcut.
687 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400689 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 cellInfo.screen = mWorkspace.getCurrentScreen();
691 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
694
695 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
698 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700 }
701
Romain Guycbb89e42009-06-08 15:52:54 -0700702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700704 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 * @param data The intent describing the appWidgetId.
707 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700711 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700712
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800713 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 // Try finding open space on Launcher screen
722 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800723 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
724 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
725 return;
726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 // Build Launcher-specific widget info and save to database
729 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 launcherInfo.spanX = spans[0];
731 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 LauncherModel.addItemToDatabase(this, launcherInfo,
734 LauncherSettings.Favorites.CONTAINER_DESKTOP,
735 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
736
737 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700742
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
749 }
Romain Guycbb89e42009-06-08 15:52:54 -0700750
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
752 mDesktopItems.remove(launcherInfo);
753 launcherInfo.hostView = null;
754 }
755
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700756 public LauncherAppWidgetHost getAppWidgetHost() {
757 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
Romain Guycbb89e42009-06-08 15:52:54 -0700759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 static ApplicationInfo addShortcut(Context context, Intent data,
761 CellLayout.CellInfo cellInfo, boolean notify) {
762
Romain Guy73b979d2009-06-09 12:57:21 -0700763 final ApplicationInfo info = infoFromShortcutIntent(context, data);
764 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
765 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
766
767 return info;
768 }
769
770 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
772 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
773 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
774
775 Drawable icon = null;
776 boolean filtered = false;
777 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700778 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
780 if (bitmap != null) {
781 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
782 filtered = true;
783 customIcon = true;
784 } else {
785 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700786 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700788 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 final PackageManager packageManager = context.getPackageManager();
790 Resources resources = packageManager.getResourcesForApplication(
791 iconResource.packageName);
792 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
793 icon = resources.getDrawable(id);
794 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800795 Log.w(TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
797 }
798 }
799
800 if (icon == null) {
801 icon = context.getPackageManager().getDefaultActivityIcon();
802 }
803
804 final ApplicationInfo info = new ApplicationInfo();
805 info.icon = icon;
806 info.filtered = filtered;
807 info.title = name;
808 info.intent = intent;
809 info.customIcon = customIcon;
810 info.iconResource = iconResource;
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 return info;
813 }
814
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800815 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800816 getWindow().closeAllPanels();
817
818 try {
819 dismissDialog(DIALOG_CREATE_SHORTCUT);
820 // Unlock the workspace if the dialog was showing
821 } catch (Exception e) {
822 // An exception is thrown if the dialog is not visible, which is fine
823 }
824
825 try {
826 dismissDialog(DIALOG_RENAME_FOLDER);
827 // Unlock the workspace if the dialog was showing
828 } catch (Exception e) {
829 // An exception is thrown if the dialog is not visible, which is fine
830 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800831
832 // Whatever we were doing is hereby canceled.
833 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800834 }
835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 @Override
837 protected void onNewIntent(Intent intent) {
838 super.onNewIntent(intent);
839
840 // Close the menu
841 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800842 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800843 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700844
Mike LeBeau736cf282009-07-02 17:46:59 -0700845 // Set this flag so that onResume knows to close the search dialog if it's open,
846 // because this was a new intent (thus a press of 'home' or some such) rather than
847 // for example onResume being called when the user pressed the 'back' button.
848 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
Joe Onorato14f122b2009-11-19 14:06:36 -0800850 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
851 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
852 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800853 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800854 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800855 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800856 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700857
Joe Onorato3a8820b2009-11-10 15:06:42 -0800858 final View v = getWindow().peekDecorView();
859 if (v != null && v.getWindowToken() != null) {
860 InputMethodManager imm = (InputMethodManager)getSystemService(
861 INPUT_METHOD_SERVICE);
862 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864 }
865 }
866
867 @Override
868 protected void onRestoreInstanceState(Bundle savedInstanceState) {
869 // Do not call super here
870 mSavedInstanceState = savedInstanceState;
871 }
872
873 @Override
874 protected void onSaveInstanceState(Bundle outState) {
875 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
876
877 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
878 if (folders.size() > 0) {
879 final int count = folders.size();
880 long[] ids = new long[count];
881 for (int i = 0; i < count; i++) {
882 final FolderInfo info = folders.get(i).getInfo();
883 ids[i] = info.id;
884 }
885 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
886 } else {
887 super.onSaveInstanceState(outState);
888 }
889
Joe Onoratofb0ca672009-09-14 17:55:46 -0400890 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800891 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894
895 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
896 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
897 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
898
899 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
900 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
901 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
906 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
907 layout.getOccupiedCells());
908 }
909
910 if (mFolderInfo != null && mWaitingForResult) {
911 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
912 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
913 }
914 }
915
916 @Override
917 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700921 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800923 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 TextKeyListener.getInstance().release();
927
Joe Onorato9c1289c2009-08-17 11:03:03 -0400928 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 unbindDesktopItems();
931 AppInfoCache.unbindDrawables();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800932
933 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800934
935 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800936 dismissPreview(mNextView);
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800937
938 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
941 @Override
942 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700943 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 super.startActivityForResult(intent, requestCode);
945 }
946
947 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700948 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700950
Joe Onorato7bb17492009-09-24 17:51:01 -0700951 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700952
Karl Rosaen138a0412009-04-23 19:00:21 -0700953 // Slide the search widget to the top, if it's on the current screen,
954 // otherwise show the search dialog immediately.
955 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
956 if (searchWidget == null) {
957 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
958 } else {
959 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
960 // show the currently typed text in the search widget while sliding
961 searchWidget.setQuery(getTypedText());
962 }
963 }
Romain Guycbb89e42009-06-08 15:52:54 -0700964
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 /**
966 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700967 *
968 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 */
Romain Guycbb89e42009-06-08 15:52:54 -0700970 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700972
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 if (initialQuery == null) {
974 // Use any text typed in the launcher as the initial query
975 initialQuery = getTypedText();
976 clearTypedText();
977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 if (appSearchData == null) {
979 appSearchData = new Bundle();
980 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
981 }
Romain Guycbb89e42009-06-08 15:52:54 -0700982
Karl Rosaen138a0412009-04-23 19:00:21 -0700983 final SearchManager searchManager =
984 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
985
986 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
987 if (searchWidget != null) {
988 // This gets called when the user leaves the search dialog to go back to
989 // the Launcher.
990 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
991 public void onCancel() {
992 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700993 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700994 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700995 });
996 }
Romain Guycbb89e42009-06-08 15:52:54 -0700997
Karl Rosaen138a0412009-04-23 19:00:21 -0700998 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700999 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001
Karl Rosaen138a0412009-04-23 19:00:21 -07001002 /**
1003 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -07001004 */
Romain Guy5a941392009-04-28 15:18:25 -07001005 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -07001006 // Close search dialog
1007 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +01001008 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -07001009 // Restore search widget to its normal position
1010 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
1011 if (searchWidget != null) {
1012 searchWidget.stopSearch(false);
1013 }
1014 }
Romain Guycbb89e42009-06-08 15:52:54 -07001015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 @Override
1017 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 if (isWorkspaceLocked()) {
1019 return false;
1020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021
1022 super.onCreateOptionsMenu(menu);
1023 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1024 .setIcon(android.R.drawable.ic_menu_add)
1025 .setAlphabeticShortcut('A');
1026 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1027 .setIcon(android.R.drawable.ic_menu_gallery)
1028 .setAlphabeticShortcut('W');
1029 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1030 .setIcon(android.R.drawable.ic_search_category_default)
1031 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1032 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1033 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1034 .setAlphabeticShortcut('N');
1035
1036 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001037 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1038 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
1040 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1041 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1042 .setIntent(settings);
1043
1044 return true;
1045 }
1046
1047 @Override
1048 public boolean onPrepareOptionsMenu(Menu menu) {
1049 super.onPrepareOptionsMenu(menu);
1050
1051 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1052 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1053
1054 return true;
1055 }
1056
1057 @Override
1058 public boolean onOptionsItemSelected(MenuItem item) {
1059 switch (item.getItemId()) {
1060 case MENU_ADD:
1061 addItems();
1062 return true;
1063 case MENU_WALLPAPER_SETTINGS:
1064 startWallpaper();
1065 return true;
1066 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001067 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 return true;
1069 case MENU_NOTIFICATIONS:
1070 showNotifications();
1071 return true;
1072 }
1073
1074 return super.onOptionsItemSelected(item);
1075 }
Romain Guycbb89e42009-06-08 15:52:54 -07001076
Karl Rosaen138a0412009-04-23 19:00:21 -07001077 /**
1078 * Indicates that we want global search for this activity by setting the globalSearch
1079 * argument for {@link #startSearch} to true.
1080 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001082 @Override
1083 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001084 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001085 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001086 }
1087
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 public boolean isWorkspaceLocked() {
1089 return mWorkspaceLoading || mWaitingForResult;
1090 }
1091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001093 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 showAddDialog(mMenuAddInfo);
1095 }
1096
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 void addAppWidget(Intent data) {
1098 // TODO: catch bad widget exception when sent
1099 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001101 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1102 if (SEARCH_WIDGET.equals(customWidget)) {
1103 // We don't need this any more, since this isn't a real app widget.
1104 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1105 // add the search widget
1106 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001108 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1109
1110 if (appWidget.configure != null) {
1111 // Launch over to configure widget, if needed
1112 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1113 intent.setComponent(appWidget.configure);
1114 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1115
1116 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1117 } else {
1118 // Otherwise just add it
1119 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 }
1122 }
Romain Guycbb89e42009-06-08 15:52:54 -07001123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 void addSearch() {
1125 final Widget info = Widget.makeSearch();
1126 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 final int[] xy = mCellCoordinates;
1129 final int spanX = info.spanX;
1130 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1135 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 final View view = mInflater.inflate(info.layoutResource, null);
1138 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001139 Search search = (Search) view.findViewById(R.id.widget_search);
1140 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1143 }
1144
Romain Guy73b979d2009-06-09 12:57:21 -07001145 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001146 // Handle case where user selected "Applications"
1147 String applicationName = getResources().getString(R.string.group_applications);
1148 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001149
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001150 if (applicationName != null && applicationName.equals(shortcutName)) {
1151 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1152 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001154 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1155 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001156 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001157 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001158 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 }
1161
1162 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001163 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001164 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001165 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001166
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001167 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001168 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001169 } else {
1170 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 }
1173
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 UserFolderInfo folderInfo = new UserFolderInfo();
1176 folderInfo.title = getText(R.string.folder_name);
1177
1178 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1179 cellInfo.screen = mWorkspace.getCurrentScreen();
1180 if (!findSingleSlot(cellInfo)) return;
1181
1182 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 LauncherModel.addItemToDatabase(this, folderInfo,
1184 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001186 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
1188 // Create the view
1189 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1190 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1191 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001192 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 void removeFolder(FolderInfo folder) {
1196 mFolders.remove(folder.id);
1197 }
1198
1199 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 cellInfo.screen = mWorkspace.getCurrentScreen();
1201 if (!findSingleSlot(cellInfo)) return;
1202
1203 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1204
1205 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1207 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001208 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1209 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
1211 }
1212
1213 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1214 CellLayout.CellInfo cellInfo, boolean notify) {
1215
1216 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1217 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1218
1219 Drawable icon = null;
1220 boolean filtered = false;
1221 Intent.ShortcutIconResource iconResource = null;
1222
1223 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1224 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1225 try {
1226 iconResource = (Intent.ShortcutIconResource) extra;
1227 final PackageManager packageManager = context.getPackageManager();
1228 Resources resources = packageManager.getResourcesForApplication(
1229 iconResource.packageName);
1230 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1231 icon = resources.getDrawable(id);
1232 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001233 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235 }
1236
1237 if (icon == null) {
1238 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1239 }
1240
1241 final LiveFolderInfo info = new LiveFolderInfo();
1242 info.icon = icon;
1243 info.filtered = filtered;
1244 info.title = name;
1245 info.iconResource = iconResource;
1246 info.uri = data.getData();
1247 info.baseIntent = baseIntent;
1248 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1249 LiveFolders.DISPLAY_MODE_GRID);
1250
1251 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1252 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254
1255 return info;
1256 }
1257
1258 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1259 final int[] xy = new int[2];
1260 if (findSlot(cellInfo, xy, 1, 1)) {
1261 cellInfo.cellX = xy[0];
1262 cellInfo.cellY = xy[1];
1263 return true;
1264 }
1265 return false;
1266 }
1267
1268 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1269 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1270 boolean[] occupied = mSavedState != null ?
1271 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1272 cellInfo = mWorkspace.findAllVacantCells(occupied);
1273 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1274 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1275 return false;
1276 }
1277 }
1278 return true;
1279 }
1280
1281 private void showNotifications() {
1282 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1283 if (statusBar != null) {
1284 statusBar.expand();
1285 }
1286 }
1287
1288 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001289 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001291 Intent chooser = Intent.createChooser(pickWallpaper,
1292 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001293 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1294 // Removed in Eclair MR1
1295// WallpaperManager wm = (WallpaperManager)
1296// getSystemService(Context.WALLPAPER_SERVICE);
1297// WallpaperInfo wi = wm.getWallpaperInfo();
1298// if (wi != null && wi.getSettingsActivity() != null) {
1299// LabeledIntent li = new LabeledIntent(getPackageName(),
1300// R.string.configure_wallpaper, 0);
1301// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1302// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1303// }
Mike Clerona0618e42009-10-22 13:55:21 -07001304 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001307 /**
1308 * Registers various content observers. The current implementation registers
1309 * only a favorites observer to keep track of the favorites applications.
1310 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001311 private void registerContentObservers() {
1312 ContentResolver resolver = getContentResolver();
1313 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1314 true, mWidgetObserver);
1315 }
1316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 @Override
1318 public boolean dispatchKeyEvent(KeyEvent event) {
1319 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1320 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001322 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001323 case KeyEvent.KEYCODE_VOLUME_DOWN:
1324 if (SystemProperties.getInt("launcher2.dumpstate", 0) != 0) {
1325 dumpState();
1326 return true;
1327 }
1328 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001329 }
1330 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1331 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001332 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 return true;
1334 }
1335 }
1336
1337 return super.dispatchKeyEvent(event);
1338 }
1339
Joe Onorato88ec0992009-11-19 13:16:06 -08001340 @Override
1341 public void onBackPressed() {
1342 if (isAllAppsVisible()) {
1343 closeAllApps(true);
1344 } else {
1345 closeFolder();
1346 }
1347 dismissPreview(mPreviousView);
1348 dismissPreview(mNextView);
1349 }
1350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 private void closeFolder() {
1352 Folder folder = mWorkspace.getOpenFolder();
1353 if (folder != null) {
1354 closeFolder(folder);
1355 }
1356 }
1357
1358 void closeFolder(Folder folder) {
1359 folder.getInfo().opened = false;
1360 ViewGroup parent = (ViewGroup) folder.getParent();
1361 if (parent != null) {
1362 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001363 if (folder instanceof DropTarget) {
1364 // Live folders aren't DropTargets.
1365 mDragController.removeDropTarget((DropTarget)folder);
1366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368 folder.onClose();
1369 }
1370
1371 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001372 * Re-listen when widgets are reset.
1373 */
1374 private void onAppWidgetReset() {
1375 mAppWidgetHost.startListening();
1376 }
1377
1378 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1380 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001382 private void unbindDesktopItems() {
1383 for (ItemInfo item: mDesktopItems) {
1384 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 /**
1389 * Launches the intent referred by the clicked shortcut.
1390 *
1391 * @param v The view representing the clicked shortcut.
1392 */
1393 public void onClick(View v) {
1394 Object tag = v.getTag();
1395 if (tag instanceof ApplicationInfo) {
1396 // Open shortcut
1397 final Intent intent = ((ApplicationInfo) tag).intent;
1398 startActivitySafely(intent);
1399 } else if (tag instanceof FolderInfo) {
1400 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001401 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001402 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001403 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001404 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001405 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408 }
1409
1410 void startActivitySafely(Intent intent) {
1411 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1412 try {
1413 startActivity(intent);
1414 } catch (ActivityNotFoundException e) {
1415 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1416 } catch (SecurityException e) {
1417 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001418 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1420 "or use the exported attribute for this activity.", e);
1421 }
1422 }
1423
1424 private void handleFolderClick(FolderInfo folderInfo) {
1425 if (!folderInfo.opened) {
1426 // Close any open folder
1427 closeFolder();
1428 // Open the requested folder
1429 openFolder(folderInfo);
1430 } else {
1431 // Find the open folder...
1432 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1433 int folderScreen;
1434 if (openFolder != null) {
1435 folderScreen = mWorkspace.getScreenForView(openFolder);
1436 // .. and close it
1437 closeFolder(openFolder);
1438 if (folderScreen != mWorkspace.getCurrentScreen()) {
1439 // Close any folder open on the current screen
1440 closeFolder();
1441 // Pull the folder onto this screen
1442 openFolder(folderInfo);
1443 }
1444 }
1445 }
1446 }
1447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 /**
1449 * Opens the user fodler described by the specified tag. The opening of the folder
1450 * is animated relative to the specified View. If the View is null, no animation
1451 * is played.
1452 *
1453 * @param folderInfo The FolderInfo describing the folder to open.
1454 */
1455 private void openFolder(FolderInfo folderInfo) {
1456 Folder openFolder;
1457
1458 if (folderInfo instanceof UserFolderInfo) {
1459 openFolder = UserFolder.fromXml(this);
1460 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001461 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 } else {
1463 return;
1464 }
1465
Joe Onorato00acb122009-08-04 16:04:30 -04001466 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 openFolder.setLauncher(this);
1468
1469 openFolder.bind(folderInfo);
1470 folderInfo.opened = true;
1471
1472 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1473 openFolder.onOpen();
1474 }
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001477 switch (v.getId()) {
1478 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001479 if (!isAllAppsVisible()) {
1480 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1481 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1482 showPreviousPreview(v);
1483 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001484 return true;
1485 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001486 if (!isAllAppsVisible()) {
1487 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1488 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1489 showNextPreview(v);
1490 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001491 return true;
1492 }
1493
Joe Onorato9c1289c2009-08-17 11:03:03 -04001494 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 return false;
1496 }
1497
1498 if (!(v instanceof CellLayout)) {
1499 v = (View) v.getParent();
1500 }
1501
1502 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1503
1504 // This happens when long clicking an item with the dpad/trackball
1505 if (cellInfo == null) {
1506 return true;
1507 }
1508
1509 if (mWorkspace.allowLongPress()) {
1510 if (cellInfo.cell == null) {
1511 if (cellInfo.valid) {
1512 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001513 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 showAddDialog(cellInfo);
1515 }
1516 } else {
1517 if (!(cellInfo.cell instanceof Folder)) {
1518 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001519 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1520 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 mWorkspace.startDrag(cellInfo);
1522 }
1523 }
1524 }
1525 return true;
1526 }
1527
Romain Guye6b8e2f2009-11-10 11:56:55 -08001528 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001529 private void dismissPreview(final View v) {
1530 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001531 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001532 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1533 public void onDismiss() {
1534 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1535 int count = group.getChildCount();
1536 for (int i = 0; i < count; i++) {
1537 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1538 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001539 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1540 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1541
1542 v.setTag(R.id.workspace, null);
1543 v.setTag(R.id.icon, null);
1544 window.setOnDismissListener(null);
1545 }
1546 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001547 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001548 }
1549 v.setTag(null);
1550 }
1551
1552 private void showPreviousPreview(View anchor) {
1553 int current = mWorkspace.getCurrentScreen();
1554 if (current <= 0) return;
1555
Romain Guy9d31e9f2009-11-11 18:54:28 -08001556 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001557 }
1558
1559 private void showNextPreview(View anchor) {
1560 int current = mWorkspace.getCurrentScreen();
1561 if (current >= mWorkspace.getChildCount() - 1) return;
1562
Romain Guy9d31e9f2009-11-11 18:54:28 -08001563 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guya6abce82009-11-10 02:54:41 -08001564 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001565
Romain Guya6abce82009-11-10 02:54:41 -08001566 private void showPreviews(final View anchor, int start, int end) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001567 Resources resources = getResources();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001568
Romain Guya6abce82009-11-10 02:54:41 -08001569 Workspace workspace = mWorkspace;
1570 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001571
1572 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001573
1574 Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001575 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001576 int extraW = (int) ((r.left + r.right) * max);
1577 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001578
1579 int aW = cell.getWidth() - extraW;
1580 float w = aW / max;
1581
1582 int width = cell.getWidth();
1583 int height = cell.getHeight();
1584 int x = cell.getLeftPadding();
1585 int y = cell.getTopPadding();
1586 width -= (x + cell.getRightPadding());
1587 height -= (y + cell.getBottomPadding());
1588
1589 float scale = w / width;
1590
1591 int count = end - start;
1592
1593 final float sWidth = width * scale;
1594 float sHeight = height * scale;
1595
Romain Guye6b8e2f2009-11-10 11:56:55 -08001596 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001597
Romain Guye6b8e2f2009-11-10 11:56:55 -08001598 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1599 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001600
1601 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001602 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001603 cell = (CellLayout) workspace.getChildAt(i);
1604
Romain Guye6b8e2f2009-11-10 11:56:55 -08001605 Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
1606 Bitmap.Config.ARGB_8888);
1607
1608 Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001609 c.scale(scale, scale);
1610 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1611 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001612
Romain Guy9d31e9f2009-11-11 18:54:28 -08001613 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001614 image.setImageBitmap(bitmap);
1615 image.setTag(i);
1616 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001617 image.setOnFocusChangeListener(handler);
1618 image.setFocusable(true);
1619 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001620
1621 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001622 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1623
1624 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001625 }
Romain Guye6b8e2f2009-11-10 11:56:55 -08001626
1627 PopupWindow p = new PopupWindow(this);
1628 p.setContentView(preview);
1629 p.setWidth((int) (sWidth * count + extraW));
1630 p.setHeight((int) (sHeight + extraH));
1631 p.setAnimationStyle(R.style.AnimationPreview);
1632 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001633 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001634 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001635 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001636
Romain Guye6b8e2f2009-11-10 11:56:55 -08001637 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1638 public void onDismiss() {
1639 dismissPreview(anchor);
1640 }
1641 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001642
1643 anchor.setTag(p);
1644 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001645 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001646 }
1647
Romain Guy9d31e9f2009-11-11 18:54:28 -08001648 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001649 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001650
Romain Guye6b8e2f2009-11-10 11:56:55 -08001651 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001652 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001653 }
1654
1655 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001656 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001657 v.post(this);
1658 }
1659
1660 public void run() {
1661 dismissPreview(mAnchor);
1662 }
1663
1664 public void onFocusChange(View v, boolean hasFocus) {
1665 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001666 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001667 }
Romain Guya6abce82009-11-10 02:54:41 -08001668 }
1669 }
1670
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001671 View getDrawerHandle() {
1672 return mHandleView;
1673 }
1674
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 Workspace getWorkspace() {
1676 return mWorkspace;
1677 }
1678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 @Override
1680 protected Dialog onCreateDialog(int id) {
1681 switch (id) {
1682 case DIALOG_CREATE_SHORTCUT:
1683 return new CreateShortcut().createDialog();
1684 case DIALOG_RENAME_FOLDER:
1685 return new RenameFolder().createDialog();
1686 }
1687
1688 return super.onCreateDialog(id);
1689 }
1690
1691 @Override
1692 protected void onPrepareDialog(int id, Dialog dialog) {
1693 switch (id) {
1694 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 break;
1696 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001697 if (mFolderInfo != null) {
1698 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1699 final CharSequence text = mFolderInfo.title;
1700 input.setText(text);
1701 input.setSelection(0, text.length());
1702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 break;
1704 }
1705 }
1706
1707 void showRenameDialog(FolderInfo info) {
1708 mFolderInfo = info;
1709 mWaitingForResult = true;
1710 showDialog(DIALOG_RENAME_FOLDER);
1711 }
1712
1713 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1714 mAddItemCellInfo = cellInfo;
1715 mWaitingForResult = true;
1716 showDialog(DIALOG_CREATE_SHORTCUT);
1717 }
1718
Romain Guy73b979d2009-06-09 12:57:21 -07001719 private void pickShortcut(int requestCode, int title) {
1720 Bundle bundle = new Bundle();
1721
1722 ArrayList<String> shortcutNames = new ArrayList<String>();
1723 shortcutNames.add(getString(R.string.group_applications));
1724 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1725
1726 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1727 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1728 R.drawable.ic_launcher_application));
1729 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1730
1731 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1732 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1733 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1734 pickIntent.putExtras(bundle);
1735
1736 startActivityForResult(pickIntent, requestCode);
1737 }
1738
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 private class RenameFolder {
1740 private EditText mInput;
1741
1742 Dialog createDialog() {
1743 mWaitingForResult = true;
1744 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1745 mInput = (EditText) layout.findViewById(R.id.folder_name);
1746
1747 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1748 builder.setIcon(0);
1749 builder.setTitle(getString(R.string.rename_folder_title));
1750 builder.setCancelable(true);
1751 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1752 public void onCancel(DialogInterface dialog) {
1753 cleanup();
1754 }
1755 });
1756 builder.setNegativeButton(getString(R.string.cancel_action),
1757 new Dialog.OnClickListener() {
1758 public void onClick(DialogInterface dialog, int which) {
1759 cleanup();
1760 }
1761 }
1762 );
1763 builder.setPositiveButton(getString(R.string.rename_action),
1764 new Dialog.OnClickListener() {
1765 public void onClick(DialogInterface dialog, int which) {
1766 changeFolderName();
1767 }
1768 }
1769 );
1770 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001771
1772 final AlertDialog dialog = builder.create();
1773 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1774 public void onShow(DialogInterface dialog) {
Joe Onoratod753b422009-11-08 13:31:11 -05001775 mInput.requestFocus();
1776 InputMethodManager inputManager = (InputMethodManager)
1777 getSystemService(Context.INPUT_METHOD_SERVICE);
1778 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001779 }
1780 });
1781
1782 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784
1785 private void changeFolderName() {
1786 final String name = mInput.getText().toString();
1787 if (!TextUtils.isEmpty(name)) {
1788 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 mFolderInfo.title = name;
1791 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1792
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001794 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 mModel.setWorkspaceDirty();
1796 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 } else {
1798 final FolderIcon folderIcon = (FolderIcon)
1799 mWorkspace.getViewForTag(mFolderInfo);
1800 if (folderIcon != null) {
1801 folderIcon.setText(name);
1802 getWorkspace().requestLayout();
1803 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001804 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 mModel.setWorkspaceDirty();
1806 mWorkspaceLoading = true;
1807 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
1809 }
1810 }
1811 cleanup();
1812 }
1813
1814 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 dismissDialog(DIALOG_RENAME_FOLDER);
1816 mWaitingForResult = false;
1817 mFolderInfo = null;
1818 }
1819 }
1820
Joe Onoratofb0ca672009-09-14 17:55:46 -04001821 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001822 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001823 }
1824
Joe Onorato3a8820b2009-11-10 15:06:42 -08001825 boolean isAllAppsOpaque() {
1826 return mAllAppsGrid.isOpaque();
1827 }
1828
1829 void showAllApps(boolean animated) {
1830 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato7bb17492009-09-24 17:51:01 -07001831 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001832
Joe Onorato3a8820b2009-11-10 15:06:42 -08001833 mWorkspace.startFading(false);
1834
Mike Cleronb6082fa02009-10-19 17:03:36 -07001835 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001836 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001837
Joe Onorato7c312c12009-08-13 21:36:53 -07001838 // TODO: fade these two too
1839 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001840 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001841 }
1842
Joe Onoratob2061212009-11-24 16:13:54 -05001843 /**
1844 * Things to test when changing this code:
1845 * - Home from workspace
1846 * - from center screen
1847 * - from other screens
1848 * - Home from all apps
1849 * - Back from all apps
1850 * - Launch app from workspace and quit
1851 * - with back
1852 * - with home
1853 * - Launch app from all apps and quit
1854 * - with back
1855 * - with home
1856 * - On workspace, long press power and go back
1857 * - with back
1858 * - with home
1859 * - On all apps, long press power and go back
1860 * - with back
1861 * - with home
1862 * - On workspace, power off
1863 * - On all apps, power off
1864 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001865 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001866 if (mAllAppsGrid.isVisible()) {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001867 mAllAppsGrid.zoom(0.0f, animated);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001868 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001869 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001870 mWorkspace.startFading(true);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001871
Joe Onorato7c312c12009-08-13 21:36:53 -07001872 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001873 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001874 mDeleteZone.setVisibility(View.VISIBLE);
1875 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001876 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001877 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001878 }
1879
Joe Onorato7c312c12009-08-13 21:36:53 -07001880 void lockAllApps() {
1881 // TODO
1882 }
1883
1884 void unlockAllApps() {
1885 // TODO
1886 }
1887
Joe Onorato7404ee42009-07-31 11:54:44 -07001888 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 * Displays the shortcut creation dialog and launches, if necessary, the
1890 * appropriate activity.
1891 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001892 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001893 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1894 DialogInterface.OnShowListener {
1895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 Dialog createDialog() {
1899 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1904 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001905 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 builder.setInverseBackgroundForced(true);
1908
1909 AlertDialog dialog = builder.create();
1910 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001911 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001912 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 return dialog;
1915 }
1916
1917 public void onCancel(DialogInterface dialog) {
1918 mWaitingForResult = false;
1919 cleanup();
1920 }
1921
Romain Guycbb89e42009-06-08 15:52:54 -07001922 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001923 }
1924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001926 try {
1927 dismissDialog(DIALOG_CREATE_SHORTCUT);
1928 } catch (Exception e) {
1929 // An exception is thrown if the dialog is not visible, which is fine
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001933 /**
1934 * Handle the action clicked in the "Add to home" dialog.
1935 */
1936 public void onClick(DialogInterface dialog, int which) {
1937 Resources res = getResources();
1938 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001939
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001940 switch (which) {
1941 case AddAdapter.ITEM_SHORTCUT: {
1942 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001943 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001944 break;
1945 }
Romain Guycbb89e42009-06-08 15:52:54 -07001946
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001947 case AddAdapter.ITEM_APPWIDGET: {
1948 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001949
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001950 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1951 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1952 // add the search widget
1953 ArrayList<AppWidgetProviderInfo> customInfo =
1954 new ArrayList<AppWidgetProviderInfo>();
1955 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1956 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1957 info.label = getString(R.string.group_search);
1958 info.icon = R.drawable.ic_search_widget;
1959 customInfo.add(info);
1960 pickIntent.putParcelableArrayListExtra(
1961 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1962 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1963 Bundle b = new Bundle();
1964 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1965 customExtras.add(b);
1966 pickIntent.putParcelableArrayListExtra(
1967 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1968 // start the pick activity
1969 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1970 break;
1971 }
Romain Guycbb89e42009-06-08 15:52:54 -07001972
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001973 case AddAdapter.ITEM_LIVE_FOLDER: {
1974 // Insert extra item to handle inserting folder
1975 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001976
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001977 ArrayList<String> shortcutNames = new ArrayList<String>();
1978 shortcutNames.add(res.getString(R.string.group_folder));
1979 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001980
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001981 ArrayList<ShortcutIconResource> shortcutIcons =
1982 new ArrayList<ShortcutIconResource>();
1983 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1984 R.drawable.ic_launcher_folder));
1985 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1986
1987 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1988 pickIntent.putExtra(Intent.EXTRA_INTENT,
1989 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1990 pickIntent.putExtra(Intent.EXTRA_TITLE,
1991 getText(R.string.title_select_live_folder));
1992 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001993
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001994 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1995 break;
1996 }
1997
1998 case AddAdapter.ITEM_WALLPAPER: {
1999 startWallpaper();
2000 break;
2001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002004
2005 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07002006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
2009 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002010 * Receives notifications when applications are added/removed.
2011 */
2012 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2013 @Override
2014 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002015 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002016 String reason = intent.getStringExtra("reason");
2017 if (!"homekey".equals(reason)) {
2018 boolean animate = true;
2019 /*
2020 if ("globalactions".equals(reason)) {
2021 // For some reason (probably the fading), this animation is
2022 // choppy, so don't show it.
2023 animate = false;
2024 }
2025 */
2026 closeAllApps(animate);
2027 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002028 }
2029 }
2030
2031 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002032 * Receives notifications whenever the appwidgets are reset.
2033 */
2034 private class AppWidgetResetObserver extends ContentObserver {
2035 public AppWidgetResetObserver() {
2036 super(new Handler());
2037 }
2038
2039 @Override
2040 public void onChange(boolean selfChange) {
2041 onAppWidgetReset();
2042 }
2043 }
2044
2045 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002048 public int getCurrentWorkspaceScreen() {
2049 return mWorkspace.getCurrentScreen();
2050 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002051
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052 /**
2053 * Refreshes the shortcuts shown on the workspace.
2054 *
2055 * Implementation of the method from LauncherModel.Callbacks.
2056 */
2057 public void startBinding() {
2058 final Workspace workspace = mWorkspace;
2059 int count = workspace.getChildCount();
2060 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002061 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002062 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2063 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002064
Joe Onorato9c1289c2009-08-17 11:03:03 -04002065 if (DEBUG_USER_INTERFACE) {
2066 android.widget.Button finishButton = new android.widget.Button(this);
2067 finishButton.setText("Finish");
2068 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2069
2070 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2071 public void onClick(View v) {
2072 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002074 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076 }
2077
2078 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 * Bind the items start-end from the list.
2080 *
2081 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2084
2085 final Workspace workspace = mWorkspace;
2086
2087 for (int i=start; i<end; i++) {
2088 final ItemInfo item = shortcuts.get(i);
2089 mDesktopItems.add(item);
2090 switch (item.itemType) {
2091 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2092 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2093 final View shortcut = createShortcut((ApplicationInfo) item);
2094 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2095 false);
2096 break;
2097 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2098 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2099 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2100 (UserFolderInfo) item);
2101 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2102 false);
2103 break;
2104 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2105 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2106 R.layout.live_folder_icon, this,
2107 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2108 (LiveFolderInfo) item);
2109 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2110 false);
2111 break;
2112 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
2113 final int screen = workspace.getCurrentScreen();
2114 final View view = mInflater.inflate(R.layout.widget_search,
2115 (ViewGroup) workspace.getChildAt(screen), false);
2116
2117 Search search = (Search) view.findViewById(R.id.widget_search);
2118 search.setLauncher(this);
2119
2120 final Widget widget = (Widget) item;
2121 view.setTag(widget);
2122
2123 workspace.addWidget(view, widget, false);
2124 break;
2125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127
Joe Onorato9c1289c2009-08-17 11:03:03 -04002128 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 }
2130
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 /**
2132 * Implementation of the method from LauncherModel.Callbacks.
2133 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002134 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2135 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138
2139 /**
2140 * Add the views for a widget to the workspace.
2141 *
2142 * Implementation of the method from LauncherModel.Callbacks.
2143 */
2144 public void bindAppWidget(LauncherAppWidgetInfo item) {
2145 final Workspace workspace = mWorkspace;
2146
2147 final int appWidgetId = item.appWidgetId;
2148 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2149 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2150
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2152 item.hostView.setTag(item);
2153
2154 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2155 item.cellY, item.spanX, item.spanY, false);
2156
2157 workspace.requestLayout();
2158
2159 mDesktopItems.add(item);
2160 }
2161
2162 /**
2163 * Callback saying that there aren't any more items to bind.
2164 *
2165 * Implementation of the method from LauncherModel.Callbacks.
2166 */
2167 public void finishBindingItems() {
2168 if (mSavedState != null) {
2169 if (!mWorkspace.hasFocus()) {
2170 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2171 }
2172
2173 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2174 if (userFolders != null) {
2175 for (long folderId : userFolders) {
2176 final FolderInfo info = mFolders.get(folderId);
2177 if (info != null) {
2178 openFolder(info);
2179 }
2180 }
2181 final Folder openFolder = mWorkspace.getOpenFolder();
2182 if (openFolder != null) {
2183 openFolder.requestFocus();
2184 }
2185 }
2186
Joe Onorato9c1289c2009-08-17 11:03:03 -04002187 mSavedState = null;
2188 }
2189
2190 if (mSavedInstanceState != null) {
2191 super.onRestoreInstanceState(mSavedInstanceState);
2192 mSavedInstanceState = null;
2193 }
2194
Joe Onorato9c1289c2009-08-17 11:03:03 -04002195 mWorkspaceLoading = false;
2196 }
2197
2198 /**
2199 * Add the icons for all apps.
2200 *
2201 * Implementation of the method from LauncherModel.Callbacks.
2202 */
2203 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002204 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 }
2206
2207 /**
2208 * A package was installed.
2209 *
2210 * Implementation of the method from LauncherModel.Callbacks.
2211 */
2212 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
2213 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002214 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002215 }
2216
2217 /**
2218 * A package was updated.
2219 *
2220 * Implementation of the method from LauncherModel.Callbacks.
2221 */
2222 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
2223 removeDialog(DIALOG_CREATE_SHORTCUT);
2224 mWorkspace.updateShortcutsForPackage(packageName);
2225 }
2226
2227 /**
2228 * A package was uninstalled.
2229 *
2230 * Implementation of the method from LauncherModel.Callbacks.
2231 */
2232 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
2233 removeDialog(DIALOG_CREATE_SHORTCUT);
2234 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002235 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002236 }
Joe Onoratobe386092009-11-17 17:32:16 -08002237
2238 /**
2239 * Prints out out state for debugging.
2240 */
2241 public void dumpState() {
2242 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002243 Log.d(TAG, "mSavedState=" + mSavedState);
2244 Log.d(TAG, "mIsNewIntent=" + mIsNewIntent);
2245 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2246 Log.d(TAG, "mRestoring=" + mRestoring);
2247 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2248 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2249 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2250 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002251 mModel.dumpState();
2252 mAllAppsGrid.dumpState();
2253 Log.d(TAG, "END launcher2 dump state");
2254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255}