blob: 69911a7886606df44cebdbe821b9fa3e2a49c6a0 [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
Winson Chungaafa03c2010-06-11 17:34:16 -070019import java.io.DataInputStream;
20import java.io.DataOutputStream;
21import java.io.FileNotFoundException;
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.HashMap;
25import java.util.List;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000026
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.Activity;
28import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070032import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.IntentFilter;
Michael Jurkaaf442092010-06-10 17:01:57 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080052import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080054import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070062import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.TextUtils;
66import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
74import android.view.View;
75import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070076import android.view.WindowManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View.OnLongClickListener;
Michael Jurkaaf442092010-06-10 17:01:57 -070078import android.view.animation.AnimationUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070081import android.widget.ImageView;
82import android.widget.LinearLayout;
83import android.widget.PopupWindow;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.widget.TextView;
85import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Winson Chungaafa03c2010-06-11 17:34:16 -070087import com.android.common.Search;
88import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -080089
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090/**
91 * Default launcher application.
92 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040093public final class Launcher extends Activity
Daniel Sandlerc351eb82010-03-03 15:05:19 -050094 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -080095 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -070096 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -040099 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400100 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 private static final int WALLPAPER_SCREENS_SPAN = 2;
103
104 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800105 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 private static final int MENU_ADD = Menu.FIRST + 1;
108 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
109 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
110 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700111 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
113 private static final int REQUEST_CREATE_SHORTCUT = 1;
114 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700115 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int REQUEST_PICK_APPLICATION = 6;
117 private static final int REQUEST_PICK_SHORTCUT = 7;
118 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700120 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
123
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800124 static final int SCREEN_COUNT = 5;
125 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 static final int NUMBER_CELLS_X = 4;
Romain Guycbb89e42009-06-08 15:52:54 -0700127 static final int NUMBER_CELLS_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Joe Onorato7c312c12009-08-13 21:36:53 -0700129 static final int DIALOG_CREATE_SHORTCUT = 1;
130 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Romain Guy98d01652009-06-30 16:21:04 -0700132 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 // Type: int
135 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
136 // Type: boolean
137 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
138 // Type: long
139 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
140 // Type: int
141 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
142 // Type: int
143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
144 // Type: int
145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
146 // Type: int
147 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
152 // Type: int
153 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
154 // Type: int[]
155 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
156 // Type: boolean
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158 // Type: long
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800166 private final BroadcastReceiver mCloseSystemDialogsReceiver
167 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800168 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private LayoutInflater mInflater;
171
Joe Onorato00acb122009-08-04 16:04:30 -0400172 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private CellLayout.CellInfo mAddItemCellInfo;
179 private CellLayout.CellInfo mMenuAddInfo;
180 private final int[] mCellCoordinates = new int[2];
181 private FolderInfo mFolderInfo;
182
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700184 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700185 private AllAppsView mAllAppsGrid;
Michael Jurkaaf442092010-06-10 17:01:57 -0700186 private WidgetChooser mWidgetChooser;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private Bundle mSavedState;
189
190 private SpannableStringBuilder mDefaultKeySsb = null;
191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 private boolean mWorkspaceLoading = true;
193
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800194 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private boolean mRestoring;
196 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
198 private Bundle mSavedInstanceState;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800201 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202
Romain Guy84f296c2009-11-04 15:00:44 -0800203 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
204 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700205
Romain Guy1fbc1c82009-11-09 20:43:08 -0800206 private ImageView mPreviousView;
207 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500208
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400209 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400210 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400211 private String[] mHotseatConfig = null;
212 private Intent[] mHotseats = null;
213 private Drawable[] mHotseatIcons = null;
214 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400215
Winson Chungaafa03c2010-06-11 17:34:16 -0700216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 @Override
218 protected void onCreate(Bundle savedInstanceState) {
219 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700220
Winson Chungaafa03c2010-06-11 17:34:16 -0700221 if (LauncherApplication.isInPlaceRotationEnabled()) {
222 // hide the status bar (temporary until we get the status bar design figured out)
223 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
224 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
225 }
226
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 LauncherApplication app = ((LauncherApplication)getApplication());
228 mModel = app.setLauncher(this);
229 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400230 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700232
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700233 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700234 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
235 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 if (PROFILE_STARTUP) {
238 android.os.Debug.startMethodTracing("/sdcard/launcher");
239 }
240
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400241 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 checkForLocaleChange();
243 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 setContentView(R.layout.launcher);
Winson Chungaafa03c2010-06-11 17:34:16 -0700245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 setupViews();
247
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 registerContentObservers();
249
Joe Onorato7c312c12009-08-13 21:36:53 -0700250 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 mSavedState = savedInstanceState;
253 restoreState(mSavedState);
254
255 if (PROFILE_STARTUP) {
256 android.os.Debug.stopMethodTracing();
257 }
258
259 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400260 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 }
262
263 // For handling default keys
264 mDefaultKeySsb = new SpannableStringBuilder();
265 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800266
267 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
268 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
Romain Guycbb89e42009-06-08 15:52:54 -0700270
Winson Chungaafa03c2010-06-11 17:34:16 -0700271 @Override
272 public void onConfigurationChanged(Configuration newConfig) {
273 // TODO Auto-generated method stub
274 super.onConfigurationChanged(newConfig);
275
276 if (LauncherApplication.isInPlaceRotationEnabled()) {
277 mModel.updateOrientation();
278 mWorkspace.refreshWorkspaceChildren();
279 mWorkspace.rotateCurrentScreensChildren();
280 }
281 }
282
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700285 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
286 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 final Configuration configuration = getResources().getConfiguration();
289
Romain Guy98d01652009-06-30 16:21:04 -0700290 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 final String locale = configuration.locale.toString();
292
Romain Guy98d01652009-06-30 16:21:04 -0700293 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 final int mcc = configuration.mcc;
295
Romain Guy98d01652009-06-30 16:21:04 -0700296 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 final int mnc = configuration.mnc;
298
Romain Guy84f296c2009-11-04 15:00:44 -0800299 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300
Romain Guy84f296c2009-11-04 15:00:44 -0800301 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700302 localeConfiguration.locale = locale;
303 localeConfiguration.mcc = mcc;
304 localeConfiguration.mnc = mnc;
305
306 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400308
309 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700310 }
311 }
312
313 private static class LocaleConfiguration {
314 public String locale;
315 public int mcc = -1;
316 public int mnc = -1;
317 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700318
Romain Guy98d01652009-06-30 16:21:04 -0700319 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
320 DataInputStream in = null;
321 try {
322 in = new DataInputStream(context.openFileInput(PREFERENCES));
323 configuration.locale = in.readUTF();
324 configuration.mcc = in.readInt();
325 configuration.mnc = in.readInt();
326 } catch (FileNotFoundException e) {
327 // Ignore
328 } catch (IOException e) {
329 // Ignore
330 } finally {
331 if (in != null) {
332 try {
333 in.close();
334 } catch (IOException e) {
335 // Ignore
336 }
337 }
338 }
339 }
340
341 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
342 DataOutputStream out = null;
343 try {
344 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
345 out.writeUTF(configuration.locale);
346 out.writeInt(configuration.mcc);
347 out.writeInt(configuration.mnc);
348 out.flush();
349 } catch (FileNotFoundException e) {
350 // Ignore
351 } catch (IOException e) {
352 //noinspection ResultOfMethodCallIgnored
353 context.getFileStreamPath(PREFERENCES).delete();
354 } finally {
355 if (out != null) {
356 try {
357 out.close();
358 } catch (IOException e) {
359 // Ignore
360 }
361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 }
363 }
364
365 static int getScreen() {
366 synchronized (sLock) {
367 return sScreen;
368 }
369 }
370
371 static void setScreen(int screen) {
372 synchronized (sLock) {
373 sScreen = screen;
374 }
375 }
376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700378 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379
380 Display display = getWindowManager().getDefaultDisplay();
381 boolean isPortrait = display.getWidth() < display.getHeight();
382
383 final int width = isPortrait ? display.getWidth() : display.getHeight();
384 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700385 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 }
387
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400388 // Note: This doesn't do all the client-id magic that BrowserProvider does
389 // in Browser. (http://b/2425179)
390 private Uri getDefaultBrowserUri() {
391 String url = getString(R.string.default_browser_url);
392 if (url.indexOf("{CID}") != -1) {
393 url = url.replace("{CID}", "android-google");
394 }
395 return Uri.parse(url);
396 }
397
398 // Load the Intent templates from arrays.xml to populate the hotseats. For
399 // each Intent, if it resolves to a single app, use that as the launch
400 // intent & use that app's label as the contentDescription. Otherwise,
401 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400402 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400403 if (mHotseatConfig == null) {
404 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
405 if (mHotseatConfig.length > 0) {
406 mHotseats = new Intent[mHotseatConfig.length];
407 mHotseatLabels = new CharSequence[mHotseatConfig.length];
408 mHotseatIcons = new Drawable[mHotseatConfig.length];
409 } else {
410 mHotseats = null;
411 mHotseatIcons = null;
412 mHotseatLabels = null;
413 }
414
415 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
416 for (int i=0; i<mHotseatConfig.length; i++) {
417 // load icon for this slot; currently unrelated to the actual activity
418 try {
419 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
420 } catch (ArrayIndexOutOfBoundsException ex) {
421 Log.w(TAG, "Missing hotseat_icons array item #" + i);
422 mHotseatIcons[i] = null;
423 }
424 }
425 hotseatIconDrawables.recycle();
426 }
427
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400428 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400429 for (int i=0; i<mHotseatConfig.length; i++) {
430 Intent intent = null;
431 if (mHotseatConfig[i].equals("*BROWSER*")) {
432 // magic value meaning "launch user's default web browser"
433 // replace it with a generic web request so we can see if there is indeed a default
434 String defaultUri = getString(R.string.default_browser_url);
435 intent = new Intent(
436 Intent.ACTION_VIEW,
437 ((defaultUri != null)
438 ? Uri.parse(defaultUri)
439 : getDefaultBrowserUri())
440 ).addCategory(Intent.CATEGORY_BROWSABLE);
441 // note: if the user launches this without a default set, she
442 // will always be taken to the default URL above; this is
443 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700444 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400445 // URL is unavoidably sent to the chosen app.
446 } else {
447 try {
448 intent = Intent.parseUri(mHotseatConfig[i], 0);
449 } catch (java.net.URISyntaxException ex) {
450 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
451 // bogus; leave intent=null
452 }
453 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700454
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400455 if (intent == null) {
456 mHotseats[i] = null;
457 mHotseatLabels[i] = getText(R.string.activity_not_found);
458 continue;
459 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400460
461 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700462 Log.d(TAG, "loadHotseats: hotseat " + i
463 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400464 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400465 + "]");
466 }
467
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400468 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
469 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700470 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400471 Log.d(TAG, "Best match for intent: " + bestMatch);
472 Log.d(TAG, "All matches: ");
473 for (ResolveInfo ri : allMatches) {
474 Log.d(TAG, " --> " + ri);
475 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400476 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400477 // did this resolve to a single app, or the resolver?
478 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700479 // can't find any activity to handle this. let's leave the
480 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400481 // to launch.
482 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400483
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400484 // set accessibility text to "Not installed"
485 mHotseatLabels[i] = getText(R.string.activity_not_found);
486 } else {
487 boolean found = false;
488 for (ResolveInfo ri : allMatches) {
489 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
490 && bestMatch.activityInfo.applicationInfo.packageName
491 .equals(ri.activityInfo.applicationInfo.packageName)) {
492 found = true;
493 break;
494 }
495 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700496
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400497 if (!found) {
498 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
499 // the bestMatch is probably the ResolveActivity, meaning the
500 // user has not yet selected a default
501 // so: we'll keep the original intent for now
502 mHotseats[i] = intent;
503
504 // set the accessibility text to "Select shortcut"
505 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
506 } else {
507 // we have an app!
508 // now reconstruct the intent to launch it through the front
509 // door
510 ComponentName com = new ComponentName(
511 bestMatch.activityInfo.applicationInfo.packageName,
512 bestMatch.activityInfo.name);
513 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
514
515 // load the app label for accessibility
516 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
517 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400518 }
519
520 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700521 Log.d(TAG, "loadHotseats: hotseat " + i
522 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400523 + ((mHotseats[i] == null)
524 ? "null"
525 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400526 + "] label=[" + mHotseatLabels[i]
527 + "]"
528 );
529 }
530 }
531 }
532
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 @Override
534 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700535 mWaitingForResult = false;
536
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 // The pattern used here is that a user PICKs a specific application,
538 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
541 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700542
Romain Guy94dabf12009-07-21 10:55:43 -0700543 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 switch (requestCode) {
545 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 break;
548 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800549 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 break;
551 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 break;
554 case REQUEST_PICK_LIVE_FOLDER:
555 addLiveFolder(data);
556 break;
557 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700560 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700561 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700563 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700564 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
565 // TODO: Is this log message meaningful?
566 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
567 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700569 case REQUEST_PICK_WALLPAPER:
570 // We just wanted the activity result here so we can clear mWaitingForResult
571 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
Romain Guy18042c82009-11-06 11:44:55 -0800573 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
574 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
575 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700576 // Clean up the appWidgetId if we canceled
577 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
578 if (appWidgetId != -1) {
579 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 }
583
584 @Override
585 protected void onResume() {
586 super.onResume();
587
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800588 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800589
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400591 mWorkspaceLoading = true;
592 mModel.startLoader(this, true);
593 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595 }
596
597 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700598 protected void onPause() {
599 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700600 // Some launcher layouts don't have a previous and next view
601 if (mPreviousView != null) {
602 dismissPreview(mPreviousView);
603 dismissPreview(mNextView);
604 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800605 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700606 }
Romain Guycbb89e42009-06-08 15:52:54 -0700607
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700608 @Override
609 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610 // Flag the loader to stop early before switching
611 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800612 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800613 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700614 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700615
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800616 // We can't hide the IME if it was forced open. So don't bother
617 /*
618 @Override
619 public void onWindowFocusChanged(boolean hasFocus) {
620 super.onWindowFocusChanged(hasFocus);
621
622 if (hasFocus) {
623 final InputMethodManager inputManager = (InputMethodManager)
624 getSystemService(Context.INPUT_METHOD_SERVICE);
625 WindowManager.LayoutParams lp = getWindow().getAttributes();
626 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
627 android.os.Handler()) {
628 protected void onReceiveResult(int resultCode, Bundle resultData) {
629 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
630 }
631 });
632 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
633 }
634 }
635 */
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 private boolean acceptFilter() {
638 final InputMethodManager inputManager = (InputMethodManager)
639 getSystemService(Context.INPUT_METHOD_SERVICE);
640 return !inputManager.isFullscreenMode();
641 }
642
643 @Override
644 public boolean onKeyDown(int keyCode, KeyEvent event) {
645 boolean handled = super.onKeyDown(keyCode, event);
646 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
647 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
648 keyCode, event);
649 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700650 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700651 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700652 // showSearchDialog()
653 // If there are multiple keystrokes before the search dialog takes focus,
654 // onSearchRequested() will be called for every keystroke,
655 // but it is idempotent, so it's fine.
656 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
658 }
659
Joe Onorato8a9625e2010-01-28 15:55:35 -0800660 // Eat the long press event so the keyboard doesn't come up.
661 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
662 return true;
663 }
664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 return handled;
666 }
667
Karl Rosaen138a0412009-04-23 19:00:21 -0700668 private String getTypedText() {
669 return mDefaultKeySsb.toString();
670 }
671
672 private void clearTypedText() {
673 mDefaultKeySsb.clear();
674 mDefaultKeySsb.clearSpans();
675 Selection.setSelection(mDefaultKeySsb, 0);
676 }
677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 /**
679 * Restores the previous state, if it exists.
680 *
681 * @param savedState The previous state.
682 */
683 private void restoreState(Bundle savedState) {
684 if (savedState == null) {
685 return;
686 }
687
Joe Onorato3a8820b2009-11-10 15:06:42 -0800688 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
689 if (allApps) {
690 showAllApps(false);
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
694 if (currentScreen > -1) {
695 mWorkspace.setCurrentScreen(currentScreen);
696 }
697
698 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
699 if (addScreen > -1) {
700 mAddItemCellInfo = new CellLayout.CellInfo();
701 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
702 addItemCellInfo.valid = true;
703 addItemCellInfo.screen = addScreen;
704 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
705 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
706 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
707 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
708 addItemCellInfo.findVacantCellsFromOccupied(
709 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
710 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
711 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
712 mRestoring = true;
713 }
714
715 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
716 if (renameFolder) {
717 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 mRestoring = true;
720 }
721 }
722
723 /**
724 * Finds all the views we need and configure them properly.
725 */
726 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400727 DragController dragController = mDragController;
728
Romain Guyb1b69f52009-08-10 15:10:15 -0700729 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400730 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731
Joe Onorato7c312c12009-08-13 21:36:53 -0700732 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700733 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700734 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800735 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700736 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700737 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
740 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500741 workspace.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742
Joe Onorato7c312c12009-08-13 21:36:53 -0700743 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
744 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745
Joe Onorato7c312c12009-08-13 21:36:53 -0700746 mHandleView = (HandleView) findViewById(R.id.all_apps_button);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700747 mHandleView.setLauncher(this);
Joe Onorato7404ee42009-07-31 11:54:44 -0700748 mHandleView.setOnClickListener(this);
Romain Guyf8e6a802009-12-07 17:48:02 -0800749 mHandleView.setOnLongClickListener(this);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800750
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 mWidgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
752 if (mWidgetChooser != null) {
753 mWidgetChooser.setDragController(dragController);
754 mWidgetChooser.setLauncher(this);
755 } else {
756 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
757 hotseatLeft.setContentDescription(mHotseatLabels[0]);
758 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
759 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
760 hotseatRight.setContentDescription(mHotseatLabels[1]);
761 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400762
Michael Jurkaaf442092010-06-10 17:01:57 -0700763 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
764 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800765
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 Drawable previous = mPreviousView.getDrawable();
767 Drawable next = mNextView.getDrawable();
768 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 mPreviousView.setHapticFeedbackEnabled(false);
771 mPreviousView.setOnLongClickListener(this);
772 mNextView.setHapticFeedbackEnabled(false);
773 mNextView.setOnLongClickListener(this);
774 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800775
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400777 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
780 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400781 deleteZone.setDragController(dragController);
Winson Chungaafa03c2010-06-11 17:34:16 -0700782 int deleteZoneHandleId = LauncherApplication.isScreenXLarge() ? R.id.add_button : R.id.all_apps_button_cluster;
Michael Jurkaaf442092010-06-10 17:01:57 -0700783 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784
Joe Onorato00acb122009-08-04 16:04:30 -0400785 dragController.setDragScoller(workspace);
786 dragController.setDragListener(deleteZone);
787 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800788 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700789
Joe Onorato00acb122009-08-04 16:04:30 -0400790 // The order here is bottom to top.
791 dragController.addDropTarget(workspace);
792 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
794
Romain Guy8a73c512009-11-09 19:19:59 -0800795 @SuppressWarnings({"UnusedDeclaration"})
796 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800797 if (!isAllAppsVisible()) {
798 mWorkspace.scrollLeft();
799 }
Romain Guy8a73c512009-11-09 19:19:59 -0800800 }
801
802 @SuppressWarnings({"UnusedDeclaration"})
803 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800804 if (!isAllAppsVisible()) {
805 mWorkspace.scrollRight();
806 }
Romain Guy8a73c512009-11-09 19:19:59 -0800807 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400808
809 @SuppressWarnings({"UnusedDeclaration"})
810 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400811 if (isAllAppsVisible()) return;
812
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400813 int index = -1;
814 if (v.getId() == R.id.hotseat_left) {
815 index = 0;
816 } else if (v.getId() == R.id.hotseat_right) {
817 index = 1;
818 }
819
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400820 // reload these every tap; you never know when they might change
821 loadHotseats();
822 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
823 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400824 startActivitySafely(
825 mHotseats[index],
826 "hotseat"
827 );
828 }
829 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 /**
832 * Creates a view representing a shortcut.
833 *
834 * @param info The data structure describing the shortcut.
835 *
836 * @return A View inflated from R.layout.application.
837 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800838 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 return createShortcut(R.layout.application,
840 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
841 }
842
843 /**
844 * Creates a view representing a shortcut inflated from the specified resource.
845 *
846 * @param layoutResId The id of the XML layout used to create the shortcut.
847 * @param parent The group the shortcut belongs to.
848 * @param info The data structure describing the shortcut.
849 *
850 * @return A View inflated from layoutResId.
851 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800852 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
854
Joe Onorato0589f0f2010-02-08 13:44:00 -0800855 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
856 new FastBitmapDrawable(info.getIcon(mIconCache)),
857 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 favorite.setText(info.title);
859 favorite.setTag(info);
860 favorite.setOnClickListener(this);
861
862 return favorite;
863 }
864
865 /**
866 * Add an application shortcut to the workspace.
867 *
868 * @param data The intent describing the application.
869 * @param cellInfo The position on screen where to create the shortcut.
870 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 cellInfo.screen = mWorkspace.getCurrentScreen();
873 if (!findSingleSlot(cellInfo)) return;
874
Joe Onorato0589f0f2010-02-08 13:44:00 -0800875 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
876 data, context);
877
Romain Guy73b979d2009-06-09 12:57:21 -0700878 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800879 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800881 info.container = ItemInfo.NO_ID;
882 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
883 } else {
884 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
Romain Guycbb89e42009-06-08 15:52:54 -0700887
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 /**
889 * Add a shortcut to the workspace.
890 *
891 * @param data The intent describing the shortcut.
892 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 cellInfo.screen = mWorkspace.getCurrentScreen();
896 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700897
Joe Onorato0589f0f2010-02-08 13:44:00 -0800898 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
900 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
903 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905 }
906
Romain Guycbb89e42009-06-08 15:52:54 -0700907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700909 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911 * @param data The intent describing the appWidgetId.
912 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700914 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700915 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700916
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700917 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700919 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight);
Romain Guycbb89e42009-06-08 15:52:54 -0700920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 // Try finding open space on Launcher screen
922 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800923 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
924 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
925 return;
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 // Build Launcher-specific widget info and save to database
929 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 launcherInfo.spanX = spans[0];
931 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 LauncherModel.addItemToDatabase(this, launcherInfo,
934 LauncherSettings.Favorites.CONTAINER_DESKTOP,
935 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
936
937 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700941 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700942
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700943 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949 }
Romain Guycbb89e42009-06-08 15:52:54 -0700950
Joe Onorato9c1289c2009-08-17 11:03:03 -0400951 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
952 mDesktopItems.remove(launcherInfo);
953 launcherInfo.hostView = null;
954 }
955
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 public LauncherAppWidgetHost getAppWidgetHost() {
957 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
Romain Guycbb89e42009-06-08 15:52:54 -0700959
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800960 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800961 getWindow().closeAllPanels();
962
963 try {
964 dismissDialog(DIALOG_CREATE_SHORTCUT);
965 // Unlock the workspace if the dialog was showing
966 } catch (Exception e) {
967 // An exception is thrown if the dialog is not visible, which is fine
968 }
969
970 try {
971 dismissDialog(DIALOG_RENAME_FOLDER);
972 // Unlock the workspace if the dialog was showing
973 } catch (Exception e) {
974 // An exception is thrown if the dialog is not visible, which is fine
975 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800976
977 // Whatever we were doing is hereby canceled.
978 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800979 }
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 @Override
982 protected void onNewIntent(Intent intent) {
983 super.onNewIntent(intent);
984
985 // Close the menu
986 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -0800987 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800988 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -0700989
Joe Onorato14f122b2009-11-19 14:06:36 -0800990 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
991 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
992 boolean allAppsVisible = isAllAppsVisible();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800993 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -0800994 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800995 }
Joe Onorato14f122b2009-11-19 14:06:36 -0800996 closeAllApps(alreadyOnHome && allAppsVisible);
Romain Guy1dd3a072009-07-16 13:21:01 -0700997
Joe Onorato3a8820b2009-11-10 15:06:42 -0800998 final View v = getWindow().peekDecorView();
999 if (v != null && v.getWindowToken() != null) {
1000 InputMethodManager imm = (InputMethodManager)getSystemService(
1001 INPUT_METHOD_SERVICE);
1002 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
1004 }
1005 }
1006
1007 @Override
1008 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1009 // Do not call super here
1010 mSavedInstanceState = savedInstanceState;
1011 }
1012
1013 @Override
1014 protected void onSaveInstanceState(Bundle outState) {
1015 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1016
1017 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1018 if (folders.size() > 0) {
1019 final int count = folders.size();
1020 long[] ids = new long[count];
1021 for (int i = 0; i < count; i++) {
1022 final FolderInfo info = folders.get(i).getInfo();
1023 ids[i] = info.id;
1024 }
1025 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1026 } else {
1027 super.onSaveInstanceState(outState);
1028 }
1029
Joe Onoratofb0ca672009-09-14 17:55:46 -04001030 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001031 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034
1035 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1036 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1037 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1038
1039 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1040 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1041 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1042 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1043 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1044 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1045 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1046 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
1047 layout.getOccupiedCells());
1048 }
1049
1050 if (mFolderInfo != null && mWaitingForResult) {
1051 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1052 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1053 }
1054 }
1055
1056 @Override
1057 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001063 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065
1066 TextKeyListener.getInstance().release();
1067
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001071
1072 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001073
Romain Guy1fbc1c82009-11-09 20:43:08 -08001074 dismissPreview(mPreviousView);
Romain Guya6abce82009-11-10 02:54:41 -08001075 dismissPreview(mNextView);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001076
1077 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079
1080 @Override
1081 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001082 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 super.startActivityForResult(intent, requestCode);
1084 }
1085
1086 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001087 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001089
Joe Onorato7bb17492009-09-24 17:51:01 -07001090 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001091
Karl Rosaen138a0412009-04-23 19:00:21 -07001092 if (initialQuery == null) {
1093 // Use any text typed in the launcher as the initial query
1094 initialQuery = getTypedText();
1095 clearTypedText();
1096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 if (appSearchData == null) {
1098 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001099 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
Karl Rosaen138a0412009-04-23 19:00:21 -07001102 final SearchManager searchManager =
1103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001104 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001105 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 }
1107
1108 @Override
1109 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 if (isWorkspaceLocked()) {
1111 return false;
1112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
1114 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1117 .setIcon(android.R.drawable.ic_menu_add)
1118 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001119 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 .setIcon(android.R.drawable.ic_menu_gallery)
1121 .setAlphabeticShortcut('W');
1122 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1123 .setIcon(android.R.drawable.ic_search_category_default)
1124 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1125 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1126 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1127 .setAlphabeticShortcut('N');
1128
1129 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001130 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1131 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132
1133 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1134 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1135 .setIntent(settings);
1136
1137 return true;
1138 }
1139
1140 @Override
1141 public boolean onPrepareOptionsMenu(Menu menu) {
1142 super.onPrepareOptionsMenu(menu);
1143
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001144 // If all apps is animating, don't show the menu, because we don't know
1145 // which one to show.
1146 if (mAllAppsGrid.isVisible() && !mAllAppsGrid.isOpaque()) {
1147 return false;
1148 }
1149
1150 // Only show the add and wallpaper options when we're not in all apps.
1151 boolean visible = !mAllAppsGrid.isOpaque();
1152 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1153 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1154
1155 // Disable add if the workspace is full.
1156 if (visible) {
1157 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1158 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
1161 return true;
1162 }
1163
1164 @Override
1165 public boolean onOptionsItemSelected(MenuItem item) {
1166 switch (item.getItemId()) {
1167 case MENU_ADD:
1168 addItems();
1169 return true;
1170 case MENU_WALLPAPER_SETTINGS:
1171 startWallpaper();
1172 return true;
1173 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001174 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 return true;
1176 case MENU_NOTIFICATIONS:
1177 showNotifications();
1178 return true;
1179 }
1180
1181 return super.onOptionsItemSelected(item);
1182 }
Romain Guycbb89e42009-06-08 15:52:54 -07001183
Karl Rosaen138a0412009-04-23 19:00:21 -07001184 /**
1185 * Indicates that we want global search for this activity by setting the globalSearch
1186 * argument for {@link #startSearch} to true.
1187 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001189 @Override
1190 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001191 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001192 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001193 }
1194
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 public boolean isWorkspaceLocked() {
1196 return mWorkspaceLoading || mWaitingForResult;
1197 }
1198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001200 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 showAddDialog(mMenuAddInfo);
1202 }
1203
Michael Jurkaaf442092010-06-10 17:01:57 -07001204 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1205 mAddItemCellInfo = cellInfo;
1206 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1207 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1208 addAppWidgetImpl(appWidgetId);
1209 }
1210
1211 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001212 // TODO: catch bad widget exception when sent
1213 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001214 // TODO: Is this log message meaningful?
1215 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1216 addAppWidgetImpl(appWidgetId);
1217 }
1218
1219 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001220 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
Bjorn Bringert7984c942009-12-09 15:38:25 +00001222 if (appWidget.configure != null) {
1223 // Launch over to configure widget, if needed
1224 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1225 intent.setComponent(appWidget.configure);
1226 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1227
Romain Guy8e633c52010-03-04 12:51:36 -08001228 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001230 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001231 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 }
1233 }
Romain Guycbb89e42009-06-08 15:52:54 -07001234
Joe Onoratodeb98af2010-02-19 14:59:39 -08001235 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001236 // Handle case where user selected "Applications"
1237 String applicationName = getResources().getString(R.string.group_applications);
1238 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001240 if (applicationName != null && applicationName.equals(shortcutName)) {
1241 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1242 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001244 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1245 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001246 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001247 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001248 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001249 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251
1252 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001253 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001254 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001255 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001256
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001257 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001258 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001259 } else {
1260 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1261 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
1263
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 UserFolderInfo folderInfo = new UserFolderInfo();
1266 folderInfo.title = getText(R.string.folder_name);
1267
1268 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1269 cellInfo.screen = mWorkspace.getCurrentScreen();
1270 if (!findSingleSlot(cellInfo)) return;
1271
1272 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 LauncherModel.addItemToDatabase(this, folderInfo,
1274 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277
1278 // Create the view
1279 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1280 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1281 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
Romain Guycbb89e42009-06-08 15:52:54 -07001284
Joe Onorato9c1289c2009-08-17 11:03:03 -04001285 void removeFolder(FolderInfo folder) {
1286 mFolders.remove(folder.id);
1287 }
1288
1289 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 cellInfo.screen = mWorkspace.getCurrentScreen();
1291 if (!findSingleSlot(cellInfo)) return;
1292
1293 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1294
1295 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1297 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001298 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1299 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
1302
1303 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1304 CellLayout.CellInfo cellInfo, boolean notify) {
1305
1306 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1307 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1308
1309 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 Intent.ShortcutIconResource iconResource = null;
1311
1312 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1313 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1314 try {
1315 iconResource = (Intent.ShortcutIconResource) extra;
1316 final PackageManager packageManager = context.getPackageManager();
1317 Resources resources = packageManager.getResourcesForApplication(
1318 iconResource.packageName);
1319 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1320 icon = resources.getDrawable(id);
1321 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001322 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324 }
1325
1326 if (icon == null) {
1327 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1328 }
1329
1330 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001331 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 info.title = name;
1333 info.iconResource = iconResource;
1334 info.uri = data.getData();
1335 info.baseIntent = baseIntent;
1336 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1337 LiveFolders.DISPLAY_MODE_GRID);
1338
1339 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1340 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342
1343 return info;
1344 }
1345
1346 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1347 final int[] xy = new int[2];
1348 if (findSlot(cellInfo, xy, 1, 1)) {
1349 cellInfo.cellX = xy[0];
1350 cellInfo.cellY = xy[1];
1351 return true;
1352 }
1353 return false;
1354 }
1355
1356 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1357 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1358 boolean[] occupied = mSavedState != null ?
1359 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1360 cellInfo = mWorkspace.findAllVacantCells(occupied);
1361 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1362 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1363 return false;
1364 }
1365 }
1366 return true;
1367 }
1368
1369 private void showNotifications() {
1370 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1371 if (statusBar != null) {
1372 statusBar.expand();
1373 }
1374 }
1375
1376 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001377 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001379 Intent chooser = Intent.createChooser(pickWallpaper,
1380 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001381 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1382 // Removed in Eclair MR1
1383// WallpaperManager wm = (WallpaperManager)
1384// getSystemService(Context.WALLPAPER_SERVICE);
1385// WallpaperInfo wi = wm.getWallpaperInfo();
1386// if (wi != null && wi.getSettingsActivity() != null) {
1387// LabeledIntent li = new LabeledIntent(getPackageName(),
1388// R.string.configure_wallpaper, 0);
1389// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1390// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1391// }
Mike Clerona0618e42009-10-22 13:55:21 -07001392 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001395 /**
1396 * Registers various content observers. The current implementation registers
1397 * only a favorites observer to keep track of the favorites applications.
1398 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001399 private void registerContentObservers() {
1400 ContentResolver resolver = getContentResolver();
1401 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1402 true, mWidgetObserver);
1403 }
1404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 @Override
1406 public boolean dispatchKeyEvent(KeyEvent event) {
1407 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1408 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001410 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001411 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001412 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001413 dumpState();
1414 return true;
1415 }
1416 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001417 }
1418 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1419 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001420 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return true;
1422 }
1423 }
1424
1425 return super.dispatchKeyEvent(event);
1426 }
1427
Joe Onorato88ec0992009-11-19 13:16:06 -08001428 @Override
1429 public void onBackPressed() {
1430 if (isAllAppsVisible()) {
1431 closeAllApps(true);
1432 } else {
1433 closeFolder();
1434 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001435 // Some launcher layouts don't have a previous and next view
1436 if (mPreviousView != null) {
1437 dismissPreview(mPreviousView);
1438 dismissPreview(mNextView);
1439 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001440 }
1441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 private void closeFolder() {
1443 Folder folder = mWorkspace.getOpenFolder();
1444 if (folder != null) {
1445 closeFolder(folder);
1446 }
1447 }
1448
1449 void closeFolder(Folder folder) {
1450 folder.getInfo().opened = false;
1451 ViewGroup parent = (ViewGroup) folder.getParent();
1452 if (parent != null) {
1453 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001454 if (folder instanceof DropTarget) {
1455 // Live folders aren't DropTargets.
1456 mDragController.removeDropTarget((DropTarget)folder);
1457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459 folder.onClose();
1460 }
1461
1462 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001463 * Re-listen when widgets are reset.
1464 */
1465 private void onAppWidgetReset() {
1466 mAppWidgetHost.startListening();
1467 }
1468
1469 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001470 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1471 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001473 private void unbindDesktopItems() {
1474 for (ItemInfo item: mDesktopItems) {
1475 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 /**
1480 * Launches the intent referred by the clicked shortcut.
1481 *
1482 * @param v The view representing the clicked shortcut.
1483 */
1484 public void onClick(View v) {
1485 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001486 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001488 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001489 int[] pos = new int[2];
1490 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001491 intent.setSourceBounds(new Rect(pos[0], pos[1],
1492 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001493 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 } else if (tag instanceof FolderInfo) {
1495 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001496 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001497 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001498 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001499 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001500 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
1503 }
1504
Michael Jurkaaf442092010-06-10 17:01:57 -07001505 /**
1506 * Event handler for the "plus" button that appears on the home screen, which
1507 * enters home screen customization mode.
1508 *
1509 * @param v The view that was clicked.
1510 */
1511 public void onClickAddButton(View v) {
1512 View widgetChooser = findViewById(R.id.widget_chooser);
1513 widgetChooser.setVisibility(View.VISIBLE);
1514
1515 // Animate the widget chooser up from the bottom of the screen
1516 widgetChooser.startAnimation(AnimationUtils.loadAnimation(this, R.anim.widget_chooser_slide_up));
1517 }
1518
1519 public void onClickAllAppsButton(View w) {
1520 showAllApps(true);
1521 }
1522
Joe Onoratof984e852010-03-25 09:47:45 -07001523 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1525 try {
1526 startActivity(intent);
1527 } catch (ActivityNotFoundException e) {
1528 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001529 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 } catch (SecurityException e) {
1531 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001532 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001534 "or use the exported attribute for this activity. "
1535 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001538
Romain Guy8e633c52010-03-04 12:51:36 -08001539 void startActivityForResultSafely(Intent intent, int requestCode) {
1540 try {
1541 startActivityForResult(intent, requestCode);
1542 } catch (ActivityNotFoundException e) {
1543 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1544 } catch (SecurityException e) {
1545 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1546 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1547 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1548 "or use the exported attribute for this activity.", e);
1549 }
1550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
1552 private void handleFolderClick(FolderInfo folderInfo) {
1553 if (!folderInfo.opened) {
1554 // Close any open folder
1555 closeFolder();
1556 // Open the requested folder
1557 openFolder(folderInfo);
1558 } else {
1559 // Find the open folder...
1560 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1561 int folderScreen;
1562 if (openFolder != null) {
1563 folderScreen = mWorkspace.getScreenForView(openFolder);
1564 // .. and close it
1565 closeFolder(openFolder);
1566 if (folderScreen != mWorkspace.getCurrentScreen()) {
1567 // Close any folder open on the current screen
1568 closeFolder();
1569 // Pull the folder onto this screen
1570 openFolder(folderInfo);
1571 }
1572 }
1573 }
1574 }
1575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 /**
1577 * Opens the user fodler described by the specified tag. The opening of the folder
1578 * is animated relative to the specified View. If the View is null, no animation
1579 * is played.
1580 *
1581 * @param folderInfo The FolderInfo describing the folder to open.
1582 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001583 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 Folder openFolder;
1585
1586 if (folderInfo instanceof UserFolderInfo) {
1587 openFolder = UserFolder.fromXml(this);
1588 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001589 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 } else {
1591 return;
1592 }
1593
Joe Onorato00acb122009-08-04 16:04:30 -04001594 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 openFolder.setLauncher(this);
1596
1597 openFolder.bind(folderInfo);
1598 folderInfo.opened = true;
1599
Winson Chungaafa03c2010-06-11 17:34:16 -07001600 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 openFolder.onOpen();
1603 }
1604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001606 switch (v.getId()) {
1607 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001608 if (!isAllAppsVisible()) {
1609 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1610 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001611 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001612 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001613 return true;
1614 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001615 if (!isAllAppsVisible()) {
1616 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1617 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001618 showPreviews(v);
1619 }
1620 return true;
1621 case R.id.all_apps_button:
1622 if (!isAllAppsVisible()) {
1623 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1624 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1625 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001626 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001627 return true;
1628 }
1629
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 return false;
1632 }
1633
1634 if (!(v instanceof CellLayout)) {
1635 v = (View) v.getParent();
1636 }
1637
1638 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1639
1640 // This happens when long clicking an item with the dpad/trackball
1641 if (cellInfo == null) {
1642 return true;
1643 }
1644
1645 if (mWorkspace.allowLongPress()) {
1646 if (cellInfo.cell == null) {
1647 if (cellInfo.valid) {
1648 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001649 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001650 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1651 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 showAddDialog(cellInfo);
1653 }
1654 } else {
1655 if (!(cellInfo.cell instanceof Folder)) {
1656 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001657 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1658 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 mWorkspace.startDrag(cellInfo);
1660 }
1661 }
1662 }
1663 return true;
1664 }
1665
Romain Guye6b8e2f2009-11-10 11:56:55 -08001666 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001667 private void dismissPreview(final View v) {
1668 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001669 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001670 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1671 public void onDismiss() {
1672 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1673 int count = group.getChildCount();
1674 for (int i = 0; i < count; i++) {
1675 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1676 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001677 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1678 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1679
1680 v.setTag(R.id.workspace, null);
1681 v.setTag(R.id.icon, null);
1682 window.setOnDismissListener(null);
1683 }
1684 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001685 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001686 }
1687 v.setTag(null);
1688 }
1689
Romain Guyf8e6a802009-12-07 17:48:02 -08001690 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001691 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001692 }
1693
Romain Guya6abce82009-11-10 02:54:41 -08001694 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001695 final Resources resources = getResources();
1696 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001697
Romain Guya6abce82009-11-10 02:54:41 -08001698 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001699
Romain Guy9d31e9f2009-11-11 18:54:28 -08001700 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001701
Romain Guyf8e6a802009-12-07 17:48:02 -08001702 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001703 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001704 int extraW = (int) ((r.left + r.right) * max);
1705 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001706
1707 int aW = cell.getWidth() - extraW;
1708 float w = aW / max;
1709
1710 int width = cell.getWidth();
1711 int height = cell.getHeight();
1712 int x = cell.getLeftPadding();
1713 int y = cell.getTopPadding();
1714 width -= (x + cell.getRightPadding());
1715 height -= (y + cell.getBottomPadding());
1716
1717 float scale = w / width;
1718
1719 int count = end - start;
1720
1721 final float sWidth = width * scale;
1722 float sHeight = height * scale;
1723
Romain Guye6b8e2f2009-11-10 11:56:55 -08001724 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001725
Romain Guye6b8e2f2009-11-10 11:56:55 -08001726 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1727 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001728
1729 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001730 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001731 cell = (CellLayout) workspace.getChildAt(i);
1732
Romain Guyf8e6a802009-12-07 17:48:02 -08001733 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001734 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001735
1736 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001737 c.scale(scale, scale);
1738 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1739 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001740
Romain Guy9d31e9f2009-11-11 18:54:28 -08001741 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001742 image.setImageBitmap(bitmap);
1743 image.setTag(i);
1744 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001745 image.setOnFocusChangeListener(handler);
1746 image.setFocusable(true);
1747 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001748
1749 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001750 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1751
Winson Chungaafa03c2010-06-11 17:34:16 -07001752 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001753 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001754
1755 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001756 p.setContentView(preview);
1757 p.setWidth((int) (sWidth * count + extraW));
1758 p.setHeight((int) (sHeight + extraH));
1759 p.setAnimationStyle(R.style.AnimationPreview);
1760 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001761 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001762 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001763 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001764
Romain Guye6b8e2f2009-11-10 11:56:55 -08001765 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1766 public void onDismiss() {
1767 dismissPreview(anchor);
1768 }
1769 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001770
1771 anchor.setTag(p);
1772 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001773 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001774 }
1775
Romain Guy9d31e9f2009-11-11 18:54:28 -08001776 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001777 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001778
Romain Guye6b8e2f2009-11-10 11:56:55 -08001779 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001780 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001781 }
1782
1783 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001784 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001785 v.post(this);
1786 }
1787
1788 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001789 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001790 }
1791
1792 public void onFocusChange(View v, boolean hasFocus) {
1793 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001794 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001795 }
Romain Guya6abce82009-11-10 02:54:41 -08001796 }
1797 }
1798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 Workspace getWorkspace() {
1800 return mWorkspace;
1801 }
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 @Override
1804 protected Dialog onCreateDialog(int id) {
1805 switch (id) {
1806 case DIALOG_CREATE_SHORTCUT:
1807 return new CreateShortcut().createDialog();
1808 case DIALOG_RENAME_FOLDER:
1809 return new RenameFolder().createDialog();
1810 }
1811
1812 return super.onCreateDialog(id);
1813 }
1814
1815 @Override
1816 protected void onPrepareDialog(int id, Dialog dialog) {
1817 switch (id) {
1818 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 break;
1820 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001821 if (mFolderInfo != null) {
1822 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1823 final CharSequence text = mFolderInfo.title;
1824 input.setText(text);
1825 input.setSelection(0, text.length());
1826 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 break;
1828 }
1829 }
1830
1831 void showRenameDialog(FolderInfo info) {
1832 mFolderInfo = info;
1833 mWaitingForResult = true;
1834 showDialog(DIALOG_RENAME_FOLDER);
1835 }
1836
1837 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1838 mAddItemCellInfo = cellInfo;
1839 mWaitingForResult = true;
1840 showDialog(DIALOG_CREATE_SHORTCUT);
1841 }
1842
Joe Onoratodeb98af2010-02-19 14:59:39 -08001843 private void pickShortcut() {
Romain Guy73b979d2009-06-09 12:57:21 -07001844 Bundle bundle = new Bundle();
1845
1846 ArrayList<String> shortcutNames = new ArrayList<String>();
1847 shortcutNames.add(getString(R.string.group_applications));
1848 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1849
1850 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1851 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1852 R.drawable.ic_launcher_application));
1853 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1854
1855 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1856 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001857 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001858 pickIntent.putExtras(bundle);
1859
Joe Onoratodeb98af2010-02-19 14:59:39 -08001860 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001861 }
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 private class RenameFolder {
1864 private EditText mInput;
1865
1866 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1868 mInput = (EditText) layout.findViewById(R.id.folder_name);
1869
1870 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1871 builder.setIcon(0);
1872 builder.setTitle(getString(R.string.rename_folder_title));
1873 builder.setCancelable(true);
1874 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1875 public void onCancel(DialogInterface dialog) {
1876 cleanup();
1877 }
1878 });
1879 builder.setNegativeButton(getString(R.string.cancel_action),
1880 new Dialog.OnClickListener() {
1881 public void onClick(DialogInterface dialog, int which) {
1882 cleanup();
1883 }
1884 }
1885 );
1886 builder.setPositiveButton(getString(R.string.rename_action),
1887 new Dialog.OnClickListener() {
1888 public void onClick(DialogInterface dialog, int which) {
1889 changeFolderName();
1890 }
1891 }
1892 );
1893 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001894
1895 final AlertDialog dialog = builder.create();
1896 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1897 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001898 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001899 mInput.requestFocus();
1900 InputMethodManager inputManager = (InputMethodManager)
1901 getSystemService(Context.INPUT_METHOD_SERVICE);
1902 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001903 }
1904 });
1905
1906 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
1908
1909 private void changeFolderName() {
1910 final String name = mInput.getText().toString();
1911 if (!TextUtils.isEmpty(name)) {
1912 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04001913 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 mFolderInfo.title = name;
1915 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1916
Joe Onorato9c1289c2009-08-17 11:03:03 -04001917 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001918 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 } else {
1921 final FolderIcon folderIcon = (FolderIcon)
1922 mWorkspace.getViewForTag(mFolderInfo);
1923 if (folderIcon != null) {
1924 folderIcon.setText(name);
1925 getWorkspace().requestLayout();
1926 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001927 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001928 mWorkspaceLoading = true;
1929 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931 }
1932 }
1933 cleanup();
1934 }
1935
1936 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 dismissDialog(DIALOG_RENAME_FOLDER);
1938 mWaitingForResult = false;
1939 mFolderInfo = null;
1940 }
1941 }
1942
Daniel Sandler843e8602010-06-07 14:59:01 -04001943 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1944 public boolean isAllAppsVisible() {
1945 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04001946 }
1947
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001948 // AllAppsView.Watcher
1949 public void zoomed(float zoom) {
1950 if (zoom == 1.0f) {
1951 mWorkspace.setVisibility(View.GONE);
1952 }
1953 }
1954
Joe Onorato3a8820b2009-11-10 15:06:42 -08001955 void showAllApps(boolean animated) {
1956 mAllAppsGrid.zoom(1.0f, animated);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001957
Romain Guyc16fea72010-03-12 17:17:56 -08001958 ((View) mAllAppsGrid).setFocusable(true);
1959 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07001960
Joe Onorato7c312c12009-08-13 21:36:53 -07001961 // TODO: fade these two too
1962 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04001963 }
1964
Joe Onoratob2061212009-11-24 16:13:54 -05001965 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08001966 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05001967 * - Home from workspace
1968 * - from center screen
1969 * - from other screens
1970 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001971 * - from center screen
1972 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001973 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001974 * - from center screen
1975 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05001976 * - Launch app from workspace and quit
1977 * - with back
1978 * - with home
1979 * - Launch app from all apps and quit
1980 * - with back
1981 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08001982 * - Go to a screen that's not the default, then all
1983 * apps, and launch and app, and go back
1984 * - with back
1985 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05001986 * - On workspace, long press power and go back
1987 * - with back
1988 * - with home
1989 * - On all apps, long press power and go back
1990 * - with back
1991 * - with home
1992 * - On workspace, power off
1993 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08001994 * - Launch an app and turn off the screen while in that app
1995 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001996 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08001997 * - From all apps
1998 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07001999 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2000 * - From all apps
2001 * - From the center workspace
2002 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002003 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002004 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002005 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002006 mWorkspace.setVisibility(View.VISIBLE);
Joe Onorato3a8820b2009-11-10 15:06:42 -08002007 mAllAppsGrid.zoom(0.0f, animated);
Daniel Sandler388f6792010-03-02 14:08:08 -05002008 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002009 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002010 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002011 }
2012
Joe Onorato7c312c12009-08-13 21:36:53 -07002013 void lockAllApps() {
2014 // TODO
2015 }
2016
2017 void unlockAllApps() {
2018 // TODO
2019 }
2020
Joe Onorato7404ee42009-07-31 11:54:44 -07002021 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 * Displays the shortcut creation dialog and launches, if necessary, the
2023 * appropriate activity.
2024 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002025 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002026 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2027 DialogInterface.OnShowListener {
2028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2035 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002036 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 builder.setInverseBackgroundForced(true);
2039
2040 AlertDialog dialog = builder.create();
2041 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002042 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002043 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 return dialog;
2046 }
2047
2048 public void onCancel(DialogInterface dialog) {
2049 mWaitingForResult = false;
2050 cleanup();
2051 }
2052
Romain Guycbb89e42009-06-08 15:52:54 -07002053 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002054 }
2055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002057 try {
2058 dismissDialog(DIALOG_CREATE_SHORTCUT);
2059 } catch (Exception e) {
2060 // An exception is thrown if the dialog is not visible, which is fine
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002064 /**
2065 * Handle the action clicked in the "Add to home" dialog.
2066 */
2067 public void onClick(DialogInterface dialog, int which) {
2068 Resources res = getResources();
2069 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002070
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002071 switch (which) {
2072 case AddAdapter.ITEM_SHORTCUT: {
2073 // Insert extra item to handle picking application
Joe Onoratodeb98af2010-02-19 14:59:39 -08002074 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002075 break;
2076 }
Romain Guycbb89e42009-06-08 15:52:54 -07002077
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002078 case AddAdapter.ITEM_APPWIDGET: {
2079 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002080
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002081 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2082 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002083 // start the pick activity
2084 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2085 break;
2086 }
Romain Guycbb89e42009-06-08 15:52:54 -07002087
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002088 case AddAdapter.ITEM_LIVE_FOLDER: {
2089 // Insert extra item to handle inserting folder
2090 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002091
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002092 ArrayList<String> shortcutNames = new ArrayList<String>();
2093 shortcutNames.add(res.getString(R.string.group_folder));
2094 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002095
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002096 ArrayList<ShortcutIconResource> shortcutIcons =
2097 new ArrayList<ShortcutIconResource>();
2098 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2099 R.drawable.ic_launcher_folder));
2100 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2101
2102 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2103 pickIntent.putExtra(Intent.EXTRA_INTENT,
2104 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2105 pickIntent.putExtra(Intent.EXTRA_TITLE,
2106 getText(R.string.title_select_live_folder));
2107 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002108
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002109 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2110 break;
2111 }
2112
2113 case AddAdapter.ITEM_WALLPAPER: {
2114 startWallpaper();
2115 break;
2116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002119
2120 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002121 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124
2125 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002126 * Receives notifications when applications are added/removed.
2127 */
2128 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2129 @Override
2130 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002131 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002132 String reason = intent.getStringExtra("reason");
2133 if (!"homekey".equals(reason)) {
2134 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002135 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002136 animate = false;
2137 }
Joe Onoratob2061212009-11-24 16:13:54 -05002138 closeAllApps(animate);
2139 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002140 }
2141 }
2142
2143 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002144 * Receives notifications whenever the appwidgets are reset.
2145 */
2146 private class AppWidgetResetObserver extends ContentObserver {
2147 public AppWidgetResetObserver() {
2148 super(new Handler());
2149 }
2150
2151 @Override
2152 public void onChange(boolean selfChange) {
2153 onAppWidgetReset();
2154 }
2155 }
2156
2157 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002158 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002160 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002161 if (mWorkspace != null) {
2162 return mWorkspace.getCurrentScreen();
2163 } else {
2164 return SCREEN_COUNT / 2;
2165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002167
Joe Onorato9c1289c2009-08-17 11:03:03 -04002168 /**
2169 * Refreshes the shortcuts shown on the workspace.
2170 *
2171 * Implementation of the method from LauncherModel.Callbacks.
2172 */
2173 public void startBinding() {
2174 final Workspace workspace = mWorkspace;
2175 int count = workspace.getChildCount();
2176 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002177 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002178 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2179 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002180
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 if (DEBUG_USER_INTERFACE) {
2182 android.widget.Button finishButton = new android.widget.Button(this);
2183 finishButton.setText("Finish");
2184 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2185
2186 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2187 public void onClick(View v) {
2188 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192 }
2193
2194 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002195 * Bind the items start-end from the list.
2196 *
2197 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2200
2201 final Workspace workspace = mWorkspace;
2202
2203 for (int i=start; i<end; i++) {
2204 final ItemInfo item = shortcuts.get(i);
2205 mDesktopItems.add(item);
2206 switch (item.itemType) {
2207 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2208 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002209 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2211 false);
2212 break;
2213 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2214 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2215 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2216 (UserFolderInfo) item);
2217 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2218 false);
2219 break;
2220 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2221 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2222 R.layout.live_folder_icon, this,
2223 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2224 (LiveFolderInfo) item);
2225 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2226 false);
2227 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 }
2230
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233
Joe Onorato9c1289c2009-08-17 11:03:03 -04002234 /**
2235 * Implementation of the method from LauncherModel.Callbacks.
2236 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002237 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2238 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002241
2242 /**
2243 * Add the views for a widget to the workspace.
2244 *
2245 * Implementation of the method from LauncherModel.Callbacks.
2246 */
2247 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002248 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2249 if (DEBUG_WIDGETS) {
2250 Log.d(TAG, "bindAppWidget: " + item);
2251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002252 final Workspace workspace = mWorkspace;
2253
2254 final int appWidgetId = item.appWidgetId;
2255 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002256 if (DEBUG_WIDGETS) {
2257 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2258 }
2259
Joe Onorato9c1289c2009-08-17 11:03:03 -04002260 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2261
Joe Onorato9c1289c2009-08-17 11:03:03 -04002262 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2263 item.hostView.setTag(item);
2264
2265 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2266 item.cellY, item.spanX, item.spanY, false);
2267
2268 workspace.requestLayout();
2269
2270 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002271
2272 if (DEBUG_WIDGETS) {
2273 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2274 + (SystemClock.uptimeMillis()-start) + "ms");
2275 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 }
2277
2278 /**
2279 * Callback saying that there aren't any more items to bind.
2280 *
2281 * Implementation of the method from LauncherModel.Callbacks.
2282 */
2283 public void finishBindingItems() {
2284 if (mSavedState != null) {
2285 if (!mWorkspace.hasFocus()) {
2286 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2287 }
2288
2289 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2290 if (userFolders != null) {
2291 for (long folderId : userFolders) {
2292 final FolderInfo info = mFolders.get(folderId);
2293 if (info != null) {
2294 openFolder(info);
2295 }
2296 }
2297 final Folder openFolder = mWorkspace.getOpenFolder();
2298 if (openFolder != null) {
2299 openFolder.requestFocus();
2300 }
2301 }
2302
Joe Onorato9c1289c2009-08-17 11:03:03 -04002303 mSavedState = null;
2304 }
2305
2306 if (mSavedInstanceState != null) {
2307 super.onRestoreInstanceState(mSavedInstanceState);
2308 mSavedInstanceState = null;
2309 }
2310
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 mWorkspaceLoading = false;
2312 }
2313
2314 /**
2315 * Add the icons for all apps.
2316 *
2317 * Implementation of the method from LauncherModel.Callbacks.
2318 */
2319 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002320 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002321 }
2322
2323 /**
2324 * A package was installed.
2325 *
2326 * Implementation of the method from LauncherModel.Callbacks.
2327 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002328 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002329 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002330 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002331 }
2332
2333 /**
2334 * A package was updated.
2335 *
2336 * Implementation of the method from LauncherModel.Callbacks.
2337 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002338 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002340 mWorkspace.updateShortcuts(apps);
2341 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002342 }
2343
2344 /**
2345 * A package was uninstalled.
2346 *
2347 * Implementation of the method from LauncherModel.Callbacks.
2348 */
Joe Onorato36115782010-06-17 13:28:48 -04002349 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002350 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002351 if (permanent) {
2352 mWorkspace.removeItems(apps);
2353 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002354 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002355 }
Joe Onoratobe386092009-11-17 17:32:16 -08002356
2357 /**
2358 * Prints out out state for debugging.
2359 */
2360 public void dumpState() {
2361 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002362 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002363 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2364 Log.d(TAG, "mRestoring=" + mRestoring);
2365 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2366 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2367 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2368 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002369 mModel.dumpState();
2370 mAllAppsGrid.dumpState();
2371 Log.d(TAG, "END launcher2 dump state");
2372 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373}