blob: 19a943b3c492291c342bd30228124e8a7a767c12 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
Michael Jurka742574b2011-02-02 23:51:01 -080022import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080025import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070038import android.content.ClipData;
39import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080045import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080046import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080049import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040053import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080054import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080056import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080068import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070069import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070070import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080071import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.Selection;
73import android.text.SpannableStringBuilder;
74import android.text.TextUtils;
75import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070076import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080078import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
82import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070083import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080084import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.View;
86import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070087import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080088import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080089import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070095import android.widget.ImageView;
96import android.widget.LinearLayout;
97import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070098import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -070099import android.widget.TextView;
100import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700101
Michael Jurka742574b2011-02-02 23:51:01 -0800102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800109
Adam Cohended9f8d2010-11-03 13:25:16 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800125 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700128 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
129 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
131 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700132 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
135 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
139 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Joe Onorato7c312c12009-08-13 21:36:53 -0700148 static final int DIALOG_CREATE_SHORTCUT = 1;
149 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Romain Guy98d01652009-06-30 16:21:04 -0700151 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
153 // Type: int
154 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700155 // Type: int
156 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: long
158 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700170 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
171
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700172 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800173 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800174 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700175 private State mState = State.WORKSPACE;
176 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800181 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800183 private final BroadcastReceiver mCloseSystemDialogsReceiver
184 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800185 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private LayoutInflater mInflater;
188
Joe Onorato00acb122009-08-04 16:04:30 -0400189 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private AppWidgetManager mAppWidgetManager;
193 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195 private int mAddScreen = -1;
196 private int mAddIntersectCellX = -1;
197 private int mAddIntersectCellY = -1;
198 private int[] mAddDropPosition;
199 private int[] mTmpAddItemCellCoordinates = new int[2];
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private FolderInfo mFolderInfo;
202
Joe Onorato7c312c12009-08-13 21:36:53 -0700203 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700205 private AllAppsView mAllAppsGrid;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800206 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700207 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700208
Winson Chung97d85d22011-04-13 11:27:36 -0700209 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800210 private View mAllAppsButton;
211 private View mDivider;
212 private View mConfigureButton;
213
Winson Chunga12a2502010-12-20 14:41:35 -0800214 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800242 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800243 private ImageView mPreviousView;
244 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400247 private String[] mHotseatConfig = null;
248 private Intent[] mHotseats = null;
249 private Drawable[] mHotseatIcons = null;
250 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400251
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700252 private Intent mAppMarketIntent = null;
253
Adam Cohended9f8d2010-11-03 13:25:16 -0700254 // Related to the auto-advancing of widgets
255 private final int ADVANCE_MSG = 1;
256 private final int mAdvanceInterval = 20000;
257 private final int mAdvanceStagger = 250;
258 private long mAutoAdvanceSentTime;
259 private long mAutoAdvanceTimeLeft = -1;
260 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
261 new HashMap<View, AppWidgetProviderInfo>();
262
Winson Chung400438b2011-01-16 17:53:48 -0800263 // Determines how long to wait after a rotation before restoring the screen orientation to
264 // match the sensor state.
265 private final int mRestoreScreenOrientationDelay = 500;
266
Michael Jurka4ef207b2010-11-29 17:05:45 -0800267 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800268 private static Drawable.ConstantState sGlobalSearchIcon;
269 private static Drawable.ConstantState sVoiceSearchIcon;
270 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271
Michael Jurkaddd62e92011-02-16 17:49:14 -0800272 private BubbleTextView mWaitingForResume;
273
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274 private static ArrayList<PendingAddArguments> sPendingAddList
275 = new ArrayList<PendingAddArguments>();
276
277 private static class PendingAddArguments {
278 int requestCode;
279 Intent intent;
280 int screen;
281 int cellX;
282 int cellY;
283 }
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 @Override
286 protected void onCreate(Bundle savedInstanceState) {
287 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700288
Winson Chungaafa03c2010-06-11 17:34:16 -0700289 if (LauncherApplication.isInPlaceRotationEnabled()) {
290 // hide the status bar (temporary until we get the status bar design figured out)
291 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
292 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
293 }
294
Joe Onorato0589f0f2010-02-08 13:44:00 -0800295 LauncherApplication app = ((LauncherApplication)getApplication());
296 mModel = app.setLauncher(this);
297 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400298 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700300
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
303 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200306 android.os.Debug.startMethodTracing(
307 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400310 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800313 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700314 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800316 mCustomizePagedView = (CustomizePagedView) findViewById(
317 R.id.customization_drawer_tab_contents);
Winson Chung80baf5a2010-08-09 16:03:15 -0700318
Michael Jurka946ad472010-07-09 18:05:18 -0700319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 setupViews();
321
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800322 registerContentObservers();
323
Joe Onorato7c312c12009-08-13 21:36:53 -0700324 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 mSavedState = savedInstanceState;
327 restoreState(mSavedState);
328
Winson Chunga12a2502010-12-20 14:41:35 -0800329 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800330 if (mCustomizePagedView != null) {
331 mCustomizePagedView.update();
332 }
Winson Chunga12a2502010-12-20 14:41:35 -0800333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 if (PROFILE_STARTUP) {
335 android.os.Debug.stopMethodTracing();
336 }
337
338 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400339 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341
342 // For handling default keys
343 mDefaultKeySsb = new SpannableStringBuilder();
344 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800345
346 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
347 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800348
349 // If we have a saved version of these external icons, we load them up immediately
350 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800351 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
352 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800353 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800354 if (sGlobalSearchIcon != null) {
355 updateGlobalSearchIcon(sGlobalSearchIcon);
356 }
357 if (sVoiceSearchIcon != null) {
358 updateVoiceSearchIcon(sVoiceSearchIcon);
359 }
360 if (sAppMarketIcon != null) {
361 updateAppMarketIcon(sAppMarketIcon);
362 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800366 @Override
367 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
368 super.dispatchPopulateAccessibilityEvent(event);
369
370 // we want to take over text population so it is context dependent
371 event.getText().clear();
372 if (mState == State.ALL_APPS) {
373 event.getText().add(getString(R.string.all_apps_button_label));
374 } else if (mState == State.WORKSPACE) {
375 event.getText().add(getString(R.string.all_apps_home_button_label));
376 }
377
378 return true;
379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 if (sLocaleConfiguration == null) {
383 new AsyncTask<Void, Void, LocaleConfiguration>() {
384 @Override
385 protected LocaleConfiguration doInBackground(Void... unused) {
386 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
387 readConfiguration(Launcher.this, localeConfiguration);
388 return localeConfiguration;
389 }
390
391 @Override
392 protected void onPostExecute(LocaleConfiguration result) {
393 sLocaleConfiguration = result;
394 checkForLocaleChange(); // recursive, but now with a locale configuration
395 }
396 }.execute();
397 return;
398 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final Configuration configuration = getResources().getConfiguration();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final String locale = configuration.locale.toString();
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mcc = configuration.mcc;
407
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 final int mnc = configuration.mnc;
410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700414 sLocaleConfiguration.locale = locale;
415 sLocaleConfiguration.mcc = mcc;
416 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700417
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400419 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700420
421 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
422 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700423 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424 public void run() {
425 writeConfiguration(Launcher.this, localeConfiguration);
426 }
427 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700428 }
429 }
430
431 private static class LocaleConfiguration {
432 public String locale;
433 public int mcc = -1;
434 public int mnc = -1;
435 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700436
Romain Guy98d01652009-06-30 16:21:04 -0700437 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
438 DataInputStream in = null;
439 try {
440 in = new DataInputStream(context.openFileInput(PREFERENCES));
441 configuration.locale = in.readUTF();
442 configuration.mcc = in.readInt();
443 configuration.mnc = in.readInt();
444 } catch (FileNotFoundException e) {
445 // Ignore
446 } catch (IOException e) {
447 // Ignore
448 } finally {
449 if (in != null) {
450 try {
451 in.close();
452 } catch (IOException e) {
453 // Ignore
454 }
455 }
456 }
457 }
458
459 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
460 DataOutputStream out = null;
461 try {
462 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
463 out.writeUTF(configuration.locale);
464 out.writeInt(configuration.mcc);
465 out.writeInt(configuration.mnc);
466 out.flush();
467 } catch (FileNotFoundException e) {
468 // Ignore
469 } catch (IOException e) {
470 //noinspection ResultOfMethodCallIgnored
471 context.getFileStreamPath(PREFERENCES).delete();
472 } finally {
473 if (out != null) {
474 try {
475 out.close();
476 } catch (IOException e) {
477 // Ignore
478 }
479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481 }
482
483 static int getScreen() {
484 synchronized (sLock) {
485 return sScreen;
486 }
487 }
488
489 static void setScreen(int screen) {
490 synchronized (sLock) {
491 sScreen = screen;
492 }
493 }
494
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400495 // Note: This doesn't do all the client-id magic that BrowserProvider does
496 // in Browser. (http://b/2425179)
497 private Uri getDefaultBrowserUri() {
498 String url = getString(R.string.default_browser_url);
499 if (url.indexOf("{CID}") != -1) {
500 url = url.replace("{CID}", "android-google");
501 }
502 return Uri.parse(url);
503 }
504
505 // Load the Intent templates from arrays.xml to populate the hotseats. For
506 // each Intent, if it resolves to a single app, use that as the launch
507 // intent & use that app's label as the contentDescription. Otherwise,
508 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400509 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400510 if (mHotseatConfig == null) {
511 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
512 if (mHotseatConfig.length > 0) {
513 mHotseats = new Intent[mHotseatConfig.length];
514 mHotseatLabels = new CharSequence[mHotseatConfig.length];
515 mHotseatIcons = new Drawable[mHotseatConfig.length];
516 } else {
517 mHotseats = null;
518 mHotseatIcons = null;
519 mHotseatLabels = null;
520 }
521
522 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
523 for (int i=0; i<mHotseatConfig.length; i++) {
524 // load icon for this slot; currently unrelated to the actual activity
525 try {
526 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
527 } catch (ArrayIndexOutOfBoundsException ex) {
528 Log.w(TAG, "Missing hotseat_icons array item #" + i);
529 mHotseatIcons[i] = null;
530 }
531 }
532 hotseatIconDrawables.recycle();
533 }
534
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400535 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400536 for (int i=0; i<mHotseatConfig.length; i++) {
537 Intent intent = null;
538 if (mHotseatConfig[i].equals("*BROWSER*")) {
539 // magic value meaning "launch user's default web browser"
540 // replace it with a generic web request so we can see if there is indeed a default
541 String defaultUri = getString(R.string.default_browser_url);
542 intent = new Intent(
543 Intent.ACTION_VIEW,
544 ((defaultUri != null)
545 ? Uri.parse(defaultUri)
546 : getDefaultBrowserUri())
547 ).addCategory(Intent.CATEGORY_BROWSABLE);
548 // note: if the user launches this without a default set, she
549 // will always be taken to the default URL above; this is
550 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700551 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400552 // URL is unavoidably sent to the chosen app.
553 } else {
554 try {
555 intent = Intent.parseUri(mHotseatConfig[i], 0);
556 } catch (java.net.URISyntaxException ex) {
557 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
558 // bogus; leave intent=null
559 }
560 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700561
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 if (intent == null) {
563 mHotseats[i] = null;
564 mHotseatLabels[i] = getText(R.string.activity_not_found);
565 continue;
566 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400567
568 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 Log.d(TAG, "loadHotseats: hotseat " + i
570 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400571 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 + "]");
573 }
574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
576 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700577 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400578 Log.d(TAG, "Best match for intent: " + bestMatch);
579 Log.d(TAG, "All matches: ");
580 for (ResolveInfo ri : allMatches) {
581 Log.d(TAG, " --> " + ri);
582 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 // did this resolve to a single app, or the resolver?
585 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 // can't find any activity to handle this. let's leave the
587 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 // to launch.
589 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400590
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 // set accessibility text to "Not installed"
592 mHotseatLabels[i] = getText(R.string.activity_not_found);
593 } else {
594 boolean found = false;
595 for (ResolveInfo ri : allMatches) {
596 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
597 && bestMatch.activityInfo.applicationInfo.packageName
598 .equals(ri.activityInfo.applicationInfo.packageName)) {
599 found = true;
600 break;
601 }
602 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700603
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400604 if (!found) {
605 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
606 // the bestMatch is probably the ResolveActivity, meaning the
607 // user has not yet selected a default
608 // so: we'll keep the original intent for now
609 mHotseats[i] = intent;
610
611 // set the accessibility text to "Select shortcut"
612 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
613 } else {
614 // we have an app!
615 // now reconstruct the intent to launch it through the front
616 // door
617 ComponentName com = new ComponentName(
618 bestMatch.activityInfo.applicationInfo.packageName,
619 bestMatch.activityInfo.name);
620 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
621
622 // load the app label for accessibility
623 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
624 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625 }
626
627 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700628 Log.d(TAG, "loadHotseats: hotseat " + i
629 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400630 + ((mHotseats[i] == null)
631 ? "null"
632 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633 + "] label=[" + mHotseatLabels[i]
634 + "]"
635 );
636 }
637 }
638 }
639
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800640 private void completeAdd(PendingAddArguments args) {
641 switch (args.requestCode) {
642 case REQUEST_PICK_APPLICATION:
643 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
644 break;
645 case REQUEST_PICK_SHORTCUT:
646 processShortcut(args.intent);
647 break;
648 case REQUEST_CREATE_SHORTCUT:
649 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
650 break;
651 case REQUEST_PICK_LIVE_FOLDER:
652 addLiveFolder(args.intent);
653 break;
654 case REQUEST_CREATE_LIVE_FOLDER:
655 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
656 break;
657 case REQUEST_PICK_APPWIDGET:
658 addAppWidgetFromPick(args.intent);
659 break;
660 case REQUEST_CREATE_APPWIDGET:
661 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
662 completeAddAppWidget(appWidgetId, args.screen);
663 break;
664 case REQUEST_PICK_WALLPAPER:
665 // We just wanted the activity result here so we can clear mWaitingForResult
666 break;
667 }
668 }
669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700672 mWaitingForResult = false;
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 // The pattern used here is that a user PICKs a specific application,
675 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
678 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700679
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 final PendingAddArguments args = new PendingAddArguments();
682 args.requestCode = requestCode;
683 args.intent = data;
684 args.screen = mAddScreen;
685 args.cellX = mAddIntersectCellX;
686 args.cellY = mAddIntersectCellY;
687
688 // If the loader is still running, defer the add until it is done.
689 if (isWorkspaceLocked()) {
690 sPendingAddList.add(args);
691 } else {
692 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
Romain Guy18042c82009-11-06 11:44:55 -0800694 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
695 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
696 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 // Clean up the appWidgetId if we canceled
698 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
699 if (appWidgetId != -1) {
700 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 }
704
705 @Override
706 protected void onResume() {
707 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800708 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700709 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 mWorkspaceLoading = true;
711 mModel.startLoader(this, true);
712 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700713 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800715 if (mWaitingForResume != null) {
716 mWaitingForResume.setStayPressed(false);
717 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700718 // When we resume Launcher, a different Activity might be responsible for the app
719 // market intent, so refresh the icon
720 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722
723 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 protected void onPause() {
725 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 // Some launcher layouts don't have a previous and next view
727 if (mPreviousView != null) {
728 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700729 }
730 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700731 dismissPreview(mNextView);
732 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700733 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800734 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 }
Romain Guycbb89e42009-06-08 15:52:54 -0700736
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700737 @Override
738 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 // Flag the loader to stop early before switching
740 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800741 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800742 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700743 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800745 // We can't hide the IME if it was forced open. So don't bother
746 /*
747 @Override
748 public void onWindowFocusChanged(boolean hasFocus) {
749 super.onWindowFocusChanged(hasFocus);
750
751 if (hasFocus) {
752 final InputMethodManager inputManager = (InputMethodManager)
753 getSystemService(Context.INPUT_METHOD_SERVICE);
754 WindowManager.LayoutParams lp = getWindow().getAttributes();
755 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
756 android.os.Handler()) {
757 protected void onReceiveResult(int resultCode, Bundle resultData) {
758 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
759 }
760 });
761 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
762 }
763 }
764 */
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 private boolean acceptFilter() {
767 final InputMethodManager inputManager = (InputMethodManager)
768 getSystemService(Context.INPUT_METHOD_SERVICE);
769 return !inputManager.isFullscreenMode();
770 }
771
772 @Override
773 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700774 final int uniChar = event.getUnicodeChar();
775 final boolean handled = super.onKeyDown(keyCode, event);
776 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
777 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
779 keyCode, event);
780 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700781 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700782 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700783 // showSearchDialog()
784 // If there are multiple keystrokes before the search dialog takes focus,
785 // onSearchRequested() will be called for every keystroke,
786 // but it is idempotent, so it's fine.
787 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
789 }
790
Joe Onorato8a9625e2010-01-28 15:55:35 -0800791 // Eat the long press event so the keyboard doesn't come up.
792 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
793 return true;
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 return handled;
797 }
798
Karl Rosaen138a0412009-04-23 19:00:21 -0700799 private String getTypedText() {
800 return mDefaultKeySsb.toString();
801 }
802
803 private void clearTypedText() {
804 mDefaultKeySsb.clear();
805 mDefaultKeySsb.clearSpans();
806 Selection.setSelection(mDefaultKeySsb, 0);
807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700810 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
811 * State
812 */
813 private static State intToState(int stateOrdinal) {
814 State state = State.WORKSPACE;
815 final State[] stateValues = State.values();
816 for (int i = 0; i < stateValues.length; i++) {
817 if (stateValues[i].ordinal() == stateOrdinal) {
818 state = stateValues[i];
819 break;
820 }
821 }
822 return state;
823 }
824
825 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 * Restores the previous state, if it exists.
827 *
828 * @param savedState The previous state.
829 */
830 private void restoreState(Bundle savedState) {
831 if (savedState == null) {
832 return;
833 }
834
Michael Jurka883f55b2010-10-21 15:47:14 -0700835 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
836
837 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800838 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700839 } else if (state == State.CUSTOMIZE) {
840 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
844 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700845 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 }
847
848 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700851 mAddScreen = addScreen;
852 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
853 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 mRestoring = true;
855 }
856
857 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
858 if (renameFolder) {
859 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700860 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 mRestoring = true;
862 }
Winson Chunga12a2502010-12-20 14:41:35 -0800863
864 // Restore the current AllApps drawer tab
865 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
866 String curTab = savedState.getString("allapps_currentTab");
867 if (curTab != null) {
868 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
869 tabhost.setCurrentTabByTag(curTab);
870 }
871 int curPage = savedState.getInt("allapps_currentPage", -1);
872 if (curPage > -1) {
873 mAllAppsPagedView.setRestorePage(curPage);
874 }
875 }
876
877 // Restore the current customization drawer tab
878 if (mHomeCustomizationDrawer != null) {
879 String curTab = savedState.getString("customize_currentTab");
880 if (curTab != null) {
881 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800882 mCustomizePagedView.setCustomizationFilter(
883 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800884 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
885 }
886
887 // Note: currently we do not restore the page for the customization tray because unlike
888 // AllApps, the page content can change drastically
889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 /**
893 * Finds all the views we need and configure them properly.
894 */
895 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700896 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400897
Romain Guyb1b69f52009-08-10 15:10:15 -0700898 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400899 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900
Joe Onorato7c312c12009-08-13 21:36:53 -0700901 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700902 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700903 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800904 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700905 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700906 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700907
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700908 if (LauncherApplication.isScreenXLarge()) {
909 // They need to be INVISIBLE initially so that they will be measured in the layout.
910 // Otherwise the animations are messed up when we show them for the first time.
911 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
912 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
913 }
914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500918 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700919
Joe Onorato7c312c12009-08-13 21:36:53 -0700920 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
921 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700923 View handleView = findViewById(R.id.all_apps_button);
924 if (handleView != null && handleView instanceof HandleView) {
925 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700926 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700927 mHandleView.setLauncher(this);
928 mHandleView.setOnClickListener(this);
929 mHandleView.setOnLongClickListener(this);
930 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800931
Winson Chung80baf5a2010-08-09 16:03:15 -0700932 if (mCustomizePagedView != null) {
933 mCustomizePagedView.setLauncher(this);
934 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700935 } else {
936 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
937 hotseatLeft.setContentDescription(mHotseatLabels[0]);
938 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
939 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
940 hotseatRight.setContentDescription(mHotseatLabels[1]);
941 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400942
Michael Jurkaaf442092010-06-10 17:01:57 -0700943 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
944 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800945
Michael Jurkaaf442092010-06-10 17:01:57 -0700946 Drawable previous = mPreviousView.getDrawable();
947 Drawable next = mNextView.getDrawable();
948 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Michael Jurkaaf442092010-06-10 17:01:57 -0700950 mPreviousView.setHapticFeedbackEnabled(false);
951 mPreviousView.setOnLongClickListener(this);
952 mNextView.setHapticFeedbackEnabled(false);
953 mNextView.setOnLongClickListener(this);
954 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400957 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800959 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960
961 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400962 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800963
964 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700965 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800966 final View configureButton = findViewById(R.id.configure_button);
967
Michael Jurka54f7ac32010-08-02 13:56:46 -0700968 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800969 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700970 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800971 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700972 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700973 dragController.addDragListener(deleteZone);
974
Adam Cohencdc30d52010-12-01 15:09:47 -0800975 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
976 if (allAppsDeleteZone != null) {
977 allAppsDeleteZone.setLauncher(this);
978 allAppsDeleteZone.setDragController(dragController);
979 allAppsDeleteZone.setDragAndDropEnabled(false);
980 dragController.addDragListener(allAppsDeleteZone);
981 dragController.addDropTarget(allAppsDeleteZone);
982 }
983
984 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
985 findViewById(R.id.all_apps_info_target);
986 if (allAppsInfoTarget != null) {
987 allAppsInfoTarget.setLauncher(this);
988 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800989 allAppsInfoTarget.setDragAndDropEnabled(false);
990 View marketButton = findViewById(R.id.market_button);
991 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800992 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -0800993 marketButton.setOnClickListener(new OnClickListener() {
994 public void onClick(View v) {
995 onClickAppMarketButton(v);
996 }
997 });
Adam Cohencdc30d52010-12-01 15:09:47 -0800998 }
999 }
1000
Joe Onorato00acb122009-08-04 16:04:30 -04001001 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001002 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001003 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001004
Joe Onorato00acb122009-08-04 16:04:30 -04001005 // The order here is bottom to top.
1006 dragController.addDropTarget(workspace);
1007 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001008 if (allAppsInfoTarget != null) {
1009 dragController.addDropTarget(allAppsInfoTarget);
1010 }
1011 if (allAppsDeleteZone != null) {
1012 dragController.addDropTarget(allAppsDeleteZone);
1013 }
Winson Chung97d85d22011-04-13 11:27:36 -07001014 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
1015 View.OnKeyListener listener = new ButtonBarKeyEventListener();
1016 int buttonCount = mButtonCluster.getChildCount();
1017 for (int i = 0; i < buttonCount; ++i) {
1018 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1019 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001020
1021 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001022 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001023 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001024
1025 // We had previously set these click handlers in XML, but the first time we launched
1026 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1027 // found the right handler. Setting the handlers directly here eliminates that cost.
1028 if (mConfigureButton != null) {
1029 mConfigureButton.setOnClickListener(new OnClickListener() {
1030 public void onClick(View v) {
1031 onClickConfigureButton(v);
1032 }
1033 });
1034 }
1035 if (mDivider != null) {
1036 mDivider.setOnClickListener(new OnClickListener() {
1037 public void onClick(View v) {
1038 onClickAllAppsButton(v);
1039 }
1040 });
1041 }
1042 if (mAllAppsButton != null) {
1043 mAllAppsButton.setOnClickListener(new OnClickListener() {
1044 public void onClick(View v) {
1045 onClickAllAppsButton(v);
1046 }
1047 });
1048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050
Romain Guy8a73c512009-11-09 19:19:59 -08001051 @SuppressWarnings({"UnusedDeclaration"})
1052 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001053 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001054 mWorkspace.scrollLeft();
1055 }
Romain Guy8a73c512009-11-09 19:19:59 -08001056 }
1057
1058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001060 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001061 mWorkspace.scrollRight();
1062 }
Romain Guy8a73c512009-11-09 19:19:59 -08001063 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001064
1065 @SuppressWarnings({"UnusedDeclaration"})
1066 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001067 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001068
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001069 int index = -1;
1070 if (v.getId() == R.id.hotseat_left) {
1071 index = 0;
1072 } else if (v.getId() == R.id.hotseat_right) {
1073 index = 1;
1074 }
1075
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001076 // reload these every tap; you never know when they might change
1077 loadHotseats();
1078 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001079 startActivitySafely(
1080 mHotseats[index],
1081 "hotseat"
1082 );
1083 }
1084 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 /**
1087 * Creates a view representing a shortcut.
1088 *
1089 * @param info The data structure describing the shortcut.
1090 *
1091 * @return A View inflated from R.layout.application.
1092 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001093 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001095 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
1097
1098 /**
1099 * Creates a view representing a shortcut inflated from the specified resource.
1100 *
1101 * @param layoutResId The id of the XML layout used to create the shortcut.
1102 * @param parent The group the shortcut belongs to.
1103 * @param info The data structure describing the shortcut.
1104 *
1105 * @return A View inflated from layoutResId.
1106 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001107 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001108 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1109 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 return favorite;
1112 }
1113
1114 /**
1115 * Add an application shortcut to the workspace.
1116 *
1117 * @param data The intent describing the application.
1118 * @param cellInfo The position on screen where to create the shortcut.
1119 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001120 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001121 int intersectCellX, int intersectCellY) {
1122 final int[] cellXY = mTmpAddItemCellCoordinates;
1123 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1124
1125 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1126 showOutOfSpaceMessage();
1127 return;
1128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001130 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001131
Romain Guy73b979d2009-06-09 12:57:21 -07001132 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001135 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1137 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001138 } else {
1139 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141 }
Romain Guycbb89e42009-06-08 15:52:54 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 /**
1144 * Add a shortcut to the workspace.
1145 *
1146 * @param data The intent describing the shortcut.
1147 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 private void completeAddShortcut(Intent data, int screen,
1150 int intersectCellX, int intersectCellY) {
1151 final int[] cellXY = mTmpAddItemCellCoordinates;
1152 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001154 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001155 boolean foundCellSpan = false;
1156 if (touchXY != null) {
1157 // when dragging and dropping, just find the closest free spot
1158 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1159 int[] result = screenLayout.findNearestVacantArea(
1160 touchXY[0], touchXY[1], 1, 1, cellXY);
1161 foundCellSpan = (result != null);
1162 } else {
1163 foundCellSpan = layout.findCellForSpanThatIntersects(
1164 cellXY, 1, 1, intersectCellX, intersectCellY);
1165 }
1166
1167 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 showOutOfSpaceMessage();
1169 return;
1170 }
1171
1172 final ShortcutInfo info = mModel.addShortcut(
1173 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174
1175 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001177 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
1179 }
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001184 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001185 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 */
Winson Chungf7640c82011-02-28 13:47:29 -08001187 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001189
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001190 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1192 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001195 // We have saved the position to which the widget was dragged-- this really only matters
1196 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001198
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001199 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 boolean foundCellSpan = false;
1201 if (touchXY != null) {
1202 // when dragging and dropping, just find the closest free spot
1203 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1204 int[] result = screenLayout.findNearestVacantArea(
1205 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001206 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001208 // if we long pressed on an empty cell to bring up a menu,
1209 // make sure we intersect the empty cell
1210 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1211 // findCellForSpanThatIntersects will just ignore them
1212 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1213 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001214 }
1215
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001217 if (appWidgetId != -1) {
1218 // Deleting an app widget ID is a void call but writes to disk before returning
1219 // to the caller...
1220 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1221 new Thread("deleteAppWidgetId") {
1222 public void run() {
1223 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1224 }
1225 }.start();
1226 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001228 return;
1229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001231 // Build Launcher-specific widget info and save to database
1232 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 launcherInfo.spanX = spanXY[0];
1234 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 LauncherModel.addItemToDatabase(this, launcherInfo,
1237 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
1240 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001245
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001246 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Michael Jurka0280c3b2010-09-17 15:00:07 -07001249 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001250 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001251
1252 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254 }
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Adam Cohended9f8d2010-11-03 13:25:16 -07001256 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1257 @Override
1258 public void onReceive(Context context, Intent intent) {
1259 final String action = intent.getAction();
1260 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1261 mUserPresent = false;
1262 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001263
1264 // Reset AllApps to it's initial state
1265 if (mAllAppsGrid != null) {
1266 mAllAppsGrid.reset();
1267 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001268 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1269 mUserPresent = true;
1270 updateRunning();
1271 }
1272 }
1273 };
1274
1275 @Override
1276 public void onAttachedToWindow() {
1277 super.onAttachedToWindow();
1278
1279 // Listen for broadcasts related to user-presence
1280 final IntentFilter filter = new IntentFilter();
1281 filter.addAction(Intent.ACTION_SCREEN_OFF);
1282 filter.addAction(Intent.ACTION_USER_PRESENT);
1283 registerReceiver(mReceiver, filter);
1284
Adam Cohend113e0c2010-11-11 10:48:05 -08001285 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 mVisible = true;
1287 }
1288
1289 @Override
1290 public void onDetachedFromWindow() {
1291 super.onDetachedFromWindow();
1292 mVisible = false;
1293
Adam Cohend113e0c2010-11-11 10:48:05 -08001294 if (mAttached) {
1295 unregisterReceiver(mReceiver);
1296 mAttached = false;
1297 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001298 updateRunning();
1299 }
1300
1301 public void onWindowVisibilityChanged(int visibility) {
1302 mVisible = visibility == View.VISIBLE;
1303 updateRunning();
1304 }
1305
1306 private void sendAdvanceMessage(long delay) {
1307 mHandler.removeMessages(ADVANCE_MSG);
1308 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1309 mHandler.sendMessageDelayed(msg, delay);
1310 mAutoAdvanceSentTime = System.currentTimeMillis();
1311 }
1312
1313 private void updateRunning() {
1314 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1315 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1316 mAutoAdvanceRunning = autoAdvanceRunning;
1317 if (autoAdvanceRunning) {
1318 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1319 sendAdvanceMessage(delay);
1320 } else {
1321 if (!mWidgetsToAdvance.isEmpty()) {
1322 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1323 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1324 }
1325 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001326 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001327 }
1328 }
1329 }
1330
1331 private final Handler mHandler = new Handler() {
1332 @Override
1333 public void handleMessage(Message msg) {
1334 if (msg.what == ADVANCE_MSG) {
1335 int i = 0;
1336 for (View key: mWidgetsToAdvance.keySet()) {
1337 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1338 final int delay = mAdvanceStagger * i;
1339 if (v instanceof Advanceable) {
1340 postDelayed(new Runnable() {
1341 public void run() {
1342 ((Advanceable) v).advance();
1343 }
1344 }, delay);
1345 }
1346 i++;
1347 }
1348 sendAdvanceMessage(mAdvanceInterval);
1349 }
1350 }
1351 };
1352
1353 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1354 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1355 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1356 if (v instanceof Advanceable) {
1357 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001358 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 updateRunning();
1360 }
1361 }
1362
1363 void removeWidgetToAutoAdvance(View hostView) {
1364 if (mWidgetsToAdvance.containsKey(hostView)) {
1365 mWidgetsToAdvance.remove(hostView);
1366 updateRunning();
1367 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 }
1369
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1371 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001372 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 launcherInfo.hostView = null;
1374 }
1375
Adam Cohended9f8d2010-11-03 13:25:16 -07001376 void showOutOfSpaceMessage() {
1377 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1378 }
1379
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 public LauncherAppWidgetHost getAppWidgetHost() {
1381 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Romain Guycbb89e42009-06-08 15:52:54 -07001383
Winson Chunga9abd0e2010-10-27 17:18:37 -07001384 public LauncherModel getModel() {
1385 return mModel;
1386 }
1387
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001388 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001389 getWindow().closeAllPanels();
1390
1391 try {
1392 dismissDialog(DIALOG_CREATE_SHORTCUT);
1393 // Unlock the workspace if the dialog was showing
1394 } catch (Exception e) {
1395 // An exception is thrown if the dialog is not visible, which is fine
1396 }
1397
1398 try {
1399 dismissDialog(DIALOG_RENAME_FOLDER);
1400 // Unlock the workspace if the dialog was showing
1401 } catch (Exception e) {
1402 // An exception is thrown if the dialog is not visible, which is fine
1403 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001404
1405 // Whatever we were doing is hereby canceled.
1406 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 @Override
1410 protected void onNewIntent(Intent intent) {
1411 super.onNewIntent(intent);
1412
1413 // Close the menu
1414 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001415 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001416 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001417
Joe Onorato14f122b2009-11-19 14:06:36 -08001418 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1419 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001420
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001421 // In all these cases, only animate if we're already on home
1422
Michael Jurka4cb37242010-08-09 21:05:32 -07001423 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001424 mWorkspace.unshrink(alreadyOnHome);
1425 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001426
Patrick Dubroy758a9232011-03-03 19:54:56 -08001427 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001428 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001429 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001430 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001431 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001432
Joe Onorato3a8820b2009-11-10 15:06:42 -08001433 final View v = getWindow().peekDecorView();
1434 if (v != null && v.getWindowToken() != null) {
1435 InputMethodManager imm = (InputMethodManager)getSystemService(
1436 INPUT_METHOD_SERVICE);
1437 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001439
1440 // Reset AllApps to it's initial state
1441 if (mAllAppsGrid != null) {
1442 mAllAppsGrid.reset();
1443 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445 }
1446
1447 @Override
1448 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1449 // Do not call super here
1450 mSavedInstanceState = savedInstanceState;
1451 }
1452
1453 @Override
1454 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001455 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
1457 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1458 if (folders.size() > 0) {
1459 final int count = folders.size();
1460 long[] ids = new long[count];
1461 for (int i = 0; i < count; i++) {
1462 final FolderInfo info = folders.get(i).getInfo();
1463 ids[i] = info.id;
1464 }
1465 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1466 } else {
1467 super.onSaveInstanceState(outState);
1468 }
1469
Michael Jurka883f55b2010-10-21 15:47:14 -07001470 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
Michael Jurka0280c3b2010-09-17 15:00:07 -07001472 if (mAddScreen > -1 && mWaitingForResult) {
1473 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1474 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1475 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
1478 if (mFolderInfo != null && mWaitingForResult) {
1479 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1480 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1481 }
Michael Jurka946ad472010-07-09 18:05:18 -07001482
Winson Chung43b119d2010-12-06 16:45:05 -08001483 // Save the current AllApps drawer tab
1484 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1485 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1486 String currentTabTag = tabhost.getCurrentTabTag();
1487 if (currentTabTag != null) {
1488 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001489 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001490 }
1491 }
1492
1493 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001494 if (mHomeCustomizationDrawer != null) {
1495 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1496 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001497 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001498 }
1499 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
1502 @Override
1503 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001507 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001509 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001511 mAppWidgetHost = null;
1512
1513 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514
1515 TextKeyListener.getInstance().release();
1516
Joe Onorato9c1289c2009-08-17 11:03:03 -04001517 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518
Joe Onorato9c1289c2009-08-17 11:03:03 -04001519 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001520
1521 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001522
Patrick Dubroyab962b72010-07-26 12:10:10 -07001523 // Some launcher layouts don't have a previous and next view
1524 if (mPreviousView != null) {
1525 dismissPreview(mPreviousView);
1526 }
1527 if (mNextView != null) {
1528 dismissPreview(mNextView);
1529 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001530
1531 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001532
1533 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1534 mWorkspace.removeAllViews();
1535 mWorkspace = null;
1536 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001537
1538 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540
1541 @Override
1542 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001543 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 super.startActivityForResult(intent, requestCode);
1545 }
1546
1547 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001548 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001550
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001551 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 if (initialQuery == null) {
1554 // Use any text typed in the launcher as the initial query
1555 initialQuery = getTypedText();
1556 clearTypedText();
1557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 if (appSearchData == null) {
1559 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001560 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Karl Rosaen138a0412009-04-23 19:00:21 -07001563 final SearchManager searchManager =
1564 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001565 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001566 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568
1569 @Override
1570 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001571 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001572 return false;
1573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574
1575 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1578 .setIcon(android.R.drawable.ic_menu_add)
1579 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001580 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1581 .setIcon(android.R.drawable.ic_menu_manage)
1582 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001583 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 .setIcon(android.R.drawable.ic_menu_gallery)
1585 .setAlphabeticShortcut('W');
1586 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1587 .setIcon(android.R.drawable.ic_search_category_default)
1588 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1589 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1590 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1591 .setAlphabeticShortcut('N');
1592
1593 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001594 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1595 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596
1597 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1598 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1599 .setIntent(settings);
1600
1601 return true;
1602 }
1603
1604 @Override
1605 public boolean onPrepareOptionsMenu(Menu menu) {
1606 super.onPrepareOptionsMenu(menu);
1607
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001608 // If all apps is animating, don't show the menu, because we don't know
1609 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001610 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001611 return false;
1612 }
1613
1614 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001615 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001616 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1617 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1618
1619 // Disable add if the workspace is full.
1620 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001621 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1622 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001623 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624
1625 return true;
1626 }
1627
1628 @Override
1629 public boolean onOptionsItemSelected(MenuItem item) {
1630 switch (item.getItemId()) {
1631 case MENU_ADD:
1632 addItems();
1633 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001634 case MENU_MANAGE_APPS:
1635 manageApps();
1636 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 case MENU_WALLPAPER_SETTINGS:
1638 startWallpaper();
1639 return true;
1640 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001641 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 return true;
1643 case MENU_NOTIFICATIONS:
1644 showNotifications();
1645 return true;
1646 }
1647
1648 return super.onOptionsItemSelected(item);
1649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Karl Rosaen138a0412009-04-23 19:00:21 -07001651 /**
1652 * Indicates that we want global search for this activity by setting the globalSearch
1653 * argument for {@link #startSearch} to true.
1654 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001656 @Override
1657 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001658 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001659 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001660 }
1661
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662 public boolean isWorkspaceLocked() {
1663 return mWorkspaceLoading || mWaitingForResult;
1664 }
1665
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001666 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1667 private boolean isPreviewVisible() {
1668 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1669 (mNextView != null && mNextView.getTag() != null);
1670 }
1671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001673 if (LauncherApplication.isScreenXLarge()) {
1674 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001675 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001676 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001677 }
1678 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001679 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001680 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683
Michael Jurka0280c3b2010-09-17 15:00:07 -07001684 private void resetAddInfo() {
1685 mAddScreen = -1;
1686 mAddIntersectCellX = -1;
1687 mAddIntersectCellY = -1;
1688 mAddDropPosition = null;
1689 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001690
Winson Chung55cef262010-10-28 14:14:18 -07001691 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001692 resetAddInfo();
1693 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 mAddDropPosition = position;
1695
Michael Jurkaaf442092010-06-10 17:01:57 -07001696 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001697 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1698 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001699 }
1700
Winson Chung7ad01412010-09-27 11:33:03 -07001701 private void manageApps() {
1702 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1703 }
1704
Michael Jurkaaf442092010-06-10 17:01:57 -07001705 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001706 // TODO: catch bad widget exception when sent
1707 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001708 // TODO: Is this log message meaningful?
1709 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001710 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001711 }
1712
Winson Chung55cef262010-10-28 14:14:18 -07001713 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001714 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715
Bjorn Bringert7984c942009-12-09 15:38:25 +00001716 if (appWidget.configure != null) {
1717 // Launch over to configure widget, if needed
1718 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1719 intent.setComponent(appWidget.configure);
1720 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001721 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001722 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1723 intent.putExtra(
1724 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1725 info.mimeType);
1726
1727 final String mimeType = info.mimeType;
1728 final ClipData clipData = (ClipData) info.configurationData;
1729 final ClipDescription clipDesc = clipData.getDescription();
1730 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1731 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001732 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001733 final CharSequence stringData = item.getText();
1734 final Uri uriData = item.getUri();
1735 final Intent intentData = item.getIntent();
1736 final String key =
1737 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1738 if (uriData != null) {
1739 intent.putExtra(key, uriData);
1740 } else if (intentData != null) {
1741 intent.putExtra(key, intentData);
1742 } else if (stringData != null) {
1743 intent.putExtra(key, stringData);
1744 }
1745 break;
1746 }
1747 }
1748 }
Winson Chung55cef262010-10-28 14:14:18 -07001749 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001750
Romain Guy8e633c52010-03-04 12:51:36 -08001751 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001753 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001754 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 }
1756 }
Romain Guycbb89e42009-06-08 15:52:54 -07001757
Michael Jurka0280c3b2010-09-17 15:00:07 -07001758 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1759 resetAddInfo();
1760 mAddScreen = screen;
1761 mAddDropPosition = position;
1762
1763 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1764 createShortcutIntent.setComponent(componentName);
1765 processShortcut(createShortcutIntent);
1766 }
1767
Joe Onoratodeb98af2010-02-19 14:59:39 -08001768 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 // Handle case where user selected "Applications"
1770 String applicationName = getResources().getString(R.string.group_applications);
1771 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001773 if (applicationName != null && applicationName.equals(shortcutName)) {
1774 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1775 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001776
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1778 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001779 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001780 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001781 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001782 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 }
1785
Winson Chung24ab2f12010-09-16 14:10:47 -07001786 void processWallpaper(Intent intent) {
1787 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1788 }
1789
Michael Jurka0280c3b2010-09-17 15:00:07 -07001790 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1791 resetAddInfo();
1792 mAddScreen = screen;
1793 mAddDropPosition = position;
1794
1795 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1796 createFolderIntent.setComponent(componentName);
1797
1798 addLiveFolder(createFolderIntent);
1799 }
1800
1801 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001802 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001803 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001804 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001805
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001806 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001807 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001808 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001809 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
1812
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 UserFolderInfo folderInfo = new UserFolderInfo();
1815 folderInfo.title = getText(R.string.folder_name);
1816
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1818 final int[] cellXY = mTmpAddItemCellCoordinates;
1819 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1820 showOutOfSpaceMessage();
1821 return;
1822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823
1824 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001825 LauncherModel.addItemToDatabase(this, folderInfo,
1826 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001828 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829
1830 // Create the view
1831 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001832 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1833 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
Romain Guycbb89e42009-06-08 15:52:54 -07001836
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001838 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001839 }
1840
Michael Jurka28750fb2010-09-24 17:43:49 -07001841 private void completeAddLiveFolder(
1842 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1844 final int[] cellXY = mTmpAddItemCellCoordinates;
1845 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1846 showOutOfSpaceMessage();
1847 return;
1848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849
Michael Jurka0280c3b2010-09-17 15:00:07 -07001850 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851
1852 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001854 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001855 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
1857 }
1858
1859 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001860 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861
1862 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1863 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1864
1865 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 Intent.ShortcutIconResource iconResource = null;
1867
1868 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1869 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1870 try {
1871 iconResource = (Intent.ShortcutIconResource) extra;
1872 final PackageManager packageManager = context.getPackageManager();
1873 Resources resources = packageManager.getResourcesForApplication(
1874 iconResource.packageName);
1875 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1876 icon = resources.getDrawable(id);
1877 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001878 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880 }
1881
1882 if (icon == null) {
1883 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1884 }
1885
1886 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001887 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 info.title = name;
1889 info.iconResource = iconResource;
1890 info.uri = data.getData();
1891 info.baseIntent = baseIntent;
1892 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1893 LiveFolders.DISPLAY_MODE_GRID);
1894
1895 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001896 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001897 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898
1899 return info;
1900 }
1901
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 private void showNotifications() {
1903 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1904 if (statusBar != null) {
1905 statusBar.expand();
1906 }
1907 }
1908
1909 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001910 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001912 Intent chooser = Intent.createChooser(pickWallpaper,
1913 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001914 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1915 // Removed in Eclair MR1
1916// WallpaperManager wm = (WallpaperManager)
1917// getSystemService(Context.WALLPAPER_SERVICE);
1918// WallpaperInfo wi = wm.getWallpaperInfo();
1919// if (wi != null && wi.getSettingsActivity() != null) {
1920// LabeledIntent li = new LabeledIntent(getPackageName(),
1921// R.string.configure_wallpaper, 0);
1922// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1923// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1924// }
Mike Clerona0618e42009-10-22 13:55:21 -07001925 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
1927
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001928 /**
1929 * Registers various content observers. The current implementation registers
1930 * only a favorites observer to keep track of the favorites applications.
1931 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001932 private void registerContentObservers() {
1933 ContentResolver resolver = getContentResolver();
1934 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1935 true, mWidgetObserver);
1936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 @Override
1939 public boolean dispatchKeyEvent(KeyEvent event) {
1940 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1941 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001943 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001944 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001945 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001946 dumpState();
1947 return true;
1948 }
1949 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001950 }
1951 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1952 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001953 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 return true;
1955 }
1956 }
1957
1958 return super.dispatchKeyEvent(event);
1959 }
1960
Joe Onorato88ec0992009-11-19 13:16:06 -08001961 @Override
1962 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001963 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1964 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001965 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001966 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001967 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001968 dismissPreview(mPreviousView);
1969 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001970 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001971 mWorkspace.exitWidgetResizeMode();
1972
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001973 // Back button is a no-op here, but give at least some feedback for the button press
1974 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001975 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001976 }
1977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 private void closeFolder() {
1979 Folder folder = mWorkspace.getOpenFolder();
1980 if (folder != null) {
1981 closeFolder(folder);
1982 }
1983 }
1984
1985 void closeFolder(Folder folder) {
1986 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001987 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001989 CellLayout cl = (CellLayout) parent;
1990 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001991 if (folder instanceof DropTarget) {
1992 // Live folders aren't DropTargets.
1993 mDragController.removeDropTarget((DropTarget)folder);
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996 folder.onClose();
1997 }
1998
1999 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002000 * Re-listen when widgets are reset.
2001 */
2002 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002003 if (mAppWidgetHost != null) {
2004 mAppWidgetHost.startListening();
2005 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002006 }
2007
2008 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002009 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2010 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012 private void unbindDesktopItems() {
2013 for (ItemInfo item: mDesktopItems) {
2014 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002016 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 /**
2020 * Launches the intent referred by the clicked shortcut.
2021 *
2022 * @param v The view representing the clicked shortcut.
2023 */
2024 public void onClick(View v) {
2025 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002026 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002028 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002029 int[] pos = new int[2];
2030 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002031 intent.setSourceBounds(new Rect(pos[0], pos[1],
2032 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002033 boolean success = startActivitySafely(intent, tag);
2034
2035 if (success && v instanceof BubbleTextView) {
2036 mWaitingForResume = (BubbleTextView) v;
2037 mWaitingForResume.setStayPressed(true);
2038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } else if (tag instanceof FolderInfo) {
2040 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002041 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002042 if (mState == State.ALL_APPS) {
2043 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002044 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002045 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
2048 }
2049
Michael Jurka0e260592010-06-30 17:07:39 -07002050 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002051 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002052 // clicking anywhere on the workspace causes the customization drawer to slide down
2053 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002054 return false;
2055 }
2056
Michael Jurkaaf442092010-06-10 17:01:57 -07002057 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002058 * Event handler for the search button
2059 *
2060 * @param v The view that was clicked.
2061 */
2062 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002063 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002064 // Use a custom animation for launching search
2065 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002066 }
2067
2068 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002069 * Event handler for the voice button
2070 *
2071 * @param v The view that was clicked.
2072 */
2073 public void onClickVoiceButton(View v) {
2074 startVoiceSearch();
2075 }
2076
2077 private void startVoiceSearch() {
2078 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2079 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2080 startActivity(intent);
2081 }
2082
2083 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002084 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002085 * enters home screen customization mode.
2086 *
2087 * @param v The view that was clicked.
2088 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002089 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002090 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002091 }
2092
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002093 /**
2094 * Event handler for the "grid" button that appears on the home screen, which
2095 * enters all apps mode.
2096 *
2097 * @param v The view that was clicked.
2098 */
2099 public void onClickAllAppsButton(View v) {
2100 showAllApps(true);
2101 }
2102
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002103 public void onClickAppMarketButton(View v) {
2104 if (mAppMarketIntent != null) {
2105 startActivitySafely(mAppMarketIntent, "app market");
2106 }
2107 }
2108
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002109 void startApplicationDetailsActivity(ComponentName componentName) {
2110 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002111 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2112 Uri.fromParts("package", packageName, null));
2113 startActivity(intent);
2114 }
2115
Patrick Dubroy5539af72010-09-07 15:22:01 -07002116 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2117 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2118 // System applications cannot be installed. For now, show a toast explaining that.
2119 // We may give them the option of disabling apps this way.
2120 int messageId = R.string.uninstall_system_app_text;
2121 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2122 } else {
2123 String packageName = appInfo.componentName.getPackageName();
2124 String className = appInfo.componentName.getClassName();
2125 Intent intent = new Intent(
2126 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2127 startActivity(intent);
2128 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002129 }
2130
Michael Jurkaddd62e92011-02-16 17:49:14 -08002131 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2133 try {
2134 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002135 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 } catch (ActivityNotFoundException e) {
2137 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002138 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 } catch (SecurityException e) {
2140 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002141 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002143 "or use the exported attribute for this activity. "
2144 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002146 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002148
Romain Guy8e633c52010-03-04 12:51:36 -08002149 void startActivityForResultSafely(Intent intent, int requestCode) {
2150 try {
2151 startActivityForResult(intent, requestCode);
2152 } catch (ActivityNotFoundException e) {
2153 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2154 } catch (SecurityException e) {
2155 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2156 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2157 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2158 "or use the exported attribute for this activity.", e);
2159 }
2160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161
2162 private void handleFolderClick(FolderInfo folderInfo) {
2163 if (!folderInfo.opened) {
2164 // Close any open folder
2165 closeFolder();
2166 // Open the requested folder
2167 openFolder(folderInfo);
2168 } else {
2169 // Find the open folder...
2170 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2171 int folderScreen;
2172 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002173 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 // .. and close it
2175 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002176 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 // Close any folder open on the current screen
2178 closeFolder();
2179 // Pull the folder onto this screen
2180 openFolder(folderInfo);
2181 }
2182 }
2183 }
2184 }
2185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002187 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 * is animated relative to the specified View. If the View is null, no animation
2189 * is played.
2190 *
2191 * @param folderInfo The FolderInfo describing the folder to open.
2192 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002193 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 Folder openFolder;
2195
2196 if (folderInfo instanceof UserFolderInfo) {
2197 openFolder = UserFolder.fromXml(this);
2198 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002199 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 } else {
2201 return;
2202 }
2203
Joe Onorato00acb122009-08-04 16:04:30 -04002204 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 openFolder.setLauncher(this);
2206
2207 openFolder.bind(folderInfo);
2208 folderInfo.opened = true;
2209
Winson Chungaafa03c2010-06-11 17:34:16 -07002210 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 openFolder.onOpen();
2213 }
2214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002216 if (mState != State.WORKSPACE) {
2217 return false;
2218 }
2219
Romain Guy1fbc1c82009-11-09 20:43:08 -08002220 switch (v.getId()) {
2221 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002222 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002223 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2224 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002225 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002226 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002227 return true;
2228 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002229 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002230 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2231 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002232 showPreviews(v);
2233 }
2234 return true;
2235 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002236 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002237 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2238 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2239 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002240 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002241 return true;
2242 }
2243
Joe Onorato9c1289c2009-08-17 11:03:03 -04002244 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 return false;
2246 }
2247
2248 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002249 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 }
2251
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 resetAddInfo();
2253 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 return true;
2257 }
2258
Michael Jurka0280c3b2010-09-17 15:00:07 -07002259 final View itemUnderLongClick = longClickCellInfo.cell;
2260
Winson Chung304dcde2011-01-07 11:17:23 -08002261 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 if (itemUnderLongClick == null) {
2263 // User long pressed on empty space
2264 mWorkspace.setAllowLongPress(false);
2265 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2266 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002267 if (LauncherApplication.isScreenXLarge()) {
2268 addItems();
2269 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
2272 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002273 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002275 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2276 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 mAddIntersectCellX = longClickCellInfo.cellX;
2278 mAddIntersectCellY = longClickCellInfo.cellY;
2279 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 }
2281 }
2282 }
2283 return true;
2284 }
2285
Romain Guye6b8e2f2009-11-10 11:56:55 -08002286 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002287 private void dismissPreview(final View v) {
2288 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002289 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002290 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2291 public void onDismiss() {
2292 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2293 int count = group.getChildCount();
2294 for (int i = 0; i < count; i++) {
2295 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2296 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002297 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2298 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2299
2300 v.setTag(R.id.workspace, null);
2301 v.setTag(R.id.icon, null);
2302 window.setOnDismissListener(null);
2303 }
2304 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002305 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002306 }
2307 v.setTag(null);
2308 }
2309
Romain Guyf8e6a802009-12-07 17:48:02 -08002310 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002311 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002312 }
2313
Romain Guya6abce82009-11-10 02:54:41 -08002314 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002315 final Resources resources = getResources();
2316 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002317
Romain Guya6abce82009-11-10 02:54:41 -08002318 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002319
Romain Guy9d31e9f2009-11-11 18:54:28 -08002320 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002321
Romain Guyf8e6a802009-12-07 17:48:02 -08002322 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002323 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002324 int extraW = (int) ((r.left + r.right) * max);
2325 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002326
2327 int aW = cell.getWidth() - extraW;
2328 float w = aW / max;
2329
2330 int width = cell.getWidth();
2331 int height = cell.getHeight();
2332 int x = cell.getLeftPadding();
2333 int y = cell.getTopPadding();
2334 width -= (x + cell.getRightPadding());
2335 height -= (y + cell.getBottomPadding());
2336
2337 float scale = w / width;
2338
2339 int count = end - start;
2340
2341 final float sWidth = width * scale;
2342 float sHeight = height * scale;
2343
Romain Guye6b8e2f2009-11-10 11:56:55 -08002344 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002345
Romain Guye6b8e2f2009-11-10 11:56:55 -08002346 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2347 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002348
2349 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002350 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002351 cell = (CellLayout) workspace.getChildAt(i);
2352
Romain Guyf8e6a802009-12-07 17:48:02 -08002353 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002354 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002355
2356 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002357 c.scale(scale, scale);
2358 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002359 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002360
Romain Guy9d31e9f2009-11-11 18:54:28 -08002361 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002362 image.setImageBitmap(bitmap);
2363 image.setTag(i);
2364 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002365 image.setOnFocusChangeListener(handler);
2366 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002367 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002368
2369 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002370 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2371
Winson Chungaafa03c2010-06-11 17:34:16 -07002372 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002373 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002374
2375 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002376 p.setContentView(preview);
2377 p.setWidth((int) (sWidth * count + extraW));
2378 p.setHeight((int) (sHeight + extraH));
2379 p.setAnimationStyle(R.style.AnimationPreview);
2380 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002381 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002382 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002383 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002384
Romain Guye6b8e2f2009-11-10 11:56:55 -08002385 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2386 public void onDismiss() {
2387 dismissPreview(anchor);
2388 }
2389 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002390
2391 anchor.setTag(p);
2392 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002393 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002394 }
2395
Romain Guy9d31e9f2009-11-11 18:54:28 -08002396 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002397 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002398
Romain Guye6b8e2f2009-11-10 11:56:55 -08002399 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002400 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002401 }
2402
2403 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002404 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002405 v.post(this);
2406 }
2407
2408 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002409 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002410 }
2411
2412 public void onFocusChange(View v, boolean hasFocus) {
2413 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002414 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002415 }
Romain Guya6abce82009-11-10 02:54:41 -08002416 }
2417 }
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 Workspace getWorkspace() {
2420 return mWorkspace;
2421 }
2422
Michael Jurka3c38d562011-02-18 19:22:12 -08002423 TabHost getCustomizationDrawer() {
2424 return mHomeCustomizationDrawer;
2425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 @Override
2428 protected Dialog onCreateDialog(int id) {
2429 switch (id) {
2430 case DIALOG_CREATE_SHORTCUT:
2431 return new CreateShortcut().createDialog();
2432 case DIALOG_RENAME_FOLDER:
2433 return new RenameFolder().createDialog();
2434 }
2435
2436 return super.onCreateDialog(id);
2437 }
2438
2439 @Override
2440 protected void onPrepareDialog(int id, Dialog dialog) {
2441 switch (id) {
2442 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 break;
2444 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002445 if (mFolderInfo != null) {
2446 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2447 final CharSequence text = mFolderInfo.title;
2448 input.setText(text);
2449 input.setSelection(0, text.length());
2450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 break;
2452 }
2453 }
2454
2455 void showRenameDialog(FolderInfo info) {
2456 mFolderInfo = info;
2457 mWaitingForResult = true;
2458 showDialog(DIALOG_RENAME_FOLDER);
2459 }
2460
Michael Jurka0280c3b2010-09-17 15:00:07 -07002461 private void showAddDialog(int intersectX, int intersectY) {
2462 resetAddInfo();
2463 mAddIntersectCellX = intersectX;
2464 mAddIntersectCellY = intersectY;
2465 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 mWaitingForResult = true;
2467 showDialog(DIALOG_CREATE_SHORTCUT);
2468 }
2469
Joe Onoratodeb98af2010-02-19 14:59:39 -08002470 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002471 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002472 Bundle bundle = new Bundle();
2473
2474 ArrayList<String> shortcutNames = new ArrayList<String>();
2475 shortcutNames.add(getString(R.string.group_applications));
2476 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2477
2478 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2479 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2480 R.drawable.ic_launcher_application));
2481 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2482
2483 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2484 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002485 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002486 pickIntent.putExtras(bundle);
2487
Joe Onoratodeb98af2010-02-19 14:59:39 -08002488 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002489 }
2490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 private class RenameFolder {
2492 private EditText mInput;
2493
2494 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2496 mInput = (EditText) layout.findViewById(R.id.folder_name);
2497
2498 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2499 builder.setIcon(0);
2500 builder.setTitle(getString(R.string.rename_folder_title));
2501 builder.setCancelable(true);
2502 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2503 public void onCancel(DialogInterface dialog) {
2504 cleanup();
2505 }
2506 });
2507 builder.setNegativeButton(getString(R.string.cancel_action),
2508 new Dialog.OnClickListener() {
2509 public void onClick(DialogInterface dialog, int which) {
2510 cleanup();
2511 }
2512 }
2513 );
2514 builder.setPositiveButton(getString(R.string.rename_action),
2515 new Dialog.OnClickListener() {
2516 public void onClick(DialogInterface dialog, int which) {
2517 changeFolderName();
2518 }
2519 }
2520 );
2521 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002522
2523 final AlertDialog dialog = builder.create();
2524 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2525 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002526 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002527 mInput.requestFocus();
2528 InputMethodManager inputManager = (InputMethodManager)
2529 getSystemService(Context.INPUT_METHOD_SERVICE);
2530 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002531 }
2532 });
2533
2534 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 }
2536
2537 private void changeFolderName() {
2538 final String name = mInput.getText().toString();
2539 if (!TextUtils.isEmpty(name)) {
2540 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002541 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 mFolderInfo.title = name;
2543 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2544
Joe Onorato9c1289c2009-08-17 11:03:03 -04002545 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002546 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 } else {
2549 final FolderIcon folderIcon = (FolderIcon)
2550 mWorkspace.getViewForTag(mFolderInfo);
2551 if (folderIcon != null) {
2552 folderIcon.setText(name);
2553 getWorkspace().requestLayout();
2554 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002555 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002556 mWorkspaceLoading = true;
2557 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559 }
2560 }
2561 cleanup();
2562 }
2563
2564 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 dismissDialog(DIALOG_RENAME_FOLDER);
2566 mWaitingForResult = false;
2567 mFolderInfo = null;
2568 }
2569 }
2570
Daniel Sandler843e8602010-06-07 14:59:01 -04002571 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2572 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002573 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002574 }
2575
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002576 // AllAppsView.Watcher
2577 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002578 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2579 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002580 mWorkspace.setVisibility(View.GONE);
2581 }
2582 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583
Michael Jurkaafca9532011-02-16 14:50:35 -08002584 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002586 button.setClickable(true);
2587 }
2588
2589 private void hideToolbarButton(View button) {
2590 button.setAlpha(0.0f);
2591 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2592 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002593 }
2594
2595 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 button.setClickable(false);
2597 }
2598
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002600 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 *
2602 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2603 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002604 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002605 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002607 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 final boolean showing = show;
2609 final boolean hiding = !show;
2610
2611 final int duration = show ?
2612 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2613 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2614
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002615 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002616 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002617 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002618 anim.addUpdateListener(new AnimatorUpdateListener() {
2619 public void onAnimationUpdate(ValueAnimator animation) {
2620 view.setAlpha((Float) animation.getAnimatedValue());
2621 }
2622 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002623 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002624 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002625 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002626 if (showing) showAndEnableToolbarButton(view);
2627 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002629 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002630 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002631 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002632 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002633 });
2634 seq.play(anim);
2635 } else {
2636 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002637 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002638 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002639 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002640 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002641 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002643 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 }
2645
2646 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002647 * Show/hide the appropriate toolbar buttons for newState.
2648 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002649 *
2650 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002651 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2652 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002654 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 switch (newState) {
2656 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002657 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2658 mDeleteZone.setOverlappingViews(
2659 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002660 mDeleteZone.setDragAndDropEnabled(true);
2661 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 break;
2663 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002664 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002665 mDeleteZone.setDragAndDropEnabled(false);
2666 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002667 break;
2668 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002669 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002670 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 break;
2672 }
2673 }
2674
2675 /**
2676 * Helper method for the cameraZoomIn/cameraZoomOut animations
2677 * @param view The view being animated
2678 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2679 * @param scaleFactor The scale factor used for the zoom
2680 */
2681 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2682 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002683
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002684 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002685 // Set pivotY so that at the starting zoom factor, the view is partially
2686 // visible. Modifying initialHeightFactor changes how much of the view is
2687 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002688 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002689 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002690 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002691 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002692 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002693 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002694 }
2695 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002696
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002697 /**
2698 * Zoom the camera out from the workspace to reveal 'toView'.
2699 * Assumes that the view to show is anchored at either the very top or very bottom
2700 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002701 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002702 */
Winson Chung20f71112011-04-06 14:22:04 -07002703 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002704 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002706
2707 final int duration = toAllApps ?
2708 res.getInteger(R.integer.config_allAppsZoomInTime) :
2709 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002710 final int fadeDuration = toAllApps ?
2711 res.getInteger(R.integer.config_allAppsFadeInTime) :
2712 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002713
2714 final float scale = toAllApps ?
2715 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2716 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2717
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002718 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002719
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002720 setPivotsForZoom(toView, toState, scale);
2721
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002722 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002723 if (!springLoaded) {
2724 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2725
2726 // Everytime we launch into AllApps, we reset the successful drop flag which
2727 // controls when it should hide/show the mini workspaces
2728 mAllAppsPagedView.resetSuccessfulDropFlag();
2729 } else {
2730 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2731 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002732 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002733 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002734 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002735
2736 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002737 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002738 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002739 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2740 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002741 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002742 toView.setFastScaleX(a * scale + b * 1f);
2743 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002744 }
2745 });
Adam Cohen61033d32010-11-15 18:29:44 -08002746
Adam Cohenf16e5712011-01-13 13:31:45 -08002747 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002748 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002749 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002750 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002751 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2752 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002753 // don't need to invalidate because we do so above
2754 toView.setFastAlpha(a * 0f + b * 1f);
2755 }
2756 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002757 alphaAnim.start();
2758 }
2759
Michael Jurkaabded662011-03-04 12:06:57 -08002760 if (toView instanceof LauncherTransitionable) {
2761 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002762 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002763 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002764 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002765 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002766 // Prepare the position
2767 toView.setTranslationX(0.0f);
2768 toView.setTranslationY(0.0f);
2769 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002770 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002771 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002772 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002773 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002774 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002775 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002776 // If we don't set the final scale values here, if this animation is cancelled
2777 // it will have the wrong scale value and subsequent cameraPan animations will
2778 // not fix that
2779 toView.setScaleX(1.0f);
2780 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002781 if (toView instanceof LauncherTransitionable) {
2782 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002783 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002784 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002785 });
2786
Chet Haaseb1254a62010-09-07 13:35:00 -07002787 AnimatorSet toolbarHideAnim = new AnimatorSet();
2788 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002789 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2790
2791 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002792 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002793
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002794 if (mStateAnimation != null) mStateAnimation.cancel();
2795 mStateAnimation = new AnimatorSet();
2796 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2797 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002798
2799 // Show the new toolbar buttons just as the main animation is ending
2800 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002801 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2802 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 } else {
2804 toView.setTranslationX(0.0f);
2805 toView.setTranslationY(0.0f);
2806 toView.setScaleX(1.0f);
2807 toView.setScaleY(1.0f);
2808 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002809 if (toView instanceof LauncherTransitionable) {
2810 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2811 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2812 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002813 hideAndShowToolbarButtons(toState, null, null);
2814 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002815 }
2816
2817 /**
2818 * Zoom the camera back into the workspace, hiding 'fromView'.
2819 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002820 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2821 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002822 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002823 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002824 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002825 final boolean fromAllApps = (fromState == State.ALL_APPS);
2826
2827 int duration = fromAllApps ?
2828 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2829 res.getInteger(R.integer.config_customizeZoomOutTime);
2830
Michael Jurka742574b2011-02-02 23:51:01 -08002831 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002832 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2833 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2834
2835 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002836
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002837 mCustomizePagedView.endChoiceMode();
2838 mAllAppsPagedView.endChoiceMode();
2839
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002840 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002841
Michael Jurkad3ef3062010-11-23 16:23:58 -08002842 if (!springLoaded) {
2843 mWorkspace.unshrink(animated);
2844 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002846 if (mStateAnimation != null) mStateAnimation.cancel();
2847 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002848
Michael Jurka742574b2011-02-02 23:51:01 -08002849 final float oldScaleX = fromView.getScaleX();
2850 final float oldScaleY = fromView.getScaleY();
2851
2852 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2853 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002854 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2855 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002856 ((View)fromView.getParent()).fastInvalidate();
2857 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2858 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2859 }
2860 });
Michael Jurkaabded662011-03-04 12:06:57 -08002861 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002862 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002863 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002864 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2865 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002866 // don't need to invalidate because we do so above
2867 fromView.setFastAlpha(a * 1f + b * 0f);
2868 }
2869 });
Michael Jurkaabded662011-03-04 12:06:57 -08002870 if (fromView instanceof LauncherTransitionable) {
2871 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002872 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002873 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002874 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002875 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002876 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002877 if (fromView instanceof LauncherTransitionable) {
2878 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002879 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002880 }
2881 });
2882
Chet Haaseb1254a62010-09-07 13:35:00 -07002883 AnimatorSet toolbarHideAnim = new AnimatorSet();
2884 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002885 if (!springLoaded) {
2886 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2887 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002888
Adam Cohen61033d32010-11-15 18:29:44 -08002889 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890
2891 // Show the new toolbar buttons at the very end of the whole animation
2892 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2893 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002894 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2895 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896 } else {
2897 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002898 if (fromView instanceof LauncherTransitionable) {
2899 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2900 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2901 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002902 if (!springLoaded) {
2903 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2904 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002905 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002906 }
2907
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002908 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002909 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002910 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002911 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002912
Michael Jurka79212d82010-07-30 16:36:20 -07002913 if (LauncherApplication.isScreenXLarge()) {
Winson Chung20f71112011-04-06 14:22:04 -07002914 cameraZoomOut(State.ALL_APPS, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002915 } else {
2916 mAllAppsGrid.zoom(1.0f, animated);
2917 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002918
Romain Guyc16fea72010-03-12 17:17:56 -08002919 ((View) mAllAppsGrid).setFocusable(true);
2920 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002921
Joe Onorato7c312c12009-08-13 21:36:53 -07002922 // TODO: fade these two too
2923 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002924
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002925 // Change the state *after* we've called all the transition code
2926 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002927
Winson Chung5fb63472011-02-02 17:03:37 -08002928 // Pause the auto-advance of widgets until we are out of AllApps
2929 mUserPresent = false;
2930 updateRunning();
2931
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002932 // send an accessibility event to announce the context change
2933 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2934 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935
2936 void showWorkspace(boolean animated) {
2937 showWorkspace(animated, null);
2938 }
2939
2940 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002941 if (layout != null) {
2942 // always animated, but that's ok since we never specify a layout and
2943 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002944 mWorkspace.unshrink(layout);
2945 } else {
2946 mWorkspace.unshrink(animated);
2947 }
2948 if (mState == State.ALL_APPS) {
2949 closeAllApps(animated);
2950 } else if (mState == State.CUSTOMIZE) {
2951 hideCustomizationDrawer(animated);
2952 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002953
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002954 // Change the state *after* we've called all the transition code
2955 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002956
Winson Chung5fb63472011-02-02 17:03:37 -08002957 // Resume the auto-advance of widgets
2958 mUserPresent = true;
2959 updateRunning();
2960
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002961 // send an accessibility event to announce the context change
2962 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002963 }
2964
Michael Jurkad3ef3062010-11-23 16:23:58 -08002965 void enterSpringLoadedDragMode(CellLayout layout) {
2966 mWorkspace.enterSpringLoadedDragMode(layout);
2967 if (mState == State.ALL_APPS) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002968 mState = State.ALL_APPS_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002969 cameraZoomIn(State.ALL_APPS, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002970 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002971 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002972 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002973 }/* else {
2974 // we're already in spring loaded mode; don't do anything
2975 }*/
2976 }
2977
2978 void exitSpringLoadedDragMode() {
2979 if (mState == State.ALL_APPS_SPRING_LOADED) {
2980 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07002981 cameraZoomOut(State.ALL_APPS, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002982 mState = State.ALL_APPS;
2983 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2984 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002985 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002986 mState = State.CUSTOMIZE;
2987 }/* else {
2988 // we're not in spring loaded mode; don't do anything
2989 }*/
2990 }
2991
Joe Onoratob2061212009-11-24 16:13:54 -05002992 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002993 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002994 * - Home from workspace
2995 * - from center screen
2996 * - from other screens
2997 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002998 * - from center screen
2999 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003000 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003001 * - from center screen
3002 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003003 * - Launch app from workspace and quit
3004 * - with back
3005 * - with home
3006 * - Launch app from all apps and quit
3007 * - with back
3008 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003009 * - Go to a screen that's not the default, then all
3010 * apps, and launch and app, and go back
3011 * - with back
3012 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003013 * - On workspace, long press power and go back
3014 * - with back
3015 * - with home
3016 * - On all apps, long press power and go back
3017 * - with back
3018 * - with home
3019 * - On workspace, power off
3020 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003021 * - Launch an app and turn off the screen while in that app
3022 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003023 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003024 * - From all apps
3025 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003026 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3027 * - From all apps
3028 * - From the center workspace
3029 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003030 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003031 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003032 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003033 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003034 if (LauncherApplication.isScreenXLarge()) {
Winson Chung20f71112011-04-06 14:22:04 -07003035 cameraZoomIn(State.ALL_APPS, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003036 } else {
3037 mAllAppsGrid.zoom(0.0f, animated);
3038 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003039 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003040 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003041 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003042 }
3043
Joe Onorato7c312c12009-08-13 21:36:53 -07003044 void lockAllApps() {
3045 // TODO
3046 }
3047
3048 void unlockAllApps() {
3049 // TODO
3050 }
3051
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003052 // Show the customization drawer (only exists in x-large configuration)
3053 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003054 if (mState != State.WORKSPACE) {
3055 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003056 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003057
Winson Chung20f71112011-04-06 14:22:04 -07003058 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003059
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003060 // Change the state *after* we've called all the transition code
3061 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003062
3063 // Pause the auto-advance of widgets until we are out of Customization drawer
3064 mUserPresent = false;
3065 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003066 }
3067
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003068 // Hide the customization drawer (only exists in x-large configuration)
3069 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003070 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003071 cameraZoomIn(State.CUSTOMIZE, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003072 }
3073 }
3074
Patrick Dubroy5f445422011-02-18 14:35:21 -08003075 /**
3076 * Add an item from all apps or customize onto the given workspace screen.
3077 * If layout is null, add to the current screen.
3078 */
3079 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003080 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3081 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003082 } else {
3083 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003084 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003085 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003086
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003087 void onWorkspaceClick(CellLayout layout) {
3088 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003089 }
3090
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003091 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003092 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003093 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003094 // Look for the toolbar icon specified in the activity meta-data
3095 Bundle metaData = packageManager.getActivityInfo(
3096 activityName, PackageManager.GET_META_DATA).metaData;
3097 if (metaData != null) {
3098 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3099 if (iconResId != 0) {
3100 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003101 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003102 }
3103 }
3104 } catch (NameNotFoundException e) {
3105 // Do nothing
3106 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003107 return null;
3108 }
3109
3110 // if successful in getting icon, return it; otherwise, set button to use default drawable
3111 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3112 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3113 TextView button = (TextView) findViewById(buttonId);
3114 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3115
3116 // If we were unable to find the icon via the meta-data, use a generic one
3117 if (toolbarIcon == null) {
3118 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3119 return null;
3120 } else {
3121 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3122 return toolbarIcon.getConstantState();
3123 }
3124 }
3125
3126 // if successful in getting icon, return it; otherwise, set button to use default drawable
3127 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3128 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3129 ImageView button = (ImageView) findViewById(buttonId);
3130 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3131
Michael Jurka0423dcf2010-10-05 14:56:18 -07003132 // If we were unable to find the icon via the meta-data, use a generic one
3133 if (toolbarIcon == null) {
3134 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003135 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003136 } else {
3137 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003138 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003139 }
3140 }
3141
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003142 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3143 TextView button = (TextView) findViewById(buttonId);
3144 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3145 }
3146
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003147 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003148 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003149 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003150 }
3151
Michael Jurka0423dcf2010-10-05 14:56:18 -07003152 private void updateGlobalSearchIcon() {
3153 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003154 final View searchButton = findViewById(R.id.search_button);
3155 final View searchDivider = findViewById(R.id.search_divider);
3156
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 final SearchManager searchManager =
3158 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3159 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003160 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003161 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003162 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003163 searchButton.setVisibility(View.VISIBLE);
3164 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003165 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003166 searchButton.setVisibility(View.GONE);
3167 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003168 }
3169 }
3170 }
3171
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003172 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003173 updateButtonWithDrawable(R.id.search_button, d);
3174 }
3175
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003176 private void updateVoiceSearchIcon() {
3177 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003178 final View searchDivider = findViewById(R.id.search_divider);
3179 final View voiceButton = findViewById(R.id.voice_button);
3180
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003181 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3182 ComponentName activityName = intent.resolveActivity(getPackageManager());
3183 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003184 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003185 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003186 searchDivider.setVisibility(View.VISIBLE);
3187 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003188 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003189 searchDivider.setVisibility(View.GONE);
3190 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003191 }
3192 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003193 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003194
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003195 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003196 updateButtonWithDrawable(R.id.voice_button, d);
3197 }
3198
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003199 /**
3200 * Sets the app market icon (shown when all apps is visible on x-large screens)
3201 */
3202 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003203 if (LauncherApplication.isScreenXLarge()) {
3204 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3205 // Find the app market activity by resolving an intent.
3206 // (If multiple app markets are installed, it will return the ResolverActivity.)
3207 ComponentName activityName = intent.resolveActivity(getPackageManager());
3208 if (activityName != null) {
3209 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003210 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003211 R.id.market_button, activityName, R.drawable.app_market_generic);
3212 }
3213 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003214 }
3215
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003216 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003217 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003218 }
3219
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003220 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 * Displays the shortcut creation dialog and launches, if necessary, the
3222 * appropriate activity.
3223 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003224 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003225 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3226 DialogInterface.OnShowListener {
3227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3234 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003235 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003237 builder.setInverseBackgroundForced(true);
3238
3239 AlertDialog dialog = builder.create();
3240 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003241 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003242 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 return dialog;
3245 }
3246
3247 public void onCancel(DialogInterface dialog) {
3248 mWaitingForResult = false;
3249 cleanup();
3250 }
3251
Romain Guycbb89e42009-06-08 15:52:54 -07003252 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003253 }
3254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003256 try {
3257 dismissDialog(DIALOG_CREATE_SHORTCUT);
3258 } catch (Exception e) {
3259 // An exception is thrown if the dialog is not visible, which is fine
3260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 }
3262
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003263 /**
3264 * Handle the action clicked in the "Add to home" dialog.
3265 */
3266 public void onClick(DialogInterface dialog, int which) {
3267 Resources res = getResources();
3268 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003269
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003270 switch (which) {
3271 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003272 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003273 break;
3274 }
Romain Guycbb89e42009-06-08 15:52:54 -07003275
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003276 case AddAdapter.ITEM_APPWIDGET: {
3277 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003278
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003279 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3280 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003281 // start the pick activity
3282 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3283 break;
3284 }
Romain Guycbb89e42009-06-08 15:52:54 -07003285
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003286 case AddAdapter.ITEM_LIVE_FOLDER: {
3287 // Insert extra item to handle inserting folder
3288 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 ArrayList<String> shortcutNames = new ArrayList<String>();
3291 shortcutNames.add(res.getString(R.string.group_folder));
3292 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003293
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003294 ArrayList<ShortcutIconResource> shortcutIcons =
3295 new ArrayList<ShortcutIconResource>();
3296 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3297 R.drawable.ic_launcher_folder));
3298 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3299
3300 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3301 pickIntent.putExtra(Intent.EXTRA_INTENT,
3302 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3303 pickIntent.putExtra(Intent.EXTRA_TITLE,
3304 getText(R.string.title_select_live_folder));
3305 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003306
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003307 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3308 break;
3309 }
3310
3311 case AddAdapter.ITEM_WALLPAPER: {
3312 startWallpaper();
3313 break;
3314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003315 }
3316 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003317
3318 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003319 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003321 }
3322
3323 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003324 * Receives notifications when applications are added/removed.
3325 */
3326 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3327 @Override
3328 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003329 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003330 String reason = intent.getStringExtra("reason");
3331 if (!"homekey".equals(reason)) {
3332 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003333 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003334 animate = false;
3335 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003336 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003337 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003338 }
3339 }
3340
3341 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003342 * Receives notifications whenever the appwidgets are reset.
3343 */
3344 private class AppWidgetResetObserver extends ContentObserver {
3345 public AppWidgetResetObserver() {
3346 super(new Handler());
3347 }
3348
3349 @Override
3350 public void onChange(boolean selfChange) {
3351 onAppWidgetReset();
3352 }
3353 }
3354
3355 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003356 * If the activity is currently paused, signal that we need to re-run the loader
3357 * in onResume.
3358 *
3359 * This needs to be called from incoming places where resources might have been loaded
3360 * while we are paused. That is becaues the Configuration might be wrong
3361 * when we're not running, and if it comes back to what it was when we
3362 * were paused, we are not restarted.
3363 *
3364 * Implementation of the method from LauncherModel.Callbacks.
3365 *
3366 * @return true if we are currently paused. The caller might be able to
3367 * skip some work in that case since we will come back again.
3368 */
3369 public boolean setLoadOnResume() {
3370 if (mPaused) {
3371 Log.i(TAG, "setLoadOnResume");
3372 mOnResumeNeedsLoad = true;
3373 return true;
3374 } else {
3375 return false;
3376 }
3377 }
3378
3379 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003381 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003383 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003384 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003385 } else {
3386 return SCREEN_COUNT / 2;
3387 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003389
Winson Chunga12a2502010-12-20 14:41:35 -08003390 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003391 mAllAppsPagedView = view;
3392 }
3393
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 /**
3395 * Refreshes the shortcuts shown on the workspace.
3396 *
3397 * Implementation of the method from LauncherModel.Callbacks.
3398 */
3399 public void startBinding() {
3400 final Workspace workspace = mWorkspace;
3401 int count = workspace.getChildCount();
3402 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003403 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003404 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3405 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003407
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 if (DEBUG_USER_INTERFACE) {
3409 android.widget.Button finishButton = new android.widget.Button(this);
3410 finishButton.setText("Finish");
3411 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3412
3413 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3414 public void onClick(View v) {
3415 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003416 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003418 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003419
3420 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3421 // mDesktopItems -> AppWidgetInfo -> hostView).
3422 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003423 }
3424
3425 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 * Bind the items start-end from the list.
3427 *
3428 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003429 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3431
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003432 setLoadOnResume();
3433
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434 final Workspace workspace = mWorkspace;
3435
3436 for (int i=start; i<end; i++) {
3437 final ItemInfo item = shortcuts.get(i);
3438 mDesktopItems.add(item);
3439 switch (item.itemType) {
3440 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3441 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003442 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3444 false);
3445 break;
3446 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3447 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003448 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003449 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003450 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3451 false);
3452 break;
3453 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3454 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3455 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003456 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003457 (LiveFolderInfo) item);
3458 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3459 false);
3460 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003462 }
3463
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003465 }
3466
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 /**
3468 * Implementation of the method from LauncherModel.Callbacks.
3469 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003470 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003471 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003472 sFolders.clear();
3473 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475
3476 /**
3477 * Add the views for a widget to the workspace.
3478 *
3479 * Implementation of the method from LauncherModel.Callbacks.
3480 */
3481 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003482 setLoadOnResume();
3483
Daniel Sandler843e8602010-06-07 14:59:01 -04003484 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3485 if (DEBUG_WIDGETS) {
3486 Log.d(TAG, "bindAppWidget: " + item);
3487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 final Workspace workspace = mWorkspace;
3489
3490 final int appWidgetId = item.appWidgetId;
3491 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003492 if (DEBUG_WIDGETS) {
3493 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3494 }
3495
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3497
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3499 item.hostView.setTag(item);
3500
3501 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3502 item.cellY, item.spanX, item.spanY, false);
3503
Adam Cohended9f8d2010-11-03 13:25:16 -07003504 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3505
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 workspace.requestLayout();
3507
3508 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003509
3510 if (DEBUG_WIDGETS) {
3511 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3512 + (SystemClock.uptimeMillis()-start) + "ms");
3513 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 }
3515
3516 /**
3517 * Callback saying that there aren't any more items to bind.
3518 *
3519 * Implementation of the method from LauncherModel.Callbacks.
3520 */
3521 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003522 setLoadOnResume();
3523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 if (mSavedState != null) {
3525 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003526 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
3528
3529 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3530 if (userFolders != null) {
3531 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003532 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003533 if (info != null) {
3534 openFolder(info);
3535 }
3536 }
3537 final Folder openFolder = mWorkspace.getOpenFolder();
3538 if (openFolder != null) {
3539 openFolder.requestFocus();
3540 }
3541 }
3542
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 mSavedState = null;
3544 }
3545
3546 if (mSavedInstanceState != null) {
3547 super.onRestoreInstanceState(mSavedInstanceState);
3548 mSavedInstanceState = null;
3549 }
3550
Winson Chung32bb5e52011-01-31 14:51:56 -08003551 // Workaround a bug that occurs when rotating the device while the customization mode is
3552 // open, we trigger a new layout on all the CellLayout children.
3553 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3554 final int childCount = mWorkspace.getChildCount();
3555 for (int i = 0; i < childCount; ++i) {
3556 mWorkspace.getChildAt(i).requestLayout();
3557 }
3558 }
3559
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003561
3562 // If we received the result of any pending adds while the loader was running (e.g. the
3563 // widget configuration forced an orientation change), process them now.
3564 for (int i = 0; i < sPendingAddList.size(); i++) {
3565 completeAdd(sPendingAddList.get(i));
3566 }
3567 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 }
3569
3570 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003571 * Updates the icons on the launcher that are affected by changes to the package list
3572 * on the device.
3573 */
3574 private void updateIconsAffectedByPackageManagerChanges() {
3575 updateAppMarketIcon();
3576 updateGlobalSearchIcon();
3577 updateVoiceSearchIcon();
3578 }
3579
3580 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 * Add the icons for all apps.
3582 *
3583 * Implementation of the method from LauncherModel.Callbacks.
3584 */
3585 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003586 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003587 if (mCustomizePagedView != null) {
3588 mCustomizePagedView.setApps(apps);
3589 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003590 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 }
3592
3593 /**
3594 * A package was installed.
3595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003598 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003599 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003601 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003602 if (mCustomizePagedView != null) {
3603 mCustomizePagedView.addApps(apps);
3604 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003605 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
3607
3608 /**
3609 * A package was updated.
3610 *
3611 * Implementation of the method from LauncherModel.Callbacks.
3612 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003613 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003614 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003616 if (mWorkspace != null) {
3617 mWorkspace.updateShortcuts(apps);
3618 }
3619 if (mAllAppsGrid != null) {
3620 mAllAppsGrid.updateApps(apps);
3621 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003622 if (mCustomizePagedView != null) {
3623 mCustomizePagedView.updateApps(apps);
3624 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003625 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 }
3627
3628 /**
3629 * A package was uninstalled.
3630 *
3631 * Implementation of the method from LauncherModel.Callbacks.
3632 */
Joe Onorato36115782010-06-17 13:28:48 -04003633 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003635 if (permanent) {
3636 mWorkspace.removeItems(apps);
3637 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003638 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003639 if (mCustomizePagedView != null) {
3640 mCustomizePagedView.removeApps(apps);
3641 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003642 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003643 }
Joe Onoratobe386092009-11-17 17:32:16 -08003644
3645 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003646 * A number of packages were updated.
3647 */
3648 public void bindPackagesUpdated() {
3649 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003650 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003651 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003652 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003653 }
3654
Winson Chung400438b2011-01-16 17:53:48 -08003655 private int mapConfigurationOriActivityInfoOri(int configOri) {
3656 final Display d = getWindowManager().getDefaultDisplay();
3657 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3658 switch (d.getRotation()) {
3659 case Surface.ROTATION_0:
3660 case Surface.ROTATION_180:
3661 // We are currently in the same basic orientation as the natural orientation
3662 naturalOri = configOri;
3663 break;
3664 case Surface.ROTATION_90:
3665 case Surface.ROTATION_270:
3666 // We are currently in the other basic orientation to the natural orientation
3667 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3668 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3669 break;
3670 }
3671
3672 int[] oriMap = {
3673 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3674 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3675 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3676 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3677 };
3678 // Since the map starts at portrait, we need to offset if this device's natural orientation
3679 // is landscape.
3680 int indexOffset = 0;
3681 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3682 indexOffset = 1;
3683 }
3684 return oriMap[(d.getRotation() + indexOffset) % 4];
3685 }
3686 public void lockScreenOrientation() {
3687 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3688 .getConfiguration().orientation));
3689 }
3690 public void unlockScreenOrientation() {
3691 mHandler.postDelayed(new Runnable() {
3692 public void run() {
3693 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3694 }
3695 }, mRestoreScreenOrientationDelay);
3696 }
3697
Winson Chung80baf5a2010-08-09 16:03:15 -07003698 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003699 * Prints out out state for debugging.
3700 */
3701 public void dumpState() {
3702 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003703 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003704 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3705 Log.d(TAG, "mRestoring=" + mRestoring);
3706 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3707 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3708 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003709 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003710 mModel.dumpState();
3711 mAllAppsGrid.dumpState();
3712 Log.d(TAG, "END launcher2 dump state");
3713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714}
Michael Jurka2763be32011-02-24 11:19:57 -08003715
Michael Jurkaabded662011-03-04 12:06:57 -08003716interface LauncherTransitionable {
3717 void onLauncherTransitionStart(Animator animation);
3718 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003719}