blob: 9aa6cd30417078139d896aac0df29bdbb1bd90c9 [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 Hackborn8355ae32009-09-07 21:47:51 -070025import android.app.WallpaperInfo;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.content.Context;
30import android.content.DialogInterface;
31import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070032import android.content.Intent.ShortcutIconResource;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.pm.ActivityInfo;
Dianne Hackborn8355ae32009-09-07 21:47:51 -070034import android.content.pm.LabeledIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070038import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.drawable.Drawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.os.Bundle;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Parcelable;
43import android.os.RemoteException;
44import android.os.ServiceManager;
Joe Onorato080d9b62009-11-02 12:01:11 -050045import android.os.SystemClock;
Karl Rosaen138a0412009-04-23 19:00:21 -070046import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.text.Selection;
48import android.text.SpannableStringBuilder;
49import android.text.TextUtils;
50import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070051import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070052import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.view.KeyEvent;
55import android.view.LayoutInflater;
56import android.view.Menu;
57import android.view.MenuItem;
58import android.view.View;
59import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.view.View.OnLongClickListener;
61import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.widget.TextView;
64import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070065import android.appwidget.AppWidgetManager;
66import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import java.util.ArrayList;
Joe Onoratoad72e172009-11-06 16:25:04 -050069import java.util.Map.Entry;
Joe Onorato9c1289c2009-08-17 11:03:03 -040070import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070071import java.io.DataOutputStream;
72import java.io.FileNotFoundException;
73import java.io.IOException;
74import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76/**
77 * Default launcher application.
78 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040079public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070080 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 static final boolean LOGD = false;
84
Joe Onorato9c1289c2009-08-17 11:03:03 -040085 static final boolean PROFILE_STARTUP = false;
86 static final boolean PROFILE_ROTATE = false;
87 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070088
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089 private static final int WALLPAPER_SCREENS_SPAN = 2;
90
91 private static final int MENU_GROUP_ADD = 1;
92 private static final int MENU_ADD = Menu.FIRST + 1;
93 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
94 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
95 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -070096 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
98 private static final int REQUEST_CREATE_SHORTCUT = 1;
99 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700100 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101 private static final int REQUEST_PICK_APPLICATION = 6;
102 private static final int REQUEST_PICK_SHORTCUT = 7;
103 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700104 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700105 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
107 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
108
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700109 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
110 static final String SEARCH_WIDGET = "search_widget";
111
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800112 static final int SCREEN_COUNT = 5;
113 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700115 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato7c312c12009-08-13 21:36:53 -0700117 static final int DIALOG_CREATE_SHORTCUT = 1;
118 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Romain Guy98d01652009-06-30 16:21:04 -0700120 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 // Type: int
123 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
124 // Type: boolean
125 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
126 // Type: long
127 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
128 // Type: int
129 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
130 // Type: int
131 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
132 // Type: int
133 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
134 // Type: int
135 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
136 // Type: int
137 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
138 // Type: int
139 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
142 // Type: int[]
143 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
144 // Type: boolean
145 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
146 // Type: long
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
148
Joe Onorato9c1289c2009-08-17 11:03:03 -0400149 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private LayoutInflater mInflater;
155
Joe Onorato00acb122009-08-04 16:04:30 -0400156 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700158
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700159 private AppWidgetManager mAppWidgetManager;
160 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private CellLayout.CellInfo mAddItemCellInfo;
163 private CellLayout.CellInfo mMenuAddInfo;
164 private final int[] mCellCoordinates = new int[2];
165 private FolderInfo mFolderInfo;
166
Joe Onorato7c312c12009-08-13 21:36:53 -0700167 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700168 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700169 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private Bundle mSavedState;
172
173 private SpannableStringBuilder mDefaultKeySsb = null;
174
Mike LeBeau736cf282009-07-02 17:46:59 -0700175 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 private boolean mWorkspaceLoading = true;
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private boolean mRestoring;
180 private boolean mWaitingForResult;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700181 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
183 private Bundle mSavedInstanceState;
184
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 private LauncherModel mModel;
186
Romain Guy84f296c2009-11-04 15:00:44 -0800187 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
188 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700189
Joe Onorato080d9b62009-11-02 12:01:11 -0500190 public static long lastStartTime;
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 @Override
193 protected void onCreate(Bundle savedInstanceState) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500194 lastStartTime = SystemClock.uptimeMillis();
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
Joe Onorato41a12d22009-10-31 18:30:00 -0400199 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700201
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700202 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
205 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 if (PROFILE_STARTUP) {
208 android.os.Debug.startMethodTracing("/sdcard/launcher");
209 }
210
211 checkForLocaleChange();
212 setWallpaperDimension();
213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 setContentView(R.layout.launcher);
215 setupViews();
216
Joe Onorato7c312c12009-08-13 21:36:53 -0700217 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 mSavedState = savedInstanceState;
220 restoreState(mSavedState);
221
222 if (PROFILE_STARTUP) {
223 android.os.Debug.stopMethodTracing();
224 }
225
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700226 // We have a new AllAppsView, we need to re-bind everything, and it could have
227 // changed in our absence.
228 mModel.setAllAppsDirty();
229 mModel.setWorkspaceDirty();
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 }
234
235 // For handling default keys
236 mDefaultKeySsb = new SpannableStringBuilder();
237 Selection.setSelection(mDefaultKeySsb, 0);
238 }
Romain Guycbb89e42009-06-08 15:52:54 -0700239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700241 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
242 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 final Configuration configuration = getResources().getConfiguration();
245
Romain Guy98d01652009-06-30 16:21:04 -0700246 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 final String locale = configuration.locale.toString();
248
Romain Guy98d01652009-06-30 16:21:04 -0700249 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 final int mcc = configuration.mcc;
251
Romain Guy98d01652009-06-30 16:21:04 -0700252 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 final int mnc = configuration.mnc;
254
Romain Guy84f296c2009-11-04 15:00:44 -0800255 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256
Romain Guy84f296c2009-11-04 15:00:44 -0800257 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700258 localeConfiguration.locale = locale;
259 localeConfiguration.mcc = mcc;
260 localeConfiguration.mnc = mnc;
261
262 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400263 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700264 }
265 }
266
267 private static class LocaleConfiguration {
268 public String locale;
269 public int mcc = -1;
270 public int mnc = -1;
271 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700272
Romain Guy98d01652009-06-30 16:21:04 -0700273 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
274 DataInputStream in = null;
275 try {
276 in = new DataInputStream(context.openFileInput(PREFERENCES));
277 configuration.locale = in.readUTF();
278 configuration.mcc = in.readInt();
279 configuration.mnc = in.readInt();
280 } catch (FileNotFoundException e) {
281 // Ignore
282 } catch (IOException e) {
283 // Ignore
284 } finally {
285 if (in != null) {
286 try {
287 in.close();
288 } catch (IOException e) {
289 // Ignore
290 }
291 }
292 }
293 }
294
295 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
296 DataOutputStream out = null;
297 try {
298 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
299 out.writeUTF(configuration.locale);
300 out.writeInt(configuration.mcc);
301 out.writeInt(configuration.mnc);
302 out.flush();
303 } catch (FileNotFoundException e) {
304 // Ignore
305 } catch (IOException e) {
306 //noinspection ResultOfMethodCallIgnored
307 context.getFileStreamPath(PREFERENCES).delete();
308 } finally {
309 if (out != null) {
310 try {
311 out.close();
312 } catch (IOException e) {
313 // Ignore
314 }
315 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 }
317 }
318
319 static int getScreen() {
320 synchronized (sLock) {
321 return sScreen;
322 }
323 }
324
325 static void setScreen(int screen) {
326 synchronized (sLock) {
327 sScreen = screen;
328 }
329 }
330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700332 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333
334 Display display = getWindowManager().getDefaultDisplay();
335 boolean isPortrait = display.getWidth() < display.getHeight();
336
337 final int width = isPortrait ? display.getWidth() : display.getHeight();
338 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700339 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341
342 @Override
343 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700344 mWaitingForResult = false;
345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 // The pattern used here is that a user PICKs a specific application,
347 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
350 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700351
Romain Guy94dabf12009-07-21 10:55:43 -0700352 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 switch (requestCode) {
354 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400355 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 break;
357 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700358 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 break;
360 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 break;
363 case REQUEST_PICK_LIVE_FOLDER:
364 addLiveFolder(data);
365 break;
366 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700369 case REQUEST_PICK_APPWIDGET:
370 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400373 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700375 case REQUEST_PICK_WALLPAPER:
376 // We just wanted the activity result here so we can clear mWaitingForResult
377 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
Romain Guy18042c82009-11-06 11:44:55 -0800379 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
380 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
381 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700382 // Clean up the appWidgetId if we canceled
383 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
384 if (appWidgetId != -1) {
385 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
389
390 @Override
391 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500392 if (lastStartTime == 0) {
393 lastStartTime = SystemClock.uptimeMillis();
394 }
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 super.onResume();
397
398 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 mWorkspaceLoading = true;
400 mModel.startLoader(this, true);
401 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700403
Mike LeBeau736cf282009-07-02 17:46:59 -0700404 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
405 // onNewIntent), then close the search dialog if needed, because it probably
406 // came from the user pressing 'home' (rather than, for example, pressing 'back').
407 if (mIsNewIntent) {
408 // Post to a handler so that this happens after the search dialog tries to open
409 // itself again.
410 mWorkspace.post(new Runnable() {
411 public void run() {
412 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
413 ServiceManager.getService(Context.SEARCH_SERVICE));
414 try {
415 searchManagerService.stopSearch();
416 } catch (RemoteException e) {
417 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700418 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700419 }
420 });
421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422
Mike LeBeau736cf282009-07-02 17:46:59 -0700423 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 protected void onPause() {
428 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700429 if (mExitingBecauseOfLaunch) {
430 mExitingBecauseOfLaunch = false;
431 closeAllApps(false);
432 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 }
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 @Override
436 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 // Flag the loader to stop early before switching
438 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700440 if (PROFILE_ROTATE) {
441 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
442 }
443 return null;
444 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 private boolean acceptFilter() {
447 final InputMethodManager inputManager = (InputMethodManager)
448 getSystemService(Context.INPUT_METHOD_SERVICE);
449 return !inputManager.isFullscreenMode();
450 }
451
452 @Override
453 public boolean onKeyDown(int keyCode, KeyEvent event) {
454 boolean handled = super.onKeyDown(keyCode, event);
455 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
456 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
457 keyCode, event);
458 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700460 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700461 // showSearchDialog()
462 // If there are multiple keystrokes before the search dialog takes focus,
463 // onSearchRequested() will be called for every keystroke,
464 // but it is idempotent, so it's fine.
465 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467 }
468
469 return handled;
470 }
471
Karl Rosaen138a0412009-04-23 19:00:21 -0700472 private String getTypedText() {
473 return mDefaultKeySsb.toString();
474 }
475
476 private void clearTypedText() {
477 mDefaultKeySsb.clear();
478 mDefaultKeySsb.clearSpans();
479 Selection.setSelection(mDefaultKeySsb, 0);
480 }
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 /**
483 * Restores the previous state, if it exists.
484 *
485 * @param savedState The previous state.
486 */
487 private void restoreState(Bundle savedState) {
488 if (savedState == null) {
489 return;
490 }
491
492 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
493 if (currentScreen > -1) {
494 mWorkspace.setCurrentScreen(currentScreen);
495 }
496
497 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
498 if (addScreen > -1) {
499 mAddItemCellInfo = new CellLayout.CellInfo();
500 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
501 addItemCellInfo.valid = true;
502 addItemCellInfo.screen = addScreen;
503 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
504 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
505 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
506 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
507 addItemCellInfo.findVacantCellsFromOccupied(
508 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
509 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
510 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
511 mRestoring = true;
512 }
513
514 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
515 if (renameFolder) {
516 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 mRestoring = true;
519 }
520 }
521
522 /**
523 * Finds all the views we need and configure them properly.
524 */
525 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400526 DragController dragController = mDragController;
527
Romain Guyb1b69f52009-08-10 15:10:15 -0700528 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400529 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Joe Onorato7c312c12009-08-13 21:36:53 -0700531 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700533 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700534 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700535 // Manage focusability manually since this thing is always visible
536 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700537
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
539 final Workspace workspace = mWorkspace;
540
Joe Onorato7c312c12009-08-13 21:36:53 -0700541 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
542 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543
Joe Onorato7c312c12009-08-13 21:36:53 -0700544 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700545 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700546 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700547 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700548 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700549 handleIocon.setCrossFadeEnabled(true);
550 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400553 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555
556 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400557 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700558 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559
Joe Onorato00acb122009-08-04 16:04:30 -0400560 dragController.setDragScoller(workspace);
561 dragController.setDragListener(deleteZone);
562 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700563
Joe Onorato00acb122009-08-04 16:04:30 -0400564 // The order here is bottom to top.
565 dragController.addDropTarget(workspace);
566 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
568
569 /**
570 * Creates a view representing a shortcut.
571 *
572 * @param info The data structure describing the shortcut.
573 *
574 * @return A View inflated from R.layout.application.
575 */
576 View createShortcut(ApplicationInfo info) {
577 return createShortcut(R.layout.application,
578 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
579 }
580
581 /**
582 * Creates a view representing a shortcut inflated from the specified resource.
583 *
584 * @param layoutResId The id of the XML layout used to create the shortcut.
585 * @param parent The group the shortcut belongs to.
586 * @param info The data structure describing the shortcut.
587 *
588 * @return A View inflated from layoutResId.
589 */
590 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
591 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
592
Joe Onorato5162ea92009-09-03 09:39:42 -0700593 if (info.icon == null) {
594 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700597 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 info.filtered = true;
599 }
600
601 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
602 favorite.setText(info.title);
603 favorite.setTag(info);
604 favorite.setOnClickListener(this);
605
606 return favorite;
607 }
608
609 /**
610 * Add an application shortcut to the workspace.
611 *
612 * @param data The intent describing the application.
613 * @param cellInfo The position on screen where to create the shortcut.
614 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 cellInfo.screen = mWorkspace.getCurrentScreen();
617 if (!findSingleSlot(cellInfo)) return;
618
Romain Guy73b979d2009-06-09 12:57:21 -0700619 final ApplicationInfo info = infoFromApplicationIntent(context, data);
620 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700622 }
623 }
624
625 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 ComponentName component = data.getComponent();
627 PackageManager packageManager = context.getPackageManager();
628 ActivityInfo activityInfo = null;
629 try {
630 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
631 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700632 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 }
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 if (activityInfo != null) {
636 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700637
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 itemInfo.title = activityInfo.loadLabel(packageManager);
639 if (itemInfo.title == null) {
640 itemInfo.title = activityInfo.name;
641 }
Romain Guycbb89e42009-06-08 15:52:54 -0700642
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
644 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
645 itemInfo.icon = activityInfo.loadIcon(packageManager);
646 itemInfo.container = ItemInfo.NO_ID;
647
Romain Guy73b979d2009-06-09 12:57:21 -0700648 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guy73b979d2009-06-09 12:57:21 -0700650
651 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Romain Guycbb89e42009-06-08 15:52:54 -0700653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 /**
655 * Add a shortcut to the workspace.
656 *
657 * @param data The intent describing the shortcut.
658 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 cellInfo.screen = mWorkspace.getCurrentScreen();
662 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
665
666 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
669 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671 }
672
Romain Guycbb89e42009-06-08 15:52:54 -0700673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 * @param data The intent describing the appWidgetId.
678 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700683
Romain Guy18042c82009-11-06 11:44:55 -0800684 if (LOGD) d(LOG_TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700685
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700687
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 // Try finding open space on Launcher screen
693 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800694 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
695 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
696 return;
697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699 // Build Launcher-specific widget info and save to database
700 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 launcherInfo.spanX = spans[0];
702 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 LauncherModel.addItemToDatabase(this, launcherInfo,
705 LauncherSettings.Favorites.CONTAINER_DESKTOP,
706 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
707
708 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400709 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700713
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700714 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700716
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720 }
Romain Guycbb89e42009-06-08 15:52:54 -0700721
Joe Onorato9c1289c2009-08-17 11:03:03 -0400722 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
723 mDesktopItems.remove(launcherInfo);
724 launcherInfo.hostView = null;
725 }
726
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 public LauncherAppWidgetHost getAppWidgetHost() {
728 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
Romain Guycbb89e42009-06-08 15:52:54 -0700730
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 static ApplicationInfo addShortcut(Context context, Intent data,
732 CellLayout.CellInfo cellInfo, boolean notify) {
733
Romain Guy73b979d2009-06-09 12:57:21 -0700734 final ApplicationInfo info = infoFromShortcutIntent(context, data);
735 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
736 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
737
738 return info;
739 }
740
741 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
743 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
744 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
745
746 Drawable icon = null;
747 boolean filtered = false;
748 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700749 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750
751 if (bitmap != null) {
752 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
753 filtered = true;
754 customIcon = true;
755 } else {
756 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700757 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700759 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 final PackageManager packageManager = context.getPackageManager();
761 Resources resources = packageManager.getResourcesForApplication(
762 iconResource.packageName);
763 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
764 icon = resources.getDrawable(id);
765 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700766 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768 }
769 }
770
771 if (icon == null) {
772 icon = context.getPackageManager().getDefaultActivityIcon();
773 }
774
775 final ApplicationInfo info = new ApplicationInfo();
776 info.icon = icon;
777 info.filtered = filtered;
778 info.title = name;
779 info.intent = intent;
780 info.customIcon = customIcon;
781 info.iconResource = iconResource;
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 return info;
784 }
785
786 @Override
787 protected void onNewIntent(Intent intent) {
788 super.onNewIntent(intent);
789
790 // Close the menu
791 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
792 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700793
Joe Onorato742966b2009-10-07 10:26:17 -0700794 // Whatever we were doing is hereby canceled.
795 mWaitingForResult = false;
796
Mike LeBeau736cf282009-07-02 17:46:59 -0700797 // Set this flag so that onResume knows to close the search dialog if it's open,
798 // because this was a new intent (thus a press of 'home' or some such) rather than
799 // for example onResume being called when the user pressed the 'back' button.
800 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801
802 try {
803 dismissDialog(DIALOG_CREATE_SHORTCUT);
804 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 } catch (Exception e) {
806 // An exception is thrown if the dialog is not visible, which is fine
807 }
808
809 try {
810 dismissDialog(DIALOG_RENAME_FOLDER);
811 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 } catch (Exception e) {
813 // An exception is thrown if the dialog is not visible, which is fine
814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
817 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700818
Romain Guy94dabf12009-07-21 10:55:43 -0700819 if (!mWorkspace.isDefaultScreenShowing()) {
820 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700822
Joe Onorato7bb17492009-09-24 17:51:01 -0700823 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700824
825 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 if (v != null && v.getWindowToken() != null) {
827 InputMethodManager imm = (InputMethodManager)getSystemService(
828 INPUT_METHOD_SERVICE);
829 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
830 }
831 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700832 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
834 }
835 }
836
837 @Override
838 protected void onRestoreInstanceState(Bundle savedInstanceState) {
839 // Do not call super here
840 mSavedInstanceState = savedInstanceState;
841 }
842
843 @Override
844 protected void onSaveInstanceState(Bundle outState) {
845 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
846
847 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
848 if (folders.size() > 0) {
849 final int count = folders.size();
850 long[] ids = new long[count];
851 for (int i = 0; i < count; i++) {
852 final FolderInfo info = folders.get(i).getInfo();
853 ids[i] = info.id;
854 }
855 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
856 } else {
857 super.onSaveInstanceState(outState);
858 }
859
Romain Guy3cf604f2009-06-16 13:12:53 -0700860 final boolean isConfigurationChange = getChangingConfigurations() != 0;
861
Romain Guy5a941392009-04-28 15:18:25 -0700862 // When the drawer is opened and we are saving the state because of a
863 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400864 // TODO should not do this if the drawer is currently closing.
865 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868
869 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
870 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
871 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
872
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
878 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
879 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
880 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
881 layout.getOccupiedCells());
882 }
883
884 if (mFolderInfo != null && mWaitingForResult) {
885 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
886 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
887 }
888 }
889
890 @Override
891 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700895 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700897 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899
900 TextKeyListener.getInstance().release();
901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
Joe Onorato9c1289c2009-08-17 11:03:03 -0400904 unbindDesktopItems();
905 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 @Override
909 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700910 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 super.startActivityForResult(intent, requestCode);
912 }
913
914 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700915 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700917
Joe Onorato7bb17492009-09-24 17:51:01 -0700918 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700919
Karl Rosaen138a0412009-04-23 19:00:21 -0700920 // Slide the search widget to the top, if it's on the current screen,
921 // otherwise show the search dialog immediately.
922 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
923 if (searchWidget == null) {
924 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
925 } else {
926 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
927 // show the currently typed text in the search widget while sliding
928 searchWidget.setQuery(getTypedText());
929 }
930 }
Romain Guycbb89e42009-06-08 15:52:54 -0700931
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 /**
933 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700934 *
935 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 */
Romain Guycbb89e42009-06-08 15:52:54 -0700937 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700939
Karl Rosaen138a0412009-04-23 19:00:21 -0700940 if (initialQuery == null) {
941 // Use any text typed in the launcher as the initial query
942 initialQuery = getTypedText();
943 clearTypedText();
944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 if (appSearchData == null) {
946 appSearchData = new Bundle();
947 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
948 }
Romain Guycbb89e42009-06-08 15:52:54 -0700949
Karl Rosaen138a0412009-04-23 19:00:21 -0700950 final SearchManager searchManager =
951 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
952
953 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
954 if (searchWidget != null) {
955 // This gets called when the user leaves the search dialog to go back to
956 // the Launcher.
957 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
958 public void onCancel() {
959 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700960 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700961 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700962 });
963 }
Romain Guycbb89e42009-06-08 15:52:54 -0700964
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700966 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 /**
970 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 */
Romain Guy5a941392009-04-28 15:18:25 -0700972 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700973 // Close search dialog
974 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100975 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700976 // Restore search widget to its normal position
977 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
978 if (searchWidget != null) {
979 searchWidget.stopSearch(false);
980 }
981 }
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 @Override
984 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 if (isWorkspaceLocked()) {
986 return false;
987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
989 super.onCreateOptionsMenu(menu);
990 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
991 .setIcon(android.R.drawable.ic_menu_add)
992 .setAlphabeticShortcut('A');
993 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
994 .setIcon(android.R.drawable.ic_menu_gallery)
995 .setAlphabeticShortcut('W');
996 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
997 .setIcon(android.R.drawable.ic_search_category_default)
998 .setAlphabeticShortcut(SearchManager.MENU_KEY);
999 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1000 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1001 .setAlphabeticShortcut('N');
1002
1003 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001004 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1005 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006
1007 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1008 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1009 .setIntent(settings);
1010
1011 return true;
1012 }
1013
1014 @Override
1015 public boolean onPrepareOptionsMenu(Menu menu) {
1016 super.onPrepareOptionsMenu(menu);
1017
1018 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1019 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1020
1021 return true;
1022 }
1023
1024 @Override
1025 public boolean onOptionsItemSelected(MenuItem item) {
1026 switch (item.getItemId()) {
1027 case MENU_ADD:
1028 addItems();
1029 return true;
1030 case MENU_WALLPAPER_SETTINGS:
1031 startWallpaper();
1032 return true;
1033 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001034 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 return true;
1036 case MENU_NOTIFICATIONS:
1037 showNotifications();
1038 return true;
1039 }
1040
1041 return super.onOptionsItemSelected(item);
1042 }
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Karl Rosaen138a0412009-04-23 19:00:21 -07001044 /**
1045 * Indicates that we want global search for this activity by setting the globalSearch
1046 * argument for {@link #startSearch} to true.
1047 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001049 @Override
1050 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001051 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001052 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001053 }
1054
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 public boolean isWorkspaceLocked() {
1056 return mWorkspaceLoading || mWaitingForResult;
1057 }
1058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 private void addItems() {
1060 showAddDialog(mMenuAddInfo);
1061 }
1062
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 void addAppWidget(Intent data) {
1064 // TODO: catch bad widget exception when sent
1065 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001067 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1068 if (SEARCH_WIDGET.equals(customWidget)) {
1069 // We don't need this any more, since this isn't a real app widget.
1070 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1071 // add the search widget
1072 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001074 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1075
1076 if (appWidget.configure != null) {
1077 // Launch over to configure widget, if needed
1078 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1079 intent.setComponent(appWidget.configure);
1080 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1081
1082 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1083 } else {
1084 // Otherwise just add it
1085 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088 }
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 void addSearch() {
1091 final Widget info = Widget.makeSearch();
1092 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 final int[] xy = mCellCoordinates;
1095 final int spanX = info.spanX;
1096 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1101 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 final View view = mInflater.inflate(info.layoutResource, null);
1104 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001105 Search search = (Search) view.findViewById(R.id.widget_search);
1106 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1109 }
1110
Romain Guy73b979d2009-06-09 12:57:21 -07001111 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 // Handle case where user selected "Applications"
1113 String applicationName = getResources().getString(R.string.group_applications);
1114 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 if (applicationName != null && applicationName.equals(shortcutName)) {
1117 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1118 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001120 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1121 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001122 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001124 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 }
1127
1128 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001130 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001132
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001133 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001135 } else {
1136 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 UserFolderInfo folderInfo = new UserFolderInfo();
1142 folderInfo.title = getText(R.string.folder_name);
1143
1144 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1145 cellInfo.screen = mWorkspace.getCurrentScreen();
1146 if (!findSingleSlot(cellInfo)) return;
1147
1148 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149 LauncherModel.addItemToDatabase(this, folderInfo,
1150 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153
1154 // Create the view
1155 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1156 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1157 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001158 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Joe Onorato9c1289c2009-08-17 11:03:03 -04001161 void removeFolder(FolderInfo folder) {
1162 mFolders.remove(folder.id);
1163 }
1164
1165 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 cellInfo.screen = mWorkspace.getCurrentScreen();
1167 if (!findSingleSlot(cellInfo)) return;
1168
1169 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1170
1171 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1173 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001174 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1175 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
1179 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1180 CellLayout.CellInfo cellInfo, boolean notify) {
1181
1182 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1183 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1184
1185 Drawable icon = null;
1186 boolean filtered = false;
1187 Intent.ShortcutIconResource iconResource = null;
1188
1189 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1190 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1191 try {
1192 iconResource = (Intent.ShortcutIconResource) extra;
1193 final PackageManager packageManager = context.getPackageManager();
1194 Resources resources = packageManager.getResourcesForApplication(
1195 iconResource.packageName);
1196 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1197 icon = resources.getDrawable(id);
1198 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001199 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
1201 }
1202
1203 if (icon == null) {
1204 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1205 }
1206
1207 final LiveFolderInfo info = new LiveFolderInfo();
1208 info.icon = icon;
1209 info.filtered = filtered;
1210 info.title = name;
1211 info.iconResource = iconResource;
1212 info.uri = data.getData();
1213 info.baseIntent = baseIntent;
1214 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1215 LiveFolders.DISPLAY_MODE_GRID);
1216
1217 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1218 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001219 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
1221 return info;
1222 }
1223
1224 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1225 final int[] xy = new int[2];
1226 if (findSlot(cellInfo, xy, 1, 1)) {
1227 cellInfo.cellX = xy[0];
1228 cellInfo.cellY = xy[1];
1229 return true;
1230 }
1231 return false;
1232 }
1233
1234 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1235 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1236 boolean[] occupied = mSavedState != null ?
1237 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1238 cellInfo = mWorkspace.findAllVacantCells(occupied);
1239 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1240 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1241 return false;
1242 }
1243 }
1244 return true;
1245 }
1246
1247 private void showNotifications() {
1248 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1249 if (statusBar != null) {
1250 statusBar.expand();
1251 }
1252 }
1253
1254 private void startWallpaper() {
1255 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001256 Intent chooser = Intent.createChooser(pickWallpaper,
1257 getText(R.string.chooser_wallpaper));
1258 WallpaperManager wm = (WallpaperManager)
1259 getSystemService(Context.WALLPAPER_SERVICE);
1260 WallpaperInfo wi = wm.getWallpaperInfo();
1261 if (wi != null && wi.getSettingsActivity() != null) {
1262 LabeledIntent li = new LabeledIntent(getPackageName(),
1263 R.string.configure_wallpaper, 0);
1264 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1265 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1266 }
Mike Clerona0618e42009-10-22 13:55:21 -07001267 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 }
1269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 @Override
1271 public boolean dispatchKeyEvent(KeyEvent event) {
1272 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1273 switch (event.getKeyCode()) {
1274 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 return true;
1276 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001277 return true;
1278 }
1279 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1280 switch (event.getKeyCode()) {
1281 case KeyEvent.KEYCODE_BACK:
1282 if (!event.isCanceled()) {
1283 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001284 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001285 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001286 } else {
1287 closeFolder();
1288 }
1289 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001290 return true;
1291 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 return true;
1293 }
1294 }
1295
1296 return super.dispatchKeyEvent(event);
1297 }
1298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 private void closeFolder() {
1300 Folder folder = mWorkspace.getOpenFolder();
1301 if (folder != null) {
1302 closeFolder(folder);
1303 }
1304 }
1305
1306 void closeFolder(Folder folder) {
1307 folder.getInfo().opened = false;
1308 ViewGroup parent = (ViewGroup) folder.getParent();
1309 if (parent != null) {
1310 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001311 if (folder instanceof DropTarget) {
1312 // Live folders aren't DropTargets.
1313 mDragController.removeDropTarget((DropTarget)folder);
1314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316 folder.onClose();
1317 }
1318
1319 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1321 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001323 private void unbindDesktopItems() {
1324 for (ItemInfo item: mDesktopItems) {
1325 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 }
1327 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 /**
1330 * Launches the intent referred by the clicked shortcut.
1331 *
1332 * @param v The view representing the clicked shortcut.
1333 */
1334 public void onClick(View v) {
1335 Object tag = v.getTag();
1336 if (tag instanceof ApplicationInfo) {
1337 // Open shortcut
1338 final Intent intent = ((ApplicationInfo) tag).intent;
1339 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001340 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 } else if (tag instanceof FolderInfo) {
1342 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001343 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001344 Log.d(TAG, "onClick");
1345 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001346 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001347 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001348 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351 }
1352
1353 void startActivitySafely(Intent intent) {
1354 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1355 try {
1356 startActivity(intent);
1357 } catch (ActivityNotFoundException e) {
1358 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1359 } catch (SecurityException e) {
1360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001361 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1363 "or use the exported attribute for this activity.", e);
1364 }
1365 }
1366
1367 private void handleFolderClick(FolderInfo folderInfo) {
1368 if (!folderInfo.opened) {
1369 // Close any open folder
1370 closeFolder();
1371 // Open the requested folder
1372 openFolder(folderInfo);
1373 } else {
1374 // Find the open folder...
1375 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1376 int folderScreen;
1377 if (openFolder != null) {
1378 folderScreen = mWorkspace.getScreenForView(openFolder);
1379 // .. and close it
1380 closeFolder(openFolder);
1381 if (folderScreen != mWorkspace.getCurrentScreen()) {
1382 // Close any folder open on the current screen
1383 closeFolder();
1384 // Pull the folder onto this screen
1385 openFolder(folderInfo);
1386 }
1387 }
1388 }
1389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 /**
1392 * Opens the user fodler described by the specified tag. The opening of the folder
1393 * is animated relative to the specified View. If the View is null, no animation
1394 * is played.
1395 *
1396 * @param folderInfo The FolderInfo describing the folder to open.
1397 */
1398 private void openFolder(FolderInfo folderInfo) {
1399 Folder openFolder;
1400
1401 if (folderInfo instanceof UserFolderInfo) {
1402 openFolder = UserFolder.fromXml(this);
1403 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001404 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 } else {
1406 return;
1407 }
1408
Joe Onorato00acb122009-08-04 16:04:30 -04001409 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 openFolder.setLauncher(this);
1411
1412 openFolder.bind(folderInfo);
1413 folderInfo.opened = true;
1414
1415 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1416 openFolder.onOpen();
1417 }
1418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return false;
1422 }
1423
1424 if (!(v instanceof CellLayout)) {
1425 v = (View) v.getParent();
1426 }
1427
1428 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1429
1430 // This happens when long clicking an item with the dpad/trackball
1431 if (cellInfo == null) {
1432 return true;
1433 }
1434
1435 if (mWorkspace.allowLongPress()) {
1436 if (cellInfo.cell == null) {
1437 if (cellInfo.valid) {
1438 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001439 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 showAddDialog(cellInfo);
1441 }
1442 } else {
1443 if (!(cellInfo.cell instanceof Folder)) {
1444 // User long pressed on an item
1445 mWorkspace.startDrag(cellInfo);
1446 }
1447 }
1448 }
1449 return true;
1450 }
1451
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001452 View getDrawerHandle() {
1453 return mHandleView;
1454 }
1455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 Workspace getWorkspace() {
1457 return mWorkspace;
1458 }
1459
Joe Onorato7404ee42009-07-31 11:54:44 -07001460 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 GridView getApplicationsGrid() {
1462 return mAllAppsGrid;
1463 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001464 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465
1466 @Override
1467 protected Dialog onCreateDialog(int id) {
1468 switch (id) {
1469 case DIALOG_CREATE_SHORTCUT:
1470 return new CreateShortcut().createDialog();
1471 case DIALOG_RENAME_FOLDER:
1472 return new RenameFolder().createDialog();
1473 }
1474
1475 return super.onCreateDialog(id);
1476 }
1477
1478 @Override
1479 protected void onPrepareDialog(int id, Dialog dialog) {
1480 switch (id) {
1481 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 break;
1483 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001484 if (mFolderInfo != null) {
1485 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1486 final CharSequence text = mFolderInfo.title;
1487 input.setText(text);
1488 input.setSelection(0, text.length());
1489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 break;
1491 }
1492 }
1493
1494 void showRenameDialog(FolderInfo info) {
1495 mFolderInfo = info;
1496 mWaitingForResult = true;
1497 showDialog(DIALOG_RENAME_FOLDER);
1498 }
1499
1500 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1501 mAddItemCellInfo = cellInfo;
1502 mWaitingForResult = true;
1503 showDialog(DIALOG_CREATE_SHORTCUT);
1504 }
1505
Romain Guy73b979d2009-06-09 12:57:21 -07001506 private void pickShortcut(int requestCode, int title) {
1507 Bundle bundle = new Bundle();
1508
1509 ArrayList<String> shortcutNames = new ArrayList<String>();
1510 shortcutNames.add(getString(R.string.group_applications));
1511 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1512
1513 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1514 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1515 R.drawable.ic_launcher_application));
1516 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1517
1518 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1519 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1520 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1521 pickIntent.putExtras(bundle);
1522
1523 startActivityForResult(pickIntent, requestCode);
1524 }
1525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 private class RenameFolder {
1527 private EditText mInput;
1528
1529 Dialog createDialog() {
1530 mWaitingForResult = true;
1531 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1532 mInput = (EditText) layout.findViewById(R.id.folder_name);
1533
1534 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1535 builder.setIcon(0);
1536 builder.setTitle(getString(R.string.rename_folder_title));
1537 builder.setCancelable(true);
1538 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1539 public void onCancel(DialogInterface dialog) {
1540 cleanup();
1541 }
1542 });
1543 builder.setNegativeButton(getString(R.string.cancel_action),
1544 new Dialog.OnClickListener() {
1545 public void onClick(DialogInterface dialog, int which) {
1546 cleanup();
1547 }
1548 }
1549 );
1550 builder.setPositiveButton(getString(R.string.rename_action),
1551 new Dialog.OnClickListener() {
1552 public void onClick(DialogInterface dialog, int which) {
1553 changeFolderName();
1554 }
1555 }
1556 );
1557 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001558
1559 final AlertDialog dialog = builder.create();
1560 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1561 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001562 }
1563 });
1564
1565 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
1567
1568 private void changeFolderName() {
1569 final String name = mInput.getText().toString();
1570 if (!TextUtils.isEmpty(name)) {
1571 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 mFolderInfo.title = name;
1574 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1575
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001577 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 mModel.setWorkspaceDirty();
1579 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 } else {
1581 final FolderIcon folderIcon = (FolderIcon)
1582 mWorkspace.getViewForTag(mFolderInfo);
1583 if (folderIcon != null) {
1584 folderIcon.setText(name);
1585 getWorkspace().requestLayout();
1586 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001587 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 mModel.setWorkspaceDirty();
1589 mWorkspaceLoading = true;
1590 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592 }
1593 }
1594 cleanup();
1595 }
1596
1597 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 dismissDialog(DIALOG_RENAME_FOLDER);
1599 mWaitingForResult = false;
1600 mFolderInfo = null;
1601 }
1602 }
1603
Joe Onoratofb0ca672009-09-14 17:55:46 -04001604 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001605 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001606 }
1607
Jason Samsfd22dac2009-09-20 17:24:16 -07001608 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001609 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001610 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001611
Mike Cleronb6082fa02009-10-19 17:03:36 -07001612 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001613 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001614
Joe Onorato7c312c12009-08-13 21:36:53 -07001615 // TODO: fade these two too
1616 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001617 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001618 }
1619
Joe Onorato7bb17492009-09-24 17:51:01 -07001620 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001621 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001622 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001623 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001624 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001625
Mike Cleronb6082fa02009-10-19 17:03:36 -07001626
Joe Onorato7c312c12009-08-13 21:36:53 -07001627 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001628 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001629 mDeleteZone.setVisibility(View.VISIBLE);
1630 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001631 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001632 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001633 }
1634
Joe Onorato7c312c12009-08-13 21:36:53 -07001635 void lockAllApps() {
1636 // TODO
1637 }
1638
1639 void unlockAllApps() {
1640 // TODO
1641 }
1642
Joe Onorato7404ee42009-07-31 11:54:44 -07001643 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 * Displays the shortcut creation dialog and launches, if necessary, the
1645 * appropriate activity.
1646 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001647 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001648 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1649 DialogInterface.OnShowListener {
1650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 Dialog createDialog() {
1654 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1659 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001660 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 builder.setInverseBackgroundForced(true);
1663
1664 AlertDialog dialog = builder.create();
1665 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001666 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001667 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 return dialog;
1670 }
1671
1672 public void onCancel(DialogInterface dialog) {
1673 mWaitingForResult = false;
1674 cleanup();
1675 }
1676
Romain Guycbb89e42009-06-08 15:52:54 -07001677 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001678 }
1679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 dismissDialog(DIALOG_CREATE_SHORTCUT);
1682 }
1683
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001684 /**
1685 * Handle the action clicked in the "Add to home" dialog.
1686 */
1687 public void onClick(DialogInterface dialog, int which) {
1688 Resources res = getResources();
1689 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001690
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001691 switch (which) {
1692 case AddAdapter.ITEM_SHORTCUT: {
1693 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001694 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001695 break;
1696 }
Romain Guycbb89e42009-06-08 15:52:54 -07001697
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001698 case AddAdapter.ITEM_APPWIDGET: {
1699 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001700
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001701 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1702 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1703 // add the search widget
1704 ArrayList<AppWidgetProviderInfo> customInfo =
1705 new ArrayList<AppWidgetProviderInfo>();
1706 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1707 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1708 info.label = getString(R.string.group_search);
1709 info.icon = R.drawable.ic_search_widget;
1710 customInfo.add(info);
1711 pickIntent.putParcelableArrayListExtra(
1712 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1713 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1714 Bundle b = new Bundle();
1715 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1716 customExtras.add(b);
1717 pickIntent.putParcelableArrayListExtra(
1718 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1719 // start the pick activity
1720 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1721 break;
1722 }
Romain Guycbb89e42009-06-08 15:52:54 -07001723
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001724 case AddAdapter.ITEM_LIVE_FOLDER: {
1725 // Insert extra item to handle inserting folder
1726 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001727
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001728 ArrayList<String> shortcutNames = new ArrayList<String>();
1729 shortcutNames.add(res.getString(R.string.group_folder));
1730 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001731
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001732 ArrayList<ShortcutIconResource> shortcutIcons =
1733 new ArrayList<ShortcutIconResource>();
1734 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1735 R.drawable.ic_launcher_folder));
1736 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1737
1738 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1739 pickIntent.putExtra(Intent.EXTRA_INTENT,
1740 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1741 pickIntent.putExtra(Intent.EXTRA_TITLE,
1742 getText(R.string.title_select_live_folder));
1743 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001744
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001745 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1746 break;
1747 }
1748
1749 case AddAdapter.ITEM_WALLPAPER: {
1750 startWallpaper();
1751 break;
1752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001755
1756 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
1759
1760 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 public int getCurrentWorkspaceScreen() {
1764 return mWorkspace.getCurrentScreen();
1765 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001766
Joe Onorato9c1289c2009-08-17 11:03:03 -04001767 /**
1768 * Refreshes the shortcuts shown on the workspace.
1769 *
1770 * Implementation of the method from LauncherModel.Callbacks.
1771 */
1772 public void startBinding() {
1773 final Workspace workspace = mWorkspace;
1774 int count = workspace.getChildCount();
1775 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001776 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1778 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001779
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 if (DEBUG_USER_INTERFACE) {
1781 android.widget.Button finishButton = new android.widget.Button(this);
1782 finishButton.setText("Finish");
1783 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1784
1785 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1786 public void onClick(View v) {
1787 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791 }
1792
1793 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 * Bind the items start-end from the list.
1795 *
1796 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1799
1800 final Workspace workspace = mWorkspace;
1801
1802 for (int i=start; i<end; i++) {
1803 final ItemInfo item = shortcuts.get(i);
1804 mDesktopItems.add(item);
1805 switch (item.itemType) {
1806 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1807 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1808 final View shortcut = createShortcut((ApplicationInfo) item);
1809 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1810 false);
1811 break;
1812 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1813 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1814 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1815 (UserFolderInfo) item);
1816 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1817 false);
1818 break;
1819 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1820 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1821 R.layout.live_folder_icon, this,
1822 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1823 (LiveFolderInfo) item);
1824 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1825 false);
1826 break;
1827 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1828 final int screen = workspace.getCurrentScreen();
1829 final View view = mInflater.inflate(R.layout.widget_search,
1830 (ViewGroup) workspace.getChildAt(screen), false);
1831
1832 Search search = (Search) view.findViewById(R.id.widget_search);
1833 search.setLauncher(this);
1834
1835 final Widget widget = (Widget) item;
1836 view.setTag(widget);
1837
1838 workspace.addWidget(view, widget, false);
1839 break;
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 }
1842
Joe Onorato9c1289c2009-08-17 11:03:03 -04001843 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 /**
1847 * Implementation of the method from LauncherModel.Callbacks.
1848 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001849 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1850 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853
1854 /**
1855 * Add the views for a widget to the workspace.
1856 *
1857 * Implementation of the method from LauncherModel.Callbacks.
1858 */
1859 public void bindAppWidget(LauncherAppWidgetInfo item) {
1860 final Workspace workspace = mWorkspace;
1861
1862 final int appWidgetId = item.appWidgetId;
1863 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1864 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1865
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1867 item.hostView.setTag(item);
1868
1869 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1870 item.cellY, item.spanX, item.spanY, false);
1871
1872 workspace.requestLayout();
1873
1874 mDesktopItems.add(item);
1875 }
1876
1877 /**
1878 * Callback saying that there aren't any more items to bind.
1879 *
1880 * Implementation of the method from LauncherModel.Callbacks.
1881 */
1882 public void finishBindingItems() {
1883 if (mSavedState != null) {
1884 if (!mWorkspace.hasFocus()) {
1885 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1886 }
1887
1888 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1889 if (userFolders != null) {
1890 for (long folderId : userFolders) {
1891 final FolderInfo info = mFolders.get(folderId);
1892 if (info != null) {
1893 openFolder(info);
1894 }
1895 }
1896 final Folder openFolder = mWorkspace.getOpenFolder();
1897 if (openFolder != null) {
1898 openFolder.requestFocus();
1899 }
1900 }
1901
1902 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1903 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001904 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001905 }
1906
1907 mSavedState = null;
1908 }
1909
1910 if (mSavedInstanceState != null) {
1911 super.onRestoreInstanceState(mSavedInstanceState);
1912 mSavedInstanceState = null;
1913 }
1914
Joe Onorato9c1289c2009-08-17 11:03:03 -04001915 Log.d(TAG, "finishBindingItems done");
1916 mWorkspaceLoading = false;
1917 }
1918
1919 /**
1920 * Add the icons for all apps.
1921 *
1922 * Implementation of the method from LauncherModel.Callbacks.
1923 */
1924 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08001925 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 }
1927
1928 /**
1929 * A package was installed.
1930 *
1931 * Implementation of the method from LauncherModel.Callbacks.
1932 */
1933 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1934 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001935 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 }
1937
1938 /**
1939 * A package was updated.
1940 *
1941 * Implementation of the method from LauncherModel.Callbacks.
1942 */
1943 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1944 removeDialog(DIALOG_CREATE_SHORTCUT);
1945 mWorkspace.updateShortcutsForPackage(packageName);
1946 }
1947
1948 /**
1949 * A package was uninstalled.
1950 *
1951 * Implementation of the method from LauncherModel.Callbacks.
1952 */
1953 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1954 removeDialog(DIALOG_CREATE_SHORTCUT);
1955 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001956 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958}