blob: 4981cb4d474e50d5beeb040852330568f15ea0c8 [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;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001986
Michael Jurka8c920dd2011-01-20 14:16:56 -08001987 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001989 CellLayout cl = (CellLayout) parent;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001990 if (!(folder instanceof UserFolder)) {
1991 // User folders will remove themselves
1992 cl.removeViewWithoutMarkingCells(folder);
1993 }
Joe Onoratob6341e92009-11-02 10:41:48 -05001994 if (folder instanceof DropTarget) {
1995 // Live folders aren't DropTargets.
1996 mDragController.removeDropTarget((DropTarget)folder);
1997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001999 if (folder instanceof UserFolder) {
2000 UserFolder uf = (UserFolder) folder;
2001 uf.animateClosed();
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 folder.onClose();
2004 }
2005
2006 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002007 * Re-listen when widgets are reset.
2008 */
2009 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002010 if (mAppWidgetHost != null) {
2011 mAppWidgetHost.startListening();
2012 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002013 }
2014
2015 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002016 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2017 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002019 private void unbindDesktopItems() {
2020 for (ItemInfo item: mDesktopItems) {
2021 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002023 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 /**
2027 * Launches the intent referred by the clicked shortcut.
2028 *
2029 * @param v The view representing the clicked shortcut.
2030 */
2031 public void onClick(View v) {
2032 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002033 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002035 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002036 int[] pos = new int[2];
2037 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002038 intent.setSourceBounds(new Rect(pos[0], pos[1],
2039 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002040 boolean success = startActivitySafely(intent, tag);
2041
2042 if (success && v instanceof BubbleTextView) {
2043 mWaitingForResume = (BubbleTextView) v;
2044 mWaitingForResume.setStayPressed(true);
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 } else if (tag instanceof FolderInfo) {
2047 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002048 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002049 if (mState == State.ALL_APPS) {
2050 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002051 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002052 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055 }
2056
Michael Jurka0e260592010-06-30 17:07:39 -07002057 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002058 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002059 // clicking anywhere on the workspace causes the customization drawer to slide down
2060 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002061 return false;
2062 }
2063
Michael Jurkaaf442092010-06-10 17:01:57 -07002064 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002065 * Event handler for the search button
2066 *
2067 * @param v The view that was clicked.
2068 */
2069 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002070 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002071 // Use a custom animation for launching search
2072 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002073 }
2074
2075 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002076 * Event handler for the voice button
2077 *
2078 * @param v The view that was clicked.
2079 */
2080 public void onClickVoiceButton(View v) {
2081 startVoiceSearch();
2082 }
2083
2084 private void startVoiceSearch() {
2085 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2086 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2087 startActivity(intent);
2088 }
2089
2090 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002091 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002092 * enters home screen customization mode.
2093 *
2094 * @param v The view that was clicked.
2095 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002096 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002097 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002098 }
2099
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002100 /**
2101 * Event handler for the "grid" button that appears on the home screen, which
2102 * enters all apps mode.
2103 *
2104 * @param v The view that was clicked.
2105 */
2106 public void onClickAllAppsButton(View v) {
2107 showAllApps(true);
2108 }
2109
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002110 public void onClickAppMarketButton(View v) {
2111 if (mAppMarketIntent != null) {
2112 startActivitySafely(mAppMarketIntent, "app market");
2113 }
2114 }
2115
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002116 void startApplicationDetailsActivity(ComponentName componentName) {
2117 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002118 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2119 Uri.fromParts("package", packageName, null));
2120 startActivity(intent);
2121 }
2122
Patrick Dubroy5539af72010-09-07 15:22:01 -07002123 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2124 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2125 // System applications cannot be installed. For now, show a toast explaining that.
2126 // We may give them the option of disabling apps this way.
2127 int messageId = R.string.uninstall_system_app_text;
2128 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2129 } else {
2130 String packageName = appInfo.componentName.getPackageName();
2131 String className = appInfo.componentName.getClassName();
2132 Intent intent = new Intent(
2133 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2134 startActivity(intent);
2135 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002136 }
2137
Michael Jurkaddd62e92011-02-16 17:49:14 -08002138 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2140 try {
2141 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002142 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 } catch (ActivityNotFoundException e) {
2144 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002145 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 } catch (SecurityException e) {
2147 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002148 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002150 "or use the exported attribute for this activity. "
2151 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002153 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002155
Romain Guy8e633c52010-03-04 12:51:36 -08002156 void startActivityForResultSafely(Intent intent, int requestCode) {
2157 try {
2158 startActivityForResult(intent, requestCode);
2159 } catch (ActivityNotFoundException e) {
2160 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2161 } catch (SecurityException e) {
2162 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2163 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2164 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2165 "or use the exported attribute for this activity.", e);
2166 }
2167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168
2169 private void handleFolderClick(FolderInfo folderInfo) {
2170 if (!folderInfo.opened) {
2171 // Close any open folder
2172 closeFolder();
2173 // Open the requested folder
2174 openFolder(folderInfo);
2175 } else {
2176 // Find the open folder...
2177 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2178 int folderScreen;
2179 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002180 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 // .. and close it
2182 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002183 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 // Close any folder open on the current screen
2185 closeFolder();
2186 // Pull the folder onto this screen
2187 openFolder(folderInfo);
2188 }
2189 }
2190 }
2191 }
2192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002194 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 * is animated relative to the specified View. If the View is null, no animation
2196 * is played.
2197 *
2198 * @param folderInfo The FolderInfo describing the folder to open.
2199 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002200 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 Folder openFolder;
2202
2203 if (folderInfo instanceof UserFolderInfo) {
2204 openFolder = UserFolder.fromXml(this);
2205 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002206 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 } else {
2208 return;
2209 }
2210
Joe Onorato00acb122009-08-04 16:04:30 -04002211 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 openFolder.setLauncher(this);
2213
2214 openFolder.bind(folderInfo);
2215 folderInfo.opened = true;
2216
Winson Chungaafa03c2010-06-11 17:34:16 -07002217 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002218 if (openFolder instanceof UserFolder) {
2219 UserFolder uf = (UserFolder) openFolder;
2220 uf.animateOpen();
2221 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 openFolder.onOpen();
2224 }
2225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002227 if (mState != State.WORKSPACE) {
2228 return false;
2229 }
2230
Romain Guy1fbc1c82009-11-09 20:43:08 -08002231 switch (v.getId()) {
2232 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002233 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002234 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2235 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002236 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002237 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002238 return true;
2239 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002240 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002241 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2242 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002243 showPreviews(v);
2244 }
2245 return true;
2246 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002247 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002248 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2249 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2250 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002251 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002252 return true;
2253 }
2254
Joe Onorato9c1289c2009-08-17 11:03:03 -04002255 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 return false;
2257 }
2258
2259 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002260 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 }
2262
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 resetAddInfo();
2264 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002266 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 return true;
2268 }
2269
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 final View itemUnderLongClick = longClickCellInfo.cell;
2271
Winson Chung304dcde2011-01-07 11:17:23 -08002272 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002273 if (itemUnderLongClick == null) {
2274 // User long pressed on empty space
2275 mWorkspace.setAllowLongPress(false);
2276 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2277 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002278 if (LauncherApplication.isScreenXLarge()) {
2279 addItems();
2280 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 }
2283 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002286 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2287 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002288 mAddIntersectCellX = longClickCellInfo.cellX;
2289 mAddIntersectCellY = longClickCellInfo.cellY;
2290 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 }
2292 }
2293 }
2294 return true;
2295 }
2296
Romain Guye6b8e2f2009-11-10 11:56:55 -08002297 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002298 private void dismissPreview(final View v) {
2299 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002300 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002301 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2302 public void onDismiss() {
2303 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2304 int count = group.getChildCount();
2305 for (int i = 0; i < count; i++) {
2306 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2307 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002308 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2309 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2310
2311 v.setTag(R.id.workspace, null);
2312 v.setTag(R.id.icon, null);
2313 window.setOnDismissListener(null);
2314 }
2315 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002316 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002317 }
2318 v.setTag(null);
2319 }
2320
Romain Guyf8e6a802009-12-07 17:48:02 -08002321 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002322 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002323 }
2324
Romain Guya6abce82009-11-10 02:54:41 -08002325 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002326 final Resources resources = getResources();
2327 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002328
Romain Guya6abce82009-11-10 02:54:41 -08002329 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002330
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002332
Romain Guyf8e6a802009-12-07 17:48:02 -08002333 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002334 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002335 int extraW = (int) ((r.left + r.right) * max);
2336 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002337
2338 int aW = cell.getWidth() - extraW;
2339 float w = aW / max;
2340
2341 int width = cell.getWidth();
2342 int height = cell.getHeight();
2343 int x = cell.getLeftPadding();
2344 int y = cell.getTopPadding();
2345 width -= (x + cell.getRightPadding());
2346 height -= (y + cell.getBottomPadding());
2347
2348 float scale = w / width;
2349
2350 int count = end - start;
2351
2352 final float sWidth = width * scale;
2353 float sHeight = height * scale;
2354
Romain Guye6b8e2f2009-11-10 11:56:55 -08002355 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002356
Romain Guye6b8e2f2009-11-10 11:56:55 -08002357 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2358 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002359
2360 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002361 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002362 cell = (CellLayout) workspace.getChildAt(i);
2363
Romain Guyf8e6a802009-12-07 17:48:02 -08002364 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002365 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002366
2367 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002368 c.scale(scale, scale);
2369 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002370 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002371
Romain Guy9d31e9f2009-11-11 18:54:28 -08002372 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002373 image.setImageBitmap(bitmap);
2374 image.setTag(i);
2375 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 image.setOnFocusChangeListener(handler);
2377 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002378 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002379
2380 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002381 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2382
Winson Chungaafa03c2010-06-11 17:34:16 -07002383 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002384 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002385
2386 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002387 p.setContentView(preview);
2388 p.setWidth((int) (sWidth * count + extraW));
2389 p.setHeight((int) (sHeight + extraH));
2390 p.setAnimationStyle(R.style.AnimationPreview);
2391 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002392 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002393 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002394 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002395
Romain Guye6b8e2f2009-11-10 11:56:55 -08002396 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2397 public void onDismiss() {
2398 dismissPreview(anchor);
2399 }
2400 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002401
2402 anchor.setTag(p);
2403 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002404 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002405 }
2406
Romain Guy9d31e9f2009-11-11 18:54:28 -08002407 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002408 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002409
Romain Guye6b8e2f2009-11-10 11:56:55 -08002410 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002411 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002412 }
2413
2414 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002415 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002416 v.post(this);
2417 }
2418
2419 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002420 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002421 }
2422
2423 public void onFocusChange(View v, boolean hasFocus) {
2424 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002425 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002426 }
Romain Guya6abce82009-11-10 02:54:41 -08002427 }
2428 }
2429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 Workspace getWorkspace() {
2431 return mWorkspace;
2432 }
2433
Michael Jurka3c38d562011-02-18 19:22:12 -08002434 TabHost getCustomizationDrawer() {
2435 return mHomeCustomizationDrawer;
2436 }
2437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 @Override
2439 protected Dialog onCreateDialog(int id) {
2440 switch (id) {
2441 case DIALOG_CREATE_SHORTCUT:
2442 return new CreateShortcut().createDialog();
2443 case DIALOG_RENAME_FOLDER:
2444 return new RenameFolder().createDialog();
2445 }
2446
2447 return super.onCreateDialog(id);
2448 }
2449
2450 @Override
2451 protected void onPrepareDialog(int id, Dialog dialog) {
2452 switch (id) {
2453 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 break;
2455 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002456 if (mFolderInfo != null) {
2457 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2458 final CharSequence text = mFolderInfo.title;
2459 input.setText(text);
2460 input.setSelection(0, text.length());
2461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 break;
2463 }
2464 }
2465
2466 void showRenameDialog(FolderInfo info) {
2467 mFolderInfo = info;
2468 mWaitingForResult = true;
2469 showDialog(DIALOG_RENAME_FOLDER);
2470 }
2471
Michael Jurka0280c3b2010-09-17 15:00:07 -07002472 private void showAddDialog(int intersectX, int intersectY) {
2473 resetAddInfo();
2474 mAddIntersectCellX = intersectX;
2475 mAddIntersectCellY = intersectY;
2476 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 mWaitingForResult = true;
2478 showDialog(DIALOG_CREATE_SHORTCUT);
2479 }
2480
Joe Onoratodeb98af2010-02-19 14:59:39 -08002481 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002482 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002483 Bundle bundle = new Bundle();
2484
2485 ArrayList<String> shortcutNames = new ArrayList<String>();
2486 shortcutNames.add(getString(R.string.group_applications));
2487 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2488
2489 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2490 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2491 R.drawable.ic_launcher_application));
2492 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2493
2494 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2495 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002496 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002497 pickIntent.putExtras(bundle);
2498
Joe Onoratodeb98af2010-02-19 14:59:39 -08002499 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002500 }
2501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 private class RenameFolder {
2503 private EditText mInput;
2504
2505 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2507 mInput = (EditText) layout.findViewById(R.id.folder_name);
2508
2509 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2510 builder.setIcon(0);
2511 builder.setTitle(getString(R.string.rename_folder_title));
2512 builder.setCancelable(true);
2513 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2514 public void onCancel(DialogInterface dialog) {
2515 cleanup();
2516 }
2517 });
2518 builder.setNegativeButton(getString(R.string.cancel_action),
2519 new Dialog.OnClickListener() {
2520 public void onClick(DialogInterface dialog, int which) {
2521 cleanup();
2522 }
2523 }
2524 );
2525 builder.setPositiveButton(getString(R.string.rename_action),
2526 new Dialog.OnClickListener() {
2527 public void onClick(DialogInterface dialog, int which) {
2528 changeFolderName();
2529 }
2530 }
2531 );
2532 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002533
2534 final AlertDialog dialog = builder.create();
2535 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2536 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002537 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002538 mInput.requestFocus();
2539 InputMethodManager inputManager = (InputMethodManager)
2540 getSystemService(Context.INPUT_METHOD_SERVICE);
2541 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002542 }
2543 });
2544
2545 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 }
2547
2548 private void changeFolderName() {
2549 final String name = mInput.getText().toString();
2550 if (!TextUtils.isEmpty(name)) {
2551 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002552 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 mFolderInfo.title = name;
2554 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2555
Joe Onorato9c1289c2009-08-17 11:03:03 -04002556 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002557 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002558 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 } else {
2560 final FolderIcon folderIcon = (FolderIcon)
2561 mWorkspace.getViewForTag(mFolderInfo);
2562 if (folderIcon != null) {
2563 folderIcon.setText(name);
2564 getWorkspace().requestLayout();
2565 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002566 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002567 mWorkspaceLoading = true;
2568 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 }
2570 }
2571 }
2572 cleanup();
2573 }
2574
2575 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 dismissDialog(DIALOG_RENAME_FOLDER);
2577 mWaitingForResult = false;
2578 mFolderInfo = null;
2579 }
2580 }
2581
Daniel Sandler843e8602010-06-07 14:59:01 -04002582 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2583 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002584 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002585 }
2586
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002587 // AllAppsView.Watcher
2588 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002589 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2590 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002591 mWorkspace.setVisibility(View.GONE);
2592 }
2593 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594
Michael Jurkaafca9532011-02-16 14:50:35 -08002595 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 button.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 button.setClickable(true);
2598 }
2599
2600 private void hideToolbarButton(View button) {
2601 button.setAlpha(0.0f);
2602 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2603 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002604 }
2605
2606 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002607 button.setClickable(false);
2608 }
2609
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002611 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 *
2613 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2614 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002615 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002616 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002618 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 final boolean showing = show;
2620 final boolean hiding = !show;
2621
2622 final int duration = show ?
2623 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2624 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2625
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002627 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002628 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002629 anim.addUpdateListener(new AnimatorUpdateListener() {
2630 public void onAnimationUpdate(ValueAnimator animation) {
2631 view.setAlpha((Float) animation.getAnimatedValue());
2632 }
2633 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002634 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002635 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002636 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002637 if (showing) showAndEnableToolbarButton(view);
2638 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002640 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002641 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002644 });
2645 seq.play(anim);
2646 } else {
2647 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002648 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002649 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002651 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002652 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002654 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 }
2656
2657 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 * Show/hide the appropriate toolbar buttons for newState.
2659 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 *
2661 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002662 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2663 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002665 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 switch (newState) {
2667 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002668 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2669 mDeleteZone.setOverlappingViews(
2670 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002671 mDeleteZone.setDragAndDropEnabled(true);
2672 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 break;
2674 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002675 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002676 mDeleteZone.setDragAndDropEnabled(false);
2677 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 break;
2679 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002680 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002681 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 break;
2683 }
2684 }
2685
2686 /**
2687 * Helper method for the cameraZoomIn/cameraZoomOut animations
2688 * @param view The view being animated
2689 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2690 * @param scaleFactor The scale factor used for the zoom
2691 */
2692 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2693 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002694
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002695 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002696 // Set pivotY so that at the starting zoom factor, the view is partially
2697 // visible. Modifying initialHeightFactor changes how much of the view is
2698 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002699 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002700 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002701 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002702 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002703 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002704 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 }
2706 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002707
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002708 /**
2709 * Zoom the camera out from the workspace to reveal 'toView'.
2710 * Assumes that the view to show is anchored at either the very top or very bottom
2711 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002713 */
Winson Chung20f71112011-04-06 14:22:04 -07002714 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002715 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002716 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002717
2718 final int duration = toAllApps ?
2719 res.getInteger(R.integer.config_allAppsZoomInTime) :
2720 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002721 final int fadeDuration = toAllApps ?
2722 res.getInteger(R.integer.config_allAppsFadeInTime) :
2723 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002724
2725 final float scale = toAllApps ?
2726 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2727 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2728
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002729 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002730
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002731 setPivotsForZoom(toView, toState, scale);
2732
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002733 if (toAllApps) {
Winson Chung20f71112011-04-06 14:22:04 -07002734 if (!springLoaded) {
2735 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2736
2737 // Everytime we launch into AllApps, we reset the successful drop flag which
2738 // controls when it should hide/show the mini workspaces
2739 mAllAppsPagedView.resetSuccessfulDropFlag();
2740 } else {
2741 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
2742 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002743 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002744 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002745 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746
2747 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002748 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002749 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002750 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2751 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002752 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002753 toView.setFastScaleX(a * scale + b * 1f);
2754 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002755 }
2756 });
Adam Cohen61033d32010-11-15 18:29:44 -08002757
Adam Cohenf16e5712011-01-13 13:31:45 -08002758 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002759 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002760 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002761 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002762 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2763 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002764 // don't need to invalidate because we do so above
2765 toView.setFastAlpha(a * 0f + b * 1f);
2766 }
2767 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002768 alphaAnim.start();
2769 }
2770
Michael Jurkaabded662011-03-04 12:06:57 -08002771 if (toView instanceof LauncherTransitionable) {
2772 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002773 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002774 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002775 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002776 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002777 // Prepare the position
2778 toView.setTranslationX(0.0f);
2779 toView.setTranslationY(0.0f);
2780 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002781 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002782 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002783 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002784 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002785 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002786 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002787 // If we don't set the final scale values here, if this animation is cancelled
2788 // it will have the wrong scale value and subsequent cameraPan animations will
2789 // not fix that
2790 toView.setScaleX(1.0f);
2791 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002792 if (toView instanceof LauncherTransitionable) {
2793 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002794 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002795 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002796 });
2797
Chet Haaseb1254a62010-09-07 13:35:00 -07002798 AnimatorSet toolbarHideAnim = new AnimatorSet();
2799 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2801
2802 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002803 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002804
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002805 if (mStateAnimation != null) mStateAnimation.cancel();
2806 mStateAnimation = new AnimatorSet();
2807 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2808 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809
2810 // Show the new toolbar buttons just as the main animation is ending
2811 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002812 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2813 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 } else {
2815 toView.setTranslationX(0.0f);
2816 toView.setTranslationY(0.0f);
2817 toView.setScaleX(1.0f);
2818 toView.setScaleY(1.0f);
2819 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002820 if (toView instanceof LauncherTransitionable) {
2821 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2822 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2823 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 hideAndShowToolbarButtons(toState, null, null);
2825 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002826 }
2827
2828 /**
2829 * Zoom the camera back into the workspace, hiding 'fromView'.
2830 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002831 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2832 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002833 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002834 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002836 final boolean fromAllApps = (fromState == State.ALL_APPS);
2837
2838 int duration = fromAllApps ?
2839 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2840 res.getInteger(R.integer.config_customizeZoomOutTime);
2841
Michael Jurka742574b2011-02-02 23:51:01 -08002842 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002843 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2844 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2845
2846 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002847
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002848 mCustomizePagedView.endChoiceMode();
2849 mAllAppsPagedView.endChoiceMode();
2850
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002851 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002852
Michael Jurkad3ef3062010-11-23 16:23:58 -08002853 if (!springLoaded) {
2854 mWorkspace.unshrink(animated);
2855 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002856 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002857 if (mStateAnimation != null) mStateAnimation.cancel();
2858 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002859
Michael Jurka742574b2011-02-02 23:51:01 -08002860 final float oldScaleX = fromView.getScaleX();
2861 final float oldScaleY = fromView.getScaleY();
2862
2863 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2864 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002865 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2866 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002867 ((View)fromView.getParent()).fastInvalidate();
2868 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2869 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2870 }
2871 });
Michael Jurkaabded662011-03-04 12:06:57 -08002872 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002873 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002874 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002875 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2876 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002877 // don't need to invalidate because we do so above
2878 fromView.setFastAlpha(a * 1f + b * 0f);
2879 }
2880 });
Michael Jurkaabded662011-03-04 12:06:57 -08002881 if (fromView instanceof LauncherTransitionable) {
2882 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002883 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002884 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002885 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002886 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002887 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002888 if (fromView instanceof LauncherTransitionable) {
2889 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002890 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002891 }
2892 });
2893
Chet Haaseb1254a62010-09-07 13:35:00 -07002894 AnimatorSet toolbarHideAnim = new AnimatorSet();
2895 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002896 if (!springLoaded) {
2897 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2898 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002899
Adam Cohen61033d32010-11-15 18:29:44 -08002900 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002901
2902 // Show the new toolbar buttons at the very end of the whole animation
2903 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2904 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002905 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2906 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002907 } else {
2908 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002909 if (fromView instanceof LauncherTransitionable) {
2910 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2911 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2912 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002913 if (!springLoaded) {
2914 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2915 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002917 }
2918
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002919 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002920 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002921 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002922 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002923
Michael Jurka79212d82010-07-30 16:36:20 -07002924 if (LauncherApplication.isScreenXLarge()) {
Winson Chung20f71112011-04-06 14:22:04 -07002925 cameraZoomOut(State.ALL_APPS, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002926 } else {
2927 mAllAppsGrid.zoom(1.0f, animated);
2928 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002929
Romain Guyc16fea72010-03-12 17:17:56 -08002930 ((View) mAllAppsGrid).setFocusable(true);
2931 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002932
Joe Onorato7c312c12009-08-13 21:36:53 -07002933 // TODO: fade these two too
2934 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002935
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002936 // Change the state *after* we've called all the transition code
2937 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002938
Winson Chung5fb63472011-02-02 17:03:37 -08002939 // Pause the auto-advance of widgets until we are out of AllApps
2940 mUserPresent = false;
2941 updateRunning();
2942
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002943 // send an accessibility event to announce the context change
2944 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2945 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002946
2947 void showWorkspace(boolean animated) {
2948 showWorkspace(animated, null);
2949 }
2950
2951 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002952 if (layout != null) {
2953 // always animated, but that's ok since we never specify a layout and
2954 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002955 mWorkspace.unshrink(layout);
2956 } else {
2957 mWorkspace.unshrink(animated);
2958 }
2959 if (mState == State.ALL_APPS) {
2960 closeAllApps(animated);
2961 } else if (mState == State.CUSTOMIZE) {
2962 hideCustomizationDrawer(animated);
2963 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002964
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002965 // Change the state *after* we've called all the transition code
2966 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002967
Winson Chung5fb63472011-02-02 17:03:37 -08002968 // Resume the auto-advance of widgets
2969 mUserPresent = true;
2970 updateRunning();
2971
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002972 // send an accessibility event to announce the context change
2973 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002974 }
2975
Michael Jurkad3ef3062010-11-23 16:23:58 -08002976 void enterSpringLoadedDragMode(CellLayout layout) {
2977 mWorkspace.enterSpringLoadedDragMode(layout);
2978 if (mState == State.ALL_APPS) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002979 mState = State.ALL_APPS_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002980 cameraZoomIn(State.ALL_APPS, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002981 } else if (mState == State.CUSTOMIZE) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002982 mState = State.CUSTOMIZE_SPRING_LOADED;
Winson Chung20f71112011-04-06 14:22:04 -07002983 cameraZoomIn(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002984 }/* else {
2985 // we're already in spring loaded mode; don't do anything
2986 }*/
2987 }
2988
2989 void exitSpringLoadedDragMode() {
2990 if (mState == State.ALL_APPS_SPRING_LOADED) {
2991 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chung20f71112011-04-06 14:22:04 -07002992 cameraZoomOut(State.ALL_APPS, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002993 mState = State.ALL_APPS;
2994 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2995 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
Winson Chung20f71112011-04-06 14:22:04 -07002996 cameraZoomOut(State.CUSTOMIZE, true, true);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002997 mState = State.CUSTOMIZE;
2998 }/* else {
2999 // we're not in spring loaded mode; don't do anything
3000 }*/
3001 }
3002
Joe Onoratob2061212009-11-24 16:13:54 -05003003 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003004 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003005 * - Home from workspace
3006 * - from center screen
3007 * - from other screens
3008 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003009 * - from center screen
3010 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003011 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003012 * - from center screen
3013 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003014 * - Launch app from workspace and quit
3015 * - with back
3016 * - with home
3017 * - Launch app from all apps and quit
3018 * - with back
3019 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003020 * - Go to a screen that's not the default, then all
3021 * apps, and launch and app, and go back
3022 * - with back
3023 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003024 * - On workspace, long press power and go back
3025 * - with back
3026 * - with home
3027 * - On all apps, long press power and go back
3028 * - with back
3029 * - with home
3030 * - On workspace, power off
3031 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003032 * - Launch an app and turn off the screen while in that app
3033 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003034 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003035 * - From all apps
3036 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003037 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3038 * - From all apps
3039 * - From the center workspace
3040 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003041 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003042 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003043 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003044 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003045 if (LauncherApplication.isScreenXLarge()) {
Winson Chung20f71112011-04-06 14:22:04 -07003046 cameraZoomIn(State.ALL_APPS, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003047 } else {
3048 mAllAppsGrid.zoom(0.0f, animated);
3049 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003050 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003051 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003052 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003053 }
3054
Joe Onorato7c312c12009-08-13 21:36:53 -07003055 void lockAllApps() {
3056 // TODO
3057 }
3058
3059 void unlockAllApps() {
3060 // TODO
3061 }
3062
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003063 // Show the customization drawer (only exists in x-large configuration)
3064 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003065 if (mState != State.WORKSPACE) {
3066 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003067 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003068
Winson Chung20f71112011-04-06 14:22:04 -07003069 cameraZoomOut(State.CUSTOMIZE, animated, false);
Michael Jurkaafca9532011-02-16 14:50:35 -08003070
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003071 // Change the state *after* we've called all the transition code
3072 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003073
3074 // Pause the auto-advance of widgets until we are out of Customization drawer
3075 mUserPresent = false;
3076 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003077 }
3078
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003079 // Hide the customization drawer (only exists in x-large configuration)
3080 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003081 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Winson Chung20f71112011-04-06 14:22:04 -07003082 cameraZoomIn(State.CUSTOMIZE, animated, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003083 }
3084 }
3085
Patrick Dubroy5f445422011-02-18 14:35:21 -08003086 /**
3087 * Add an item from all apps or customize onto the given workspace screen.
3088 * If layout is null, add to the current screen.
3089 */
3090 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003091 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3092 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003093 } else {
3094 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003095 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003096 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003097
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003098 void onWorkspaceClick(CellLayout layout) {
3099 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003100 }
3101
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003102 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003103 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003104 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003105 // Look for the toolbar icon specified in the activity meta-data
3106 Bundle metaData = packageManager.getActivityInfo(
3107 activityName, PackageManager.GET_META_DATA).metaData;
3108 if (metaData != null) {
3109 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3110 if (iconResId != 0) {
3111 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003112 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003113 }
3114 }
3115 } catch (NameNotFoundException e) {
3116 // Do nothing
3117 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003118 return null;
3119 }
3120
3121 // if successful in getting icon, return it; otherwise, set button to use default drawable
3122 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3123 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3124 TextView button = (TextView) findViewById(buttonId);
3125 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3126
3127 // If we were unable to find the icon via the meta-data, use a generic one
3128 if (toolbarIcon == null) {
3129 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3130 return null;
3131 } else {
3132 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3133 return toolbarIcon.getConstantState();
3134 }
3135 }
3136
3137 // if successful in getting icon, return it; otherwise, set button to use default drawable
3138 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3139 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3140 ImageView button = (ImageView) findViewById(buttonId);
3141 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3142
Michael Jurka0423dcf2010-10-05 14:56:18 -07003143 // If we were unable to find the icon via the meta-data, use a generic one
3144 if (toolbarIcon == null) {
3145 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003146 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147 } else {
3148 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003149 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003150 }
3151 }
3152
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003153 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3154 TextView button = (TextView) findViewById(buttonId);
3155 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3156 }
3157
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003158 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003159 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003160 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003161 }
3162
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 private void updateGlobalSearchIcon() {
3164 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003165 final View searchButton = findViewById(R.id.search_button);
3166 final View searchDivider = findViewById(R.id.search_divider);
3167
Michael Jurka0423dcf2010-10-05 14:56:18 -07003168 final SearchManager searchManager =
3169 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3170 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003171 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003172 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003173 R.id.search_button, activityName, R.drawable.ic_generic_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003174 searchButton.setVisibility(View.VISIBLE);
3175 searchDivider.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003176 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003177 searchButton.setVisibility(View.GONE);
3178 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003179 }
3180 }
3181 }
3182
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003183 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003184 updateButtonWithDrawable(R.id.search_button, d);
3185 }
3186
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003187 private void updateVoiceSearchIcon() {
3188 if (LauncherApplication.isScreenXLarge()) {
Winson Chung97d85d22011-04-13 11:27:36 -07003189 final View searchDivider = findViewById(R.id.search_divider);
3190 final View voiceButton = findViewById(R.id.voice_button);
3191
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003192 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3193 ComponentName activityName = intent.resolveActivity(getPackageManager());
3194 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003195 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003196 R.id.voice_button, activityName, R.drawable.ic_voice_search);
Winson Chung97d85d22011-04-13 11:27:36 -07003197 searchDivider.setVisibility(View.VISIBLE);
3198 voiceButton.setVisibility(View.VISIBLE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003199 } else {
Winson Chung97d85d22011-04-13 11:27:36 -07003200 searchDivider.setVisibility(View.GONE);
3201 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003202 }
3203 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003204 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003205
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003206 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003207 updateButtonWithDrawable(R.id.voice_button, d);
3208 }
3209
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003210 /**
3211 * Sets the app market icon (shown when all apps is visible on x-large screens)
3212 */
3213 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003214 if (LauncherApplication.isScreenXLarge()) {
3215 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3216 // Find the app market activity by resolving an intent.
3217 // (If multiple app markets are installed, it will return the ResolverActivity.)
3218 ComponentName activityName = intent.resolveActivity(getPackageManager());
3219 if (activityName != null) {
3220 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003221 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003222 R.id.market_button, activityName, R.drawable.app_market_generic);
3223 }
3224 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003225 }
3226
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003227 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003228 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003229 }
3230
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003231 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 * Displays the shortcut creation dialog and launches, if necessary, the
3233 * appropriate activity.
3234 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003235 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003236 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3237 DialogInterface.OnShowListener {
3238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3245 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003246 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248 builder.setInverseBackgroundForced(true);
3249
3250 AlertDialog dialog = builder.create();
3251 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003252 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003253 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 return dialog;
3256 }
3257
3258 public void onCancel(DialogInterface dialog) {
3259 mWaitingForResult = false;
3260 cleanup();
3261 }
3262
Romain Guycbb89e42009-06-08 15:52:54 -07003263 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003264 }
3265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003266 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003267 try {
3268 dismissDialog(DIALOG_CREATE_SHORTCUT);
3269 } catch (Exception e) {
3270 // An exception is thrown if the dialog is not visible, which is fine
3271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003272 }
3273
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003274 /**
3275 * Handle the action clicked in the "Add to home" dialog.
3276 */
3277 public void onClick(DialogInterface dialog, int which) {
3278 Resources res = getResources();
3279 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003280
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003281 switch (which) {
3282 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003283 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003284 break;
3285 }
Romain Guycbb89e42009-06-08 15:52:54 -07003286
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003287 case AddAdapter.ITEM_APPWIDGET: {
3288 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3291 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003292 // start the pick activity
3293 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3294 break;
3295 }
Romain Guycbb89e42009-06-08 15:52:54 -07003296
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003297 case AddAdapter.ITEM_LIVE_FOLDER: {
3298 // Insert extra item to handle inserting folder
3299 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003300
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003301 ArrayList<String> shortcutNames = new ArrayList<String>();
3302 shortcutNames.add(res.getString(R.string.group_folder));
3303 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003304
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003305 ArrayList<ShortcutIconResource> shortcutIcons =
3306 new ArrayList<ShortcutIconResource>();
3307 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3308 R.drawable.ic_launcher_folder));
3309 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3310
3311 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3312 pickIntent.putExtra(Intent.EXTRA_INTENT,
3313 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3314 pickIntent.putExtra(Intent.EXTRA_TITLE,
3315 getText(R.string.title_select_live_folder));
3316 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003317
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003318 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3319 break;
3320 }
3321
3322 case AddAdapter.ITEM_WALLPAPER: {
3323 startWallpaper();
3324 break;
3325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003326 }
3327 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003328
3329 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003330 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003332 }
3333
3334 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003335 * Receives notifications when applications are added/removed.
3336 */
3337 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3338 @Override
3339 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003340 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003341 String reason = intent.getStringExtra("reason");
3342 if (!"homekey".equals(reason)) {
3343 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003344 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003345 animate = false;
3346 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003347 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003348 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003349 }
3350 }
3351
3352 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003353 * Receives notifications whenever the appwidgets are reset.
3354 */
3355 private class AppWidgetResetObserver extends ContentObserver {
3356 public AppWidgetResetObserver() {
3357 super(new Handler());
3358 }
3359
3360 @Override
3361 public void onChange(boolean selfChange) {
3362 onAppWidgetReset();
3363 }
3364 }
3365
3366 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003367 * If the activity is currently paused, signal that we need to re-run the loader
3368 * in onResume.
3369 *
3370 * This needs to be called from incoming places where resources might have been loaded
3371 * while we are paused. That is becaues the Configuration might be wrong
3372 * when we're not running, and if it comes back to what it was when we
3373 * were paused, we are not restarted.
3374 *
3375 * Implementation of the method from LauncherModel.Callbacks.
3376 *
3377 * @return true if we are currently paused. The caller might be able to
3378 * skip some work in that case since we will come back again.
3379 */
3380 public boolean setLoadOnResume() {
3381 if (mPaused) {
3382 Log.i(TAG, "setLoadOnResume");
3383 mOnResumeNeedsLoad = true;
3384 return true;
3385 } else {
3386 return false;
3387 }
3388 }
3389
3390 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003392 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003394 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003395 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003396 } else {
3397 return SCREEN_COUNT / 2;
3398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003400
Winson Chunga12a2502010-12-20 14:41:35 -08003401 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003402 mAllAppsPagedView = view;
3403 }
3404
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 /**
3406 * Refreshes the shortcuts shown on the workspace.
3407 *
3408 * Implementation of the method from LauncherModel.Callbacks.
3409 */
3410 public void startBinding() {
3411 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003412
3413 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 int count = workspace.getChildCount();
3415 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003416 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003417 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3418 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003420
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 if (DEBUG_USER_INTERFACE) {
3422 android.widget.Button finishButton = new android.widget.Button(this);
3423 finishButton.setText("Finish");
3424 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3425
3426 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3427 public void onClick(View v) {
3428 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003429 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003431 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003432
3433 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3434 // mDesktopItems -> AppWidgetInfo -> hostView).
3435 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003436 }
3437
3438 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 * Bind the items start-end from the list.
3440 *
3441 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003442 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3444
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003445 setLoadOnResume();
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 final Workspace workspace = mWorkspace;
3448
3449 for (int i=start; i<end; i++) {
3450 final ItemInfo item = shortcuts.get(i);
3451 mDesktopItems.add(item);
3452 switch (item.itemType) {
3453 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3454 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003455 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3457 false);
3458 break;
3459 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3460 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003461 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003462 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3464 false);
3465 break;
3466 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3467 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3468 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003469 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 (LiveFolderInfo) item);
3471 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3472 false);
3473 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003475 }
3476
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478 }
3479
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 /**
3481 * Implementation of the method from LauncherModel.Callbacks.
3482 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003483 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003484 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003485 sFolders.clear();
3486 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003487 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488
3489 /**
3490 * Add the views for a widget to the workspace.
3491 *
3492 * Implementation of the method from LauncherModel.Callbacks.
3493 */
3494 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003495 setLoadOnResume();
3496
Daniel Sandler843e8602010-06-07 14:59:01 -04003497 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3498 if (DEBUG_WIDGETS) {
3499 Log.d(TAG, "bindAppWidget: " + item);
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 final Workspace workspace = mWorkspace;
3502
3503 final int appWidgetId = item.appWidgetId;
3504 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003505 if (DEBUG_WIDGETS) {
3506 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3507 }
3508
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3510
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3512 item.hostView.setTag(item);
3513
3514 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3515 item.cellY, item.spanX, item.spanY, false);
3516
Adam Cohended9f8d2010-11-03 13:25:16 -07003517 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3518
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 workspace.requestLayout();
3520
3521 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003522
3523 if (DEBUG_WIDGETS) {
3524 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3525 + (SystemClock.uptimeMillis()-start) + "ms");
3526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
3528
3529 /**
3530 * Callback saying that there aren't any more items to bind.
3531 *
3532 * Implementation of the method from LauncherModel.Callbacks.
3533 */
3534 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003535 setLoadOnResume();
3536
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 if (mSavedState != null) {
3538 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003539 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 }
3541
3542 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3543 if (userFolders != null) {
3544 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003545 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 if (info != null) {
3547 openFolder(info);
3548 }
3549 }
3550 final Folder openFolder = mWorkspace.getOpenFolder();
3551 if (openFolder != null) {
3552 openFolder.requestFocus();
3553 }
3554 }
3555
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 mSavedState = null;
3557 }
3558
3559 if (mSavedInstanceState != null) {
3560 super.onRestoreInstanceState(mSavedInstanceState);
3561 mSavedInstanceState = null;
3562 }
3563
Winson Chung32bb5e52011-01-31 14:51:56 -08003564 // Workaround a bug that occurs when rotating the device while the customization mode is
3565 // open, we trigger a new layout on all the CellLayout children.
3566 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3567 final int childCount = mWorkspace.getChildCount();
3568 for (int i = 0; i < childCount; ++i) {
3569 mWorkspace.getChildAt(i).requestLayout();
3570 }
3571 }
3572
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003574
3575 // If we received the result of any pending adds while the loader was running (e.g. the
3576 // widget configuration forced an orientation change), process them now.
3577 for (int i = 0; i < sPendingAddList.size(); i++) {
3578 completeAdd(sPendingAddList.get(i));
3579 }
3580 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003581 }
3582
3583 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003584 * Updates the icons on the launcher that are affected by changes to the package list
3585 * on the device.
3586 */
3587 private void updateIconsAffectedByPackageManagerChanges() {
3588 updateAppMarketIcon();
3589 updateGlobalSearchIcon();
3590 updateVoiceSearchIcon();
3591 }
3592
3593 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 * Add the icons for all apps.
3595 *
3596 * Implementation of the method from LauncherModel.Callbacks.
3597 */
3598 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003599 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003600 if (mCustomizePagedView != null) {
3601 mCustomizePagedView.setApps(apps);
3602 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003603 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 }
3605
3606 /**
3607 * A package was installed.
3608 *
3609 * Implementation of the method from LauncherModel.Callbacks.
3610 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003611 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003612 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003614 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003615 if (mCustomizePagedView != null) {
3616 mCustomizePagedView.addApps(apps);
3617 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003618 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 }
3620
3621 /**
3622 * A package was updated.
3623 *
3624 * Implementation of the method from LauncherModel.Callbacks.
3625 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003626 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003627 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003629 if (mWorkspace != null) {
3630 mWorkspace.updateShortcuts(apps);
3631 }
3632 if (mAllAppsGrid != null) {
3633 mAllAppsGrid.updateApps(apps);
3634 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003635 if (mCustomizePagedView != null) {
3636 mCustomizePagedView.updateApps(apps);
3637 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003638 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 }
3640
3641 /**
3642 * A package was uninstalled.
3643 *
3644 * Implementation of the method from LauncherModel.Callbacks.
3645 */
Joe Onorato36115782010-06-17 13:28:48 -04003646 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003647 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003648 if (permanent) {
3649 mWorkspace.removeItems(apps);
3650 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003651 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003652 if (mCustomizePagedView != null) {
3653 mCustomizePagedView.removeApps(apps);
3654 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003655 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 }
Joe Onoratobe386092009-11-17 17:32:16 -08003657
3658 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003659 * A number of packages were updated.
3660 */
3661 public void bindPackagesUpdated() {
3662 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003663 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003664 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003665 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003666 }
3667
Winson Chung400438b2011-01-16 17:53:48 -08003668 private int mapConfigurationOriActivityInfoOri(int configOri) {
3669 final Display d = getWindowManager().getDefaultDisplay();
3670 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3671 switch (d.getRotation()) {
3672 case Surface.ROTATION_0:
3673 case Surface.ROTATION_180:
3674 // We are currently in the same basic orientation as the natural orientation
3675 naturalOri = configOri;
3676 break;
3677 case Surface.ROTATION_90:
3678 case Surface.ROTATION_270:
3679 // We are currently in the other basic orientation to the natural orientation
3680 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3681 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3682 break;
3683 }
3684
3685 int[] oriMap = {
3686 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3687 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3688 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3689 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3690 };
3691 // Since the map starts at portrait, we need to offset if this device's natural orientation
3692 // is landscape.
3693 int indexOffset = 0;
3694 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3695 indexOffset = 1;
3696 }
3697 return oriMap[(d.getRotation() + indexOffset) % 4];
3698 }
3699 public void lockScreenOrientation() {
3700 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3701 .getConfiguration().orientation));
3702 }
3703 public void unlockScreenOrientation() {
3704 mHandler.postDelayed(new Runnable() {
3705 public void run() {
3706 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3707 }
3708 }, mRestoreScreenOrientationDelay);
3709 }
3710
Winson Chung80baf5a2010-08-09 16:03:15 -07003711 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003712 * Prints out out state for debugging.
3713 */
3714 public void dumpState() {
3715 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003716 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003717 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3718 Log.d(TAG, "mRestoring=" + mRestoring);
3719 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3720 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3721 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003722 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003723 mModel.dumpState();
3724 mAllAppsGrid.dumpState();
3725 Log.d(TAG, "END launcher2 dump state");
3726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003727}
Michael Jurka2763be32011-02-24 11:19:57 -08003728
Michael Jurkaabded662011-03-04 12:06:57 -08003729interface LauncherTransitionable {
3730 void onLauncherTransitionStart(Animator animation);
3731 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003732}