blob: 5a2a7d37ebd6a738542705db22c6e8a64399baad [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000019import com.android.common.Search;
20
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.app.Activity;
22import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.app.Dialog;
24import android.app.SearchManager;
25import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070026import android.app.WallpaperManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080028import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040029import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080030import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -070034import android.content.Intent.ShortcutIconResource;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.IntentFilter;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040036import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.pm.PackageManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070039import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.graphics.Bitmap;
Romain Guy1fbc1c82009-11-09 20:43:08 -080042import android.graphics.Rect;
Romain Guya6abce82009-11-10 02:54:41 -080043import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.drawable.Drawable;
Romain Guyff0c2e22009-11-10 12:09:59 -080045import android.graphics.drawable.ColorDrawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040046import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.os.Parcelable;
Joe Onoratobe386092009-11-17 17:32:16 -080050import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.text.Selection;
53import android.text.SpannableStringBuilder;
54import android.text.TextUtils;
55import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070056import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080058import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.view.KeyEvent;
60import android.view.LayoutInflater;
61import android.view.Menu;
62import android.view.MenuItem;
63import android.view.View;
64import android.view.ViewGroup;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.view.View.OnLongClickListener;
66import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.widget.EditText;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.widget.TextView;
69import android.widget.Toast;
Romain Guy8a73c512009-11-09 19:19:59 -080070import android.widget.ImageView;
Romain Guy1fbc1c82009-11-09 20:43:08 -080071import android.widget.PopupWindow;
Romain Guye6b8e2f2009-11-10 11:56:55 -080072import android.widget.LinearLayout;
The Android Open Source Project7376fae2009-03-11 12:11:58 -070073import android.appwidget.AppWidgetManager;
74import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import java.util.ArrayList;
Joe Onorato9c1289c2009-08-17 11:03:03 -040077import java.util.HashMap;
Romain Guy98d01652009-06-30 16:21:04 -070078import java.io.DataOutputStream;
79import java.io.FileNotFoundException;
80import java.io.IOException;
81import java.io.DataInputStream;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082
Romain Guyedcce092010-03-04 13:03:17 -080083import com.android.launcher.R;
84
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085/**
86 * Default launcher application.
87 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040088public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050089 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080090 static final String TAG = "Launcher";
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091 static final boolean LOGD = false;
92
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 static final boolean PROFILE_STARTUP = false;
94 static final boolean PROFILE_ROTATE = false;
95 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 private static final int WALLPAPER_SCREENS_SPAN = 2;
98
99 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800100 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 private static final int MENU_ADD = Menu.FIRST + 1;
103 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
104 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
105 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700106 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
108 private static final int REQUEST_CREATE_SHORTCUT = 1;
109 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700110 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 private static final int REQUEST_PICK_APPLICATION = 6;
112 private static final int REQUEST_PICK_SHORTCUT = 7;
113 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700114 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700115 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
117 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
118
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800119 static final int SCREEN_COUNT = 5;
120 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700122 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato7c312c12009-08-13 21:36:53 -0700124 static final int DIALOG_CREATE_SHORTCUT = 1;
125 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Romain Guy98d01652009-06-30 16:21:04 -0700127 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 // Type: int
130 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
131 // Type: boolean
132 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
133 // Type: long
134 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
135 // Type: int
136 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
137 // Type: int
138 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
139 // Type: int
140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
141 // Type: int
142 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
143 // Type: int
144 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
145 // Type: int
146 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
147 // Type: int
148 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
149 // Type: int[]
150 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
151 // Type: boolean
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
153 // Type: long
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800161 private final BroadcastReceiver mCloseSystemDialogsReceiver
162 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800163 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private LayoutInflater mInflater;
166
Joe Onorato00acb122009-08-04 16:04:30 -0400167 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700169
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700170 private AppWidgetManager mAppWidgetManager;
171 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private CellLayout.CellInfo mAddItemCellInfo;
174 private CellLayout.CellInfo mMenuAddInfo;
175 private final int[] mCellCoordinates = new int[2];
176 private FolderInfo mFolderInfo;
177
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700179 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700180 private AllAppsView mAllAppsGrid;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Bundle mSavedState;
183
184 private SpannableStringBuilder mDefaultKeySsb = null;
185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 private boolean mWorkspaceLoading = true;
187
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800188 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private boolean mRestoring;
190 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
192 private Bundle mSavedInstanceState;
193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800195 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196
Romain Guy84f296c2009-11-04 15:00:44 -0800197 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
198 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Romain Guy1fbc1c82009-11-09 20:43:08 -0800200 private ImageView mPreviousView;
201 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500202
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400203 // Hotseats (quick-launch icons next to AllApps)
204 // TODO: move these intial intents out to Uris in an XML resource
205 private static final int NUM_HOTSEATS = 2;
206 private Intent[] mHotseats = new Intent[] {
207 new Intent(Intent.ACTION_MAIN)
208 .setComponent(ComponentName.unflattenFromString(
209 "com.android.contacts/.ContactsLaunchActivity")),
210 new Intent(Intent.ACTION_WEB_SEARCH, Uri.EMPTY),
211 };
212 private CharSequence[] mHotseatLabels = new CharSequence[2];
213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 @Override
215 protected void onCreate(Bundle savedInstanceState) {
216 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700217
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 LauncherApplication app = ((LauncherApplication)getApplication());
219 mModel = app.setLauncher(this);
220 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400221 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700223
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700224 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
226 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 if (PROFILE_STARTUP) {
229 android.os.Debug.startMethodTracing("/sdcard/launcher");
230 }
231
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400232 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 checkForLocaleChange();
234 setWallpaperDimension();
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 setContentView(R.layout.launcher);
237 setupViews();
238
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800239 registerContentObservers();
240
Joe Onorato7c312c12009-08-13 21:36:53 -0700241 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 mSavedState = savedInstanceState;
244 restoreState(mSavedState);
245
246 if (PROFILE_STARTUP) {
247 android.os.Debug.stopMethodTracing();
248 }
249
Joe Onorato1d8e7bb2009-10-15 19:49:43 -0700250 // We have a new AllAppsView, we need to re-bind everything, and it could have
251 // changed in our absence.
252 mModel.setAllAppsDirty();
253 mModel.setWorkspaceDirty();
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 }
258
259 // For handling default keys
260 mDefaultKeySsb = new SpannableStringBuilder();
261 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800262
263 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
264 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 }
Romain Guycbb89e42009-06-08 15:52:54 -0700266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700268 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
269 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 final Configuration configuration = getResources().getConfiguration();
272
Romain Guy98d01652009-06-30 16:21:04 -0700273 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 final String locale = configuration.locale.toString();
275
Romain Guy98d01652009-06-30 16:21:04 -0700276 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 final int mcc = configuration.mcc;
278
Romain Guy98d01652009-06-30 16:21:04 -0700279 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 final int mnc = configuration.mnc;
281
Romain Guy84f296c2009-11-04 15:00:44 -0800282 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
Romain Guy84f296c2009-11-04 15:00:44 -0800284 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700285 localeConfiguration.locale = locale;
286 localeConfiguration.mcc = mcc;
287 localeConfiguration.mnc = mnc;
288
289 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800290 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400291
292 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700293 }
294 }
295
296 private static class LocaleConfiguration {
297 public String locale;
298 public int mcc = -1;
299 public int mnc = -1;
300 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700301
Romain Guy98d01652009-06-30 16:21:04 -0700302 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
303 DataInputStream in = null;
304 try {
305 in = new DataInputStream(context.openFileInput(PREFERENCES));
306 configuration.locale = in.readUTF();
307 configuration.mcc = in.readInt();
308 configuration.mnc = in.readInt();
309 } catch (FileNotFoundException e) {
310 // Ignore
311 } catch (IOException e) {
312 // Ignore
313 } finally {
314 if (in != null) {
315 try {
316 in.close();
317 } catch (IOException e) {
318 // Ignore
319 }
320 }
321 }
322 }
323
324 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
325 DataOutputStream out = null;
326 try {
327 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
328 out.writeUTF(configuration.locale);
329 out.writeInt(configuration.mcc);
330 out.writeInt(configuration.mnc);
331 out.flush();
332 } catch (FileNotFoundException e) {
333 // Ignore
334 } catch (IOException e) {
335 //noinspection ResultOfMethodCallIgnored
336 context.getFileStreamPath(PREFERENCES).delete();
337 } finally {
338 if (out != null) {
339 try {
340 out.close();
341 } catch (IOException e) {
342 // Ignore
343 }
344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 }
346 }
347
348 static int getScreen() {
349 synchronized (sLock) {
350 return sScreen;
351 }
352 }
353
354 static void setScreen(int screen) {
355 synchronized (sLock) {
356 sScreen = screen;
357 }
358 }
359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700361 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
363 Display display = getWindowManager().getDefaultDisplay();
364 boolean isPortrait = display.getWidth() < display.getHeight();
365
366 final int width = isPortrait ? display.getWidth() : display.getHeight();
367 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700368 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
370
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400371 private void loadHotseats() {
372 PackageManager pm = getPackageManager();
373 for (int i=0; i<mHotseats.length; i++) {
374 Intent intent = mHotseats[i];
375
376 if (LOGD) {
377 Log.d(TAG, "loadHotseats: hotseat " + i
378 + " initial intent=[" + intent.toUri(Intent.URI_INTENT_SCHEME)
379 + "]");
380 }
381
382 // fix up the default intents
383 if (intent.getAction().equals(Intent.ACTION_WEB_SEARCH)
384 && intent.getData().equals(Uri.EMPTY)) {
385 // use this to represent "default web browser"
386 intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
387 }
388 ComponentName com = intent.resolveActivity(pm);
389 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
390
391 // load the labels for accessibility
392 try {
393 ActivityInfo ai = pm.getActivityInfo(com, 0);
394 mHotseatLabels[i] = ai.loadLabel(pm);
395 } catch (PackageManager.NameNotFoundException ex) {
396 mHotseatLabels[i] = "";
397 }
398
399 if (LOGD) {
400 Log.d(TAG, "loadHotseats: hotseat " + i
401 + " intent=[" + mHotseats[i].toUri(Intent.URI_INTENT_SCHEME)
402 + "] label=[" + mHotseatLabels[i]
403 + "]"
404 );
405 }
406 }
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 @Override
410 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700411 mWaitingForResult = false;
412
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 // The pattern used here is that a user PICKs a specific application,
414 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
417 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Romain Guy94dabf12009-07-21 10:55:43 -0700419 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 switch (requestCode) {
421 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400422 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 break;
424 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800425 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 break;
427 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400428 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 break;
430 case REQUEST_PICK_LIVE_FOLDER:
431 addLiveFolder(data);
432 break;
433 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400434 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 case REQUEST_PICK_APPWIDGET:
437 addAppWidget(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 case REQUEST_CREATE_APPWIDGET:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400440 completeAddAppWidget(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700442 case REQUEST_PICK_WALLPAPER:
443 // We just wanted the activity result here so we can clear mWaitingForResult
444 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
Romain Guy18042c82009-11-06 11:44:55 -0800446 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
447 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
448 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700449 // Clean up the appWidgetId if we canceled
450 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
451 if (appWidgetId != -1) {
452 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
456
457 @Override
458 protected void onResume() {
459 super.onResume();
460
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800461 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 mWorkspaceLoading = true;
465 mModel.startLoader(this, true);
466 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468 }
469
470 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700471 protected void onPause() {
472 super.onPause();
Romain Guy1fbc1c82009-11-09 20:43:08 -0800473 dismissPreview(mPreviousView);
474 dismissPreview(mNextView);
Joe Onorato24b6fd82009-11-12 13:47:09 -0800475 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700476 }
Romain Guycbb89e42009-06-08 15:52:54 -0700477
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700478 @Override
479 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400480 // Flag the loader to stop early before switching
481 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800482 mAllAppsGrid.surrender();
Romain Guycbb89e42009-06-08 15:52:54 -0700483
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700484 if (PROFILE_ROTATE) {
485 android.os.Debug.startMethodTracing("/sdcard/launcher-rotate");
486 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800487 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700488 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700489
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800490 // We can't hide the IME if it was forced open. So don't bother
491 /*
492 @Override
493 public void onWindowFocusChanged(boolean hasFocus) {
494 super.onWindowFocusChanged(hasFocus);
495
496 if (hasFocus) {
497 final InputMethodManager inputManager = (InputMethodManager)
498 getSystemService(Context.INPUT_METHOD_SERVICE);
499 WindowManager.LayoutParams lp = getWindow().getAttributes();
500 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
501 android.os.Handler()) {
502 protected void onReceiveResult(int resultCode, Bundle resultData) {
503 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
504 }
505 });
506 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
507 }
508 }
509 */
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 private boolean acceptFilter() {
512 final InputMethodManager inputManager = (InputMethodManager)
513 getSystemService(Context.INPUT_METHOD_SERVICE);
514 return !inputManager.isFullscreenMode();
515 }
516
517 @Override
518 public boolean onKeyDown(int keyCode, KeyEvent event) {
519 boolean handled = super.onKeyDown(keyCode, event);
520 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
521 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
522 keyCode, event);
523 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700524 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700525 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700526 // showSearchDialog()
527 // If there are multiple keystrokes before the search dialog takes focus,
528 // onSearchRequested() will be called for every keystroke,
529 // but it is idempotent, so it's fine.
530 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 }
532 }
533
Joe Onorato8a9625e2010-01-28 15:55:35 -0800534 // Eat the long press event so the keyboard doesn't come up.
535 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
536 return true;
537 }
538
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 return handled;
540 }
541
Karl Rosaen138a0412009-04-23 19:00:21 -0700542 private String getTypedText() {
543 return mDefaultKeySsb.toString();
544 }
545
546 private void clearTypedText() {
547 mDefaultKeySsb.clear();
548 mDefaultKeySsb.clearSpans();
549 Selection.setSelection(mDefaultKeySsb, 0);
550 }
551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 /**
553 * Restores the previous state, if it exists.
554 *
555 * @param savedState The previous state.
556 */
557 private void restoreState(Bundle savedState) {
558 if (savedState == null) {
559 return;
560 }
561
Joe Onorato3a8820b2009-11-10 15:06:42 -0800562 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
563 if (allApps) {
564 showAllApps(false);
565 }
566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
568 if (currentScreen > -1) {
569 mWorkspace.setCurrentScreen(currentScreen);
570 }
571
572 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
573 if (addScreen > -1) {
574 mAddItemCellInfo = new CellLayout.CellInfo();
575 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
576 addItemCellInfo.valid = true;
577 addItemCellInfo.screen = addScreen;
578 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
579 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
580 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
581 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
582 addItemCellInfo.findVacantCellsFromOccupied(
583 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
584 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
585 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
586 mRestoring = true;
587 }
588
589 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
590 if (renameFolder) {
591 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400592 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 mRestoring = true;
594 }
595 }
596
597 /**
598 * Finds all the views we need and configure them properly.
599 */
600 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400601 DragController dragController = mDragController;
602
Romain Guyb1b69f52009-08-10 15:10:15 -0700603 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400604 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605
Joe Onorato7c312c12009-08-13 21:36:53 -0700606 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700607 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700608 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800609 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa2009-10-19 17:03:36 -0700610 // Manage focusability manually since this thing is always visible
Romain Guyc16fea72010-03-12 17:17:56 -0800611 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
614 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500615 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616
Joe Onorato7c312c12009-08-13 21:36:53 -0700617 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
618 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619
Joe Onorato7c312c12009-08-13 21:36:53 -0700620 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700621 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700622 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800623 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800624
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625 findViewById(R.id.hotseat_left).setContentDescription(mHotseatLabels[0]);
626 findViewById(R.id.hotseat_right).setContentDescription(mHotseatLabels[1]);
627
Romain Guy1fbc1c82009-11-09 20:43:08 -0800628 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
629 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
630
631 Drawable previous = mPreviousView.getDrawable();
632 Drawable next = mNextView.getDrawable();
Romain Guy8a73c512009-11-09 19:19:59 -0800633 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634
Joe Onorato0d44e942009-11-16 18:20:51 -0800635 mPreviousView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800636 mPreviousView.setOnLongClickListener(this);
Joe Onorato0d44e942009-11-16 18:20:51 -0800637 mNextView.setHapticFeedbackEnabled(false);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800638 mNextView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400641 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643
644 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400645 deleteZone.setDragController(dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400646 deleteZone.setHandle(findViewById(R.id.all_apps_button_cluster));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647
Joe Onorato00acb122009-08-04 16:04:30 -0400648 dragController.setDragScoller(workspace);
649 dragController.setDragListener(deleteZone);
650 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800651 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700652
Joe Onorato00acb122009-08-04 16:04:30 -0400653 // The order here is bottom to top.
654 dragController.addDropTarget(workspace);
655 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657
Romain Guy8a73c512009-11-09 19:19:59 -0800658 @SuppressWarnings({"UnusedDeclaration"})
659 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800660 if (!isAllAppsVisible()) {
661 mWorkspace.scrollLeft();
662 }
Romain Guy8a73c512009-11-09 19:19:59 -0800663 }
664
665 @SuppressWarnings({"UnusedDeclaration"})
666 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800667 if (!isAllAppsVisible()) {
668 mWorkspace.scrollRight();
669 }
Romain Guy8a73c512009-11-09 19:19:59 -0800670 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400671
672 @SuppressWarnings({"UnusedDeclaration"})
673 public void launchHotSeat(View v) {
674 int index = -1;
675 if (v.getId() == R.id.hotseat_left) {
676 index = 0;
677 } else if (v.getId() == R.id.hotseat_right) {
678 index = 1;
679 }
680
681 if (index >= 0 && mHotseats[index] != null) {
682 startActivitySafely(
683 mHotseats[index],
684 "hotseat"
685 );
686 }
687 }
Romain Guy8a73c512009-11-09 19:19:59 -0800688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 /**
690 * Creates a view representing a shortcut.
691 *
692 * @param info The data structure describing the shortcut.
693 *
694 * @return A View inflated from R.layout.application.
695 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800696 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 return createShortcut(R.layout.application,
698 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
699 }
700
701 /**
702 * Creates a view representing a shortcut inflated from the specified resource.
703 *
704 * @param layoutResId The id of the XML layout used to create the shortcut.
705 * @param parent The group the shortcut belongs to.
706 * @param info The data structure describing the shortcut.
707 *
708 * @return A View inflated from layoutResId.
709 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800710 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
712
Joe Onorato0589f0f2010-02-08 13:44:00 -0800713 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
714 new FastBitmapDrawable(info.getIcon(mIconCache)),
715 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 favorite.setText(info.title);
717 favorite.setTag(info);
718 favorite.setOnClickListener(this);
719
720 return favorite;
721 }
722
723 /**
724 * Add an application shortcut to the workspace.
725 *
726 * @param data The intent describing the application.
727 * @param cellInfo The position on screen where to create the shortcut.
728 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 cellInfo.screen = mWorkspace.getCurrentScreen();
731 if (!findSingleSlot(cellInfo)) return;
732
Joe Onorato0589f0f2010-02-08 13:44:00 -0800733 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
734 data, context);
735
Romain Guy73b979d2009-06-09 12:57:21 -0700736 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800737 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800739 info.container = ItemInfo.NO_ID;
740 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
741 } else {
742 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744 }
Romain Guycbb89e42009-06-08 15:52:54 -0700745
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 /**
747 * Add a shortcut to the workspace.
748 *
749 * @param data The intent describing the shortcut.
750 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 cellInfo.screen = mWorkspace.getCurrentScreen();
754 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700755
Joe Onorato0589f0f2010-02-08 13:44:00 -0800756 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757
758 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
761 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763 }
764
Romain Guycbb89e42009-06-08 15:52:54 -0700765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700769 * @param data The intent describing the appWidgetId.
770 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 Bundle extras = data.getExtras();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700774 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Romain Guycbb89e42009-06-08 15:52:54 -0700775
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800776 if (LOGD) Log.d(TAG, "dumping extras content=" + extras.toString());
Romain Guycbb89e42009-06-08 15:52:54 -0700777
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700778 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700779
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700780 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700782 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 // Try finding open space on Launcher screen
785 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800786 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
787 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
788 return;
789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700791 // Build Launcher-specific widget info and save to database
792 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 launcherInfo.spanX = spans[0];
794 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 LauncherModel.addItemToDatabase(this, launcherInfo,
797 LauncherSettings.Favorites.CONTAINER_DESKTOP,
798 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
799
800 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700804 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700805
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700806 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400810 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
812 }
Romain Guycbb89e42009-06-08 15:52:54 -0700813
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
815 mDesktopItems.remove(launcherInfo);
816 launcherInfo.hostView = null;
817 }
818
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700819 public LauncherAppWidgetHost getAppWidgetHost() {
820 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
Romain Guycbb89e42009-06-08 15:52:54 -0700822
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800823 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800824 getWindow().closeAllPanels();
825
826 try {
827 dismissDialog(DIALOG_CREATE_SHORTCUT);
828 // Unlock the workspace if the dialog was showing
829 } catch (Exception e) {
830 // An exception is thrown if the dialog is not visible, which is fine
831 }
832
833 try {
834 dismissDialog(DIALOG_RENAME_FOLDER);
835 // Unlock the workspace if the dialog was showing
836 } catch (Exception e) {
837 // An exception is thrown if the dialog is not visible, which is fine
838 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800839
840 // Whatever we were doing is hereby canceled.
841 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800842 }
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 @Override
845 protected void onNewIntent(Intent intent) {
846 super.onNewIntent(intent);
847
848 // Close the menu
849 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800850 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800851 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700852
Joe Onorato14f122b2009-11-19 14:06:36 -0800853 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
854 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
855 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800856 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800857 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800858 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800859 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700860
Joe Onorato3a8820b2009-11-10 15:06:42 -0800861 final View v = getWindow().peekDecorView();
862 if (v != null && v.getWindowToken() != null) {
863 InputMethodManager imm = (InputMethodManager)getSystemService(
864 INPUT_METHOD_SERVICE);
865 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
867 }
868 }
869
870 @Override
871 protected void onRestoreInstanceState(Bundle savedInstanceState) {
872 // Do not call super here
873 mSavedInstanceState = savedInstanceState;
874 }
875
876 @Override
877 protected void onSaveInstanceState(Bundle outState) {
878 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
879
880 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
881 if (folders.size() > 0) {
882 final int count = folders.size();
883 long[] ids = new long[count];
884 for (int i = 0; i < count; i++) {
885 final FolderInfo info = folders.get(i).getInfo();
886 ids[i] = info.id;
887 }
888 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
889 } else {
890 super.onSaveInstanceState(outState);
891 }
892
Joe Onoratofb0ca672009-09-14 17:55:46 -0400893 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -0800894 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -0700896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
898 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
899 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
900 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
901
902 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
903 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
904 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
905 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
906 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
907 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
908 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
909 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
910 layout.getOccupiedCells());
911 }
912
913 if (mFolderInfo != null && mWaitingForResult) {
914 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
915 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
916 }
917 }
918
919 @Override
920 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -0700922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700924 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800926 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928
929 TextKeyListener.getInstance().release();
930
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800934
935 getContentResolver().unregisterContentObserver(mWidgetObserver);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800936
937 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -0800938 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800939
940 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
943 @Override
944 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -0700945 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 super.startActivityForResult(intent, requestCode);
947 }
948
949 @Override
Romain Guycbb89e42009-06-08 15:52:54 -0700950 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700952
Joe Onorato7bb17492009-09-24 17:51:01 -0700953 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -0700954
Karl Rosaen138a0412009-04-23 19:00:21 -0700955 if (initialQuery == null) {
956 // Use any text typed in the launcher as the initial query
957 initialQuery = getTypedText();
958 clearTypedText();
959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 if (appSearchData == null) {
961 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +0000962 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 }
Romain Guycbb89e42009-06-08 15:52:54 -0700964
Karl Rosaen138a0412009-04-23 19:00:21 -0700965 final SearchManager searchManager =
966 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -0700968 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970
971 @Override
972 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400973 if (isWorkspaceLocked()) {
974 return false;
975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
977 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800978
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
980 .setIcon(android.R.drawable.ic_menu_add)
981 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800982 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 .setIcon(android.R.drawable.ic_menu_gallery)
984 .setAlphabeticShortcut('W');
985 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
986 .setIcon(android.R.drawable.ic_search_category_default)
987 .setAlphabeticShortcut(SearchManager.MENU_KEY);
988 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
989 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
990 .setAlphabeticShortcut('N');
991
992 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -0700993 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
994 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
996 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
997 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
998 .setIntent(settings);
999
1000 return true;
1001 }
1002
1003 @Override
1004 public boolean onPrepareOptionsMenu(Menu menu) {
1005 super.onPrepareOptionsMenu(menu);
1006
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001007 // If all apps is animating, don't show the menu, because we don't know
1008 // which one to show.
1009 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1010 return false;
1011 }
1012
1013 // Only show the add and wallpaper options when we're not in all apps.
1014 boolean visible = !mAllAppsGrid.isOpaque();
1015 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1016 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1017
1018 // Disable add if the workspace is full.
1019 if (visible) {
1020 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1021 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023
1024 return true;
1025 }
1026
1027 @Override
1028 public boolean onOptionsItemSelected(MenuItem item) {
1029 switch (item.getItemId()) {
1030 case MENU_ADD:
1031 addItems();
1032 return true;
1033 case MENU_WALLPAPER_SETTINGS:
1034 startWallpaper();
1035 return true;
1036 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001037 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 return true;
1039 case MENU_NOTIFICATIONS:
1040 showNotifications();
1041 return true;
1042 }
1043
1044 return super.onOptionsItemSelected(item);
1045 }
Romain Guycbb89e42009-06-08 15:52:54 -07001046
Karl Rosaen138a0412009-04-23 19:00:21 -07001047 /**
1048 * Indicates that we want global search for this activity by setting the globalSearch
1049 * argument for {@link #startSearch} to true.
1050 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001052 @Override
1053 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001054 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001055 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001056 }
1057
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 public boolean isWorkspaceLocked() {
1059 return mWorkspaceLoading || mWaitingForResult;
1060 }
1061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001063 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 showAddDialog(mMenuAddInfo);
1065 }
1066
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 void addAppWidget(Intent data) {
1068 // TODO: catch bad widget exception when sent
1069 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001070 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071
Bjorn Bringert7984c942009-12-09 15:38:25 +00001072 if (appWidget.configure != null) {
1073 // Launch over to configure widget, if needed
1074 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1075 intent.setComponent(appWidget.configure);
1076 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1077
Romain Guy8e633c52010-03-04 12:51:36 -08001078 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001080 // Otherwise just add it
1081 onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083 }
Romain Guycbb89e42009-06-08 15:52:54 -07001084
Joe Onoratodeb98af2010-02-19 14:59:39 -08001085 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001086 // Handle case where user selected "Applications"
1087 String applicationName = getResources().getString(R.string.group_applications);
1088 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001089
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001090 if (applicationName != null && applicationName.equals(shortcutName)) {
1091 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1092 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001094 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1095 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001096 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001097 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001098 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
1101
1102 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001103 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001104 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001105 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001107 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001109 } else {
1110 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 UserFolderInfo folderInfo = new UserFolderInfo();
1116 folderInfo.title = getText(R.string.folder_name);
1117
1118 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1119 cellInfo.screen = mWorkspace.getCurrentScreen();
1120 if (!findSingleSlot(cellInfo)) return;
1121
1122 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 LauncherModel.addItemToDatabase(this, folderInfo,
1124 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127
1128 // Create the view
1129 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1130 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1131 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
Romain Guycbb89e42009-06-08 15:52:54 -07001134
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 void removeFolder(FolderInfo folder) {
1136 mFolders.remove(folder.id);
1137 }
1138
1139 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 cellInfo.screen = mWorkspace.getCurrentScreen();
1141 if (!findSingleSlot(cellInfo)) return;
1142
1143 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1144
1145 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1147 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1149 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
1151 }
1152
1153 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1154 CellLayout.CellInfo cellInfo, boolean notify) {
1155
1156 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1157 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1158
1159 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 Intent.ShortcutIconResource iconResource = null;
1161
1162 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1163 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1164 try {
1165 iconResource = (Intent.ShortcutIconResource) extra;
1166 final PackageManager packageManager = context.getPackageManager();
1167 Resources resources = packageManager.getResourcesForApplication(
1168 iconResource.packageName);
1169 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1170 icon = resources.getDrawable(id);
1171 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001172 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 }
1174 }
1175
1176 if (icon == null) {
1177 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1178 }
1179
1180 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001181 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 info.title = name;
1183 info.iconResource = iconResource;
1184 info.uri = data.getData();
1185 info.baseIntent = baseIntent;
1186 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1187 LiveFolders.DISPLAY_MODE_GRID);
1188
1189 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1190 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
1193 return info;
1194 }
1195
1196 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1197 final int[] xy = new int[2];
1198 if (findSlot(cellInfo, xy, 1, 1)) {
1199 cellInfo.cellX = xy[0];
1200 cellInfo.cellY = xy[1];
1201 return true;
1202 }
1203 return false;
1204 }
1205
1206 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1207 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1208 boolean[] occupied = mSavedState != null ?
1209 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1210 cellInfo = mWorkspace.findAllVacantCells(occupied);
1211 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1212 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1213 return false;
1214 }
1215 }
1216 return true;
1217 }
1218
1219 private void showNotifications() {
1220 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1221 if (statusBar != null) {
1222 statusBar.expand();
1223 }
1224 }
1225
1226 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001227 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001229 Intent chooser = Intent.createChooser(pickWallpaper,
1230 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001231 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1232 // Removed in Eclair MR1
1233// WallpaperManager wm = (WallpaperManager)
1234// getSystemService(Context.WALLPAPER_SERVICE);
1235// WallpaperInfo wi = wm.getWallpaperInfo();
1236// if (wi != null && wi.getSettingsActivity() != null) {
1237// LabeledIntent li = new LabeledIntent(getPackageName(),
1238// R.string.configure_wallpaper, 0);
1239// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1240// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1241// }
Mike Clerona0618e42009-10-22 13:55:21 -07001242 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001245 /**
1246 * Registers various content observers. The current implementation registers
1247 * only a favorites observer to keep track of the favorites applications.
1248 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001249 private void registerContentObservers() {
1250 ContentResolver resolver = getContentResolver();
1251 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1252 true, mWidgetObserver);
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 @Override
1256 public boolean dispatchKeyEvent(KeyEvent event) {
1257 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1258 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001260 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001261 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001262 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001263 dumpState();
1264 return true;
1265 }
1266 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001267 }
1268 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1269 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001270 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 return true;
1272 }
1273 }
1274
1275 return super.dispatchKeyEvent(event);
1276 }
1277
Joe Onorato88ec0992009-11-19 13:16:06 -08001278 @Override
1279 public void onBackPressed() {
1280 if (isAllAppsVisible()) {
1281 closeAllApps(true);
1282 } else {
1283 closeFolder();
1284 }
1285 dismissPreview(mPreviousView);
1286 dismissPreview(mNextView);
1287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 private void closeFolder() {
1290 Folder folder = mWorkspace.getOpenFolder();
1291 if (folder != null) {
1292 closeFolder(folder);
1293 }
1294 }
1295
1296 void closeFolder(Folder folder) {
1297 folder.getInfo().opened = false;
1298 ViewGroup parent = (ViewGroup) folder.getParent();
1299 if (parent != null) {
1300 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001301 if (folder instanceof DropTarget) {
1302 // Live folders aren't DropTargets.
1303 mDragController.removeDropTarget((DropTarget)folder);
1304 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306 folder.onClose();
1307 }
1308
1309 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001310 * Re-listen when widgets are reset.
1311 */
1312 private void onAppWidgetReset() {
1313 mAppWidgetHost.startListening();
1314 }
1315
1316 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1318 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001320 private void unbindDesktopItems() {
1321 for (ItemInfo item: mDesktopItems) {
1322 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001325
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 /**
1327 * Launches the intent referred by the clicked shortcut.
1328 *
1329 * @param v The view representing the clicked shortcut.
1330 */
1331 public void onClick(View v) {
1332 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001333 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001335 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001336 int[] pos = new int[2];
1337 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001338 intent.setSourceBounds(new Rect(pos[0], pos[1],
1339 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001340 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 } else if (tag instanceof FolderInfo) {
1342 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001343 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001344 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001345 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001346 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001347 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350 }
1351
Joe Onoratof984e852010-03-25 09:47:45 -07001352 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1354 try {
1355 startActivity(intent);
1356 } catch (ActivityNotFoundException e) {
1357 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001358 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 } catch (SecurityException e) {
1360 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001361 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001363 "or use the exported attribute for this activity. "
1364 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366 }
Romain Guy8e633c52010-03-04 12:51:36 -08001367
1368 void startActivityForResultSafely(Intent intent, int requestCode) {
1369 try {
1370 startActivityForResult(intent, requestCode);
1371 } catch (ActivityNotFoundException e) {
1372 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1373 } catch (SecurityException e) {
1374 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1375 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1376 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1377 "or use the exported attribute for this activity.", e);
1378 }
1379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380
1381 private void handleFolderClick(FolderInfo folderInfo) {
1382 if (!folderInfo.opened) {
1383 // Close any open folder
1384 closeFolder();
1385 // Open the requested folder
1386 openFolder(folderInfo);
1387 } else {
1388 // Find the open folder...
1389 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1390 int folderScreen;
1391 if (openFolder != null) {
1392 folderScreen = mWorkspace.getScreenForView(openFolder);
1393 // .. and close it
1394 closeFolder(openFolder);
1395 if (folderScreen != mWorkspace.getCurrentScreen()) {
1396 // Close any folder open on the current screen
1397 closeFolder();
1398 // Pull the folder onto this screen
1399 openFolder(folderInfo);
1400 }
1401 }
1402 }
1403 }
1404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 /**
1406 * Opens the user fodler described by the specified tag. The opening of the folder
1407 * is animated relative to the specified View. If the View is null, no animation
1408 * is played.
1409 *
1410 * @param folderInfo The FolderInfo describing the folder to open.
1411 */
1412 private void openFolder(FolderInfo folderInfo) {
1413 Folder openFolder;
1414
1415 if (folderInfo instanceof UserFolderInfo) {
1416 openFolder = UserFolder.fromXml(this);
1417 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001418 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 } else {
1420 return;
1421 }
1422
Joe Onorato00acb122009-08-04 16:04:30 -04001423 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 openFolder.setLauncher(this);
1425
1426 openFolder.bind(folderInfo);
1427 folderInfo.opened = true;
1428
1429 mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4);
1430 openFolder.onOpen();
1431 }
1432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001434 switch (v.getId()) {
1435 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001436 if (!isAllAppsVisible()) {
1437 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1438 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001439 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001440 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001441 return true;
1442 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001443 if (!isAllAppsVisible()) {
1444 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1445 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001446 showPreviews(v);
1447 }
1448 return true;
1449 case R.id.all_apps_button:
1450 if (!isAllAppsVisible()) {
1451 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1452 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1453 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001454 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001455 return true;
1456 }
1457
Joe Onorato9c1289c2009-08-17 11:03:03 -04001458 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 return false;
1460 }
1461
1462 if (!(v instanceof CellLayout)) {
1463 v = (View) v.getParent();
1464 }
1465
1466 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1467
1468 // This happens when long clicking an item with the dpad/trackball
1469 if (cellInfo == null) {
1470 return true;
1471 }
1472
1473 if (mWorkspace.allowLongPress()) {
1474 if (cellInfo.cell == null) {
1475 if (cellInfo.valid) {
1476 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001477 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001478 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1479 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 showAddDialog(cellInfo);
1481 }
1482 } else {
1483 if (!(cellInfo.cell instanceof Folder)) {
1484 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001485 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1486 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 mWorkspace.startDrag(cellInfo);
1488 }
1489 }
1490 }
1491 return true;
1492 }
1493
Romain Guye6b8e2f2009-11-10 11:56:55 -08001494 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001495 private void dismissPreview(final View v) {
1496 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001497 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001498 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1499 public void onDismiss() {
1500 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1501 int count = group.getChildCount();
1502 for (int i = 0; i < count; i++) {
1503 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1504 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001505 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1506 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1507
1508 v.setTag(R.id.workspace, null);
1509 v.setTag(R.id.icon, null);
1510 window.setOnDismissListener(null);
1511 }
1512 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001513 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001514 }
1515 v.setTag(null);
1516 }
1517
Romain Guyf8e6a802009-12-07 17:48:02 -08001518 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001519 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001520 }
1521
Romain Guya6abce82009-11-10 02:54:41 -08001522 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001523 final Resources resources = getResources();
1524 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001525
Romain Guya6abce82009-11-10 02:54:41 -08001526 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001527
1528 float max = workspace.getChildCount();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001529
Romain Guyf8e6a802009-12-07 17:48:02 -08001530 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001531 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001532 int extraW = (int) ((r.left + r.right) * max);
1533 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001534
1535 int aW = cell.getWidth() - extraW;
1536 float w = aW / max;
1537
1538 int width = cell.getWidth();
1539 int height = cell.getHeight();
1540 int x = cell.getLeftPadding();
1541 int y = cell.getTopPadding();
1542 width -= (x + cell.getRightPadding());
1543 height -= (y + cell.getBottomPadding());
1544
1545 float scale = w / width;
1546
1547 int count = end - start;
1548
1549 final float sWidth = width * scale;
1550 float sHeight = height * scale;
1551
Romain Guye6b8e2f2009-11-10 11:56:55 -08001552 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001553
Romain Guye6b8e2f2009-11-10 11:56:55 -08001554 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1555 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001556
1557 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001558 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001559 cell = (CellLayout) workspace.getChildAt(i);
1560
Romain Guyf8e6a802009-12-07 17:48:02 -08001561 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001562 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001563
1564 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001565 c.scale(scale, scale);
1566 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1567 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001568
Romain Guy9d31e9f2009-11-11 18:54:28 -08001569 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001570 image.setImageBitmap(bitmap);
1571 image.setTag(i);
1572 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001573 image.setOnFocusChangeListener(handler);
1574 image.setFocusable(true);
1575 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001576
1577 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001578 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1579
1580 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001581 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001582
1583 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001584 p.setContentView(preview);
1585 p.setWidth((int) (sWidth * count + extraW));
1586 p.setHeight((int) (sHeight + extraH));
1587 p.setAnimationStyle(R.style.AnimationPreview);
1588 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001589 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001590 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001591 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001592
Romain Guye6b8e2f2009-11-10 11:56:55 -08001593 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1594 public void onDismiss() {
1595 dismissPreview(anchor);
1596 }
1597 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001598
1599 anchor.setTag(p);
1600 anchor.setTag(R.id.workspace, preview);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001601 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001602 }
1603
Romain Guy9d31e9f2009-11-11 18:54:28 -08001604 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001605 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001606
Romain Guye6b8e2f2009-11-10 11:56:55 -08001607 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001608 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001609 }
1610
1611 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001612 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001613 v.post(this);
1614 }
1615
1616 public void run() {
1617 dismissPreview(mAnchor);
1618 }
1619
1620 public void onFocusChange(View v, boolean hasFocus) {
1621 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001622 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001623 }
Romain Guya6abce82009-11-10 02:54:41 -08001624 }
1625 }
1626
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 Workspace getWorkspace() {
1628 return mWorkspace;
1629 }
1630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 @Override
1632 protected Dialog onCreateDialog(int id) {
1633 switch (id) {
1634 case DIALOG_CREATE_SHORTCUT:
1635 return new CreateShortcut().createDialog();
1636 case DIALOG_RENAME_FOLDER:
1637 return new RenameFolder().createDialog();
1638 }
1639
1640 return super.onCreateDialog(id);
1641 }
1642
1643 @Override
1644 protected void onPrepareDialog(int id, Dialog dialog) {
1645 switch (id) {
1646 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 break;
1648 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001649 if (mFolderInfo != null) {
1650 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1651 final CharSequence text = mFolderInfo.title;
1652 input.setText(text);
1653 input.setSelection(0, text.length());
1654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 break;
1656 }
1657 }
1658
1659 void showRenameDialog(FolderInfo info) {
1660 mFolderInfo = info;
1661 mWaitingForResult = true;
1662 showDialog(DIALOG_RENAME_FOLDER);
1663 }
1664
1665 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1666 mAddItemCellInfo = cellInfo;
1667 mWaitingForResult = true;
1668 showDialog(DIALOG_CREATE_SHORTCUT);
1669 }
1670
Joe Onoratodeb98af2010-02-19 14:59:39 -08001671 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001672 Bundle bundle = new Bundle();
1673
1674 ArrayList<String> shortcutNames = new ArrayList<String>();
1675 shortcutNames.add(getString(R.string.group_applications));
1676 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1677
1678 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1679 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1680 R.drawable.ic_launcher_application));
1681 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1682
1683 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1684 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001685 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001686 pickIntent.putExtras(bundle);
1687
Joe Onoratodeb98af2010-02-19 14:59:39 -08001688 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 private class RenameFolder {
1692 private EditText mInput;
1693
1694 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1696 mInput = (EditText) layout.findViewById(R.id.folder_name);
1697
1698 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1699 builder.setIcon(0);
1700 builder.setTitle(getString(R.string.rename_folder_title));
1701 builder.setCancelable(true);
1702 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1703 public void onCancel(DialogInterface dialog) {
1704 cleanup();
1705 }
1706 });
1707 builder.setNegativeButton(getString(R.string.cancel_action),
1708 new Dialog.OnClickListener() {
1709 public void onClick(DialogInterface dialog, int which) {
1710 cleanup();
1711 }
1712 }
1713 );
1714 builder.setPositiveButton(getString(R.string.rename_action),
1715 new Dialog.OnClickListener() {
1716 public void onClick(DialogInterface dialog, int which) {
1717 changeFolderName();
1718 }
1719 }
1720 );
1721 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001722
1723 final AlertDialog dialog = builder.create();
1724 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1725 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001726 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001727 mInput.requestFocus();
1728 InputMethodManager inputManager = (InputMethodManager)
1729 getSystemService(Context.INPUT_METHOD_SERVICE);
1730 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001731 }
1732 });
1733
1734 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
1736
1737 private void changeFolderName() {
1738 final String name = mInput.getText().toString();
1739 if (!TextUtils.isEmpty(name)) {
1740 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 mFolderInfo.title = name;
1743 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1744
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001746 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 mModel.setWorkspaceDirty();
1748 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 } else {
1750 final FolderIcon folderIcon = (FolderIcon)
1751 mWorkspace.getViewForTag(mFolderInfo);
1752 if (folderIcon != null) {
1753 folderIcon.setText(name);
1754 getWorkspace().requestLayout();
1755 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001756 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 mModel.setWorkspaceDirty();
1758 mWorkspaceLoading = true;
1759 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
1761 }
1762 }
1763 cleanup();
1764 }
1765
1766 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 dismissDialog(DIALOG_RENAME_FOLDER);
1768 mWaitingForResult = false;
1769 mFolderInfo = null;
1770 }
1771 }
1772
Joe Onoratofb0ca672009-09-14 17:55:46 -04001773 boolean isAllAppsVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -07001774 return mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04001775 }
1776
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001777 // AllAppsView.Watcher
1778 public void zoomed(float zoom) {
1779 if (zoom == 1.0f) {
1780 mWorkspace.setVisibility(View.GONE);
1781 }
1782 }
1783
Joe Onorato3a8820b2009-11-10 15:06:42 -08001784 void showAllApps(boolean animated) {
1785 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001786
Romain Guyc16fea72010-03-12 17:17:56 -08001787 ((View) mAllAppsGrid).setFocusable(true);
1788 ((View) mAllAppsGrid).requestFocus();
Mike Cleronb6082fa2009-10-19 17:03:36 -07001789
Joe Onorato7c312c12009-08-13 21:36:53 -07001790 // TODO: fade these two too
1791 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001792 }
1793
Joe Onoratob2061212009-11-24 16:13:54 -05001794 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001795 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001796 * - Home from workspace
1797 * - from center screen
1798 * - from other screens
1799 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001800 * - from center screen
1801 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001802 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001803 * - from center screen
1804 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001805 * - Launch app from workspace and quit
1806 * - with back
1807 * - with home
1808 * - Launch app from all apps and quit
1809 * - with back
1810 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001811 * - Go to a screen that's not the default, then all
1812 * apps, and launch and app, and go back
1813 * - with back
1814 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001815 * - On workspace, long press power and go back
1816 * - with back
1817 * - with home
1818 * - On all apps, long press power and go back
1819 * - with back
1820 * - with home
1821 * - On workspace, power off
1822 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001823 * - Launch an app and turn off the screen while in that app
1824 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001825 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001826 * - From all apps
1827 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001828 * - Enter and exit car mode (becuase it causes an extra configuration changed)
1829 * - From all apps
1830 * - From the center workspace
1831 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05001832 */
Joe Onorato7bb17492009-09-24 17:51:01 -07001833 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001834 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001835 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001836 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05001837 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001838 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07001839 }
Joe Onorato7404ee42009-07-31 11:54:44 -07001840 }
1841
Joe Onorato7c312c12009-08-13 21:36:53 -07001842 void lockAllApps() {
1843 // TODO
1844 }
1845
1846 void unlockAllApps() {
1847 // TODO
1848 }
1849
Joe Onorato7404ee42009-07-31 11:54:44 -07001850 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 * Displays the shortcut creation dialog and launches, if necessary, the
1852 * appropriate activity.
1853 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001854 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07001855 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
1856 DialogInterface.OnShowListener {
1857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07001859
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07001862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1864 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001865 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07001866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 builder.setInverseBackgroundForced(true);
1868
1869 AlertDialog dialog = builder.create();
1870 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07001871 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07001872 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 return dialog;
1875 }
1876
1877 public void onCancel(DialogInterface dialog) {
1878 mWaitingForResult = false;
1879 cleanup();
1880 }
1881
Romain Guycbb89e42009-06-08 15:52:54 -07001882 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07001883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08001886 try {
1887 dismissDialog(DIALOG_CREATE_SHORTCUT);
1888 } catch (Exception e) {
1889 // An exception is thrown if the dialog is not visible, which is fine
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
1892
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001893 /**
1894 * Handle the action clicked in the "Add to home" dialog.
1895 */
1896 public void onClick(DialogInterface dialog, int which) {
1897 Resources res = getResources();
1898 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07001899
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 switch (which) {
1901 case AddAdapter.ITEM_SHORTCUT: {
1902 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08001903 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 break;
1905 }
Romain Guycbb89e42009-06-08 15:52:54 -07001906
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001907 case AddAdapter.ITEM_APPWIDGET: {
1908 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001910 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
1911 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001912 // start the pick activity
1913 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
1914 break;
1915 }
Romain Guycbb89e42009-06-08 15:52:54 -07001916
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001917 case AddAdapter.ITEM_LIVE_FOLDER: {
1918 // Insert extra item to handle inserting folder
1919 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001921 ArrayList<String> shortcutNames = new ArrayList<String>();
1922 shortcutNames.add(res.getString(R.string.group_folder));
1923 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07001924
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001925 ArrayList<ShortcutIconResource> shortcutIcons =
1926 new ArrayList<ShortcutIconResource>();
1927 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1928 R.drawable.ic_launcher_folder));
1929 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1930
1931 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1932 pickIntent.putExtra(Intent.EXTRA_INTENT,
1933 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
1934 pickIntent.putExtra(Intent.EXTRA_TITLE,
1935 getText(R.string.title_select_live_folder));
1936 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07001937
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001938 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
1939 break;
1940 }
1941
1942 case AddAdapter.ITEM_WALLPAPER: {
1943 startWallpaper();
1944 break;
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947 }
Romain Guy7b4ef332009-07-14 13:58:08 -07001948
1949 public void onShow(DialogInterface dialog) {
Romain Guy406d7032010-03-04 18:59:31 -08001950 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07001951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 }
1953
1954 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001955 * Receives notifications when applications are added/removed.
1956 */
1957 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
1958 @Override
1959 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001960 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05001961 String reason = intent.getStringExtra("reason");
1962 if (!"homekey".equals(reason)) {
1963 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001964 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05001965 animate = false;
1966 }
Joe Onoratob2061212009-11-24 16:13:54 -05001967 closeAllApps(animate);
1968 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001969 }
1970 }
1971
1972 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001973 * Receives notifications whenever the appwidgets are reset.
1974 */
1975 private class AppWidgetResetObserver extends ContentObserver {
1976 public AppWidgetResetObserver() {
1977 super(new Handler());
1978 }
1979
1980 @Override
1981 public void onChange(boolean selfChange) {
1982 onAppWidgetReset();
1983 }
1984 }
1985
1986 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001987 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 public int getCurrentWorkspaceScreen() {
1990 return mWorkspace.getCurrentScreen();
1991 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07001992
Joe Onorato9c1289c2009-08-17 11:03:03 -04001993 /**
1994 * Refreshes the shortcuts shown on the workspace.
1995 *
1996 * Implementation of the method from LauncherModel.Callbacks.
1997 */
1998 public void startBinding() {
1999 final Workspace workspace = mWorkspace;
2000 int count = workspace.getChildCount();
2001 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002002 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002003 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2004 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002005
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 if (DEBUG_USER_INTERFACE) {
2007 android.widget.Button finishButton = new android.widget.Button(this);
2008 finishButton.setText("Finish");
2009 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2010
2011 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2012 public void onClick(View v) {
2013 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002015 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
2017 }
2018
2019 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002020 * Bind the items start-end from the list.
2021 *
2022 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002024 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2025
2026 final Workspace workspace = mWorkspace;
2027
2028 for (int i=start; i<end; i++) {
2029 final ItemInfo item = shortcuts.get(i);
2030 mDesktopItems.add(item);
2031 switch (item.itemType) {
2032 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2033 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002034 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002035 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2036 false);
2037 break;
2038 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2039 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2040 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2041 (UserFolderInfo) item);
2042 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2043 false);
2044 break;
2045 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2046 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2047 R.layout.live_folder_icon, this,
2048 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2049 (LiveFolderInfo) item);
2050 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2051 false);
2052 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 /**
2060 * Implementation of the method from LauncherModel.Callbacks.
2061 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002062 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2063 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002064 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002066
2067 /**
2068 * Add the views for a widget to the workspace.
2069 *
2070 * Implementation of the method from LauncherModel.Callbacks.
2071 */
2072 public void bindAppWidget(LauncherAppWidgetInfo item) {
2073 final Workspace workspace = mWorkspace;
2074
2075 final int appWidgetId = item.appWidgetId;
2076 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
2077 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2078
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2080 item.hostView.setTag(item);
2081
2082 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2083 item.cellY, item.spanX, item.spanY, false);
2084
2085 workspace.requestLayout();
2086
2087 mDesktopItems.add(item);
2088 }
2089
2090 /**
2091 * Callback saying that there aren't any more items to bind.
2092 *
2093 * Implementation of the method from LauncherModel.Callbacks.
2094 */
2095 public void finishBindingItems() {
2096 if (mSavedState != null) {
2097 if (!mWorkspace.hasFocus()) {
2098 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2099 }
2100
2101 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2102 if (userFolders != null) {
2103 for (long folderId : userFolders) {
2104 final FolderInfo info = mFolders.get(folderId);
2105 if (info != null) {
2106 openFolder(info);
2107 }
2108 }
2109 final Folder openFolder = mWorkspace.getOpenFolder();
2110 if (openFolder != null) {
2111 openFolder.requestFocus();
2112 }
2113 }
2114
Joe Onorato9c1289c2009-08-17 11:03:03 -04002115 mSavedState = null;
2116 }
2117
2118 if (mSavedInstanceState != null) {
2119 super.onRestoreInstanceState(mSavedInstanceState);
2120 mSavedInstanceState = null;
2121 }
2122
Joe Onorato9c1289c2009-08-17 11:03:03 -04002123 mWorkspaceLoading = false;
2124 }
2125
2126 /**
2127 * Add the icons for all apps.
2128 *
2129 * Implementation of the method from LauncherModel.Callbacks.
2130 */
2131 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002132 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002133 }
2134
2135 /**
2136 * A package was installed.
2137 *
2138 * Implementation of the method from LauncherModel.Callbacks.
2139 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002140 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002141 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002142 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002143 }
2144
2145 /**
2146 * A package was updated.
2147 *
2148 * Implementation of the method from LauncherModel.Callbacks.
2149 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002150 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002152 mWorkspace.updateShortcuts(apps);
2153 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002154 }
2155
2156 /**
2157 * A package was uninstalled.
2158 *
2159 * Implementation of the method from LauncherModel.Callbacks.
2160 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002161 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002162 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002163 mWorkspace.removeItems(apps);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002164 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 }
Joe Onoratobe386092009-11-17 17:32:16 -08002166
2167 /**
2168 * Prints out out state for debugging.
2169 */
2170 public void dumpState() {
2171 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002172 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002173 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2174 Log.d(TAG, "mRestoring=" + mRestoring);
2175 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2176 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2177 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2178 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002179 mModel.dumpState();
2180 mAllAppsGrid.dumpState();
2181 Log.d(TAG, "END launcher2 dump state");
2182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183}