blob: 6fb8e4c9629888afdcea82230817a3cb31a23f4c [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;
41import android.graphics.drawable.TransitionDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.os.Bundle;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070043import android.os.Looper;
Karl Rosaen138a0412009-04-23 19:00:21 -070044import android.os.Message;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070045import android.os.MessageQueue;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.ServiceManager;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.text.Selection;
51import android.text.SpannableStringBuilder;
52import android.text.TextUtils;
53import android.text.method.TextKeyListener;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070054import static android.util.Log.*;
Joe Onorato7c312c12009-08-13 21:36:53 -070055import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.Display;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.KeyEvent;
58import android.view.LayoutInflater;
59import android.view.Menu;
60import android.view.MenuItem;
61import android.view.View;
62import android.view.ViewGroup;
Romain Guyb1b69f52009-08-10 15:10:15 -070063import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.view.View.OnLongClickListener;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.TextView;
68import android.widget.Toast;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070069import android.appwidget.AppWidgetManager;
70import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071
72import java.lang.ref.WeakReference;
73import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040074import java.util.HashMap;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -070075import java.util.LinkedList;
Romain Guy98d01652009-06-30 16:21:04 -070076import java.io.DataOutputStream;
77import java.io.FileNotFoundException;
78import java.io.IOException;
79import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080
81/**
82 * Default launcher application.
83 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040084public final class Launcher extends Activity
Joe Onorato7bb17492009-09-24 17:51:01 -070085 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 static final String LOG_TAG = "Launcher";
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 static final String TAG = LOG_TAG;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088 static final boolean LOGD = false;
89
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static final boolean PROFILE_STARTUP = false;
91 static final boolean PROFILE_ROTATE = false;
92 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070093
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094 private static final int WALLPAPER_SCREENS_SPAN = 2;
95
96 private static final int MENU_GROUP_ADD = 1;
97 private static final int MENU_ADD = Menu.FIRST + 1;
98 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
99 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
100 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700101 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102
103 private static final int REQUEST_CREATE_SHORTCUT = 1;
104 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700105 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 private static final int REQUEST_PICK_APPLICATION = 6;
107 private static final int REQUEST_PICK_SHORTCUT = 7;
108 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700109 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700110 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
112 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
113
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700114 static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
115 static final String SEARCH_WIDGET = "search_widget";
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 static final int SCREEN_COUNT = 3;
118 static final int DEFAULT_SCREN = 1;
119 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700120 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Joe Onorato7c312c12009-08-13 21:36:53 -0700122 static final int DIALOG_CREATE_SHORTCUT = 1;
123 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Romain Guy98d01652009-06-30 16:21:04 -0700125 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 // Type: int
128 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
129 // Type: boolean
130 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
131 // Type: long
132 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
133 // Type: int
134 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
147 // Type: int[]
148 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 private static final Object sLock = new Object();
157 private static int sScreen = DEFAULT_SCREN;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private LayoutInflater mInflater;
160
Joe Onorato00acb122009-08-04 16:04:30 -0400161 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700163
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700164 private AppWidgetManager mAppWidgetManager;
165 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private CellLayout.CellInfo mAddItemCellInfo;
168 private CellLayout.CellInfo mMenuAddInfo;
169 private final int[] mCellCoordinates = new int[2];
170 private FolderInfo mFolderInfo;
171
Joe Onorato7c312c12009-08-13 21:36:53 -0700172 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700173 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700174 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Bundle mSavedState;
177
178 private SpannableStringBuilder mDefaultKeySsb = null;
179
Mike LeBeau736cf282009-07-02 17:46:59 -0700180 private boolean mIsNewIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 private boolean mWorkspaceLoading = true;
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private boolean mRestoring;
185 private boolean mWaitingForResult;
186 private boolean mLocaleChanged;
Joe Onoratoff3862d2009-10-07 10:35:51 -0700187 private boolean mExitingBecauseOfLaunch;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Dianne Hackborn67800862009-07-24 17:15:20 -0700189 private boolean mHomeDown;
190 private boolean mBackDown;
Jason Samsfd22dac2009-09-20 17:24:16 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private Bundle mSavedInstanceState;
193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 private LauncherModel mModel;
195
196 private ArrayList<ItemInfo> mDesktopItems = new ArrayList();
197 private static HashMap<Long, FolderInfo> mFolders = new HashMap();
198 private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 @Override
201 protected void onCreate(Bundle savedInstanceState) {
202 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 mModel = ((LauncherApplication)getApplication()).setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700206
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700207 mAppWidgetManager = AppWidgetManager.getInstance(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700208
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
210 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 if (PROFILE_STARTUP) {
213 android.os.Debug.startMethodTracing("/sdcard/launcher");
214 }
215
216 checkForLocaleChange();
217 setWallpaperDimension();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 setContentView(R.layout.launcher);
220 setupViews();
221
Joe Onorato7c312c12009-08-13 21:36:53 -0700222 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 mSavedState = savedInstanceState;
225 restoreState(mSavedState);
226
227 if (PROFILE_STARTUP) {
228 android.os.Debug.stopMethodTracing();
229 }
230
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700231 // We have a new AllAppsView, we need to re-bind everything, and it could have
232 // changed in our absence.
233 mModel.setAllAppsDirty();
234 mModel.setWorkspaceDirty();
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 }
239
240 // For handling default keys
241 mDefaultKeySsb = new SpannableStringBuilder();
242 Selection.setSelection(mDefaultKeySsb, 0);
243 }
Romain Guycbb89e42009-06-08 15:52:54 -0700244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700246 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
247 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 final Configuration configuration = getResources().getConfiguration();
250
Romain Guy98d01652009-06-30 16:21:04 -0700251 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 final String locale = configuration.locale.toString();
253
Romain Guy98d01652009-06-30 16:21:04 -0700254 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 final int mcc = configuration.mcc;
256
Romain Guy98d01652009-06-30 16:21:04 -0700257 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 final int mnc = configuration.mnc;
259
260 mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
261
262 if (mLocaleChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700263 localeConfiguration.locale = locale;
264 localeConfiguration.mcc = mcc;
265 localeConfiguration.mnc = mnc;
266
267 writeConfiguration(this, localeConfiguration);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 AppInfoCache.flush();
Romain Guy98d01652009-06-30 16:21:04 -0700269 }
270 }
271
272 private static class LocaleConfiguration {
273 public String locale;
274 public int mcc = -1;
275 public int mnc = -1;
276 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700277
Romain Guy98d01652009-06-30 16:21:04 -0700278 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
279 DataInputStream in = null;
280 try {
281 in = new DataInputStream(context.openFileInput(PREFERENCES));
282 configuration.locale = in.readUTF();
283 configuration.mcc = in.readInt();
284 configuration.mnc = in.readInt();
285 } catch (FileNotFoundException e) {
286 // Ignore
287 } catch (IOException e) {
288 // Ignore
289 } finally {
290 if (in != null) {
291 try {
292 in.close();
293 } catch (IOException e) {
294 // Ignore
295 }
296 }
297 }
298 }
299
300 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
301 DataOutputStream out = null;
302 try {
303 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
304 out.writeUTF(configuration.locale);
305 out.writeInt(configuration.mcc);
306 out.writeInt(configuration.mnc);
307 out.flush();
308 } catch (FileNotFoundException e) {
309 // Ignore
310 } catch (IOException e) {
311 //noinspection ResultOfMethodCallIgnored
312 context.getFileStreamPath(PREFERENCES).delete();
313 } finally {
314 if (out != null) {
315 try {
316 out.close();
317 } catch (IOException e) {
318 // Ignore
319 }
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
322 }
323
324 static int getScreen() {
325 synchronized (sLock) {
326 return sScreen;
327 }
328 }
329
330 static void setScreen(int screen) {
331 synchronized (sLock) {
332 sScreen = screen;
333 }
334 }
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700337 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338
339 Display display = getWindowManager().getDefaultDisplay();
340 boolean isPortrait = display.getWidth() < display.getHeight();
341
342 final int width = isPortrait ? display.getWidth() : display.getHeight();
343 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700344 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
346
347 @Override
348 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700349 mWaitingForResult = false;
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 // The pattern used here is that a user PICKs a specific application,
352 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
355 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700356
Romain Guy94dabf12009-07-21 10:55:43 -0700357 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 switch (requestCode) {
359 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400360 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 break;
362 case REQUEST_PICK_SHORTCUT:
Romain Guy73b979d2009-06-09 12:57:21 -0700363 processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 break;
365 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 break;
368 case REQUEST_PICK_LIVE_FOLDER:
369 addLiveFolder(data);
370 break;
371 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700374 case REQUEST_PICK_APPWIDGET:
375 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700380 case REQUEST_PICK_WALLPAPER:
381 // We just wanted the activity result here so we can clear mWaitingForResult
382 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 } else if (requestCode == REQUEST_PICK_APPWIDGET &&
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 resultCode == RESULT_CANCELED && data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700386 // Clean up the appWidgetId if we canceled
387 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
388 if (appWidgetId != -1) {
389 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
393
394 @Override
395 protected void onResume() {
396 super.onResume();
397
398 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 mWorkspaceLoading = true;
400 mModel.startLoader(this, true);
401 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700403
Mike LeBeau736cf282009-07-02 17:46:59 -0700404 // If this was a new intent (i.e., the mIsNewIntent flag got set to true by
405 // onNewIntent), then close the search dialog if needed, because it probably
406 // came from the user pressing 'home' (rather than, for example, pressing 'back').
407 if (mIsNewIntent) {
408 // Post to a handler so that this happens after the search dialog tries to open
409 // itself again.
410 mWorkspace.post(new Runnable() {
411 public void run() {
412 ISearchManager searchManagerService = ISearchManager.Stub.asInterface(
413 ServiceManager.getService(Context.SEARCH_SERVICE));
414 try {
415 searchManagerService.stopSearch();
416 } catch (RemoteException e) {
417 e(LOG_TAG, "error stopping search", e);
Jason Samsfd22dac2009-09-20 17:24:16 -0700418 }
Mike LeBeau736cf282009-07-02 17:46:59 -0700419 }
420 });
421 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700422
Mike LeBeau736cf282009-07-02 17:46:59 -0700423 mIsNewIntent = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 protected void onPause() {
428 super.onPause();
Joe Onoratoff3862d2009-10-07 10:35:51 -0700429 if (mExitingBecauseOfLaunch) {
430 mExitingBecauseOfLaunch = false;
431 closeAllApps(false);
432 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 }
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700435 @Override
436 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400437 // Flag the loader to stop early before switching
438 mModel.stopLoader();
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700440 if (PROFILE_ROTATE) {
441 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
442 }
443 return null;
444 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 private boolean acceptFilter() {
447 final InputMethodManager inputManager = (InputMethodManager)
448 getSystemService(Context.INPUT_METHOD_SERVICE);
449 return !inputManager.isFullscreenMode();
450 }
451
452 @Override
453 public boolean onKeyDown(int keyCode, KeyEvent event) {
454 boolean handled = super.onKeyDown(keyCode, event);
455 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
456 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
457 keyCode, event);
458 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700459 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700460 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700461 // showSearchDialog()
462 // If there are multiple keystrokes before the search dialog takes focus,
463 // onSearchRequested() will be called for every keystroke,
464 // but it is idempotent, so it's fine.
465 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467 }
468
469 return handled;
470 }
471
Karl Rosaen138a0412009-04-23 19:00:21 -0700472 private String getTypedText() {
473 return mDefaultKeySsb.toString();
474 }
475
476 private void clearTypedText() {
477 mDefaultKeySsb.clear();
478 mDefaultKeySsb.clearSpans();
479 Selection.setSelection(mDefaultKeySsb, 0);
480 }
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 /**
483 * Restores the previous state, if it exists.
484 *
485 * @param savedState The previous state.
486 */
487 private void restoreState(Bundle savedState) {
488 if (savedState == null) {
489 return;
490 }
491
492 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
493 if (currentScreen > -1) {
494 mWorkspace.setCurrentScreen(currentScreen);
495 }
496
497 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
498 if (addScreen > -1) {
499 mAddItemCellInfo = new CellLayout.CellInfo();
500 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
501 addItemCellInfo.valid = true;
502 addItemCellInfo.screen = addScreen;
503 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
504 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
505 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
506 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
507 addItemCellInfo.findVacantCellsFromOccupied(
508 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
509 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
510 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
511 mRestoring = true;
512 }
513
514 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
515 if (renameFolder) {
516 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 mRestoring = true;
519 }
520 }
521
522 /**
523 * Finds all the views we need and configure them properly.
524 */
525 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400526 mDragController = new DragController(this);
527 DragController dragController = mDragController;
528
Romain Guyb1b69f52009-08-10 15:10:15 -0700529 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400530 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531
Joe Onorato7c312c12009-08-13 21:36:53 -0700532 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700533 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700534 mAllAppsGrid.setDragController(dragController);
Joe Onorato85a02a82009-09-08 12:34:22 -0700535 mAllAppsGrid.setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700536 // Manage focusability manually since this thing is always visible
537 mAllAppsGrid.setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
540 final Workspace workspace = mWorkspace;
541
Joe Onorato7c312c12009-08-13 21:36:53 -0700542 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
543 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544
Joe Onorato7c312c12009-08-13 21:36:53 -0700545 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700547 mHandleView.setOnClickListener(this);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700548 /* TODO
Romain Guyb1b69f52009-08-10 15:10:15 -0700549 TransitionDrawable handleIcon = (TransitionDrawable) mHandleView.getDrawable();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700550 handleIocon.setCrossFadeEnabled(true);
551 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400554 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556
557 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400558 deleteZone.setDragController(dragController);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559 deleteZone.setHandle(mHandleView);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560
Joe Onorato00acb122009-08-04 16:04:30 -0400561 dragController.setDragScoller(workspace);
562 dragController.setDragListener(deleteZone);
563 dragController.setScrollView(dragLayer);
Jason Samsfd22dac2009-09-20 17:24:16 -0700564
Joe Onorato00acb122009-08-04 16:04:30 -0400565 // The order here is bottom to top.
566 dragController.addDropTarget(workspace);
567 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569
570 /**
571 * Creates a view representing a shortcut.
572 *
573 * @param info The data structure describing the shortcut.
574 *
575 * @return A View inflated from R.layout.application.
576 */
577 View createShortcut(ApplicationInfo info) {
578 return createShortcut(R.layout.application,
579 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
580 }
581
582 /**
583 * Creates a view representing a shortcut inflated from the specified resource.
584 *
585 * @param layoutResId The id of the XML layout used to create the shortcut.
586 * @param parent The group the shortcut belongs to.
587 * @param info The data structure describing the shortcut.
588 *
589 * @return A View inflated from layoutResId.
590 */
591 View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) {
592 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
593
Joe Onorato5162ea92009-09-03 09:39:42 -0700594 if (info.icon == null) {
595 info.icon = AppInfoCache.getIconDrawable(getPackageManager(), info);
596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 if (!info.filtered) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 info.icon = Utilities.createIconThumbnail(info.icon, this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 info.filtered = true;
600 }
601
602 favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null);
603 favorite.setText(info.title);
604 favorite.setTag(info);
605 favorite.setOnClickListener(this);
606
607 return favorite;
608 }
609
610 /**
611 * Add an application shortcut to the workspace.
612 *
613 * @param data The intent describing the application.
614 * @param cellInfo The position on screen where to create the shortcut.
615 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 cellInfo.screen = mWorkspace.getCurrentScreen();
618 if (!findSingleSlot(cellInfo)) return;
619
Romain Guy73b979d2009-06-09 12:57:21 -0700620 final ApplicationInfo info = infoFromApplicationIntent(context, data);
621 if (info != null) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
Romain Guy73b979d2009-06-09 12:57:21 -0700623 }
624 }
625
626 private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 ComponentName component = data.getComponent();
628 PackageManager packageManager = context.getPackageManager();
629 ActivityInfo activityInfo = null;
630 try {
631 activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
632 } catch (NameNotFoundException e) {
Romain Guy73b979d2009-06-09 12:57:21 -0700633 e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 }
Romain Guycbb89e42009-06-08 15:52:54 -0700635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 if (activityInfo != null) {
637 ApplicationInfo itemInfo = new ApplicationInfo();
Romain Guycbb89e42009-06-08 15:52:54 -0700638
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 itemInfo.title = activityInfo.loadLabel(packageManager);
640 if (itemInfo.title == null) {
641 itemInfo.title = activityInfo.name;
642 }
Romain Guycbb89e42009-06-08 15:52:54 -0700643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK |
645 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
646 itemInfo.icon = activityInfo.loadIcon(packageManager);
647 itemInfo.container = ItemInfo.NO_ID;
648
Romain Guy73b979d2009-06-09 12:57:21 -0700649 return itemInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Romain Guy73b979d2009-06-09 12:57:21 -0700651
652 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 /**
656 * Add a shortcut to the workspace.
657 *
658 * @param data The intent describing the shortcut.
659 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 cellInfo.screen = mWorkspace.getCurrentScreen();
663 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 final ApplicationInfo info = addShortcut(this, data, cellInfo, false);
666
667 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
670 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
672 }
673
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 * @param data The intent describing the appWidgetId.
679 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700685 d(LOG_TAG, "dumping extras content="+extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700686
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700688
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 // Try finding open space on Launcher screen
694 final int[] xy = mCellCoordinates;
695 if (!findSlot(cellInfo, xy, spans[0], spans[1])) return;
696
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 // Build Launcher-specific widget info and save to database
698 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 launcherInfo.spanX = spans[0];
700 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700701
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 LauncherModel.addItemToDatabase(this, launcherInfo,
703 LauncherSettings.Favorites.CONTAINER_DESKTOP,
704 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
705
706 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700710 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700711
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400716 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718 }
Romain Guycbb89e42009-06-08 15:52:54 -0700719
Joe Onorato9c1289c2009-08-17 11:03:03 -0400720 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
721 mDesktopItems.remove(launcherInfo);
722 launcherInfo.hostView = null;
723 }
724
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700725 public LauncherAppWidgetHost getAppWidgetHost() {
726 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 static ApplicationInfo addShortcut(Context context, Intent data,
730 CellLayout.CellInfo cellInfo, boolean notify) {
731
Romain Guy73b979d2009-06-09 12:57:21 -0700732 final ApplicationInfo info = infoFromShortcutIntent(context, data);
733 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
734 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
735
736 return info;
737 }
738
739 private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
741 String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
742 Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
743
744 Drawable icon = null;
745 boolean filtered = false;
746 boolean customIcon = false;
Romain Guy73b979d2009-06-09 12:57:21 -0700747 ShortcutIconResource iconResource = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748
749 if (bitmap != null) {
750 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context));
751 filtered = true;
752 customIcon = true;
753 } else {
754 Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
Romain Guy73b979d2009-06-09 12:57:21 -0700755 if (extra != null && extra instanceof ShortcutIconResource) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 try {
Romain Guy73b979d2009-06-09 12:57:21 -0700757 iconResource = (ShortcutIconResource) extra;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 final PackageManager packageManager = context.getPackageManager();
759 Resources resources = packageManager.getResourcesForApplication(
760 iconResource.packageName);
761 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
762 icon = resources.getDrawable(id);
763 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700764 w(LOG_TAG, "Could not load shortcut icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
766 }
767 }
768
769 if (icon == null) {
770 icon = context.getPackageManager().getDefaultActivityIcon();
771 }
772
773 final ApplicationInfo info = new ApplicationInfo();
774 info.icon = icon;
775 info.filtered = filtered;
776 info.title = name;
777 info.intent = intent;
778 info.customIcon = customIcon;
779 info.iconResource = iconResource;
780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 return info;
782 }
783
784 @Override
785 protected void onNewIntent(Intent intent) {
786 super.onNewIntent(intent);
787
788 // Close the menu
789 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
790 getWindow().closeAllPanels();
Jason Samsfd22dac2009-09-20 17:24:16 -0700791
Joe Onorato742966b2009-10-07 10:26:17 -0700792 // Whatever we were doing is hereby canceled.
793 mWaitingForResult = false;
794
Mike LeBeau736cf282009-07-02 17:46:59 -0700795 // Set this flag so that onResume knows to close the search dialog if it's open,
796 // because this was a new intent (thus a press of 'home' or some such) rather than
797 // for example onResume being called when the user pressed the 'back' button.
798 mIsNewIntent = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799
800 try {
801 dismissDialog(DIALOG_CREATE_SHORTCUT);
802 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 } catch (Exception e) {
804 // An exception is thrown if the dialog is not visible, which is fine
805 }
806
807 try {
808 dismissDialog(DIALOG_RENAME_FOLDER);
809 // Unlock the workspace if the dialog was showing
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 } catch (Exception e) {
811 // An exception is thrown if the dialog is not visible, which is fine
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) !=
815 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
Romain Guy73b979d2009-06-09 12:57:21 -0700816
Romain Guy94dabf12009-07-21 10:55:43 -0700817 if (!mWorkspace.isDefaultScreenShowing()) {
818 mWorkspace.moveToDefaultScreen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
Romain Guy1dd3a072009-07-16 13:21:01 -0700820
Joe Onorato7bb17492009-09-24 17:51:01 -0700821 closeAllApps(true);
Romain Guy73b979d2009-06-09 12:57:21 -0700822
823 final View v = getWindow().peekDecorView();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 if (v != null && v.getWindowToken() != null) {
825 InputMethodManager imm = (InputMethodManager)getSystemService(
826 INPUT_METHOD_SERVICE);
827 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
828 }
829 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700830 closeAllApps(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
833 }
834
835 @Override
836 protected void onRestoreInstanceState(Bundle savedInstanceState) {
837 // Do not call super here
838 mSavedInstanceState = savedInstanceState;
839 }
840
841 @Override
842 protected void onSaveInstanceState(Bundle outState) {
843 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
844
845 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
846 if (folders.size() > 0) {
847 final int count = folders.size();
848 long[] ids = new long[count];
849 for (int i = 0; i < count; i++) {
850 final FolderInfo info = folders.get(i).getInfo();
851 ids[i] = info.id;
852 }
853 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
854 } else {
855 super.onSaveInstanceState(outState);
856 }
857
Romain Guy3cf604f2009-06-16 13:12:53 -0700858 final boolean isConfigurationChange = getChangingConfigurations() != 0;
859
Romain Guy5a941392009-04-28 15:18:25 -0700860 // When the drawer is opened and we are saving the state because of a
861 // configuration change
Joe Onoratofb0ca672009-09-14 17:55:46 -0400862 // TODO should not do this if the drawer is currently closing.
863 if (isAllAppsVisible() && isConfigurationChange) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700865 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866
867 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
868 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
869 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
870
871 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
872 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
873 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
874 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
875 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
876 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
877 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
878 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
879 layout.getOccupiedCells());
880 }
881
882 if (mFolderInfo != null && mWaitingForResult) {
883 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
884 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
885 }
886 }
887
888 @Override
889 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700893 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 } catch (NullPointerException ex) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -0700895 w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
898 TextKeyListener.getInstance().release();
899
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 unbindDesktopItems();
903 AppInfoCache.unbindDrawables();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 @Override
907 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700908 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 super.startActivityForResult(intent, requestCode);
910 }
911
912 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700913 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700915
Joe Onorato7bb17492009-09-24 17:51:01 -0700916 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
Karl Rosaen138a0412009-04-23 19:00:21 -0700918 // Slide the search widget to the top, if it's on the current screen,
919 // otherwise show the search dialog immediately.
920 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
921 if (searchWidget == null) {
922 showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch);
923 } else {
924 searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
925 // show the currently typed text in the search widget while sliding
926 searchWidget.setQuery(getTypedText());
927 }
928 }
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Karl Rosaen138a0412009-04-23 19:00:21 -0700930 /**
931 * Show the search dialog immediately, without changing the search widget.
Romain Guy5a941392009-04-28 15:18:25 -0700932 *
933 * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean)
Karl Rosaen138a0412009-04-23 19:00:21 -0700934 */
Romain Guycbb89e42009-06-08 15:52:54 -0700935 void showSearchDialog(String initialQuery, boolean selectInitialQuery,
Karl Rosaen138a0412009-04-23 19:00:21 -0700936 Bundle appSearchData, boolean globalSearch) {
Romain Guycbb89e42009-06-08 15:52:54 -0700937
Karl Rosaen138a0412009-04-23 19:00:21 -0700938 if (initialQuery == null) {
939 // Use any text typed in the launcher as the initial query
940 initialQuery = getTypedText();
941 clearTypedText();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 if (appSearchData == null) {
944 appSearchData = new Bundle();
945 appSearchData.putString(SearchManager.SOURCE, "launcher-search");
946 }
Romain Guycbb89e42009-06-08 15:52:54 -0700947
Karl Rosaen138a0412009-04-23 19:00:21 -0700948 final SearchManager searchManager =
949 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
950
951 final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
952 if (searchWidget != null) {
953 // This gets called when the user leaves the search dialog to go back to
954 // the Launcher.
955 searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
956 public void onCancel() {
957 searchManager.setOnCancelListener(null);
Romain Guy5a941392009-04-28 15:18:25 -0700958 stopSearch();
Romain Guycbb89e42009-06-08 15:52:54 -0700959 }
Karl Rosaen138a0412009-04-23 19:00:21 -0700960 });
961 }
Romain Guycbb89e42009-06-08 15:52:54 -0700962
Karl Rosaen138a0412009-04-23 19:00:21 -0700963 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700964 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 }
966
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 /**
968 * Cancel search dialog if it is open.
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 */
Romain Guy5a941392009-04-28 15:18:25 -0700970 void stopSearch() {
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 // Close search dialog
972 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Bjorn Bringert0879cef2009-07-08 12:38:25 +0100973 searchManager.stopSearch();
Karl Rosaen138a0412009-04-23 19:00:21 -0700974 // Restore search widget to its normal position
975 Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen();
976 if (searchWidget != null) {
977 searchWidget.stopSearch(false);
978 }
979 }
Romain Guycbb89e42009-06-08 15:52:54 -0700980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 @Override
982 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 if (isWorkspaceLocked()) {
984 return false;
985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986
987 super.onCreateOptionsMenu(menu);
988 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
989 .setIcon(android.R.drawable.ic_menu_add)
990 .setAlphabeticShortcut('A');
991 menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
992 .setIcon(android.R.drawable.ic_menu_gallery)
993 .setAlphabeticShortcut('W');
994 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
995 .setIcon(android.R.drawable.ic_search_category_default)
996 .setAlphabeticShortcut(SearchManager.MENU_KEY);
997 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
998 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
999 .setAlphabeticShortcut('N');
1000
1001 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001002 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1003 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004
1005 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1006 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1007 .setIntent(settings);
1008
1009 return true;
1010 }
1011
1012 @Override
1013 public boolean onPrepareOptionsMenu(Menu menu) {
1014 super.onPrepareOptionsMenu(menu);
1015
1016 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1017 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1018
1019 return true;
1020 }
1021
1022 @Override
1023 public boolean onOptionsItemSelected(MenuItem item) {
1024 switch (item.getItemId()) {
1025 case MENU_ADD:
1026 addItems();
1027 return true;
1028 case MENU_WALLPAPER_SETTINGS:
1029 startWallpaper();
1030 return true;
1031 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001032 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 return true;
1034 case MENU_NOTIFICATIONS:
1035 showNotifications();
1036 return true;
1037 }
1038
1039 return super.onOptionsItemSelected(item);
1040 }
Romain Guycbb89e42009-06-08 15:52:54 -07001041
Karl Rosaen138a0412009-04-23 19:00:21 -07001042 /**
1043 * Indicates that we want global search for this activity by setting the globalSearch
1044 * argument for {@link #startSearch} to true.
1045 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001047 @Override
1048 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001049 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001050 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001051 }
1052
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 public boolean isWorkspaceLocked() {
1054 return mWorkspaceLoading || mWaitingForResult;
1055 }
1056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 private void addItems() {
1058 showAddDialog(mMenuAddInfo);
1059 }
1060
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 void addAppWidget(Intent data) {
1062 // TODO: catch bad widget exception when sent
1063 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001065 String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET);
1066 if (SEARCH_WIDGET.equals(customWidget)) {
1067 // We don't need this any more, since this isn't a real app widget.
1068 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1069 // add the search widget
1070 addSearch();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 } else {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001072 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1073
1074 if (appWidget.configure != null) {
1075 // Launch over to configure widget, if needed
1076 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1077 intent.setComponent(appWidget.configure);
1078 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1079
1080 startActivityForResult(intent, REQUEST_CREATE_APPWIDGET);
1081 } else {
1082 // Otherwise just add it
1083 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
1084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
1086 }
Romain Guycbb89e42009-06-08 15:52:54 -07001087
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 void addSearch() {
1089 final Widget info = Widget.makeSearch();
1090 final CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Romain Guycbb89e42009-06-08 15:52:54 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 final int[] xy = mCellCoordinates;
1093 final int spanX = info.spanX;
1094 final int spanY = info.spanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 if (!findSlot(cellInfo, xy, spanX, spanY)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1099 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 final View view = mInflater.inflate(info.layoutResource, null);
1102 view.setTag(info);
Karl Rosaen138a0412009-04-23 19:00:21 -07001103 Search search = (Search) view.findViewById(R.id.widget_search);
1104 search.setLauncher(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY);
1107 }
1108
Romain Guy73b979d2009-06-09 12:57:21 -07001109 void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001110 // Handle case where user selected "Applications"
1111 String applicationName = getResources().getString(R.string.group_applications);
1112 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001114 if (applicationName != null && applicationName.equals(shortcutName)) {
1115 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1116 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001118 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1119 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Romain Guy73b979d2009-06-09 12:57:21 -07001120 startActivityForResult(pickIntent, requestCodeApplication);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001121 } else {
Romain Guy73b979d2009-06-09 12:57:21 -07001122 startActivityForResult(intent, requestCodeShortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125
1126 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001127 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001128 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001129 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001131 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001133 } else {
1134 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
1137
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 UserFolderInfo folderInfo = new UserFolderInfo();
1140 folderInfo.title = getText(R.string.folder_name);
1141
1142 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1143 cellInfo.screen = mWorkspace.getCurrentScreen();
1144 if (!findSingleSlot(cellInfo)) return;
1145
1146 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 LauncherModel.addItemToDatabase(this, folderInfo,
1148 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
1152 // Create the view
1153 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1154 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1155 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Joe Onorato9c1289c2009-08-17 11:03:03 -04001159 void removeFolder(FolderInfo folder) {
1160 mFolders.remove(folder.id);
1161 }
1162
1163 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 cellInfo.screen = mWorkspace.getCurrentScreen();
1165 if (!findSingleSlot(cellInfo)) return;
1166
1167 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1168
1169 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1171 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1173 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
1176
1177 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1178 CellLayout.CellInfo cellInfo, boolean notify) {
1179
1180 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1181 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1182
1183 Drawable icon = null;
1184 boolean filtered = false;
1185 Intent.ShortcutIconResource iconResource = null;
1186
1187 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1188 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1189 try {
1190 iconResource = (Intent.ShortcutIconResource) extra;
1191 final PackageManager packageManager = context.getPackageManager();
1192 Resources resources = packageManager.getResourcesForApplication(
1193 iconResource.packageName);
1194 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1195 icon = resources.getDrawable(id);
1196 } catch (Exception e) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001197 w(LOG_TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
1199 }
1200
1201 if (icon == null) {
1202 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1203 }
1204
1205 final LiveFolderInfo info = new LiveFolderInfo();
1206 info.icon = icon;
1207 info.filtered = filtered;
1208 info.title = name;
1209 info.iconResource = iconResource;
1210 info.uri = data.getData();
1211 info.baseIntent = baseIntent;
1212 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1213 LiveFolders.DISPLAY_MODE_GRID);
1214
1215 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1216 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
1219 return info;
1220 }
1221
1222 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1223 final int[] xy = new int[2];
1224 if (findSlot(cellInfo, xy, 1, 1)) {
1225 cellInfo.cellX = xy[0];
1226 cellInfo.cellY = xy[1];
1227 return true;
1228 }
1229 return false;
1230 }
1231
1232 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1233 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1234 boolean[] occupied = mSavedState != null ?
1235 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1236 cellInfo = mWorkspace.findAllVacantCells(occupied);
1237 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1238 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1239 return false;
1240 }
1241 }
1242 return true;
1243 }
1244
1245 private void showNotifications() {
1246 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1247 if (statusBar != null) {
1248 statusBar.expand();
1249 }
1250 }
1251
1252 private void startWallpaper() {
1253 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001254 Intent chooser = Intent.createChooser(pickWallpaper,
1255 getText(R.string.chooser_wallpaper));
1256 WallpaperManager wm = (WallpaperManager)
1257 getSystemService(Context.WALLPAPER_SERVICE);
1258 WallpaperInfo wi = wm.getWallpaperInfo();
1259 if (wi != null && wi.getSettingsActivity() != null) {
1260 LabeledIntent li = new LabeledIntent(getPackageName(),
1261 R.string.configure_wallpaper, 0);
1262 li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1263 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1264 }
Mike Clerona0618e42009-10-22 13:55:21 -07001265 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 @Override
Dianne Hackborn67800862009-07-24 17:15:20 -07001269 public void onWindowFocusChanged(boolean hasFocus) {
1270 super.onWindowFocusChanged(hasFocus);
1271 if (!hasFocus) {
1272 mBackDown = mHomeDown = false;
1273 }
1274 }
1275
1276 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 public boolean dispatchKeyEvent(KeyEvent event) {
1278 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1279 switch (event.getKeyCode()) {
1280 case KeyEvent.KEYCODE_BACK:
Dianne Hackborn67800862009-07-24 17:15:20 -07001281 mBackDown = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 return true;
1283 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001284 mHomeDown = true;
1285 return true;
1286 }
1287 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1288 switch (event.getKeyCode()) {
1289 case KeyEvent.KEYCODE_BACK:
1290 if (!event.isCanceled()) {
1291 mWorkspace.dispatchKeyEvent(event);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001292 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001293 closeAllApps(true);
Dianne Hackborn67800862009-07-24 17:15:20 -07001294 } else {
1295 closeFolder();
1296 }
1297 }
1298 mBackDown = false;
1299 return true;
1300 case KeyEvent.KEYCODE_HOME:
1301 mHomeDown = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 return true;
1303 }
1304 }
1305
1306 return super.dispatchKeyEvent(event);
1307 }
1308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 private void closeFolder() {
1310 Folder folder = mWorkspace.getOpenFolder();
1311 if (folder != null) {
1312 closeFolder(folder);
1313 }
1314 }
1315
1316 void closeFolder(Folder folder) {
1317 folder.getInfo().opened = false;
1318 ViewGroup parent = (ViewGroup) folder.getParent();
1319 if (parent != null) {
1320 parent.removeView(folder);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001321 // TODO: this line crashes.
1322 //mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324 folder.onClose();
1325 }
1326
1327 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1329 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 private void unbindDesktopItems() {
1332 for (ItemInfo item: mDesktopItems) {
1333 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001336
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 /**
1338 * Launches the intent referred by the clicked shortcut.
1339 *
1340 * @param v The view representing the clicked shortcut.
1341 */
1342 public void onClick(View v) {
1343 Object tag = v.getTag();
1344 if (tag instanceof ApplicationInfo) {
1345 // Open shortcut
1346 final Intent intent = ((ApplicationInfo) tag).intent;
1347 startActivitySafely(intent);
Joe Onoratoff3862d2009-10-07 10:35:51 -07001348 mExitingBecauseOfLaunch = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 } else if (tag instanceof FolderInfo) {
1350 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001351 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001352 Log.d(TAG, "onClick");
1353 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001354 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001355 } else {
Jason Samsfd22dac2009-09-20 17:24:16 -07001356 showAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -07001357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359 }
1360
1361 void startActivitySafely(Intent intent) {
1362 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1363 try {
1364 startActivity(intent);
1365 } catch (ActivityNotFoundException e) {
1366 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1367 } catch (SecurityException e) {
1368 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Romain Guy73b979d2009-06-09 12:57:21 -07001369 e(LOG_TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1371 "or use the exported attribute for this activity.", e);
1372 }
1373 }
1374
1375 private void handleFolderClick(FolderInfo folderInfo) {
1376 if (!folderInfo.opened) {
1377 // Close any open folder
1378 closeFolder();
1379 // Open the requested folder
1380 openFolder(folderInfo);
1381 } else {
1382 // Find the open folder...
1383 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1384 int folderScreen;
1385 if (openFolder != null) {
1386 folderScreen = mWorkspace.getScreenForView(openFolder);
1387 // .. and close it
1388 closeFolder(openFolder);
1389 if (folderScreen != mWorkspace.getCurrentScreen()) {
1390 // Close any folder open on the current screen
1391 closeFolder();
1392 // Pull the folder onto this screen
1393 openFolder(folderInfo);
1394 }
1395 }
1396 }
1397 }
1398
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 /**
1400 * Opens the user fodler described by the specified tag. The opening of the folder
1401 * is animated relative to the specified View. If the View is null, no animation
1402 * is played.
1403 *
1404 * @param folderInfo The FolderInfo describing the folder to open.
1405 */
1406 private void openFolder(FolderInfo folderInfo) {
1407 Folder openFolder;
1408
1409 if (folderInfo instanceof UserFolderInfo) {
1410 openFolder = UserFolder.fromXml(this);
1411 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001412 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 } else {
1414 return;
1415 }
1416
Joe Onorato00acb122009-08-04 16:04:30 -04001417 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 openFolder.setLauncher(this);
1419
1420 openFolder.bind(folderInfo);
1421 folderInfo.opened = true;
1422
1423 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1424 openFolder.onOpen();
1425 }
1426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 public boolean onLongClick(View v) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001428 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 return false;
1430 }
1431
1432 if (!(v instanceof CellLayout)) {
1433 v = (View) v.getParent();
1434 }
1435
1436 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1437
1438 // This happens when long clicking an item with the dpad/trackball
1439 if (cellInfo == null) {
1440 return true;
1441 }
1442
1443 if (mWorkspace.allowLongPress()) {
1444 if (cellInfo.cell == null) {
1445 if (cellInfo.valid) {
1446 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001447 mWorkspace.setAllowLongPress(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 showAddDialog(cellInfo);
1449 }
1450 } else {
1451 if (!(cellInfo.cell instanceof Folder)) {
1452 // User long pressed on an item
1453 mWorkspace.startDrag(cellInfo);
1454 }
1455 }
1456 }
1457 return true;
1458 }
1459
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001460 View getDrawerHandle() {
1461 return mHandleView;
1462 }
1463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 Workspace getWorkspace() {
1465 return mWorkspace;
1466 }
1467
Joe Onorato7404ee42009-07-31 11:54:44 -07001468 /* TODO
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 GridView getApplicationsGrid() {
1470 return mAllAppsGrid;
1471 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001472 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473
1474 @Override
1475 protected Dialog onCreateDialog(int id) {
1476 switch (id) {
1477 case DIALOG_CREATE_SHORTCUT:
1478 return new CreateShortcut().createDialog();
1479 case DIALOG_RENAME_FOLDER:
1480 return new RenameFolder().createDialog();
1481 }
1482
1483 return super.onCreateDialog(id);
1484 }
1485
1486 @Override
1487 protected void onPrepareDialog(int id, Dialog dialog) {
1488 switch (id) {
1489 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 break;
1491 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001492 if (mFolderInfo != null) {
1493 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1494 final CharSequence text = mFolderInfo.title;
1495 input.setText(text);
1496 input.setSelection(0, text.length());
1497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 break;
1499 }
1500 }
1501
1502 void showRenameDialog(FolderInfo info) {
1503 mFolderInfo = info;
1504 mWaitingForResult = true;
1505 showDialog(DIALOG_RENAME_FOLDER);
1506 }
1507
1508 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1509 mAddItemCellInfo = cellInfo;
1510 mWaitingForResult = true;
1511 showDialog(DIALOG_CREATE_SHORTCUT);
1512 }
1513
Romain Guy73b979d2009-06-09 12:57:21 -07001514 private void pickShortcut(int requestCode, int title) {
1515 Bundle bundle = new Bundle();
1516
1517 ArrayList<String> shortcutNames = new ArrayList<String>();
1518 shortcutNames.add(getString(R.string.group_applications));
1519 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1520
1521 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1522 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1523 R.drawable.ic_launcher_application));
1524 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1525
1526 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1527 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
1528 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title));
1529 pickIntent.putExtras(bundle);
1530
1531 startActivityForResult(pickIntent, requestCode);
1532 }
1533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 private class RenameFolder {
1535 private EditText mInput;
1536
1537 Dialog createDialog() {
1538 mWaitingForResult = true;
1539 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1540 mInput = (EditText) layout.findViewById(R.id.folder_name);
1541
1542 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1543 builder.setIcon(0);
1544 builder.setTitle(getString(R.string.rename_folder_title));
1545 builder.setCancelable(true);
1546 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1547 public void onCancel(DialogInterface dialog) {
1548 cleanup();
1549 }
1550 });
1551 builder.setNegativeButton(getString(R.string.cancel_action),
1552 new Dialog.OnClickListener() {
1553 public void onClick(DialogInterface dialog, int which) {
1554 cleanup();
1555 }
1556 }
1557 );
1558 builder.setPositiveButton(getString(R.string.rename_action),
1559 new Dialog.OnClickListener() {
1560 public void onClick(DialogInterface dialog, int which) {
1561 changeFolderName();
1562 }
1563 }
1564 );
1565 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001566
1567 final AlertDialog dialog = builder.create();
1568 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1569 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001570 }
1571 });
1572
1573 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 }
1575
1576 private void changeFolderName() {
1577 final String name = mInput.getText().toString();
1578 if (!TextUtils.isEmpty(name)) {
1579 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 mFolderInfo.title = name;
1582 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1583
Joe Onorato9c1289c2009-08-17 11:03:03 -04001584 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001585 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586 mModel.setWorkspaceDirty();
1587 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 } else {
1589 final FolderIcon folderIcon = (FolderIcon)
1590 mWorkspace.getViewForTag(mFolderInfo);
1591 if (folderIcon != null) {
1592 folderIcon.setText(name);
1593 getWorkspace().requestLayout();
1594 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001595 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596 mModel.setWorkspaceDirty();
1597 mWorkspaceLoading = true;
1598 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600 }
1601 }
1602 cleanup();
1603 }
1604
1605 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 dismissDialog(DIALOG_RENAME_FOLDER);
1607 mWaitingForResult = false;
1608 mFolderInfo = null;
1609 }
1610 }
1611
Joe Onoratofb0ca672009-09-14 17:55:46 -04001612 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001613 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001614 }
1615
Jason Samsfd22dac2009-09-20 17:24:16 -07001616 void showAllApps() {
Jason Sams12c14a82009-10-06 14:33:15 -07001617 mAllAppsGrid.zoom(1.0f);
Joe Onorato7bb17492009-09-24 17:51:01 -07001618 //mWorkspace.hide();
Jason Samsfd22dac2009-09-20 17:24:16 -07001619
Mike Cleronb6082fa02009-10-19 17:03:36 -07001620 mAllAppsGrid.setFocusable(true);
Mike Clerona0618e42009-10-22 13:55:21 -07001621 mAllAppsGrid.requestFocus();
Mike Cleronb6082fa02009-10-19 17:03:36 -07001622
Joe Onorato7c312c12009-08-13 21:36:53 -07001623 // TODO: fade these two too
1624 mDeleteZone.setVisibility(View.GONE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001625 //mHandleView.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001626 }
1627
Joe Onorato7bb17492009-09-24 17:51:01 -07001628 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001629 if (mAllAppsGrid.isVisible()) {
Jason Sams12c14a82009-10-06 14:33:15 -07001630 mAllAppsGrid.zoom(0.0f);
Mike Cleronb6082fa02009-10-19 17:03:36 -07001631 mAllAppsGrid.setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001632 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onorato7c312c12009-08-13 21:36:53 -07001633
Mike Cleronb6082fa02009-10-19 17:03:36 -07001634
Joe Onorato7c312c12009-08-13 21:36:53 -07001635 // TODO: fade these two too
Joe Onoratofb0ca672009-09-14 17:55:46 -04001636 /*
Joe Onorato7c312c12009-08-13 21:36:53 -07001637 mDeleteZone.setVisibility(View.VISIBLE);
1638 mHandleView.setVisibility(View.VISIBLE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001639 */
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001640 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001641 }
1642
Joe Onorato7c312c12009-08-13 21:36:53 -07001643 void lockAllApps() {
1644 // TODO
1645 }
1646
1647 void unlockAllApps() {
1648 // TODO
1649 }
1650
Joe Onorato7404ee42009-07-31 11:54:44 -07001651 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 * Displays the shortcut creation dialog and launches, if necessary, the
1653 * appropriate activity.
1654 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001655 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001656 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1657 DialogInterface.OnShowListener {
1658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 Dialog createDialog() {
1662 mWaitingForResult = true;
Romain Guycbb89e42009-06-08 15:52:54 -07001663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1667 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001668 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 builder.setInverseBackgroundForced(true);
1671
1672 AlertDialog dialog = builder.create();
1673 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001674 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001675 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 return dialog;
1678 }
1679
1680 public void onCancel(DialogInterface dialog) {
1681 mWaitingForResult = false;
1682 cleanup();
1683 }
1684
Romain Guycbb89e42009-06-08 15:52:54 -07001685 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001686 }
1687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 dismissDialog(DIALOG_CREATE_SHORTCUT);
1690 }
1691
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001692 /**
1693 * Handle the action clicked in the "Add to home" dialog.
1694 */
1695 public void onClick(DialogInterface dialog, int which) {
1696 Resources res = getResources();
1697 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001698
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001699 switch (which) {
1700 case AddAdapter.ITEM_SHORTCUT: {
1701 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001702 pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001703 break;
1704 }
Romain Guycbb89e42009-06-08 15:52:54 -07001705
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001706 case AddAdapter.ITEM_APPWIDGET: {
1707 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001708
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001709 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1710 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1711 // add the search widget
1712 ArrayList<AppWidgetProviderInfo> customInfo =
1713 new ArrayList<AppWidgetProviderInfo>();
1714 AppWidgetProviderInfo info = new AppWidgetProviderInfo();
1715 info.provider = new ComponentName(getPackageName(), "XXX.YYY");
1716 info.label = getString(R.string.group_search);
1717 info.icon = R.drawable.ic_search_widget;
1718 customInfo.add(info);
1719 pickIntent.putParcelableArrayListExtra(
1720 AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
1721 ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
1722 Bundle b = new Bundle();
1723 b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET);
1724 customExtras.add(b);
1725 pickIntent.putParcelableArrayListExtra(
1726 AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
1727 // start the pick activity
1728 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1729 break;
1730 }
Romain Guycbb89e42009-06-08 15:52:54 -07001731
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001732 case AddAdapter.ITEM_LIVE_FOLDER: {
1733 // Insert extra item to handle inserting folder
1734 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001735
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001736 ArrayList<String> shortcutNames = new ArrayList<String>();
1737 shortcutNames.add(res.getString(R.string.group_folder));
1738 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001739
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001740 ArrayList<ShortcutIconResource> shortcutIcons =
1741 new ArrayList<ShortcutIconResource>();
1742 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1743 R.drawable.ic_launcher_folder));
1744 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1745
1746 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1747 pickIntent.putExtra(Intent.EXTRA_INTENT,
1748 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1749 pickIntent.putExtra(Intent.EXTRA_TITLE,
1750 getText(R.string.title_select_live_folder));
1751 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001752
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001753 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1754 break;
1755 }
1756
1757 case AddAdapter.ITEM_WALLPAPER: {
1758 startWallpaper();
1759 break;
1760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
1762 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001763
1764 public void onShow(DialogInterface dialog) {
Romain Guy7b4ef332009-07-14 13:58:08 -07001765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
1767
1768 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001769 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 public int getCurrentWorkspaceScreen() {
1772 return mWorkspace.getCurrentScreen();
1773 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001774
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 /**
1776 * Refreshes the shortcuts shown on the workspace.
1777 *
1778 * Implementation of the method from LauncherModel.Callbacks.
1779 */
1780 public void startBinding() {
1781 final Workspace workspace = mWorkspace;
1782 int count = workspace.getChildCount();
1783 for (int i = 0; i < count; i++) {
1784 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
1785 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001786
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787 if (DEBUG_USER_INTERFACE) {
1788 android.widget.Button finishButton = new android.widget.Button(this);
1789 finishButton.setText("Finish");
1790 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
1791
1792 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
1793 public void onClick(View v) {
1794 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798 }
1799
1800 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001801 * Bind the items start-end from the list.
1802 *
1803 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
1806
1807 final Workspace workspace = mWorkspace;
1808
1809 for (int i=start; i<end; i++) {
1810 final ItemInfo item = shortcuts.get(i);
1811 mDesktopItems.add(item);
1812 switch (item.itemType) {
1813 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1814 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1815 final View shortcut = createShortcut((ApplicationInfo) item);
1816 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
1817 false);
1818 break;
1819 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1820 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1821 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1822 (UserFolderInfo) item);
1823 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
1824 false);
1825 break;
1826 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
1827 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
1828 R.layout.live_folder_icon, this,
1829 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
1830 (LiveFolderInfo) item);
1831 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
1832 false);
1833 break;
1834 case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
1835 final int screen = workspace.getCurrentScreen();
1836 final View view = mInflater.inflate(R.layout.widget_search,
1837 (ViewGroup) workspace.getChildAt(screen), false);
1838
1839 Search search = (Search) view.findViewById(R.id.widget_search);
1840 search.setLauncher(this);
1841
1842 final Widget widget = (Widget) item;
1843 view.setTag(widget);
1844
1845 workspace.addWidget(view, widget, false);
1846 break;
1847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
1849
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 }
1852
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853 /**
1854 * Implementation of the method from LauncherModel.Callbacks.
1855 */
1856 void bindFolders(HashMap<Long, FolderInfo> folders) {
1857 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859
1860 /**
1861 * Add the views for a widget to the workspace.
1862 *
1863 * Implementation of the method from LauncherModel.Callbacks.
1864 */
1865 public void bindAppWidget(LauncherAppWidgetInfo item) {
1866 final Workspace workspace = mWorkspace;
1867
1868 final int appWidgetId = item.appWidgetId;
1869 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1870 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1871
1872 if (true) {
1873 Log.d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s",
1874 appWidgetId, appWidgetInfo));
1875 }
1876
1877 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1878 item.hostView.setTag(item);
1879
1880 workspace.addInScreen(item.hostView, item.screen, item.cellX,
1881 item.cellY, item.spanX, item.spanY, false);
1882
1883 workspace.requestLayout();
1884
1885 mDesktopItems.add(item);
1886 }
1887
1888 /**
1889 * Callback saying that there aren't any more items to bind.
1890 *
1891 * Implementation of the method from LauncherModel.Callbacks.
1892 */
1893 public void finishBindingItems() {
1894 if (mSavedState != null) {
1895 if (!mWorkspace.hasFocus()) {
1896 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
1897 }
1898
1899 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
1900 if (userFolders != null) {
1901 for (long folderId : userFolders) {
1902 final FolderInfo info = mFolders.get(folderId);
1903 if (info != null) {
1904 openFolder(info);
1905 }
1906 }
1907 final Folder openFolder = mWorkspace.getOpenFolder();
1908 if (openFolder != null) {
1909 openFolder.requestFocus();
1910 }
1911 }
1912
1913 final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
1914 if (allApps) {
Jason Samsfd22dac2009-09-20 17:24:16 -07001915 showAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001916 }
1917
1918 mSavedState = null;
1919 }
1920
1921 if (mSavedInstanceState != null) {
1922 super.onRestoreInstanceState(mSavedInstanceState);
1923 mSavedInstanceState = null;
1924 }
1925
1926 /* TODO
1927 if (mAllAppsVisible && !mDrawer.hasFocus()) {
1928 mDrawer.requestFocus();
1929 }
1930 */
1931
1932 Log.d(TAG, "finishBindingItems done");
1933 mWorkspaceLoading = false;
1934 }
1935
1936 /**
1937 * Add the icons for all apps.
1938 *
1939 * Implementation of the method from LauncherModel.Callbacks.
1940 */
1941 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 mAllAppsList = apps;
1943 mAllAppsGrid.setApps(mAllAppsList);
1944 }
1945
1946 /**
1947 * A package was installed.
1948 *
1949 * Implementation of the method from LauncherModel.Callbacks.
1950 */
1951 public void bindPackageAdded(ArrayList<ApplicationInfo> apps) {
1952 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001953 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 }
1955
1956 /**
1957 * A package was updated.
1958 *
1959 * Implementation of the method from LauncherModel.Callbacks.
1960 */
1961 public void bindPackageUpdated(String packageName, ArrayList<ApplicationInfo> apps) {
1962 removeDialog(DIALOG_CREATE_SHORTCUT);
1963 mWorkspace.updateShortcutsForPackage(packageName);
1964 }
1965
1966 /**
1967 * A package was uninstalled.
1968 *
1969 * Implementation of the method from LauncherModel.Callbacks.
1970 */
1971 public void bindPackageRemoved(String packageName, ArrayList<ApplicationInfo> apps) {
1972 removeDialog(DIALOG_CREATE_SHORTCUT);
1973 mWorkspace.removeShortcutsForPackage(packageName);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001974 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976}