blob: 6cccb77f29badd176d6bc4bc580a07ee3fde4e12 [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 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 // Clean up the appWidgetId if we canceled
382 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
383 if (appWidgetId != -1) {
384 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 }
388
389 @Override
390 protected void onResume() {
Joe Onorato080d9b62009-11-02 12:01:11 -0500391 if (lastStartTime == 0) {
392 lastStartTime = SystemClock.uptimeMillis();
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onResume();
396
397 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400398 mWorkspaceLoading = true;
399 mModel.startLoader(this, true);
400 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700402
Mike LeBeau736cf282009-07-02 17:46:59 -0700403 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
404 // onNewIntent), then close the search dialog if needed, because it probably
405 // came from the user pressing 'home' (rather than, for example, pressing 'back').
406 if (mIsNewIntent) {
407 // Post to a handler so that this happens after the search dialog tries to open
408 // itself again.
409 mWorkspace.post(new Runnable() {
410 public void run() {
411 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
412 ServiceManager.getService(Context.SEARCH_SERVICE));
413 try {
414 searchManagerService.stopSearch();
415 } catch (RemoteException e) {
416 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700417 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700418 }
419 });
420 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700421
Mike LeBeau736cf282009-07-02 17:46:59 -0700422 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
425 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700426 protected void onPause() {
427 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700428 if (mExitingBecauseOfLaunch) {
429 mExitingBecauseOfLaunch = false;
430 closeAllApps(false);
431 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 }
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700434 @Override
435 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400436 // Flag the loader to stop early before switching
437 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700439 if (PROFILE_ROTATE) {
440 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
441 }
442 return null;
443 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 private boolean acceptFilter() {
446 final InputMethodManager inputManager = (InputMethodManager)
447 getSystemService(Context.INPUT_METHOD_SERVICE);
448 return !inputManager.isFullscreenMode();
449 }
450
451 @Override
452 public boolean onKeyDown(int keyCode, KeyEvent event) {
453 boolean handled = super.onKeyDown(keyCode, event);
454 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
455 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
456 keyCode, event);
457 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700458 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700459 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700460 // showSearchDialog()
461 // If there are multiple keystrokes before the search dialog takes focus,
462 // onSearchRequested() will be called for every keystroke,
463 // but it is idempotent, so it's fine.
464 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 }
466 }
467
468 return handled;
469 }
470
Karl Rosaen138a0412009-04-23 19:00:21 -0700471 private String getTypedText() {
472 return mDefaultKeySsb.toString();
473 }
474
475 private void clearTypedText() {
476 mDefaultKeySsb.clear();
477 mDefaultKeySsb.clearSpans();
478 Selection.setSelection(mDefaultKeySsb, 0);
479 }
480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 /**
482 * Restores the previous state, if it exists.
483 *
484 * @param savedState The previous state.
485 */
486 private void restoreState(Bundle savedState) {
487 if (savedState == null) {
488 return;
489 }
490
491 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
492 if (currentScreen > -1) {
493 mWorkspace.setCurrentScreen(currentScreen);
494 }
495
496 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
497 if (addScreen > -1) {
498 mAddItemCellInfo = new CellLayout.CellInfo();
499 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
500 addItemCellInfo.valid = true;
501 addItemCellInfo.screen = addScreen;
502 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
503 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
504 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
505 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
506 addItemCellInfo.findVacantCellsFromOccupied(
507 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
508 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
509 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
510 mRestoring = true;
511 }
512
513 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
514 if (renameFolder) {
515 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 mRestoring = true;
518 }
519 }
520
521 /**
522 * Finds all the views we need and configure them properly.
523 */
524 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400525 DragController dragController = mDragController;
526
Romain Guyb1b69f52009-08-10 15:10:15 -0700527 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400528 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529
Joe Onorato7c312c12009-08-13 21:36:53 -0700530 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700532 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700533 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700534 // Manage focusability manually since this thing is always visible
535 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
538 final Workspace workspace = mWorkspace;
539
Joe Onorato7c312c12009-08-13 21:36:53 -0700540 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
541 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542
Joe Onorato7c312c12009-08-13 21:36:53 -0700543 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700545 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700546 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700547 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700548 handleIocon.setCrossFadeEnabled(true);
549 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400552 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554
555 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400556 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700557 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558
Joe Onorato00acb122009-08-04 16:04:30 -0400559 dragController.setDragScoller(workspace);
560 dragController.setDragListener(deleteZone);
561 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700562
Joe Onorato00acb122009-08-04 16:04:30 -0400563 // The order here is bottom to top.
564 dragController.addDropTarget(workspace);
565 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
567
568 /**
569 * Creates a view representing a shortcut.
570 *
571 * @param info The data structure describing the shortcut.
572 *
573 * @return A View inflated from R.layout.application.
574 */
575 View createShortcut(ApplicationInfo info) {
576 return createShortcut(R.layout.application,
577 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
578 }
579
580 /**
581 * Creates a view representing a shortcut inflated from the specified resource.
582 *
583 * @param layoutResId The id of the XML layout used to create the shortcut.
584 * @param parent The group the shortcut belongs to.
585 * @param info The data structure describing the shortcut.
586 *
587 * @return A View inflated from layoutResId.
588 */
589 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
590 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
591
Joe Onorato5162ea92009-09-03 09:39:42 -0700592 if (info.icon == null) {
593 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700596 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 info.filtered = true;
598 }
599
600 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
601 favorite.setText(info.title);
602 favorite.setTag(info);
603 favorite.setOnClickListener(this);
604
605 return favorite;
606 }
607
608 /**
609 * Add an application shortcut to the workspace.
610 *
611 * @param data The intent describing the application.
612 * @param cellInfo The position on screen where to create the shortcut.
613 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400614 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 cellInfo.screen = mWorkspace.getCurrentScreen();
616 if (!findSingleSlot(cellInfo)) return;
617
Romain Guy73b979d2009-06-09 12:57:21 -0700618 final ApplicationInfo info = infoFromApplicationIntent(context, data);
619 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400620 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700621 }
622 }
623
624 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 ComponentName component = data.getComponent();
626 PackageManager packageManager = context.getPackageManager();
627 ActivityInfo activityInfo = null;
628 try {
629 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
630 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700631 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 }
Romain Guycbb89e42009-06-08 15:52:54 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 if (activityInfo != null) {
635 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 itemInfo.title = activityInfo.loadLabel(packageManager);
638 if (itemInfo.title == null) {
639 itemInfo.title = activityInfo.name;
640 }
Romain Guycbb89e42009-06-08 15:52:54 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
643 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
644 itemInfo.icon = activityInfo.loadIcon(packageManager);
645 itemInfo.container = ItemInfo.NO_ID;
646
Romain Guy73b979d2009-06-09 12:57:21 -0700647 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Romain Guy73b979d2009-06-09 12:57:21 -0700649
650 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 /**
654 * Add a shortcut to the workspace.
655 *
656 * @param data The intent describing the shortcut.
657 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400659 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 cellInfo.screen = mWorkspace.getCurrentScreen();
661 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
664
665 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
668 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
670 }
671
Romain Guycbb89e42009-06-08 15:52:54 -0700672
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 * @param data The intent describing the appWidgetId.
677 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700682
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700683 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700690
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 // Try finding open space on Launcher screen
692 final int[] xy = mCellCoordinates;
693 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
694
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 // Build Launcher-specific widget info and save to database
696 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 launcherInfo.spanX = spans[0];
698 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 LauncherModel.addItemToDatabase(this, launcherInfo,
701 LauncherSettings.Favorites.CONTAINER_DESKTOP,
702 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
703
704 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700709
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400714 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
716 }
Romain Guycbb89e42009-06-08 15:52:54 -0700717
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
719 mDesktopItems.remove(launcherInfo);
720 launcherInfo.hostView = null;
721 }
722
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 public LauncherAppWidgetHost getAppWidgetHost() {
724 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
Romain Guycbb89e42009-06-08 15:52:54 -0700726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 static ApplicationInfo addShortcut(Context context, Intent data,
728 CellLayout.CellInfo cellInfo, boolean notify) {
729
Romain Guy73b979d2009-06-09 12:57:21 -0700730 final ApplicationInfo info = infoFromShortcutIntent(context, data);
731 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
732 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
733
734 return info;
735 }
736
737 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
739 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
740 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
741
742 Drawable icon = null;
743 boolean filtered = false;
744 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700745 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746
747 if (bitmap != null) {
748 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
749 filtered = true;
750 customIcon = true;
751 } else {
752 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700753 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700755 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 final PackageManager packageManager = context.getPackageManager();
757 Resources resources = packageManager.getResourcesForApplication(
758 iconResource.packageName);
759 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
760 icon = resources.getDrawable(id);
761 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700762 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
764 }
765 }
766
767 if (icon == null) {
768 icon = context.getPackageManager().getDefaultActivityIcon();
769 }
770
771 final ApplicationInfo info = new ApplicationInfo();
772 info.icon = icon;
773 info.filtered = filtered;
774 info.title = name;
775 info.intent = intent;
776 info.customIcon = customIcon;
777 info.iconResource = iconResource;
778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 return info;
780 }
781
782 @Override
783 protected void onNewIntent(Intent intent) {
784 super.onNewIntent(intent);
785
786 // Close the menu
787 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
788 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700789
Joe Onorato742966b2009-10-07 10:26:17 -0700790 // Whatever we were doing is hereby canceled.
791 mWaitingForResult = false;
792
Mike LeBeau736cf282009-07-02 17:46:59 -0700793 // Set this flag so that onResume knows to close the search dialog if it's open,
794 // because this was a new intent (thus a press of 'home' or some such) rather than
795 // for example onResume being called when the user pressed the 'back' button.
796 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797
798 try {
799 dismissDialog(DIALOG_CREATE_SHORTCUT);
800 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 } catch (Exception e) {
802 // An exception is thrown if the dialog is not visible, which is fine
803 }
804
805 try {
806 dismissDialog(DIALOG_RENAME_FOLDER);
807 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 } catch (Exception e) {
809 // An exception is thrown if the dialog is not visible, which is fine
810 }
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
813 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700814
Romain Guy94dabf12009-07-21 10:55:43 -0700815 if (!mWorkspace.isDefaultScreenShowing()) {
816 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700818
Joe Onorato7bb17492009-09-24 17:51:01 -0700819 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700820
821 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 if (v != null && v.getWindowToken() != null) {
823 InputMethodManager imm = (InputMethodManager)getSystemService(
824 INPUT_METHOD_SERVICE);
825 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
826 }
827 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700828 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
830 }
831 }
832
833 @Override
834 protected void onRestoreInstanceState(Bundle savedInstanceState) {
835 // Do not call super here
836 mSavedInstanceState = savedInstanceState;
837 }
838
839 @Override
840 protected void onSaveInstanceState(Bundle outState) {
841 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
842
843 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
844 if (folders.size() > 0) {
845 final int count = folders.size();
846 long[] ids = new long[count];
847 for (int i = 0; i < count; i++) {
848 final FolderInfo info = folders.get(i).getInfo();
849 ids[i] = info.id;
850 }
851 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
852 } else {
853 super.onSaveInstanceState(outState);
854 }
855
Romain Guy3cf604f2009-06-16 13:12:53 -0700856 final boolean isConfigurationChange = getChangingConfigurations() != 0;
857
Romain Guy5a941392009-04-28 15:18:25 -0700858 // When the drawer is opened and we are saving the state because of a
859 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400860 // TODO should not do this if the drawer is currently closing.
861 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864
865 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
866 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
867 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
868
869 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
870 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
876 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
877 layout.getOccupiedCells());
878 }
879
880 if (mFolderInfo != null && mWaitingForResult) {
881 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
882 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
883 }
884 }
885
886 @Override
887 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700891 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700893 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
896 TextKeyListener.getInstance().release();
897
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 unbindDesktopItems();
901 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
903
904 @Override
905 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700906 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 super.startActivityForResult(intent, requestCode);
908 }
909
910 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700911 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700913
Joe Onorato7bb17492009-09-24 17:51:01 -0700914 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700915
Karl Rosaen138a0412009-04-23 19:00:21 -0700916 // Slide the search widget to the top, if it's on the current screen,
917 // otherwise show the search dialog immediately.
918 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
919 if (searchWidget == null) {
920 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
921 } else {
922 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
923 // show the currently typed text in the search widget while sliding
924 searchWidget.setQuery(getTypedText());
925 }
926 }
Romain Guycbb89e42009-06-08 15:52:54 -0700927
Karl Rosaen138a0412009-04-23 19:00:21 -0700928 /**
929 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700930 *
931 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700932 */
Romain Guycbb89e42009-06-08 15:52:54 -0700933 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700934 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700935
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 if (initialQuery == null) {
937 // Use any text typed in the launcher as the initial query
938 initialQuery = getTypedText();
939 clearTypedText();
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 if (appSearchData == null) {
942 appSearchData = new Bundle();
943 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
944 }
Romain Guycbb89e42009-06-08 15:52:54 -0700945
Karl Rosaen138a0412009-04-23 19:00:21 -0700946 final SearchManager searchManager =
947 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
948
949 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
950 if (searchWidget != null) {
951 // This gets called when the user leaves the search dialog to go back to
952 // the Launcher.
953 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
954 public void onCancel() {
955 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700956 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700957 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700958 });
959 }
Romain Guycbb89e42009-06-08 15:52:54 -0700960
Karl Rosaen138a0412009-04-23 19:00:21 -0700961 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700962 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
964
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 /**
966 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 */
Romain Guy5a941392009-04-28 15:18:25 -0700968 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 // Close search dialog
970 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100971 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 // Restore search widget to its normal position
973 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
974 if (searchWidget != null) {
975 searchWidget.stopSearch(false);
976 }
977 }
Romain Guycbb89e42009-06-08 15:52:54 -0700978
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 @Override
980 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 if (isWorkspaceLocked()) {
982 return false;
983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984
985 super.onCreateOptionsMenu(menu);
986 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
987 .setIcon(android.R.drawable.ic_menu_add)
988 .setAlphabeticShortcut('A');
989 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
990 .setIcon(android.R.drawable.ic_menu_gallery)
991 .setAlphabeticShortcut('W');
992 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
993 .setIcon(android.R.drawable.ic_search_category_default)
994 .setAlphabeticShortcut(SearchManager.MENU_KEY);
995 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
996 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
997 .setAlphabeticShortcut('N');
998
999 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001000 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1001 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002
1003 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1004 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1005 .setIntent(settings);
1006
1007 return true;
1008 }
1009
1010 @Override
1011 public boolean onPrepareOptionsMenu(Menu menu) {
1012 super.onPrepareOptionsMenu(menu);
1013
1014 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1015 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1016
1017 return true;
1018 }
1019
1020 @Override
1021 public boolean onOptionsItemSelected(MenuItem item) {
1022 switch (item.getItemId()) {
1023 case MENU_ADD:
1024 addItems();
1025 return true;
1026 case MENU_WALLPAPER_SETTINGS:
1027 startWallpaper();
1028 return true;
1029 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001030 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 return true;
1032 case MENU_NOTIFICATIONS:
1033 showNotifications();
1034 return true;
1035 }
1036
1037 return super.onOptionsItemSelected(item);
1038 }
Romain Guycbb89e42009-06-08 15:52:54 -07001039
Karl Rosaen138a0412009-04-23 19:00:21 -07001040 /**
1041 * Indicates that we want global search for this activity by setting the globalSearch
1042 * argument for {@link #startSearch} to true.
1043 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001045 @Override
1046 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001047 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001048 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001049 }
1050
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 public boolean isWorkspaceLocked() {
1052 return mWorkspaceLoading || mWaitingForResult;
1053 }
1054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 private void addItems() {
1056 showAddDialog(mMenuAddInfo);
1057 }
1058
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 void addAppWidget(Intent data) {
1060 // TODO: catch bad widget exception when sent
1061 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001063 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1064 if (SEARCH_WIDGET.equals(customWidget)) {
1065 // We don't need this any more, since this isn't a real app widget.
1066 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1067 // add the search widget
1068 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001070 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1071
1072 if (appWidget.configure != null) {
1073 // Launch over to configure widget, if needed
1074 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1075 intent.setComponent(appWidget.configure);
1076 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1077
1078 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1079 } else {
1080 // Otherwise just add it
1081 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
1084 }
Romain Guycbb89e42009-06-08 15:52:54 -07001085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 void addSearch() {
1087 final Widget info = Widget.makeSearch();
1088 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 final int[] xy = mCellCoordinates;
1091 final int spanX = info.spanX;
1092 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1097 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 final View view = mInflater.inflate(info.layoutResource, null);
1100 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001101 Search search = (Search) view.findViewById(R.id.widget_search);
1102 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1105 }
1106
Romain Guy73b979d2009-06-09 12:57:21 -07001107 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001108 // Handle case where user selected "Applications"
1109 String applicationName = getResources().getString(R.string.group_applications);
1110 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001112 if (applicationName != null && applicationName.equals(shortcutName)) {
1113 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1114 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001116 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1117 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001118 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001119 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001120 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 }
1123
1124 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001125 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001126 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 } else {
1132 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
1135
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 UserFolderInfo folderInfo = new UserFolderInfo();
1138 folderInfo.title = getText(R.string.folder_name);
1139
1140 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1141 cellInfo.screen = mWorkspace.getCurrentScreen();
1142 if (!findSingleSlot(cellInfo)) return;
1143
1144 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145 LauncherModel.addItemToDatabase(this, folderInfo,
1146 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
1150 // Create the view
1151 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1152 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1153 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001154 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Joe Onorato9c1289c2009-08-17 11:03:03 -04001157 void removeFolder(FolderInfo folder) {
1158 mFolders.remove(folder.id);
1159 }
1160
1161 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 cellInfo.screen = mWorkspace.getCurrentScreen();
1163 if (!findSingleSlot(cellInfo)) return;
1164
1165 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1166
1167 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1169 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001170 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1171 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 }
1173 }
1174
1175 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1176 CellLayout.CellInfo cellInfo, boolean notify) {
1177
1178 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1179 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1180
1181 Drawable icon = null;
1182 boolean filtered = false;
1183 Intent.ShortcutIconResource iconResource = null;
1184
1185 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1186 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1187 try {
1188 iconResource = (Intent.ShortcutIconResource) extra;
1189 final PackageManager packageManager = context.getPackageManager();
1190 Resources resources = packageManager.getResourcesForApplication(
1191 iconResource.packageName);
1192 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1193 icon = resources.getDrawable(id);
1194 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001195 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
1197 }
1198
1199 if (icon == null) {
1200 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1201 }
1202
1203 final LiveFolderInfo info = new LiveFolderInfo();
1204 info.icon = icon;
1205 info.filtered = filtered;
1206 info.title = name;
1207 info.iconResource = iconResource;
1208 info.uri = data.getData();
1209 info.baseIntent = baseIntent;
1210 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1211 LiveFolders.DISPLAY_MODE_GRID);
1212
1213 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1214 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
1217 return info;
1218 }
1219
1220 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1221 final int[] xy = new int[2];
1222 if (findSlot(cellInfo, xy, 1, 1)) {
1223 cellInfo.cellX = xy[0];
1224 cellInfo.cellY = xy[1];
1225 return true;
1226 }
1227 return false;
1228 }
1229
1230 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1231 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1232 boolean[] occupied = mSavedState != null ?
1233 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1234 cellInfo = mWorkspace.findAllVacantCells(occupied);
1235 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1236 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1237 return false;
1238 }
1239 }
1240 return true;
1241 }
1242
1243 private void showNotifications() {
1244 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1245 if (statusBar != null) {
1246 statusBar.expand();
1247 }
1248 }
1249
1250 private void startWallpaper() {
1251 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001252 Intent chooser = Intent.createChooser(pickWallpaper,
1253 getText(R.string.chooser_wallpaper));
1254 WallpaperManager wm = (WallpaperManager)
1255 getSystemService(Context.WALLPAPER_SERVICE);
1256 WallpaperInfo wi = wm.getWallpaperInfo();
1257 if (wi != null && wi.getSettingsActivity() != null) {
1258 LabeledIntent li = new LabeledIntent(getPackageName(),
1259 R.string.configure_wallpaper, 0);
1260 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1261 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1262 }
Mike Clerona0618e42009-10-22 13:55:21 -07001263 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 @Override
1267 public boolean dispatchKeyEvent(KeyEvent event) {
1268 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1269 switch (event.getKeyCode()) {
1270 case KeyEvent.KEYCODE_BACK:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 return true;
1272 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001273 return true;
1274 }
1275 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1276 switch (event.getKeyCode()) {
1277 case KeyEvent.KEYCODE_BACK:
1278 if (!event.isCanceled()) {
1279 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001280 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001281 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001282 } else {
1283 closeFolder();
1284 }
1285 }
Dianne Hackborn67800862009-07-24 17:15:20 -07001286 return true;
1287 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 return true;
1289 }
1290 }
1291
1292 return super.dispatchKeyEvent(event);
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 private void closeFolder() {
1296 Folder folder = mWorkspace.getOpenFolder();
1297 if (folder != null) {
1298 closeFolder(folder);
1299 }
1300 }
1301
1302 void closeFolder(Folder folder) {
1303 folder.getInfo().opened = false;
1304 ViewGroup parent = (ViewGroup) folder.getParent();
1305 if (parent != null) {
1306 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001307 if (folder instanceof DropTarget) {
1308 // Live folders aren't DropTargets.
1309 mDragController.removeDropTarget((DropTarget)folder);
1310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312 folder.onClose();
1313 }
1314
1315 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001316 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1317 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319 private void unbindDesktopItems() {
1320 for (ItemInfo item: mDesktopItems) {
1321 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 /**
1326 * Launches the intent referred by the clicked shortcut.
1327 *
1328 * @param v The view representing the clicked shortcut.
1329 */
1330 public void onClick(View v) {
1331 Object tag = v.getTag();
1332 if (tag instanceof ApplicationInfo) {
1333 // Open shortcut
1334 final Intent intent = ((ApplicationInfo) tag).intent;
1335 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001336 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 } else if (tag instanceof FolderInfo) {
1338 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001339 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001340 Log.d(TAG, "onClick");
1341 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001342 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001343 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001344 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347 }
1348
1349 void startActivitySafely(Intent intent) {
1350 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1351 try {
1352 startActivity(intent);
1353 } catch (ActivityNotFoundException e) {
1354 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1355 } catch (SecurityException e) {
1356 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001357 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1359 "or use the exported attribute for this activity.", e);
1360 }
1361 }
1362
1363 private void handleFolderClick(FolderInfo folderInfo) {
1364 if (!folderInfo.opened) {
1365 // Close any open folder
1366 closeFolder();
1367 // Open the requested folder
1368 openFolder(folderInfo);
1369 } else {
1370 // Find the open folder...
1371 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1372 int folderScreen;
1373 if (openFolder != null) {
1374 folderScreen = mWorkspace.getScreenForView(openFolder);
1375 // .. and close it
1376 closeFolder(openFolder);
1377 if (folderScreen != mWorkspace.getCurrentScreen()) {
1378 // Close any folder open on the current screen
1379 closeFolder();
1380 // Pull the folder onto this screen
1381 openFolder(folderInfo);
1382 }
1383 }
1384 }
1385 }
1386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 /**
1388 * Opens the user fodler described by the specified tag. The opening of the folder
1389 * is animated relative to the specified View. If the View is null, no animation
1390 * is played.
1391 *
1392 * @param folderInfo The FolderInfo describing the folder to open.
1393 */
1394 private void openFolder(FolderInfo folderInfo) {
1395 Folder openFolder;
1396
1397 if (folderInfo instanceof UserFolderInfo) {
1398 openFolder = UserFolder.fromXml(this);
1399 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001400 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 } else {
1402 return;
1403 }
1404
Joe Onorato00acb122009-08-04 16:04:30 -04001405 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 openFolder.setLauncher(this);
1407
1408 openFolder.bind(folderInfo);
1409 folderInfo.opened = true;
1410
1411 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1412 openFolder.onOpen();
1413 }
1414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 return false;
1418 }
1419
1420 if (!(v instanceof CellLayout)) {
1421 v = (View) v.getParent();
1422 }
1423
1424 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1425
1426 // This happens when long clicking an item with the dpad/trackball
1427 if (cellInfo == null) {
1428 return true;
1429 }
1430
1431 if (mWorkspace.allowLongPress()) {
1432 if (cellInfo.cell == null) {
1433 if (cellInfo.valid) {
1434 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001435 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 showAddDialog(cellInfo);
1437 }
1438 } else {
1439 if (!(cellInfo.cell instanceof Folder)) {
1440 // User long pressed on an item
1441 mWorkspace.startDrag(cellInfo);
1442 }
1443 }
1444 }
1445 return true;
1446 }
1447
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001448 View getDrawerHandle() {
1449 return mHandleView;
1450 }
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 Workspace getWorkspace() {
1453 return mWorkspace;
1454 }
1455
Joe Onorato7404ee42009-07-31 11:54:44 -07001456 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 GridView getApplicationsGrid() {
1458 return mAllAppsGrid;
1459 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001460 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461
1462 @Override
1463 protected Dialog onCreateDialog(int id) {
1464 switch (id) {
1465 case DIALOG_CREATE_SHORTCUT:
1466 return new CreateShortcut().createDialog();
1467 case DIALOG_RENAME_FOLDER:
1468 return new RenameFolder().createDialog();
1469 }
1470
1471 return super.onCreateDialog(id);
1472 }
1473
1474 @Override
1475 protected void onPrepareDialog(int id, Dialog dialog) {
1476 switch (id) {
1477 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 break;
1479 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001480 if (mFolderInfo != null) {
1481 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1482 final CharSequence text = mFolderInfo.title;
1483 input.setText(text);
1484 input.setSelection(0, text.length());
1485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 break;
1487 }
1488 }
1489
1490 void showRenameDialog(FolderInfo info) {
1491 mFolderInfo = info;
1492 mWaitingForResult = true;
1493 showDialog(DIALOG_RENAME_FOLDER);
1494 }
1495
1496 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1497 mAddItemCellInfo = cellInfo;
1498 mWaitingForResult = true;
1499 showDialog(DIALOG_CREATE_SHORTCUT);
1500 }
1501
Romain Guy73b979d2009-06-09 12:57:21 -07001502 private void pickShortcut(int requestCode, int title) {
1503 Bundle bundle = new Bundle();
1504
1505 ArrayList<String> shortcutNames = new ArrayList<String>();
1506 shortcutNames.add(getString(R.string.group_applications));
1507 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1508
1509 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1510 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1511 R.drawable.ic_launcher_application));
1512 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1513
1514 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1515 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1516 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1517 pickIntent.putExtras(bundle);
1518
1519 startActivityForResult(pickIntent, requestCode);
1520 }
1521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 private class RenameFolder {
1523 private EditText mInput;
1524
1525 Dialog createDialog() {
1526 mWaitingForResult = true;
1527 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1528 mInput = (EditText) layout.findViewById(R.id.folder_name);
1529
1530 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1531 builder.setIcon(0);
1532 builder.setTitle(getString(R.string.rename_folder_title));
1533 builder.setCancelable(true);
1534 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1535 public void onCancel(DialogInterface dialog) {
1536 cleanup();
1537 }
1538 });
1539 builder.setNegativeButton(getString(R.string.cancel_action),
1540 new Dialog.OnClickListener() {
1541 public void onClick(DialogInterface dialog, int which) {
1542 cleanup();
1543 }
1544 }
1545 );
1546 builder.setPositiveButton(getString(R.string.rename_action),
1547 new Dialog.OnClickListener() {
1548 public void onClick(DialogInterface dialog, int which) {
1549 changeFolderName();
1550 }
1551 }
1552 );
1553 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001554
1555 final AlertDialog dialog = builder.create();
1556 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1557 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001558 }
1559 });
1560
1561 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563
1564 private void changeFolderName() {
1565 final String name = mInput.getText().toString();
1566 if (!TextUtils.isEmpty(name)) {
1567 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 mFolderInfo.title = name;
1570 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1571
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001573 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001574 mModel.setWorkspaceDirty();
1575 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 } else {
1577 final FolderIcon folderIcon = (FolderIcon)
1578 mWorkspace.getViewForTag(mFolderInfo);
1579 if (folderIcon != null) {
1580 folderIcon.setText(name);
1581 getWorkspace().requestLayout();
1582 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001583 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584 mModel.setWorkspaceDirty();
1585 mWorkspaceLoading = true;
1586 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588 }
1589 }
1590 cleanup();
1591 }
1592
1593 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 dismissDialog(DIALOG_RENAME_FOLDER);
1595 mWaitingForResult = false;
1596 mFolderInfo = null;
1597 }
1598 }
1599
Joe Onoratofb0ca672009-09-14 17:55:46 -04001600 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001601 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001602 }
1603
Jason Samsfd22dac2009-09-20 17:24:16 -07001604 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001605 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001606 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001607
Mike Cleronb6082fa02009-10-19 17:03:36 -07001608 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001609 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001610
Joe Onorato7c312c12009-08-13 21:36:53 -07001611 // TODO: fade these two too
1612 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001613 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001614 }
1615
Joe Onorato7bb17492009-09-24 17:51:01 -07001616 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001617 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001618 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001619 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001620 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001621
Mike Cleronb6082fa02009-10-19 17:03:36 -07001622
Joe Onorato7c312c12009-08-13 21:36:53 -07001623 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001624 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001625 mDeleteZone.setVisibility(View.VISIBLE);
1626 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001627 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001628 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001629 }
1630
Joe Onorato7c312c12009-08-13 21:36:53 -07001631 void lockAllApps() {
1632 // TODO
1633 }
1634
1635 void unlockAllApps() {
1636 // TODO
1637 }
1638
Joe Onorato7404ee42009-07-31 11:54:44 -07001639 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 * Displays the shortcut creation dialog and launches, if necessary, the
1641 * appropriate activity.
1642 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001643 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001644 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1645 DialogInterface.OnShowListener {
1646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 Dialog createDialog() {
1650 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1655 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 builder.setInverseBackgroundForced(true);
1659
1660 AlertDialog dialog = builder.create();
1661 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001662 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001663 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 return dialog;
1666 }
1667
1668 public void onCancel(DialogInterface dialog) {
1669 mWaitingForResult = false;
1670 cleanup();
1671 }
1672
Romain Guycbb89e42009-06-08 15:52:54 -07001673 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 dismissDialog(DIALOG_CREATE_SHORTCUT);
1678 }
1679
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001680 /**
1681 * Handle the action clicked in the "Add to home" dialog.
1682 */
1683 public void onClick(DialogInterface dialog, int which) {
1684 Resources res = getResources();
1685 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 switch (which) {
1688 case AddAdapter.ITEM_SHORTCUT: {
1689 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001690 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001691 break;
1692 }
Romain Guycbb89e42009-06-08 15:52:54 -07001693
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001694 case AddAdapter.ITEM_APPWIDGET: {
1695 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001696
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1698 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1699 // add the search widget
1700 ArrayList<AppWidgetProviderInfo> customInfo =
1701 new ArrayList<AppWidgetProviderInfo>();
1702 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1703 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1704 info.label = getString(R.string.group_search);
1705 info.icon = R.drawable.ic_search_widget;
1706 customInfo.add(info);
1707 pickIntent.putParcelableArrayListExtra(
1708 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1709 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1710 Bundle b = new Bundle();
1711 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1712 customExtras.add(b);
1713 pickIntent.putParcelableArrayListExtra(
1714 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1715 // start the pick activity
1716 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1717 break;
1718 }
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001720 case AddAdapter.ITEM_LIVE_FOLDER: {
1721 // Insert extra item to handle inserting folder
1722 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001723
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001724 ArrayList<String> shortcutNames = new ArrayList<String>();
1725 shortcutNames.add(res.getString(R.string.group_folder));
1726 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001727
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001728 ArrayList<ShortcutIconResource> shortcutIcons =
1729 new ArrayList<ShortcutIconResource>();
1730 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1731 R.drawable.ic_launcher_folder));
1732 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1733
1734 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1735 pickIntent.putExtra(Intent.EXTRA_INTENT,
1736 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1737 pickIntent.putExtra(Intent.EXTRA_TITLE,
1738 getText(R.string.title_select_live_folder));
1739 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001740
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001741 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1742 break;
1743 }
1744
1745 case AddAdapter.ITEM_WALLPAPER: {
1746 startWallpaper();
1747 break;
1748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001751
1752 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755
1756 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 public int getCurrentWorkspaceScreen() {
1760 return mWorkspace.getCurrentScreen();
1761 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001762
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 /**
1764 * Refreshes the shortcuts shown on the workspace.
1765 *
1766 * Implementation of the method from LauncherModel.Callbacks.
1767 */
1768 public void startBinding() {
1769 final Workspace workspace = mWorkspace;
1770 int count = workspace.getChildCount();
1771 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04001772 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1774 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001775
Joe Onorato9c1289c2009-08-17 11:03:03 -04001776 if (DEBUG_USER_INTERFACE) {
1777 android.widget.Button finishButton = new android.widget.Button(this);
1778 finishButton.setText("Finish");
1779 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1780
1781 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1782 public void onClick(View v) {
1783 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001784 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787 }
1788
1789 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001790 * Bind the items start-end from the list.
1791 *
1792 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1795
1796 final Workspace workspace = mWorkspace;
1797
1798 for (int i=start; i<end; i++) {
1799 final ItemInfo item = shortcuts.get(i);
1800 mDesktopItems.add(item);
1801 switch (item.itemType) {
1802 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1803 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1804 final View shortcut = createShortcut((ApplicationInfo) item);
1805 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1806 false);
1807 break;
1808 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1809 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1810 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1811 (UserFolderInfo) item);
1812 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1813 false);
1814 break;
1815 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1816 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1817 R.layout.live_folder_icon, this,
1818 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1819 (LiveFolderInfo) item);
1820 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1821 false);
1822 break;
1823 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1824 final int screen = workspace.getCurrentScreen();
1825 final View view = mInflater.inflate(R.layout.widget_search,
1826 (ViewGroup) workspace.getChildAt(screen), false);
1827
1828 Search search = (Search) view.findViewById(R.id.widget_search);
1829 search.setLauncher(this);
1830
1831 final Widget widget = (Widget) item;
1832 view.setTag(widget);
1833
1834 workspace.addWidget(view, widget, false);
1835 break;
1836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
1838
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 /**
1843 * Implementation of the method from LauncherModel.Callbacks.
1844 */
Joe Onoratoad72e172009-11-06 16:25:04 -05001845 public void bindFolders(HashMap<Long, FolderInfo> folders) {
1846 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849
1850 /**
1851 * Add the views for a widget to the workspace.
1852 *
1853 * Implementation of the method from LauncherModel.Callbacks.
1854 */
1855 public void bindAppWidget(LauncherAppWidgetInfo item) {
1856 final Workspace workspace = mWorkspace;
1857
1858 final int appWidgetId = item.appWidgetId;
1859 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1860 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1861
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1863 item.hostView.setTag(item);
1864
1865 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1866 item.cellY, item.spanX, item.spanY, false);
1867
1868 workspace.requestLayout();
1869
1870 mDesktopItems.add(item);
1871 }
1872
1873 /**
1874 * Callback saying that there aren't any more items to bind.
1875 *
1876 * Implementation of the method from LauncherModel.Callbacks.
1877 */
1878 public void finishBindingItems() {
1879 if (mSavedState != null) {
1880 if (!mWorkspace.hasFocus()) {
1881 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1882 }
1883
1884 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1885 if (userFolders != null) {
1886 for (long folderId : userFolders) {
1887 final FolderInfo info = mFolders.get(folderId);
1888 if (info != null) {
1889 openFolder(info);
1890 }
1891 }
1892 final Folder openFolder = mWorkspace.getOpenFolder();
1893 if (openFolder != null) {
1894 openFolder.requestFocus();
1895 }
1896 }
1897
1898 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1899 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001900 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001901 }
1902
1903 mSavedState = null;
1904 }
1905
1906 if (mSavedInstanceState != null) {
1907 super.onRestoreInstanceState(mSavedInstanceState);
1908 mSavedInstanceState = null;
1909 }
1910
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 Log.d(TAG, "finishBindingItems done");
1912 mWorkspaceLoading = false;
1913 }
1914
1915 /**
1916 * Add the icons for all apps.
1917 *
1918 * Implementation of the method from LauncherModel.Callbacks.
1919 */
1920 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08001921 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001922 }
1923
1924 /**
1925 * A package was installed.
1926 *
1927 * Implementation of the method from LauncherModel.Callbacks.
1928 */
1929 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1930 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001931 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001932 }
1933
1934 /**
1935 * A package was updated.
1936 *
1937 * Implementation of the method from LauncherModel.Callbacks.
1938 */
1939 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1940 removeDialog(DIALOG_CREATE_SHORTCUT);
1941 mWorkspace.updateShortcutsForPackage(packageName);
1942 }
1943
1944 /**
1945 * A package was uninstalled.
1946 *
1947 * Implementation of the method from LauncherModel.Callbacks.
1948 */
1949 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1950 removeDialog(DIALOG_CREATE_SHORTCUT);
1951 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001952 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954}