blob: 60cab8ecb8b367de164a859d1f53dd10e8196fe1 [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
134 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
138 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Joe Onorato7c312c12009-08-13 21:36:53 -0700147 static final int DIALOG_CREATE_SHORTCUT = 1;
148 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: long
157 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
168
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700169 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
170
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700171 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800172 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800173 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700174 private State mState = State.WORKSPACE;
175 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800180 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800182 private final BroadcastReceiver mCloseSystemDialogsReceiver
183 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800184 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private LayoutInflater mInflater;
187
Joe Onorato00acb122009-08-04 16:04:30 -0400188 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700190
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private AppWidgetManager mAppWidgetManager;
192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 private int mAddScreen = -1;
195 private int mAddIntersectCellX = -1;
196 private int mAddIntersectCellY = -1;
197 private int[] mAddDropPosition;
198 private int[] mTmpAddItemCellCoordinates = new int[2];
199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private FolderInfo mFolderInfo;
201
Joe Onorato7c312c12009-08-13 21:36:53 -0700202 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700204 private AllAppsView mAllAppsGrid;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800205 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700206 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700207
Winson Chung97d85d22011-04-13 11:27:36 -0700208 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mAllAppsButton;
210 private View mDivider;
211 private View mConfigureButton;
212
Winson Chunga12a2502010-12-20 14:41:35 -0800213 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700214 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800241 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Winson Chung400438b2011-01-16 17:53:48 -0800262 // Determines how long to wait after a rotation before restoring the screen orientation to
263 // match the sensor state.
264 private final int mRestoreScreenOrientationDelay = 500;
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800267 private static Drawable.ConstantState sGlobalSearchIcon;
268 private static Drawable.ConstantState sVoiceSearchIcon;
269 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Michael Jurkaddd62e92011-02-16 17:49:14 -0800271 private BubbleTextView mWaitingForResume;
272
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800273 private static ArrayList<PendingAddArguments> sPendingAddList
274 = new ArrayList<PendingAddArguments>();
275
276 private static class PendingAddArguments {
277 int requestCode;
278 Intent intent;
279 int screen;
280 int cellX;
281 int cellY;
282 }
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 @Override
285 protected void onCreate(Bundle savedInstanceState) {
286 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700287
Winson Chungaafa03c2010-06-11 17:34:16 -0700288 if (LauncherApplication.isInPlaceRotationEnabled()) {
289 // hide the status bar (temporary until we get the status bar design figured out)
290 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
291 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
292 }
293
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 LauncherApplication app = ((LauncherApplication)getApplication());
295 mModel = app.setLauncher(this);
296 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700299
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700300 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
302 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200305 android.os.Debug.startMethodTracing(
306 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 }
308
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400309 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800312 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700313 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700314 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800315 mCustomizePagedView = (CustomizePagedView) findViewById(
316 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 setupViews();
319
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800320 registerContentObservers();
321
Joe Onorato7c312c12009-08-13 21:36:53 -0700322 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700323
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 mSavedState = savedInstanceState;
325 restoreState(mSavedState);
326
Winson Chunga12a2502010-12-20 14:41:35 -0800327 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800328 if (mCustomizePagedView != null) {
329 mCustomizePagedView.update();
330 }
Winson Chunga12a2502010-12-20 14:41:35 -0800331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 if (PROFILE_STARTUP) {
333 android.os.Debug.stopMethodTracing();
334 }
335
336 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400337 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
339
340 // For handling default keys
341 mDefaultKeySsb = new SpannableStringBuilder();
342 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800343
344 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
345 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800346
347 // If we have a saved version of these external icons, we load them up immediately
348 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800349 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
350 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800351 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800352 if (sGlobalSearchIcon != null) {
353 updateGlobalSearchIcon(sGlobalSearchIcon);
354 }
355 if (sVoiceSearchIcon != null) {
356 updateVoiceSearchIcon(sVoiceSearchIcon);
357 }
358 if (sAppMarketIcon != null) {
359 updateAppMarketIcon(sAppMarketIcon);
360 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 }
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800364 @Override
365 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
366 super.dispatchPopulateAccessibilityEvent(event);
367
368 // we want to take over text population so it is context dependent
369 event.getText().clear();
370 if (mState == State.ALL_APPS) {
371 event.getText().add(getString(R.string.all_apps_button_label));
372 } else if (mState == State.WORKSPACE) {
373 event.getText().add(getString(R.string.all_apps_home_button_label));
374 }
375
376 return true;
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 if (sLocaleConfiguration == null) {
381 new AsyncTask<Void, Void, LocaleConfiguration>() {
382 @Override
383 protected LocaleConfiguration doInBackground(Void... unused) {
384 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
385 readConfiguration(Launcher.this, localeConfiguration);
386 return localeConfiguration;
387 }
388
389 @Override
390 protected void onPostExecute(LocaleConfiguration result) {
391 sLocaleConfiguration = result;
392 checkForLocaleChange(); // recursive, but now with a locale configuration
393 }
394 }.execute();
395 return;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final Configuration configuration = getResources().getConfiguration();
399
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 final String locale = configuration.locale.toString();
402
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final int mcc = configuration.mcc;
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final int mnc = configuration.mnc;
408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 sLocaleConfiguration.locale = locale;
413 sLocaleConfiguration.mcc = mcc;
414 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700415
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400417 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418
419 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
420 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700421 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 public void run() {
423 writeConfiguration(Launcher.this, localeConfiguration);
424 }
425 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700426 }
427 }
428
429 private static class LocaleConfiguration {
430 public String locale;
431 public int mcc = -1;
432 public int mnc = -1;
433 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700434
Romain Guy98d01652009-06-30 16:21:04 -0700435 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
436 DataInputStream in = null;
437 try {
438 in = new DataInputStream(context.openFileInput(PREFERENCES));
439 configuration.locale = in.readUTF();
440 configuration.mcc = in.readInt();
441 configuration.mnc = in.readInt();
442 } catch (FileNotFoundException e) {
443 // Ignore
444 } catch (IOException e) {
445 // Ignore
446 } finally {
447 if (in != null) {
448 try {
449 in.close();
450 } catch (IOException e) {
451 // Ignore
452 }
453 }
454 }
455 }
456
457 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
458 DataOutputStream out = null;
459 try {
460 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
461 out.writeUTF(configuration.locale);
462 out.writeInt(configuration.mcc);
463 out.writeInt(configuration.mnc);
464 out.flush();
465 } catch (FileNotFoundException e) {
466 // Ignore
467 } catch (IOException e) {
468 //noinspection ResultOfMethodCallIgnored
469 context.getFileStreamPath(PREFERENCES).delete();
470 } finally {
471 if (out != null) {
472 try {
473 out.close();
474 } catch (IOException e) {
475 // Ignore
476 }
477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479 }
480
481 static int getScreen() {
482 synchronized (sLock) {
483 return sScreen;
484 }
485 }
486
487 static void setScreen(int screen) {
488 synchronized (sLock) {
489 sScreen = screen;
490 }
491 }
492
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400493 // Note: This doesn't do all the client-id magic that BrowserProvider does
494 // in Browser. (http://b/2425179)
495 private Uri getDefaultBrowserUri() {
496 String url = getString(R.string.default_browser_url);
497 if (url.indexOf("{CID}") != -1) {
498 url = url.replace("{CID}", "android-google");
499 }
500 return Uri.parse(url);
501 }
502
503 // Load the Intent templates from arrays.xml to populate the hotseats. For
504 // each Intent, if it resolves to a single app, use that as the launch
505 // intent & use that app's label as the contentDescription. Otherwise,
506 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400507 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 if (mHotseatConfig == null) {
509 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
510 if (mHotseatConfig.length > 0) {
511 mHotseats = new Intent[mHotseatConfig.length];
512 mHotseatLabels = new CharSequence[mHotseatConfig.length];
513 mHotseatIcons = new Drawable[mHotseatConfig.length];
514 } else {
515 mHotseats = null;
516 mHotseatIcons = null;
517 mHotseatLabels = null;
518 }
519
520 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
521 for (int i=0; i<mHotseatConfig.length; i++) {
522 // load icon for this slot; currently unrelated to the actual activity
523 try {
524 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
525 } catch (ArrayIndexOutOfBoundsException ex) {
526 Log.w(TAG, "Missing hotseat_icons array item #" + i);
527 mHotseatIcons[i] = null;
528 }
529 }
530 hotseatIconDrawables.recycle();
531 }
532
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400533 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400534 for (int i=0; i<mHotseatConfig.length; i++) {
535 Intent intent = null;
536 if (mHotseatConfig[i].equals("*BROWSER*")) {
537 // magic value meaning "launch user's default web browser"
538 // replace it with a generic web request so we can see if there is indeed a default
539 String defaultUri = getString(R.string.default_browser_url);
540 intent = new Intent(
541 Intent.ACTION_VIEW,
542 ((defaultUri != null)
543 ? Uri.parse(defaultUri)
544 : getDefaultBrowserUri())
545 ).addCategory(Intent.CATEGORY_BROWSABLE);
546 // note: if the user launches this without a default set, she
547 // will always be taken to the default URL above; this is
548 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700549 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400550 // URL is unavoidably sent to the chosen app.
551 } else {
552 try {
553 intent = Intent.parseUri(mHotseatConfig[i], 0);
554 } catch (java.net.URISyntaxException ex) {
555 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
556 // bogus; leave intent=null
557 }
558 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 if (intent == null) {
561 mHotseats[i] = null;
562 mHotseatLabels[i] = getText(R.string.activity_not_found);
563 continue;
564 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400565
566 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700567 Log.d(TAG, "loadHotseats: hotseat " + i
568 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400569 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400570 + "]");
571 }
572
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
574 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 Log.d(TAG, "Best match for intent: " + bestMatch);
577 Log.d(TAG, "All matches: ");
578 for (ResolveInfo ri : allMatches) {
579 Log.d(TAG, " --> " + ri);
580 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 // did this resolve to a single app, or the resolver?
583 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 // can't find any activity to handle this. let's leave the
585 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 // to launch.
587 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400588
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 // set accessibility text to "Not installed"
590 mHotseatLabels[i] = getText(R.string.activity_not_found);
591 } else {
592 boolean found = false;
593 for (ResolveInfo ri : allMatches) {
594 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
595 && bestMatch.activityInfo.applicationInfo.packageName
596 .equals(ri.activityInfo.applicationInfo.packageName)) {
597 found = true;
598 break;
599 }
600 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700601
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 if (!found) {
603 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
604 // the bestMatch is probably the ResolveActivity, meaning the
605 // user has not yet selected a default
606 // so: we'll keep the original intent for now
607 mHotseats[i] = intent;
608
609 // set the accessibility text to "Select shortcut"
610 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
611 } else {
612 // we have an app!
613 // now reconstruct the intent to launch it through the front
614 // door
615 ComponentName com = new ComponentName(
616 bestMatch.activityInfo.applicationInfo.packageName,
617 bestMatch.activityInfo.name);
618 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
619
620 // load the app label for accessibility
621 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
622 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400623 }
624
625 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 Log.d(TAG, "loadHotseats: hotseat " + i
627 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 + ((mHotseats[i] == null)
629 ? "null"
630 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631 + "] label=[" + mHotseatLabels[i]
632 + "]"
633 );
634 }
635 }
636 }
637
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 private void completeAdd(PendingAddArguments args) {
639 switch (args.requestCode) {
640 case REQUEST_PICK_APPLICATION:
641 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
642 break;
643 case REQUEST_PICK_SHORTCUT:
644 processShortcut(args.intent);
645 break;
646 case REQUEST_CREATE_SHORTCUT:
647 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
648 break;
649 case REQUEST_PICK_LIVE_FOLDER:
650 addLiveFolder(args.intent);
651 break;
652 case REQUEST_CREATE_LIVE_FOLDER:
653 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
654 break;
655 case REQUEST_PICK_APPWIDGET:
656 addAppWidgetFromPick(args.intent);
657 break;
658 case REQUEST_CREATE_APPWIDGET:
659 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
660 completeAddAppWidget(appWidgetId, args.screen);
661 break;
662 case REQUEST_PICK_WALLPAPER:
663 // We just wanted the activity result here so we can clear mWaitingForResult
664 break;
665 }
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700670 mWaitingForResult = false;
671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 // The pattern used here is that a user PICKs a specific application,
673 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
676 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700677
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 final PendingAddArguments args = new PendingAddArguments();
680 args.requestCode = requestCode;
681 args.intent = data;
682 args.screen = mAddScreen;
683 args.cellX = mAddIntersectCellX;
684 args.cellY = mAddIntersectCellY;
685
686 // If the loader is still running, defer the add until it is done.
687 if (isWorkspaceLocked()) {
688 sPendingAddList.add(args);
689 } else {
690 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Romain Guy18042c82009-11-06 11:44:55 -0800692 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
693 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
694 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 // Clean up the appWidgetId if we canceled
696 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
697 if (appWidgetId != -1) {
698 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702
703 @Override
704 protected void onResume() {
705 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800706 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700707 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 mWorkspaceLoading = true;
709 mModel.startLoader(this, true);
710 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700711 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800713 if (mWaitingForResume != null) {
714 mWaitingForResume.setStayPressed(false);
715 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700716 // When we resume Launcher, a different Activity might be responsible for the app
717 // market intent, so refresh the icon
718 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720
721 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 protected void onPause() {
723 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 // Some launcher layouts don't have a previous and next view
725 if (mPreviousView != null) {
726 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700727 }
728 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 dismissPreview(mNextView);
730 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700731 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800732 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700733 }
Romain Guycbb89e42009-06-08 15:52:54 -0700734
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 @Override
736 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 // Flag the loader to stop early before switching
738 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800739 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800740 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700741 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800743 // We can't hide the IME if it was forced open. So don't bother
744 /*
745 @Override
746 public void onWindowFocusChanged(boolean hasFocus) {
747 super.onWindowFocusChanged(hasFocus);
748
749 if (hasFocus) {
750 final InputMethodManager inputManager = (InputMethodManager)
751 getSystemService(Context.INPUT_METHOD_SERVICE);
752 WindowManager.LayoutParams lp = getWindow().getAttributes();
753 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
754 android.os.Handler()) {
755 protected void onReceiveResult(int resultCode, Bundle resultData) {
756 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
757 }
758 });
759 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
760 }
761 }
762 */
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 private boolean acceptFilter() {
765 final InputMethodManager inputManager = (InputMethodManager)
766 getSystemService(Context.INPUT_METHOD_SERVICE);
767 return !inputManager.isFullscreenMode();
768 }
769
770 @Override
771 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700772 final int uniChar = event.getUnicodeChar();
773 final boolean handled = super.onKeyDown(keyCode, event);
774 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
775 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
777 keyCode, event);
778 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700779 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700780 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700781 // showSearchDialog()
782 // If there are multiple keystrokes before the search dialog takes focus,
783 // onSearchRequested() will be called for every keystroke,
784 // but it is idempotent, so it's fine.
785 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 }
787 }
788
Joe Onorato8a9625e2010-01-28 15:55:35 -0800789 // Eat the long press event so the keyboard doesn't come up.
790 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
791 return true;
792 }
793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 return handled;
795 }
796
Karl Rosaen138a0412009-04-23 19:00:21 -0700797 private String getTypedText() {
798 return mDefaultKeySsb.toString();
799 }
800
801 private void clearTypedText() {
802 mDefaultKeySsb.clear();
803 mDefaultKeySsb.clearSpans();
804 Selection.setSelection(mDefaultKeySsb, 0);
805 }
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700808 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
809 * State
810 */
811 private static State intToState(int stateOrdinal) {
812 State state = State.WORKSPACE;
813 final State[] stateValues = State.values();
814 for (int i = 0; i < stateValues.length; i++) {
815 if (stateValues[i].ordinal() == stateOrdinal) {
816 state = stateValues[i];
817 break;
818 }
819 }
820 return state;
821 }
822
823 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 * Restores the previous state, if it exists.
825 *
826 * @param savedState The previous state.
827 */
828 private void restoreState(Bundle savedState) {
829 if (savedState == null) {
830 return;
831 }
832
Michael Jurka883f55b2010-10-21 15:47:14 -0700833 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
834
835 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800836 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700837 } else if (state == State.CUSTOMIZE) {
838 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
842 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700843 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 }
845
846 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700849 mAddScreen = addScreen;
850 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
851 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 mRestoring = true;
853 }
854
855 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
856 if (renameFolder) {
857 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700858 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 mRestoring = true;
860 }
Winson Chunga12a2502010-12-20 14:41:35 -0800861
862 // Restore the current AllApps drawer tab
863 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
864 String curTab = savedState.getString("allapps_currentTab");
865 if (curTab != null) {
866 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
867 tabhost.setCurrentTabByTag(curTab);
868 }
869 int curPage = savedState.getInt("allapps_currentPage", -1);
870 if (curPage > -1) {
871 mAllAppsPagedView.setRestorePage(curPage);
872 }
873 }
874
875 // Restore the current customization drawer tab
876 if (mHomeCustomizationDrawer != null) {
877 String curTab = savedState.getString("customize_currentTab");
878 if (curTab != null) {
879 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800880 mCustomizePagedView.setCustomizationFilter(
881 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800882 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
883 }
884
885 // Note: currently we do not restore the page for the customization tray because unlike
886 // AllApps, the page content can change drastically
887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
889
890 /**
891 * Finds all the views we need and configure them properly.
892 */
893 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700894 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400895
Romain Guyb1b69f52009-08-10 15:10:15 -0700896 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400897 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
Joe Onorato7c312c12009-08-13 21:36:53 -0700899 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700900 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700901 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800902 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700903 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700904 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700905
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700906 if (LauncherApplication.isScreenXLarge()) {
907 // They need to be INVISIBLE initially so that they will be measured in the layout.
908 // Otherwise the animations are messed up when we show them for the first time.
909 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
910 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
911 }
912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500916 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700917
Joe Onorato7c312c12009-08-13 21:36:53 -0700918 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
919 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700921 View handleView = findViewById(R.id.all_apps_button);
922 if (handleView != null && handleView instanceof HandleView) {
923 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700924 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700925 mHandleView.setLauncher(this);
926 mHandleView.setOnClickListener(this);
927 mHandleView.setOnLongClickListener(this);
928 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800929
Winson Chung80baf5a2010-08-09 16:03:15 -0700930 if (mCustomizePagedView != null) {
931 mCustomizePagedView.setLauncher(this);
932 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700933 } else {
934 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
935 hotseatLeft.setContentDescription(mHotseatLabels[0]);
936 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
937 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
938 hotseatRight.setContentDescription(mHotseatLabels[1]);
939 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400940
Michael Jurkaaf442092010-06-10 17:01:57 -0700941 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
942 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800943
Michael Jurkaaf442092010-06-10 17:01:57 -0700944 Drawable previous = mPreviousView.getDrawable();
945 Drawable next = mNextView.getDrawable();
946 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947
Michael Jurkaaf442092010-06-10 17:01:57 -0700948 mPreviousView.setHapticFeedbackEnabled(false);
949 mPreviousView.setOnLongClickListener(this);
950 mNextView.setHapticFeedbackEnabled(false);
951 mNextView.setOnLongClickListener(this);
952 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400955 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800957 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958
959 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400960 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800961
962 final View allAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -0700963 final View divider = findViewById(R.id.all_apps_divider);
Michael Jurkab8e14472010-12-20 16:06:10 -0800964 final View configureButton = findViewById(R.id.configure_button);
965
Michael Jurka54f7ac32010-08-02 13:56:46 -0700966 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800967 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700968 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800969 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700970 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700971 dragController.addDragListener(deleteZone);
972
Adam Cohencdc30d52010-12-01 15:09:47 -0800973 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
974 if (allAppsDeleteZone != null) {
975 allAppsDeleteZone.setLauncher(this);
976 allAppsDeleteZone.setDragController(dragController);
977 allAppsDeleteZone.setDragAndDropEnabled(false);
978 dragController.addDragListener(allAppsDeleteZone);
979 dragController.addDropTarget(allAppsDeleteZone);
980 }
981
982 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
983 findViewById(R.id.all_apps_info_target);
984 if (allAppsInfoTarget != null) {
985 allAppsInfoTarget.setLauncher(this);
986 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800987 allAppsInfoTarget.setDragAndDropEnabled(false);
988 View marketButton = findViewById(R.id.market_button);
989 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800990 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -0800991 marketButton.setOnClickListener(new OnClickListener() {
992 public void onClick(View v) {
993 onClickAppMarketButton(v);
994 }
995 });
Adam Cohencdc30d52010-12-01 15:09:47 -0800996 }
997 }
998
Joe Onorato00acb122009-08-04 16:04:30 -0400999 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001000 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001001 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001002
Joe Onorato00acb122009-08-04 16:04:30 -04001003 // The order here is bottom to top.
1004 dragController.addDropTarget(workspace);
1005 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001006 if (allAppsInfoTarget != null) {
1007 dragController.addDropTarget(allAppsInfoTarget);
1008 }
1009 if (allAppsDeleteZone != null) {
1010 dragController.addDropTarget(allAppsDeleteZone);
1011 }
Winson Chung97d85d22011-04-13 11:27:36 -07001012 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
1013 View.OnKeyListener listener = new ButtonBarKeyEventListener();
1014 int buttonCount = mButtonCluster.getChildCount();
1015 for (int i = 0; i < buttonCount; ++i) {
1016 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
1017 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001018
1019 mAllAppsButton = findViewById(R.id.all_apps_button);
Winson Chung97d85d22011-04-13 11:27:36 -07001020 mDivider = findViewById(R.id.all_apps_divider);
Michael Jurka838a4ca2011-02-07 13:33:06 -08001021 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001022
1023 // We had previously set these click handlers in XML, but the first time we launched
1024 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1025 // found the right handler. Setting the handlers directly here eliminates that cost.
1026 if (mConfigureButton != null) {
1027 mConfigureButton.setOnClickListener(new OnClickListener() {
1028 public void onClick(View v) {
1029 onClickConfigureButton(v);
1030 }
1031 });
1032 }
1033 if (mDivider != null) {
1034 mDivider.setOnClickListener(new OnClickListener() {
1035 public void onClick(View v) {
1036 onClickAllAppsButton(v);
1037 }
1038 });
1039 }
1040 if (mAllAppsButton != null) {
1041 mAllAppsButton.setOnClickListener(new OnClickListener() {
1042 public void onClick(View v) {
1043 onClickAllAppsButton(v);
1044 }
1045 });
1046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
1048
Romain Guy8a73c512009-11-09 19:19:59 -08001049 @SuppressWarnings({"UnusedDeclaration"})
1050 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001051 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001052 mWorkspace.scrollLeft();
1053 }
Romain Guy8a73c512009-11-09 19:19:59 -08001054 }
1055
1056 @SuppressWarnings({"UnusedDeclaration"})
1057 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001058 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001059 mWorkspace.scrollRight();
1060 }
Romain Guy8a73c512009-11-09 19:19:59 -08001061 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001062
1063 @SuppressWarnings({"UnusedDeclaration"})
1064 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001065 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001066
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001067 int index = -1;
1068 if (v.getId() == R.id.hotseat_left) {
1069 index = 0;
1070 } else if (v.getId() == R.id.hotseat_right) {
1071 index = 1;
1072 }
1073
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001074 // reload these every tap; you never know when they might change
1075 loadHotseats();
1076 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001077 startActivitySafely(
1078 mHotseats[index],
1079 "hotseat"
1080 );
1081 }
1082 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 /**
1085 * Creates a view representing a shortcut.
1086 *
1087 * @param info The data structure describing the shortcut.
1088 *
1089 * @return A View inflated from R.layout.application.
1090 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001091 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001093 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095
1096 /**
1097 * Creates a view representing a shortcut inflated from the specified resource.
1098 *
1099 * @param layoutResId The id of the XML layout used to create the shortcut.
1100 * @param parent The group the shortcut belongs to.
1101 * @param info The data structure describing the shortcut.
1102 *
1103 * @return A View inflated from layoutResId.
1104 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001105 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001106 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1107 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 return favorite;
1110 }
1111
1112 /**
1113 * Add an application shortcut to the workspace.
1114 *
1115 * @param data The intent describing the application.
1116 * @param cellInfo The position on screen where to create the shortcut.
1117 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001118 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 int intersectCellX, int intersectCellY) {
1120 final int[] cellXY = mTmpAddItemCellCoordinates;
1121 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1122
1123 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1124 showOutOfSpaceMessage();
1125 return;
1126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001128 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001129
Romain Guy73b979d2009-06-09 12:57:21 -07001130 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001131 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1135 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001136 } else {
1137 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
1139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 /**
1142 * Add a shortcut to the workspace.
1143 *
1144 * @param data The intent describing the shortcut.
1145 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 private void completeAddShortcut(Intent data, int screen,
1148 int intersectCellX, int intersectCellY) {
1149 final int[] cellXY = mTmpAddItemCellCoordinates;
1150 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001152 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001153 boolean foundCellSpan = false;
1154 if (touchXY != null) {
1155 // when dragging and dropping, just find the closest free spot
1156 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1157 int[] result = screenLayout.findNearestVacantArea(
1158 touchXY[0], touchXY[1], 1, 1, cellXY);
1159 foundCellSpan = (result != null);
1160 } else {
1161 foundCellSpan = layout.findCellForSpanThatIntersects(
1162 cellXY, 1, 1, intersectCellX, intersectCellY);
1163 }
1164
1165 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 showOutOfSpaceMessage();
1167 return;
1168 }
1169
1170 final ShortcutInfo info = mModel.addShortcut(
1171 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
1173 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001182 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 */
Winson Chungf7640c82011-02-28 13:47:29 -08001185 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1190 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001193 // We have saved the position to which the widget was dragged-- this really only matters
1194 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001196
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001197 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 boolean foundCellSpan = false;
1199 if (touchXY != null) {
1200 // when dragging and dropping, just find the closest free spot
1201 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1202 int[] result = screenLayout.findNearestVacantArea(
1203 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001204 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001206 // if we long pressed on an empty cell to bring up a menu,
1207 // make sure we intersect the empty cell
1208 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1209 // findCellForSpanThatIntersects will just ignore them
1210 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1211 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001212 }
1213
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001215 if (appWidgetId != -1) {
1216 // Deleting an app widget ID is a void call but writes to disk before returning
1217 // to the caller...
1218 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1219 new Thread("deleteAppWidgetId") {
1220 public void run() {
1221 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1222 }
1223 }.start();
1224 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001226 return;
1227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001229 // Build Launcher-specific widget info and save to database
1230 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 launcherInfo.spanX = spanXY[0];
1232 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 LauncherModel.addItemToDatabase(this, launcherInfo,
1235 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
1238 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001242 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001243
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001246
Michael Jurka0280c3b2010-09-17 15:00:07 -07001247 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001248 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001249
1250 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 }
1252 }
Romain Guycbb89e42009-06-08 15:52:54 -07001253
Adam Cohended9f8d2010-11-03 13:25:16 -07001254 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1255 @Override
1256 public void onReceive(Context context, Intent intent) {
1257 final String action = intent.getAction();
1258 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1259 mUserPresent = false;
1260 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001261
1262 // Reset AllApps to it's initial state
1263 if (mAllAppsGrid != null) {
1264 mAllAppsGrid.reset();
1265 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001266 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1267 mUserPresent = true;
1268 updateRunning();
1269 }
1270 }
1271 };
1272
1273 @Override
1274 public void onAttachedToWindow() {
1275 super.onAttachedToWindow();
1276
1277 // Listen for broadcasts related to user-presence
1278 final IntentFilter filter = new IntentFilter();
1279 filter.addAction(Intent.ACTION_SCREEN_OFF);
1280 filter.addAction(Intent.ACTION_USER_PRESENT);
1281 registerReceiver(mReceiver, filter);
1282
Adam Cohend113e0c2010-11-11 10:48:05 -08001283 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 mVisible = true;
1285 }
1286
1287 @Override
1288 public void onDetachedFromWindow() {
1289 super.onDetachedFromWindow();
1290 mVisible = false;
1291
Adam Cohend113e0c2010-11-11 10:48:05 -08001292 if (mAttached) {
1293 unregisterReceiver(mReceiver);
1294 mAttached = false;
1295 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001296 updateRunning();
1297 }
1298
1299 public void onWindowVisibilityChanged(int visibility) {
1300 mVisible = visibility == View.VISIBLE;
1301 updateRunning();
1302 }
1303
1304 private void sendAdvanceMessage(long delay) {
1305 mHandler.removeMessages(ADVANCE_MSG);
1306 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1307 mHandler.sendMessageDelayed(msg, delay);
1308 mAutoAdvanceSentTime = System.currentTimeMillis();
1309 }
1310
1311 private void updateRunning() {
1312 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1313 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1314 mAutoAdvanceRunning = autoAdvanceRunning;
1315 if (autoAdvanceRunning) {
1316 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1317 sendAdvanceMessage(delay);
1318 } else {
1319 if (!mWidgetsToAdvance.isEmpty()) {
1320 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1321 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1322 }
1323 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001324 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001325 }
1326 }
1327 }
1328
1329 private final Handler mHandler = new Handler() {
1330 @Override
1331 public void handleMessage(Message msg) {
1332 if (msg.what == ADVANCE_MSG) {
1333 int i = 0;
1334 for (View key: mWidgetsToAdvance.keySet()) {
1335 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1336 final int delay = mAdvanceStagger * i;
1337 if (v instanceof Advanceable) {
1338 postDelayed(new Runnable() {
1339 public void run() {
1340 ((Advanceable) v).advance();
1341 }
1342 }, delay);
1343 }
1344 i++;
1345 }
1346 sendAdvanceMessage(mAdvanceInterval);
1347 }
1348 }
1349 };
1350
1351 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1352 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1353 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1354 if (v instanceof Advanceable) {
1355 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001356 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001357 updateRunning();
1358 }
1359 }
1360
1361 void removeWidgetToAutoAdvance(View hostView) {
1362 if (mWidgetsToAdvance.containsKey(hostView)) {
1363 mWidgetsToAdvance.remove(hostView);
1364 updateRunning();
1365 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001366 }
1367
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1369 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001370 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 launcherInfo.hostView = null;
1372 }
1373
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 void showOutOfSpaceMessage() {
1375 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1376 }
1377
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001378 public LauncherAppWidgetHost getAppWidgetHost() {
1379 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
Romain Guycbb89e42009-06-08 15:52:54 -07001381
Winson Chunga9abd0e2010-10-27 17:18:37 -07001382 public LauncherModel getModel() {
1383 return mModel;
1384 }
1385
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001386 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001387 getWindow().closeAllPanels();
1388
1389 try {
1390 dismissDialog(DIALOG_CREATE_SHORTCUT);
1391 // Unlock the workspace if the dialog was showing
1392 } catch (Exception e) {
1393 // An exception is thrown if the dialog is not visible, which is fine
1394 }
1395
1396 try {
1397 dismissDialog(DIALOG_RENAME_FOLDER);
1398 // Unlock the workspace if the dialog was showing
1399 } catch (Exception e) {
1400 // An exception is thrown if the dialog is not visible, which is fine
1401 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001402
1403 // Whatever we were doing is hereby canceled.
1404 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001405 }
1406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 @Override
1408 protected void onNewIntent(Intent intent) {
1409 super.onNewIntent(intent);
1410
1411 // Close the menu
1412 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001413 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001414 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001415
Joe Onorato14f122b2009-11-19 14:06:36 -08001416 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1417 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001418
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001419 // In all these cases, only animate if we're already on home
1420
Michael Jurka4cb37242010-08-09 21:05:32 -07001421 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001422 mWorkspace.unshrink(alreadyOnHome);
1423 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001424
Patrick Dubroy758a9232011-03-03 19:54:56 -08001425 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001426 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001427 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001428 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001429 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001430
Joe Onorato3a8820b2009-11-10 15:06:42 -08001431 final View v = getWindow().peekDecorView();
1432 if (v != null && v.getWindowToken() != null) {
1433 InputMethodManager imm = (InputMethodManager)getSystemService(
1434 INPUT_METHOD_SERVICE);
1435 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001437
1438 // Reset AllApps to it's initial state
1439 if (mAllAppsGrid != null) {
1440 mAllAppsGrid.reset();
1441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443 }
1444
1445 @Override
1446 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1447 // Do not call super here
1448 mSavedInstanceState = savedInstanceState;
1449 }
1450
1451 @Override
1452 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001453 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454
1455 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1456 if (folders.size() > 0) {
1457 final int count = folders.size();
1458 long[] ids = new long[count];
1459 for (int i = 0; i < count; i++) {
1460 final FolderInfo info = folders.get(i).getInfo();
1461 ids[i] = info.id;
1462 }
1463 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1464 } else {
1465 super.onSaveInstanceState(outState);
1466 }
1467
Michael Jurka883f55b2010-10-21 15:47:14 -07001468 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 if (mAddScreen > -1 && mWaitingForResult) {
1471 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1472 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1473 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
1476 if (mFolderInfo != null && mWaitingForResult) {
1477 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1478 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1479 }
Michael Jurka946ad472010-07-09 18:05:18 -07001480
Winson Chung43b119d2010-12-06 16:45:05 -08001481 // Save the current AllApps drawer tab
1482 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1483 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1484 String currentTabTag = tabhost.getCurrentTabTag();
1485 if (currentTabTag != null) {
1486 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001487 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001488 }
1489 }
1490
1491 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001492 if (mHomeCustomizationDrawer != null) {
1493 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1494 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001495 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001496 }
1497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
1500 @Override
1501 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001505 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001507 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001509 mAppWidgetHost = null;
1510
1511 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512
1513 TextKeyListener.getInstance().release();
1514
Joe Onorato9c1289c2009-08-17 11:03:03 -04001515 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516
Joe Onorato9c1289c2009-08-17 11:03:03 -04001517 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001518
1519 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001520
Patrick Dubroyab962b72010-07-26 12:10:10 -07001521 // Some launcher layouts don't have a previous and next view
1522 if (mPreviousView != null) {
1523 dismissPreview(mPreviousView);
1524 }
1525 if (mNextView != null) {
1526 dismissPreview(mNextView);
1527 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001528
1529 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001530
1531 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1532 mWorkspace.removeAllViews();
1533 mWorkspace = null;
1534 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001535
1536 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538
1539 @Override
1540 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001541 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 super.startActivityForResult(intent, requestCode);
1543 }
1544
1545 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001546 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001548
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001549 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Karl Rosaen138a0412009-04-23 19:00:21 -07001551 if (initialQuery == null) {
1552 // Use any text typed in the launcher as the initial query
1553 initialQuery = getTypedText();
1554 clearTypedText();
1555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 if (appSearchData == null) {
1557 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001558 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
Romain Guycbb89e42009-06-08 15:52:54 -07001560
Karl Rosaen138a0412009-04-23 19:00:21 -07001561 final SearchManager searchManager =
1562 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001563 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001564 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 }
1566
1567 @Override
1568 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001569 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 return false;
1571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572
1573 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1576 .setIcon(android.R.drawable.ic_menu_add)
1577 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001578 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1579 .setIcon(android.R.drawable.ic_menu_manage)
1580 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001581 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 .setIcon(android.R.drawable.ic_menu_gallery)
1583 .setAlphabeticShortcut('W');
1584 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1585 .setIcon(android.R.drawable.ic_search_category_default)
1586 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1587 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1588 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1589 .setAlphabeticShortcut('N');
1590
1591 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001592 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1593 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594
1595 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1596 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1597 .setIntent(settings);
1598
1599 return true;
1600 }
1601
1602 @Override
1603 public boolean onPrepareOptionsMenu(Menu menu) {
1604 super.onPrepareOptionsMenu(menu);
1605
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001606 // If all apps is animating, don't show the menu, because we don't know
1607 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001608 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001609 return false;
1610 }
1611
1612 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001613 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001614 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1615 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1616
1617 // Disable add if the workspace is full.
1618 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001619 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1620 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622
1623 return true;
1624 }
1625
1626 @Override
1627 public boolean onOptionsItemSelected(MenuItem item) {
1628 switch (item.getItemId()) {
1629 case MENU_ADD:
1630 addItems();
1631 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001632 case MENU_MANAGE_APPS:
1633 manageApps();
1634 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 case MENU_WALLPAPER_SETTINGS:
1636 startWallpaper();
1637 return true;
1638 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001639 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 return true;
1641 case MENU_NOTIFICATIONS:
1642 showNotifications();
1643 return true;
1644 }
1645
1646 return super.onOptionsItemSelected(item);
1647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Karl Rosaen138a0412009-04-23 19:00:21 -07001649 /**
1650 * Indicates that we want global search for this activity by setting the globalSearch
1651 * argument for {@link #startSearch} to true.
1652 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001654 @Override
1655 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001656 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001657 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001658 }
1659
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 public boolean isWorkspaceLocked() {
1661 return mWorkspaceLoading || mWaitingForResult;
1662 }
1663
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001664 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1665 private boolean isPreviewVisible() {
1666 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1667 (mNextView != null && mNextView.getTag() != null);
1668 }
1669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001671 if (LauncherApplication.isScreenXLarge()) {
1672 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001673 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001674 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001675 }
1676 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001677 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001678 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681
Michael Jurka0280c3b2010-09-17 15:00:07 -07001682 private void resetAddInfo() {
1683 mAddScreen = -1;
1684 mAddIntersectCellX = -1;
1685 mAddIntersectCellY = -1;
1686 mAddDropPosition = null;
1687 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001688
Winson Chung55cef262010-10-28 14:14:18 -07001689 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001690 resetAddInfo();
1691 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001692 mAddDropPosition = position;
1693
Michael Jurkaaf442092010-06-10 17:01:57 -07001694 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001695 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1696 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001697 }
1698
Winson Chung7ad01412010-09-27 11:33:03 -07001699 private void manageApps() {
1700 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1701 }
1702
Michael Jurkaaf442092010-06-10 17:01:57 -07001703 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001704 // TODO: catch bad widget exception when sent
1705 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 // TODO: Is this log message meaningful?
1707 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001708 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001709 }
1710
Winson Chung55cef262010-10-28 14:14:18 -07001711 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001712 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713
Bjorn Bringert7984c942009-12-09 15:38:25 +00001714 if (appWidget.configure != null) {
1715 // Launch over to configure widget, if needed
1716 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1717 intent.setComponent(appWidget.configure);
1718 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001719 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001720 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1721 intent.putExtra(
1722 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1723 info.mimeType);
1724
1725 final String mimeType = info.mimeType;
1726 final ClipData clipData = (ClipData) info.configurationData;
1727 final ClipDescription clipDesc = clipData.getDescription();
1728 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1729 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001730 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001731 final CharSequence stringData = item.getText();
1732 final Uri uriData = item.getUri();
1733 final Intent intentData = item.getIntent();
1734 final String key =
1735 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1736 if (uriData != null) {
1737 intent.putExtra(key, uriData);
1738 } else if (intentData != null) {
1739 intent.putExtra(key, intentData);
1740 } else if (stringData != null) {
1741 intent.putExtra(key, stringData);
1742 }
1743 break;
1744 }
1745 }
1746 }
Winson Chung55cef262010-10-28 14:14:18 -07001747 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001748
Romain Guy8e633c52010-03-04 12:51:36 -08001749 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001751 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001752 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754 }
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Michael Jurka0280c3b2010-09-17 15:00:07 -07001756 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1757 resetAddInfo();
1758 mAddScreen = screen;
1759 mAddDropPosition = position;
1760
1761 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1762 createShortcutIntent.setComponent(componentName);
1763 processShortcut(createShortcutIntent);
1764 }
1765
Joe Onoratodeb98af2010-02-19 14:59:39 -08001766 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001767 // Handle case where user selected "Applications"
1768 String applicationName = getResources().getString(R.string.group_applications);
1769 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001770
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 if (applicationName != null && applicationName.equals(shortcutName)) {
1772 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1773 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001774
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1776 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001777 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001778 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001780 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
Winson Chung24ab2f12010-09-16 14:10:47 -07001784 void processWallpaper(Intent intent) {
1785 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1786 }
1787
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1789 resetAddInfo();
1790 mAddScreen = screen;
1791 mAddDropPosition = position;
1792
1793 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1794 createFolderIntent.setComponent(componentName);
1795
1796 addLiveFolder(createFolderIntent);
1797 }
1798
1799 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001800 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001801 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001802 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001804 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001806 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001807 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810
Adam Cohendf035382011-04-11 17:22:04 -07001811 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 UserFolderInfo folderInfo = new UserFolderInfo();
1813 folderInfo.title = getText(R.string.folder_name);
1814
Michael Jurka0280c3b2010-09-17 15:00:07 -07001815 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1816 final int[] cellXY = mTmpAddItemCellCoordinates;
1817 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1818 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001819 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821
1822 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001823 LauncherModel.addItemToDatabase(this, folderInfo,
1824 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001826 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827
1828 // Create the view
1829 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001830 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1831 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001832 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001833 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
Romain Guycbb89e42009-06-08 15:52:54 -07001835
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001837 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 }
1839
Michael Jurka28750fb2010-09-24 17:43:49 -07001840 private void completeAddLiveFolder(
1841 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1843 final int[] cellXY = mTmpAddItemCellCoordinates;
1844 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1845 showOutOfSpaceMessage();
1846 return;
1847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848
Michael Jurka0280c3b2010-09-17 15:00:07 -07001849 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850
1851 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001853 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001854 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 }
1856 }
1857
1858 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001859 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860
1861 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1862 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1863
1864 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 Intent.ShortcutIconResource iconResource = null;
1866
1867 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1868 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1869 try {
1870 iconResource = (Intent.ShortcutIconResource) extra;
1871 final PackageManager packageManager = context.getPackageManager();
1872 Resources resources = packageManager.getResourcesForApplication(
1873 iconResource.packageName);
1874 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1875 icon = resources.getDrawable(id);
1876 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001877 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
1879 }
1880
1881 if (icon == null) {
1882 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1883 }
1884
1885 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001886 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 info.title = name;
1888 info.iconResource = iconResource;
1889 info.uri = data.getData();
1890 info.baseIntent = baseIntent;
1891 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1892 LiveFolders.DISPLAY_MODE_GRID);
1893
1894 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001895 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001896 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897
1898 return info;
1899 }
1900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 private void showNotifications() {
1902 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1903 if (statusBar != null) {
1904 statusBar.expand();
1905 }
1906 }
1907
1908 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001909 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001911 Intent chooser = Intent.createChooser(pickWallpaper,
1912 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001913 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1914 // Removed in Eclair MR1
1915// WallpaperManager wm = (WallpaperManager)
1916// getSystemService(Context.WALLPAPER_SERVICE);
1917// WallpaperInfo wi = wm.getWallpaperInfo();
1918// if (wi != null && wi.getSettingsActivity() != null) {
1919// LabeledIntent li = new LabeledIntent(getPackageName(),
1920// R.string.configure_wallpaper, 0);
1921// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1922// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1923// }
Mike Clerona0618e42009-10-22 13:55:21 -07001924 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001927 /**
1928 * Registers various content observers. The current implementation registers
1929 * only a favorites observer to keep track of the favorites applications.
1930 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001931 private void registerContentObservers() {
1932 ContentResolver resolver = getContentResolver();
1933 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1934 true, mWidgetObserver);
1935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 @Override
1938 public boolean dispatchKeyEvent(KeyEvent event) {
1939 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1940 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001942 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001943 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001944 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001945 dumpState();
1946 return true;
1947 }
1948 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001949 }
1950 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1951 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001952 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 return true;
1954 }
1955 }
1956
1957 return super.dispatchKeyEvent(event);
1958 }
1959
Joe Onorato88ec0992009-11-19 13:16:06 -08001960 @Override
1961 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001962 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1963 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001964 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001965 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001966 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001967 dismissPreview(mPreviousView);
1968 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001969 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001970 mWorkspace.exitWidgetResizeMode();
1971
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001972 // Back button is a no-op here, but give at least some feedback for the button press
1973 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001974 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 private void closeFolder() {
1978 Folder folder = mWorkspace.getOpenFolder();
1979 if (folder != null) {
1980 closeFolder(folder);
1981 }
1982 }
1983
1984 void closeFolder(Folder folder) {
1985 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001986 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001988 CellLayout cl = (CellLayout) parent;
1989 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001990 if (folder instanceof DropTarget) {
1991 // Live folders aren't DropTargets.
1992 mDragController.removeDropTarget((DropTarget)folder);
1993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995 folder.onClose();
1996 }
1997
1998 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001999 * Re-listen when widgets are reset.
2000 */
2001 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002002 if (mAppWidgetHost != null) {
2003 mAppWidgetHost.startListening();
2004 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002005 }
2006
2007 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2009 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002011 private void unbindDesktopItems() {
2012 for (ItemInfo item: mDesktopItems) {
2013 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002015 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 /**
2019 * Launches the intent referred by the clicked shortcut.
2020 *
2021 * @param v The view representing the clicked shortcut.
2022 */
2023 public void onClick(View v) {
2024 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002025 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002027 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002028 int[] pos = new int[2];
2029 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002030 intent.setSourceBounds(new Rect(pos[0], pos[1],
2031 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002032 boolean success = startActivitySafely(intent, tag);
2033
2034 if (success && v instanceof BubbleTextView) {
2035 mWaitingForResume = (BubbleTextView) v;
2036 mWaitingForResume.setStayPressed(true);
2037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 } else if (tag instanceof FolderInfo) {
2039 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002040 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002041 if (mState == State.ALL_APPS) {
2042 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002043 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002044 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047 }
2048
Michael Jurka0e260592010-06-30 17:07:39 -07002049 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002050 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002051 // clicking anywhere on the workspace causes the customization drawer to slide down
2052 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002053 return false;
2054 }
2055
Michael Jurkaaf442092010-06-10 17:01:57 -07002056 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002057 * Event handler for the search button
2058 *
2059 * @param v The view that was clicked.
2060 */
2061 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002062 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002063 // Use a custom animation for launching search
2064 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002065 }
2066
2067 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002068 * Event handler for the voice button
2069 *
2070 * @param v The view that was clicked.
2071 */
2072 public void onClickVoiceButton(View v) {
2073 startVoiceSearch();
2074 }
2075
2076 private void startVoiceSearch() {
2077 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2078 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2079 startActivity(intent);
2080 }
2081
2082 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002083 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002084 * enters home screen customization mode.
2085 *
2086 * @param v The view that was clicked.
2087 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002088 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002089 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002090 }
2091
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002092 /**
2093 * Event handler for the "grid" button that appears on the home screen, which
2094 * enters all apps mode.
2095 *
2096 * @param v The view that was clicked.
2097 */
2098 public void onClickAllAppsButton(View v) {
2099 showAllApps(true);
2100 }
2101
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002102 public void onClickAppMarketButton(View v) {
2103 if (mAppMarketIntent != null) {
2104 startActivitySafely(mAppMarketIntent, "app market");
2105 }
2106 }
2107
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002108 void startApplicationDetailsActivity(ComponentName componentName) {
2109 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002110 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2111 Uri.fromParts("package", packageName, null));
2112 startActivity(intent);
2113 }
2114
Patrick Dubroy5539af72010-09-07 15:22:01 -07002115 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2116 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2117 // System applications cannot be installed. For now, show a toast explaining that.
2118 // We may give them the option of disabling apps this way.
2119 int messageId = R.string.uninstall_system_app_text;
2120 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2121 } else {
2122 String packageName = appInfo.componentName.getPackageName();
2123 String className = appInfo.componentName.getClassName();
2124 Intent intent = new Intent(
2125 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2126 startActivity(intent);
2127 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002128 }
2129
Michael Jurkaddd62e92011-02-16 17:49:14 -08002130 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2132 try {
2133 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002134 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 } catch (ActivityNotFoundException e) {
2136 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002137 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 } catch (SecurityException e) {
2139 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002140 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002142 "or use the exported attribute for this activity. "
2143 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002145 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002147
Romain Guy8e633c52010-03-04 12:51:36 -08002148 void startActivityForResultSafely(Intent intent, int requestCode) {
2149 try {
2150 startActivityForResult(intent, requestCode);
2151 } catch (ActivityNotFoundException e) {
2152 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2153 } catch (SecurityException e) {
2154 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2155 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2156 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2157 "or use the exported attribute for this activity.", e);
2158 }
2159 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160
2161 private void handleFolderClick(FolderInfo folderInfo) {
2162 if (!folderInfo.opened) {
2163 // Close any open folder
2164 closeFolder();
2165 // Open the requested folder
2166 openFolder(folderInfo);
2167 } else {
2168 // Find the open folder...
2169 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2170 int folderScreen;
2171 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002172 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 // .. and close it
2174 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002175 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 // Close any folder open on the current screen
2177 closeFolder();
2178 // Pull the folder onto this screen
2179 openFolder(folderInfo);
2180 }
2181 }
2182 }
2183 }
2184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002186 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 * is animated relative to the specified View. If the View is null, no animation
2188 * is played.
2189 *
2190 * @param folderInfo The FolderInfo describing the folder to open.
2191 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002192 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 Folder openFolder;
2194
2195 if (folderInfo instanceof UserFolderInfo) {
2196 openFolder = UserFolder.fromXml(this);
2197 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002198 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 } else {
2200 return;
2201 }
2202
Joe Onorato00acb122009-08-04 16:04:30 -04002203 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 openFolder.setLauncher(this);
2205
2206 openFolder.bind(folderInfo);
2207 folderInfo.opened = true;
2208
Winson Chungaafa03c2010-06-11 17:34:16 -07002209 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 openFolder.onOpen();
2212 }
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002215 if (mState != State.WORKSPACE) {
2216 return false;
2217 }
2218
Romain Guy1fbc1c82009-11-09 20:43:08 -08002219 switch (v.getId()) {
2220 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002221 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002222 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2223 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002224 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002225 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226 return true;
2227 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002229 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2230 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002231 showPreviews(v);
2232 }
2233 return true;
2234 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002235 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002236 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2237 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2238 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002239 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002240 return true;
2241 }
2242
Joe Onorato9c1289c2009-08-17 11:03:03 -04002243 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 return false;
2245 }
2246
2247 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002248 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
2250
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 resetAddInfo();
2252 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 return true;
2256 }
2257
Michael Jurka0280c3b2010-09-17 15:00:07 -07002258 final View itemUnderLongClick = longClickCellInfo.cell;
2259
Winson Chung304dcde2011-01-07 11:17:23 -08002260 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 if (itemUnderLongClick == null) {
2262 // User long pressed on empty space
2263 mWorkspace.setAllowLongPress(false);
2264 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2265 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002266 if (LauncherApplication.isScreenXLarge()) {
2267 addItems();
2268 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 }
2271 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002274 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2275 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276 mAddIntersectCellX = longClickCellInfo.cellX;
2277 mAddIntersectCellY = longClickCellInfo.cellY;
2278 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 }
2280 }
2281 }
2282 return true;
2283 }
2284
Romain Guye6b8e2f2009-11-10 11:56:55 -08002285 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002286 private void dismissPreview(final View v) {
2287 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002288 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002289 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2290 public void onDismiss() {
2291 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2292 int count = group.getChildCount();
2293 for (int i = 0; i < count; i++) {
2294 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2295 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002296 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2297 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2298
2299 v.setTag(R.id.workspace, null);
2300 v.setTag(R.id.icon, null);
2301 window.setOnDismissListener(null);
2302 }
2303 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002304 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002305 }
2306 v.setTag(null);
2307 }
2308
Romain Guyf8e6a802009-12-07 17:48:02 -08002309 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002310 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002311 }
2312
Romain Guya6abce82009-11-10 02:54:41 -08002313 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002314 final Resources resources = getResources();
2315 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002316
Romain Guya6abce82009-11-10 02:54:41 -08002317 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002318
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002320
Romain Guyf8e6a802009-12-07 17:48:02 -08002321 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002322 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002323 int extraW = (int) ((r.left + r.right) * max);
2324 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002325
2326 int aW = cell.getWidth() - extraW;
2327 float w = aW / max;
2328
2329 int width = cell.getWidth();
2330 int height = cell.getHeight();
2331 int x = cell.getLeftPadding();
2332 int y = cell.getTopPadding();
2333 width -= (x + cell.getRightPadding());
2334 height -= (y + cell.getBottomPadding());
2335
2336 float scale = w / width;
2337
2338 int count = end - start;
2339
2340 final float sWidth = width * scale;
2341 float sHeight = height * scale;
2342
Romain Guye6b8e2f2009-11-10 11:56:55 -08002343 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002344
Romain Guye6b8e2f2009-11-10 11:56:55 -08002345 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2346 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002347
2348 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002349 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002350 cell = (CellLayout) workspace.getChildAt(i);
2351
Romain Guyf8e6a802009-12-07 17:48:02 -08002352 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002353 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002354
2355 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002356 c.scale(scale, scale);
2357 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002358 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002359
Romain Guy9d31e9f2009-11-11 18:54:28 -08002360 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002361 image.setImageBitmap(bitmap);
2362 image.setTag(i);
2363 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002364 image.setOnFocusChangeListener(handler);
2365 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002366 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002367
2368 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002369 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2370
Winson Chungaafa03c2010-06-11 17:34:16 -07002371 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002372 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002373
2374 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002375 p.setContentView(preview);
2376 p.setWidth((int) (sWidth * count + extraW));
2377 p.setHeight((int) (sHeight + extraH));
2378 p.setAnimationStyle(R.style.AnimationPreview);
2379 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002380 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002381 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002382 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002383
Romain Guye6b8e2f2009-11-10 11:56:55 -08002384 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2385 public void onDismiss() {
2386 dismissPreview(anchor);
2387 }
2388 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002389
2390 anchor.setTag(p);
2391 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002392 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002393 }
2394
Romain Guy9d31e9f2009-11-11 18:54:28 -08002395 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002396 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002397
Romain Guye6b8e2f2009-11-10 11:56:55 -08002398 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002399 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002400 }
2401
2402 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002403 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002404 v.post(this);
2405 }
2406
2407 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002408 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002409 }
2410
2411 public void onFocusChange(View v, boolean hasFocus) {
2412 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002413 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002414 }
Romain Guya6abce82009-11-10 02:54:41 -08002415 }
2416 }
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 Workspace getWorkspace() {
2419 return mWorkspace;
2420 }
2421
Michael Jurka3c38d562011-02-18 19:22:12 -08002422 TabHost getCustomizationDrawer() {
2423 return mHomeCustomizationDrawer;
2424 }
2425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 @Override
2427 protected Dialog onCreateDialog(int id) {
2428 switch (id) {
2429 case DIALOG_CREATE_SHORTCUT:
2430 return new CreateShortcut().createDialog();
2431 case DIALOG_RENAME_FOLDER:
2432 return new RenameFolder().createDialog();
2433 }
2434
2435 return super.onCreateDialog(id);
2436 }
2437
2438 @Override
2439 protected void onPrepareDialog(int id, Dialog dialog) {
2440 switch (id) {
2441 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 break;
2443 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002444 if (mFolderInfo != null) {
2445 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2446 final CharSequence text = mFolderInfo.title;
2447 input.setText(text);
2448 input.setSelection(0, text.length());
2449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 break;
2451 }
2452 }
2453
2454 void showRenameDialog(FolderInfo info) {
2455 mFolderInfo = info;
2456 mWaitingForResult = true;
2457 showDialog(DIALOG_RENAME_FOLDER);
2458 }
2459
Michael Jurka0280c3b2010-09-17 15:00:07 -07002460 private void showAddDialog(int intersectX, int intersectY) {
2461 resetAddInfo();
2462 mAddIntersectCellX = intersectX;
2463 mAddIntersectCellY = intersectY;
2464 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 mWaitingForResult = true;
2466 showDialog(DIALOG_CREATE_SHORTCUT);
2467 }
2468
Joe Onoratodeb98af2010-02-19 14:59:39 -08002469 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002470 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002471 Bundle bundle = new Bundle();
2472
2473 ArrayList<String> shortcutNames = new ArrayList<String>();
2474 shortcutNames.add(getString(R.string.group_applications));
2475 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2476
2477 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2478 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2479 R.drawable.ic_launcher_application));
2480 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2481
2482 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2483 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002484 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002485 pickIntent.putExtras(bundle);
2486
Joe Onoratodeb98af2010-02-19 14:59:39 -08002487 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002488 }
2489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 private class RenameFolder {
2491 private EditText mInput;
2492
2493 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2495 mInput = (EditText) layout.findViewById(R.id.folder_name);
2496
2497 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2498 builder.setIcon(0);
2499 builder.setTitle(getString(R.string.rename_folder_title));
2500 builder.setCancelable(true);
2501 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2502 public void onCancel(DialogInterface dialog) {
2503 cleanup();
2504 }
2505 });
2506 builder.setNegativeButton(getString(R.string.cancel_action),
2507 new Dialog.OnClickListener() {
2508 public void onClick(DialogInterface dialog, int which) {
2509 cleanup();
2510 }
2511 }
2512 );
2513 builder.setPositiveButton(getString(R.string.rename_action),
2514 new Dialog.OnClickListener() {
2515 public void onClick(DialogInterface dialog, int which) {
2516 changeFolderName();
2517 }
2518 }
2519 );
2520 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002521
2522 final AlertDialog dialog = builder.create();
2523 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2524 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002525 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002526 mInput.requestFocus();
2527 InputMethodManager inputManager = (InputMethodManager)
2528 getSystemService(Context.INPUT_METHOD_SERVICE);
2529 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002530 }
2531 });
2532
2533 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534 }
2535
2536 private void changeFolderName() {
2537 final String name = mInput.getText().toString();
2538 if (!TextUtils.isEmpty(name)) {
2539 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002540 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 mFolderInfo.title = name;
2542 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2543
Joe Onorato9c1289c2009-08-17 11:03:03 -04002544 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002545 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002546 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 } else {
2548 final FolderIcon folderIcon = (FolderIcon)
2549 mWorkspace.getViewForTag(mFolderInfo);
2550 if (folderIcon != null) {
2551 folderIcon.setText(name);
2552 getWorkspace().requestLayout();
2553 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002554 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002555 mWorkspaceLoading = true;
2556 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
2558 }
2559 }
2560 cleanup();
2561 }
2562
2563 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 dismissDialog(DIALOG_RENAME_FOLDER);
2565 mWaitingForResult = false;
2566 mFolderInfo = null;
2567 }
2568 }
2569
Daniel Sandler843e8602010-06-07 14:59:01 -04002570 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2571 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002572 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002573 }
2574
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002575 // AllAppsView.Watcher
2576 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002577 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2578 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002579 mWorkspace.setVisibility(View.GONE);
2580 }
2581 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002582
Michael Jurkaafca9532011-02-16 14:50:35 -08002583 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 button.setClickable(true);
2586 }
2587
2588 private void hideToolbarButton(View button) {
2589 button.setAlpha(0.0f);
2590 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2591 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002592 }
2593
2594 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 button.setClickable(false);
2596 }
2597
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 *
2601 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2602 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002603 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002604 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002606 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 final boolean showing = show;
2608 final boolean hiding = !show;
2609
2610 final int duration = show ?
2611 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2612 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2613
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002615 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002616 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002617 anim.addUpdateListener(new AnimatorUpdateListener() {
2618 public void onAnimationUpdate(ValueAnimator animation) {
2619 view.setAlpha((Float) animation.getAnimatedValue());
2620 }
2621 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002622 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002623 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002624 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002625 if (showing) showAndEnableToolbarButton(view);
2626 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002628 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002629 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002630 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002631 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002632 });
2633 seq.play(anim);
2634 } else {
2635 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002636 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002637 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002638 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002639 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002640 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002641 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 }
2644
2645 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002646 * Show/hide the appropriate toolbar buttons for newState.
2647 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 *
2649 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002650 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2651 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002652 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002653 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002654 switch (newState) {
2655 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002656 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2657 mDeleteZone.setOverlappingViews(
2658 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002659 mDeleteZone.setDragAndDropEnabled(true);
2660 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002661 break;
2662 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002663 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002664 mDeleteZone.setDragAndDropEnabled(false);
2665 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 break;
2667 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002668 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002669 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002670 break;
2671 }
2672 }
2673
2674 /**
2675 * Helper method for the cameraZoomIn/cameraZoomOut animations
2676 * @param view The view being animated
2677 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2678 * @param scaleFactor The scale factor used for the zoom
2679 */
2680 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2681 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002682
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002684 // Set pivotY so that at the starting zoom factor, the view is partially
2685 // visible. Modifying initialHeightFactor changes how much of the view is
2686 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002688 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002689 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002690 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002691 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002692 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002693 }
2694 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002695
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002696 /**
2697 * Zoom the camera out from the workspace to reveal 'toView'.
2698 * Assumes that the view to show is anchored at either the very top or very bottom
2699 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002701 */
Winson Chung20f71112011-04-06 14:22:04 -07002702 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002703 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002704 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002705
2706 final int duration = toAllApps ?
2707 res.getInteger(R.integer.config_allAppsZoomInTime) :
2708 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002709 final int fadeDuration = toAllApps ?
2710 res.getInteger(R.integer.config_allAppsFadeInTime) :
2711 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002712
2713 final float scale = toAllApps ?
2714 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2715 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2716
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002717 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002718
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 setPivotsForZoom(toView, toState, scale);
2720
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002721 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002722 if (!springLoaded) {
2723 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2724
2725 // Everytime we launch into AllApps, we reset the successful drop flag which
2726 // controls when it should hide/show the mini workspaces
2727 mAllAppsPagedView.resetSuccessfulDropFlag();
2728 } else {
2729 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2730 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002732 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002733 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734
2735 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002736 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002737 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002738 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2739 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002740 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002741 toView.setFastScaleX(a * scale + b * 1f);
2742 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002743 }
2744 });
Adam Cohen61033d32010-11-15 18:29:44 -08002745
Adam Cohenf16e5712011-01-13 13:31:45 -08002746 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002747 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002748 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002749 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002750 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2751 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002752 // don't need to invalidate because we do so above
2753 toView.setFastAlpha(a * 0f + b * 1f);
2754 }
2755 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002756 alphaAnim.start();
2757 }
2758
Michael Jurkaabded662011-03-04 12:06:57 -08002759 if (toView instanceof LauncherTransitionable) {
2760 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002761 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002762 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002763 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002764 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002765 // Prepare the position
2766 toView.setTranslationX(0.0f);
2767 toView.setTranslationY(0.0f);
2768 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002769 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002770 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002771 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002773 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002774 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002775 // If we don't set the final scale values here, if this animation is cancelled
2776 // it will have the wrong scale value and subsequent cameraPan animations will
2777 // not fix that
2778 toView.setScaleX(1.0f);
2779 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002780 if (toView instanceof LauncherTransitionable) {
2781 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002782 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002783 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002784 });
2785
Chet Haaseb1254a62010-09-07 13:35:00 -07002786 AnimatorSet toolbarHideAnim = new AnimatorSet();
2787 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002788 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2789
2790 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002791 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002793 if (mStateAnimation != null) mStateAnimation.cancel();
2794 mStateAnimation = new AnimatorSet();
2795 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2796 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797
2798 // Show the new toolbar buttons just as the main animation is ending
2799 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002800 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2801 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002802 } else {
2803 toView.setTranslationX(0.0f);
2804 toView.setTranslationY(0.0f);
2805 toView.setScaleX(1.0f);
2806 toView.setScaleY(1.0f);
2807 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002808 if (toView instanceof LauncherTransitionable) {
2809 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2810 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2811 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002812 hideAndShowToolbarButtons(toState, null, null);
2813 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002814 }
2815
2816 /**
2817 * Zoom the camera back into the workspace, hiding 'fromView'.
2818 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002819 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2820 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002821 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002822 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002823 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002824 final boolean fromAllApps = (fromState == State.ALL_APPS);
2825
2826 int duration = fromAllApps ?
2827 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2828 res.getInteger(R.integer.config_customizeZoomOutTime);
2829
Michael Jurka742574b2011-02-02 23:51:01 -08002830 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002831 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2832 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2833
2834 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002835
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002836 mCustomizePagedView.endChoiceMode();
2837 mAllAppsPagedView.endChoiceMode();
2838
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002839 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002840
Michael Jurkad3ef3062010-11-23 16:23:58 -08002841 if (!springLoaded) {
2842 mWorkspace.unshrink(animated);
2843 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002844 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 if (mStateAnimation != null) mStateAnimation.cancel();
2846 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002847
Michael Jurka742574b2011-02-02 23:51:01 -08002848 final float oldScaleX = fromView.getScaleX();
2849 final float oldScaleY = fromView.getScaleY();
2850
2851 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2852 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002853 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2854 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002855 ((View)fromView.getParent()).fastInvalidate();
2856 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2857 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2858 }
2859 });
Michael Jurkaabded662011-03-04 12:06:57 -08002860 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002861 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002862 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002863 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2864 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002865 // don't need to invalidate because we do so above
2866 fromView.setFastAlpha(a * 1f + b * 0f);
2867 }
2868 });
Michael Jurkaabded662011-03-04 12:06:57 -08002869 if (fromView instanceof LauncherTransitionable) {
2870 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002871 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002872 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002873 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002874 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002875 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002876 if (fromView instanceof LauncherTransitionable) {
2877 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002878 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002879 }
2880 });
2881
Chet Haaseb1254a62010-09-07 13:35:00 -07002882 AnimatorSet toolbarHideAnim = new AnimatorSet();
2883 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002884 if (!springLoaded) {
2885 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2886 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002887
Adam Cohen61033d32010-11-15 18:29:44 -08002888 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002889
2890 // Show the new toolbar buttons at the very end of the whole animation
2891 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2892 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002893 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2894 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 } else {
2896 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002897 if (fromView instanceof LauncherTransitionable) {
2898 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2899 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2900 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002901 if (!springLoaded) {
2902 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2903 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002904 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002905 }
2906
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002907 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002908 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002909 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002910 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002911
Michael Jurka79212d82010-07-30 16:36:20 -07002912 if (LauncherApplication.isScreenXLarge()) {
Winson Chung20f71112011-04-06 14:22:04 -07002913 cameraZoomOut(State.ALL_APPS, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002914 } else {
2915 mAllAppsGrid.zoom(1.0f, animated);
2916 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002917
Romain Guyc16fea72010-03-12 17:17:56 -08002918 ((View) mAllAppsGrid).setFocusable(true);
2919 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002920
Joe Onorato7c312c12009-08-13 21:36:53 -07002921 // TODO: fade these two too
2922 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002923
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002924 // Change the state *after* we've called all the transition code
2925 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002926
Winson Chung5fb63472011-02-02 17:03:37 -08002927 // Pause the auto-advance of widgets until we are out of AllApps
2928 mUserPresent = false;
2929 updateRunning();
2930
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002931 // send an accessibility event to announce the context change
2932 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2933 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002934
2935 void showWorkspace(boolean animated) {
2936 showWorkspace(animated, null);
2937 }
2938
2939 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002940 if (layout != null) {
2941 // always animated, but that's ok since we never specify a layout and
2942 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002943 mWorkspace.unshrink(layout);
2944 } else {
2945 mWorkspace.unshrink(animated);
2946 }
2947 if (mState == State.ALL_APPS) {
2948 closeAllApps(animated);
2949 } else if (mState == State.CUSTOMIZE) {
2950 hideCustomizationDrawer(animated);
2951 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002952
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953 // Change the state *after* we've called all the transition code
2954 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002955
Winson Chung5fb63472011-02-02 17:03:37 -08002956 // Resume the auto-advance of widgets
2957 mUserPresent = true;
2958 updateRunning();
2959
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002960 // send an accessibility event to announce the context change
2961 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002962 }
2963
Michael Jurkad3ef3062010-11-23 16:23:58 -08002964 void enterSpringLoadedDragMode(CellLayout layout) {
2965 mWorkspace.enterSpringLoadedDragMode(layout);
2966 if (mState == State.ALL_APPS) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002967 mState = State.ALL_APPS_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002968 cameraZoomIn(State.ALL_APPS, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002969 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002970 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002971 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002972 }/* else {
2973 // we're already in spring loaded mode; don't do anything
2974 }*/
2975 }
2976
2977 void exitSpringLoadedDragMode() {
2978 if (mState == State.ALL_APPS_SPRING_LOADED) {
2979 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07002980 cameraZoomOut(State.ALL_APPS, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002981 mState = State.ALL_APPS;
2982 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2983 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002984 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002985 mState = State.CUSTOMIZE;
2986 }/* else {
2987 // we're not in spring loaded mode; don't do anything
2988 }*/
2989 }
2990
Joe Onoratob2061212009-11-24 16:13:54 -05002991 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002992 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002993 * - Home from workspace
2994 * - from center screen
2995 * - from other screens
2996 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002997 * - from center screen
2998 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002999 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003000 * - from center screen
3001 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003002 * - Launch app from workspace and quit
3003 * - with back
3004 * - with home
3005 * - Launch app from all apps and quit
3006 * - with back
3007 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003008 * - Go to a screen that's not the default, then all
3009 * apps, and launch and app, and go back
3010 * - with back
3011 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003012 * - On workspace, long press power and go back
3013 * - with back
3014 * - with home
3015 * - On all apps, long press power and go back
3016 * - with back
3017 * - with home
3018 * - On workspace, power off
3019 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003020 * - Launch an app and turn off the screen while in that app
3021 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003022 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003023 * - From all apps
3024 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003025 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3026 * - From all apps
3027 * - From the center workspace
3028 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003029 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003030 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003031 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003032 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003033 if (LauncherApplication.isScreenXLarge()) {
Winson Chung20f71112011-04-06 14:22:04 -07003034 cameraZoomIn(State.ALL_APPS, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003035 } else {
3036 mAllAppsGrid.zoom(0.0f, animated);
3037 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003038 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003039 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003040 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003041 }
3042
Joe Onorato7c312c12009-08-13 21:36:53 -07003043 void lockAllApps() {
3044 // TODO
3045 }
3046
3047 void unlockAllApps() {
3048 // TODO
3049 }
3050
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003051 // Show the customization drawer (only exists in x-large configuration)
3052 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003053 if (mState != State.WORKSPACE) {
3054 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003055 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003056
Winson Chung20f71112011-04-06 14:22:04 -07003057 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003058
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003059 // Change the state *after* we've called all the transition code
3060 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003061
3062 // Pause the auto-advance of widgets until we are out of Customization drawer
3063 mUserPresent = false;
3064 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003065 }
3066
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003067 // Hide the customization drawer (only exists in x-large configuration)
3068 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003069 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003070 cameraZoomIn(State.CUSTOMIZE, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003071 }
3072 }
3073
Patrick Dubroy5f445422011-02-18 14:35:21 -08003074 /**
3075 * Add an item from all apps or customize onto the given workspace screen.
3076 * If layout is null, add to the current screen.
3077 */
3078 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003079 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3080 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003081 } else {
3082 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003083 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003084 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003085
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003086 void onWorkspaceClick(CellLayout layout) {
3087 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003088 }
3089
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003090 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003091 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003092 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 // Look for the toolbar icon specified in the activity meta-data
3094 Bundle metaData = packageManager.getActivityInfo(
3095 activityName, PackageManager.GET_META_DATA).metaData;
3096 if (metaData != null) {
3097 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3098 if (iconResId != 0) {
3099 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003100 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003101 }
3102 }
3103 } catch (NameNotFoundException e) {
3104 // Do nothing
3105 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003106 return null;
3107 }
3108
3109 // if successful in getting icon, return it; otherwise, set button to use default drawable
3110 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3111 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3112 TextView button = (TextView) findViewById(buttonId);
3113 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3114
3115 // If we were unable to find the icon via the meta-data, use a generic one
3116 if (toolbarIcon == null) {
3117 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3118 return null;
3119 } else {
3120 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3121 return toolbarIcon.getConstantState();
3122 }
3123 }
3124
3125 // if successful in getting icon, return it; otherwise, set button to use default drawable
3126 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3127 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3128 ImageView button = (ImageView) findViewById(buttonId);
3129 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3130
Michael Jurka0423dcf2010-10-05 14:56:18 -07003131 // If we were unable to find the icon via the meta-data, use a generic one
3132 if (toolbarIcon == null) {
3133 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003134 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003135 } else {
3136 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003137 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003138 }
3139 }
3140
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003141 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3142 TextView button = (TextView) findViewById(buttonId);
3143 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3144 }
3145
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003146 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003147 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003148 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003149 }
3150
Michael Jurka0423dcf2010-10-05 14:56:18 -07003151 private void updateGlobalSearchIcon() {
3152 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003153 final View searchButton = findViewById(R.id.search_button);
3154 final View searchDivider = findViewById(R.id.search_divider);
3155
Michael Jurka0423dcf2010-10-05 14:56:18 -07003156 final SearchManager searchManager =
3157 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3158 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003159 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003160 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003161 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003162 searchButton.setVisibility(View.VISIBLE);
3163 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003164 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003165 searchButton.setVisibility(View.GONE);
3166 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003167 }
3168 }
3169 }
3170
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003171 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003172 updateButtonWithDrawable(R.id.search_button, d);
3173 }
3174
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003175 private void updateVoiceSearchIcon() {
3176 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003177 final View searchDivider = findViewById(R.id.search_divider);
3178 final View voiceButton = findViewById(R.id.voice_button);
3179
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003180 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3181 ComponentName activityName = intent.resolveActivity(getPackageManager());
3182 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003183 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003184 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003185 searchDivider.setVisibility(View.VISIBLE);
3186 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003187 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003188 searchDivider.setVisibility(View.GONE);
3189 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003190 }
3191 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003192 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003193
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003194 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003195 updateButtonWithDrawable(R.id.voice_button, d);
3196 }
3197
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003198 /**
3199 * Sets the app market icon (shown when all apps is visible on x-large screens)
3200 */
3201 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003202 if (LauncherApplication.isScreenXLarge()) {
3203 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3204 // Find the app market activity by resolving an intent.
3205 // (If multiple app markets are installed, it will return the ResolverActivity.)
3206 ComponentName activityName = intent.resolveActivity(getPackageManager());
3207 if (activityName != null) {
3208 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003209 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003210 R.id.market_button, activityName, R.drawable.app_market_generic);
3211 }
3212 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003213 }
3214
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003215 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003216 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003217 }
3218
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003219 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 * Displays the shortcut creation dialog and launches, if necessary, the
3221 * appropriate activity.
3222 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003223 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003224 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3225 DialogInterface.OnShowListener {
3226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003229 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3233 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003234 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 builder.setInverseBackgroundForced(true);
3237
3238 AlertDialog dialog = builder.create();
3239 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003240 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003241 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003243 return dialog;
3244 }
3245
3246 public void onCancel(DialogInterface dialog) {
3247 mWaitingForResult = false;
3248 cleanup();
3249 }
3250
Romain Guycbb89e42009-06-08 15:52:54 -07003251 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003252 }
3253
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003255 try {
3256 dismissDialog(DIALOG_CREATE_SHORTCUT);
3257 } catch (Exception e) {
3258 // An exception is thrown if the dialog is not visible, which is fine
3259 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003260 }
3261
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003262 /**
3263 * Handle the action clicked in the "Add to home" dialog.
3264 */
3265 public void onClick(DialogInterface dialog, int which) {
3266 Resources res = getResources();
3267 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003268
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003269 switch (which) {
3270 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003271 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003272 break;
3273 }
Romain Guycbb89e42009-06-08 15:52:54 -07003274
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003275 case AddAdapter.ITEM_APPWIDGET: {
3276 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003277
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003278 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3279 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003280 // start the pick activity
3281 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3282 break;
3283 }
Romain Guycbb89e42009-06-08 15:52:54 -07003284
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003285 case AddAdapter.ITEM_LIVE_FOLDER: {
3286 // Insert extra item to handle inserting folder
3287 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003288
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003289 ArrayList<String> shortcutNames = new ArrayList<String>();
3290 shortcutNames.add(res.getString(R.string.group_folder));
3291 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003292
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003293 ArrayList<ShortcutIconResource> shortcutIcons =
3294 new ArrayList<ShortcutIconResource>();
3295 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3296 R.drawable.ic_launcher_folder));
3297 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3298
3299 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3300 pickIntent.putExtra(Intent.EXTRA_INTENT,
3301 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3302 pickIntent.putExtra(Intent.EXTRA_TITLE,
3303 getText(R.string.title_select_live_folder));
3304 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003305
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003306 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3307 break;
3308 }
3309
3310 case AddAdapter.ITEM_WALLPAPER: {
3311 startWallpaper();
3312 break;
3313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003314 }
3315 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003316
3317 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003318 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003320 }
3321
3322 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003323 * Receives notifications when applications are added/removed.
3324 */
3325 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3326 @Override
3327 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003328 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003329 String reason = intent.getStringExtra("reason");
3330 if (!"homekey".equals(reason)) {
3331 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003332 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003333 animate = false;
3334 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003335 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003336 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003337 }
3338 }
3339
3340 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003341 * Receives notifications whenever the appwidgets are reset.
3342 */
3343 private class AppWidgetResetObserver extends ContentObserver {
3344 public AppWidgetResetObserver() {
3345 super(new Handler());
3346 }
3347
3348 @Override
3349 public void onChange(boolean selfChange) {
3350 onAppWidgetReset();
3351 }
3352 }
3353
3354 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003355 * If the activity is currently paused, signal that we need to re-run the loader
3356 * in onResume.
3357 *
3358 * This needs to be called from incoming places where resources might have been loaded
3359 * while we are paused. That is becaues the Configuration might be wrong
3360 * when we're not running, and if it comes back to what it was when we
3361 * were paused, we are not restarted.
3362 *
3363 * Implementation of the method from LauncherModel.Callbacks.
3364 *
3365 * @return true if we are currently paused. The caller might be able to
3366 * skip some work in that case since we will come back again.
3367 */
3368 public boolean setLoadOnResume() {
3369 if (mPaused) {
3370 Log.i(TAG, "setLoadOnResume");
3371 mOnResumeNeedsLoad = true;
3372 return true;
3373 } else {
3374 return false;
3375 }
3376 }
3377
3378 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003379 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003380 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003382 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003383 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003384 } else {
3385 return SCREEN_COUNT / 2;
3386 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003388
Winson Chunga12a2502010-12-20 14:41:35 -08003389 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003390 mAllAppsPagedView = view;
3391 }
3392
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 /**
3394 * Refreshes the shortcuts shown on the workspace.
3395 *
3396 * Implementation of the method from LauncherModel.Callbacks.
3397 */
3398 public void startBinding() {
3399 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003400
3401 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 int count = workspace.getChildCount();
3403 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003404 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003405 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3406 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003408
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 if (DEBUG_USER_INTERFACE) {
3410 android.widget.Button finishButton = new android.widget.Button(this);
3411 finishButton.setText("Finish");
3412 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3413
3414 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3415 public void onClick(View v) {
3416 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003419 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003420
3421 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3422 // mDesktopItems -> AppWidgetInfo -> hostView).
3423 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003424 }
3425
3426 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 * Bind the items start-end from the list.
3428 *
3429 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003430 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3432
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003433 setLoadOnResume();
3434
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 final Workspace workspace = mWorkspace;
3436
3437 for (int i=start; i<end; i++) {
3438 final ItemInfo item = shortcuts.get(i);
3439 mDesktopItems.add(item);
3440 switch (item.itemType) {
3441 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3442 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003443 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3445 false);
3446 break;
3447 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3448 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003449 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003450 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3452 false);
3453 break;
3454 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3455 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3456 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003457 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 (LiveFolderInfo) item);
3459 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3460 false);
3461 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003463 }
3464
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003466 }
3467
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 /**
3469 * Implementation of the method from LauncherModel.Callbacks.
3470 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003471 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003472 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003473 sFolders.clear();
3474 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476
3477 /**
3478 * Add the views for a widget to the workspace.
3479 *
3480 * Implementation of the method from LauncherModel.Callbacks.
3481 */
3482 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003483 setLoadOnResume();
3484
Daniel Sandler843e8602010-06-07 14:59:01 -04003485 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3486 if (DEBUG_WIDGETS) {
3487 Log.d(TAG, "bindAppWidget: " + item);
3488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 final Workspace workspace = mWorkspace;
3490
3491 final int appWidgetId = item.appWidgetId;
3492 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003493 if (DEBUG_WIDGETS) {
3494 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3495 }
3496
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3498
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3500 item.hostView.setTag(item);
3501
3502 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3503 item.cellY, item.spanX, item.spanY, false);
3504
Adam Cohended9f8d2010-11-03 13:25:16 -07003505 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3506
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 workspace.requestLayout();
3508
3509 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003510
3511 if (DEBUG_WIDGETS) {
3512 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3513 + (SystemClock.uptimeMillis()-start) + "ms");
3514 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 }
3516
3517 /**
3518 * Callback saying that there aren't any more items to bind.
3519 *
3520 * Implementation of the method from LauncherModel.Callbacks.
3521 */
3522 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003523 setLoadOnResume();
3524
Joe Onorato9c1289c2009-08-17 11:03:03 -04003525 if (mSavedState != null) {
3526 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003527 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 }
3529
3530 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3531 if (userFolders != null) {
3532 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003533 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 if (info != null) {
3535 openFolder(info);
3536 }
3537 }
3538 final Folder openFolder = mWorkspace.getOpenFolder();
3539 if (openFolder != null) {
3540 openFolder.requestFocus();
3541 }
3542 }
3543
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 mSavedState = null;
3545 }
3546
3547 if (mSavedInstanceState != null) {
3548 super.onRestoreInstanceState(mSavedInstanceState);
3549 mSavedInstanceState = null;
3550 }
3551
Winson Chung32bb5e52011-01-31 14:51:56 -08003552 // Workaround a bug that occurs when rotating the device while the customization mode is
3553 // open, we trigger a new layout on all the CellLayout children.
3554 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3555 final int childCount = mWorkspace.getChildCount();
3556 for (int i = 0; i < childCount; ++i) {
3557 mWorkspace.getChildAt(i).requestLayout();
3558 }
3559 }
3560
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003562
3563 // If we received the result of any pending adds while the loader was running (e.g. the
3564 // widget configuration forced an orientation change), process them now.
3565 for (int i = 0; i < sPendingAddList.size(); i++) {
3566 completeAdd(sPendingAddList.get(i));
3567 }
3568 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 }
3570
3571 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003572 * Updates the icons on the launcher that are affected by changes to the package list
3573 * on the device.
3574 */
3575 private void updateIconsAffectedByPackageManagerChanges() {
3576 updateAppMarketIcon();
3577 updateGlobalSearchIcon();
3578 updateVoiceSearchIcon();
3579 }
3580
3581 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 * Add the icons for all apps.
3583 *
3584 * Implementation of the method from LauncherModel.Callbacks.
3585 */
3586 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003587 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003588 if (mCustomizePagedView != null) {
3589 mCustomizePagedView.setApps(apps);
3590 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003591 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 }
3593
3594 /**
3595 * A package was installed.
3596 *
3597 * Implementation of the method from LauncherModel.Callbacks.
3598 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003599 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003600 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003602 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003603 if (mCustomizePagedView != null) {
3604 mCustomizePagedView.addApps(apps);
3605 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003606 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 }
3608
3609 /**
3610 * A package was updated.
3611 *
3612 * Implementation of the method from LauncherModel.Callbacks.
3613 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003614 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003615 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003617 if (mWorkspace != null) {
3618 mWorkspace.updateShortcuts(apps);
3619 }
3620 if (mAllAppsGrid != null) {
3621 mAllAppsGrid.updateApps(apps);
3622 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003623 if (mCustomizePagedView != null) {
3624 mCustomizePagedView.updateApps(apps);
3625 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003626 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 }
3628
3629 /**
3630 * A package was uninstalled.
3631 *
3632 * Implementation of the method from LauncherModel.Callbacks.
3633 */
Joe Onorato36115782010-06-17 13:28:48 -04003634 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003635 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003636 if (permanent) {
3637 mWorkspace.removeItems(apps);
3638 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003639 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003640 if (mCustomizePagedView != null) {
3641 mCustomizePagedView.removeApps(apps);
3642 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003643 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003644 }
Joe Onoratobe386092009-11-17 17:32:16 -08003645
3646 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003647 * A number of packages were updated.
3648 */
3649 public void bindPackagesUpdated() {
3650 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003651 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003652 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003653 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003654 }
3655
Winson Chung400438b2011-01-16 17:53:48 -08003656 private int mapConfigurationOriActivityInfoOri(int configOri) {
3657 final Display d = getWindowManager().getDefaultDisplay();
3658 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3659 switch (d.getRotation()) {
3660 case Surface.ROTATION_0:
3661 case Surface.ROTATION_180:
3662 // We are currently in the same basic orientation as the natural orientation
3663 naturalOri = configOri;
3664 break;
3665 case Surface.ROTATION_90:
3666 case Surface.ROTATION_270:
3667 // We are currently in the other basic orientation to the natural orientation
3668 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3669 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3670 break;
3671 }
3672
3673 int[] oriMap = {
3674 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3675 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3676 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3677 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3678 };
3679 // Since the map starts at portrait, we need to offset if this device's natural orientation
3680 // is landscape.
3681 int indexOffset = 0;
3682 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3683 indexOffset = 1;
3684 }
3685 return oriMap[(d.getRotation() + indexOffset) % 4];
3686 }
3687 public void lockScreenOrientation() {
3688 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3689 .getConfiguration().orientation));
3690 }
3691 public void unlockScreenOrientation() {
3692 mHandler.postDelayed(new Runnable() {
3693 public void run() {
3694 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3695 }
3696 }, mRestoreScreenOrientationDelay);
3697 }
3698
Winson Chung80baf5a2010-08-09 16:03:15 -07003699 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003700 * Prints out out state for debugging.
3701 */
3702 public void dumpState() {
3703 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003704 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003705 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3706 Log.d(TAG, "mRestoring=" + mRestoring);
3707 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3708 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3709 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003710 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003711 mModel.dumpState();
3712 mAllAppsGrid.dumpState();
3713 Log.d(TAG, "END launcher2 dump state");
3714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003715}
Michael Jurka2763be32011-02-24 11:19:57 -08003716
Michael Jurkaabded662011-03-04 12:06:57 -08003717interface LauncherTransitionable {
3718 void onLauncherTransitionStart(Animator animation);
3719 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003720}