blob: fc17643b196eccf77a96a6d7f1efe2682c419764 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
Michael Jurka742574b2011-02-02 23:51:01 -080022import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080025import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070038import android.content.ClipData;
39import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080045import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080046import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080049import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040053import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080054import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080056import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080068import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070069import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070070import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080071import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.Selection;
73import android.text.SpannableStringBuilder;
74import android.text.TextUtils;
75import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070076import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080078import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
82import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070083import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080084import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.View;
86import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070087import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080088import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080089import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070095import android.widget.ImageView;
96import android.widget.LinearLayout;
97import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070098import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -070099import android.widget.TextView;
100import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700101
Michael Jurka742574b2011-02-02 23:51:01 -0800102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800109
Adam Cohended9f8d2010-11-03 13:25:16 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800125 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700128 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
129 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
131 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700132 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
135 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
139 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Joe Onorato7c312c12009-08-13 21:36:53 -0700148 static final int DIALOG_CREATE_SHORTCUT = 1;
149 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Romain Guy98d01652009-06-30 16:21:04 -0700151 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
153 // Type: int
154 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700155 // Type: int
156 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: long
158 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700170 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
171
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700172 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800173 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800174 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700175 private State mState = State.WORKSPACE;
176 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800181 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800183 private final BroadcastReceiver mCloseSystemDialogsReceiver
184 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800185 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private LayoutInflater mInflater;
188
Joe Onorato00acb122009-08-04 16:04:30 -0400189 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private AppWidgetManager mAppWidgetManager;
193 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195 private int mAddScreen = -1;
196 private int mAddIntersectCellX = -1;
197 private int mAddIntersectCellY = -1;
198 private int[] mAddDropPosition;
199 private int[] mTmpAddItemCellCoordinates = new int[2];
200
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private FolderInfo mFolderInfo;
202
Joe Onorato7c312c12009-08-13 21:36:53 -0700203 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700205 private AllAppsView mAllAppsGrid;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800206 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700207 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700208
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mButtonCluster;
210 private View mAllAppsButton;
211 private View mDivider;
212 private View mConfigureButton;
213
Winson Chunga12a2502010-12-20 14:41:35 -0800214 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800242 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800243 private ImageView mPreviousView;
244 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400247 private String[] mHotseatConfig = null;
248 private Intent[] mHotseats = null;
249 private Drawable[] mHotseatIcons = null;
250 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400251
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700252 private Intent mAppMarketIntent = null;
253
Adam Cohended9f8d2010-11-03 13:25:16 -0700254 // Related to the auto-advancing of widgets
255 private final int ADVANCE_MSG = 1;
256 private final int mAdvanceInterval = 20000;
257 private final int mAdvanceStagger = 250;
258 private long mAutoAdvanceSentTime;
259 private long mAutoAdvanceTimeLeft = -1;
260 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
261 new HashMap<View, AppWidgetProviderInfo>();
262
Winson Chung400438b2011-01-16 17:53:48 -0800263 // Determines how long to wait after a rotation before restoring the screen orientation to
264 // match the sensor state.
265 private final int mRestoreScreenOrientationDelay = 500;
266
Michael Jurka4ef207b2010-11-29 17:05:45 -0800267 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800268 private static Drawable.ConstantState sGlobalSearchIcon;
269 private static Drawable.ConstantState sVoiceSearchIcon;
270 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271
Michael Jurkaddd62e92011-02-16 17:49:14 -0800272 private BubbleTextView mWaitingForResume;
273
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274 private static ArrayList<PendingAddArguments> sPendingAddList
275 = new ArrayList<PendingAddArguments>();
276
277 private static class PendingAddArguments {
278 int requestCode;
279 Intent intent;
280 int screen;
281 int cellX;
282 int cellY;
283 }
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 @Override
286 protected void onCreate(Bundle savedInstanceState) {
287 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700288
Winson Chungaafa03c2010-06-11 17:34:16 -0700289 if (LauncherApplication.isInPlaceRotationEnabled()) {
290 // hide the status bar (temporary until we get the status bar design figured out)
291 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
292 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
293 }
294
Joe Onorato0589f0f2010-02-08 13:44:00 -0800295 LauncherApplication app = ((LauncherApplication)getApplication());
296 mModel = app.setLauncher(this);
297 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400298 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700300
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
303 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200306 android.os.Debug.startMethodTracing(
307 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400310 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800313 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700314 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800316 mCustomizePagedView = (CustomizePagedView) findViewById(
317 R.id.customization_drawer_tab_contents);
Winson Chung80baf5a2010-08-09 16:03:15 -0700318
Michael Jurka946ad472010-07-09 18:05:18 -0700319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 setupViews();
321
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800322 registerContentObservers();
323
Joe Onorato7c312c12009-08-13 21:36:53 -0700324 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 mSavedState = savedInstanceState;
327 restoreState(mSavedState);
328
Winson Chunga12a2502010-12-20 14:41:35 -0800329 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800330 if (mCustomizePagedView != null) {
331 mCustomizePagedView.update();
332 }
Winson Chunga12a2502010-12-20 14:41:35 -0800333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 if (PROFILE_STARTUP) {
335 android.os.Debug.stopMethodTracing();
336 }
337
338 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400339 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 }
341
342 // For handling default keys
343 mDefaultKeySsb = new SpannableStringBuilder();
344 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800345
346 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
347 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800348
349 // If we have a saved version of these external icons, we load them up immediately
350 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800351 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
352 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800353 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800354 if (sGlobalSearchIcon != null) {
355 updateGlobalSearchIcon(sGlobalSearchIcon);
356 }
357 if (sVoiceSearchIcon != null) {
358 updateVoiceSearchIcon(sVoiceSearchIcon);
359 }
360 if (sAppMarketIcon != null) {
361 updateAppMarketIcon(sAppMarketIcon);
362 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 }
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800366 @Override
367 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
368 super.dispatchPopulateAccessibilityEvent(event);
369
370 // we want to take over text population so it is context dependent
371 event.getText().clear();
372 if (mState == State.ALL_APPS) {
373 event.getText().add(getString(R.string.all_apps_button_label));
374 } else if (mState == State.WORKSPACE) {
375 event.getText().add(getString(R.string.all_apps_home_button_label));
376 }
377
378 return true;
379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 if (sLocaleConfiguration == null) {
383 new AsyncTask<Void, Void, LocaleConfiguration>() {
384 @Override
385 protected LocaleConfiguration doInBackground(Void... unused) {
386 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
387 readConfiguration(Launcher.this, localeConfiguration);
388 return localeConfiguration;
389 }
390
391 @Override
392 protected void onPostExecute(LocaleConfiguration result) {
393 sLocaleConfiguration = result;
394 checkForLocaleChange(); // recursive, but now with a locale configuration
395 }
396 }.execute();
397 return;
398 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final Configuration configuration = getResources().getConfiguration();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final String locale = configuration.locale.toString();
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mcc = configuration.mcc;
407
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 final int mnc = configuration.mnc;
410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700414 sLocaleConfiguration.locale = locale;
415 sLocaleConfiguration.mcc = mcc;
416 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700417
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400419 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700420
421 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
422 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700423 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424 public void run() {
425 writeConfiguration(Launcher.this, localeConfiguration);
426 }
427 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700428 }
429 }
430
431 private static class LocaleConfiguration {
432 public String locale;
433 public int mcc = -1;
434 public int mnc = -1;
435 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700436
Romain Guy98d01652009-06-30 16:21:04 -0700437 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
438 DataInputStream in = null;
439 try {
440 in = new DataInputStream(context.openFileInput(PREFERENCES));
441 configuration.locale = in.readUTF();
442 configuration.mcc = in.readInt();
443 configuration.mnc = in.readInt();
444 } catch (FileNotFoundException e) {
445 // Ignore
446 } catch (IOException e) {
447 // Ignore
448 } finally {
449 if (in != null) {
450 try {
451 in.close();
452 } catch (IOException e) {
453 // Ignore
454 }
455 }
456 }
457 }
458
459 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
460 DataOutputStream out = null;
461 try {
462 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
463 out.writeUTF(configuration.locale);
464 out.writeInt(configuration.mcc);
465 out.writeInt(configuration.mnc);
466 out.flush();
467 } catch (FileNotFoundException e) {
468 // Ignore
469 } catch (IOException e) {
470 //noinspection ResultOfMethodCallIgnored
471 context.getFileStreamPath(PREFERENCES).delete();
472 } finally {
473 if (out != null) {
474 try {
475 out.close();
476 } catch (IOException e) {
477 // Ignore
478 }
479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481 }
482
483 static int getScreen() {
484 synchronized (sLock) {
485 return sScreen;
486 }
487 }
488
489 static void setScreen(int screen) {
490 synchronized (sLock) {
491 sScreen = screen;
492 }
493 }
494
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400495 // Note: This doesn't do all the client-id magic that BrowserProvider does
496 // in Browser. (http://b/2425179)
497 private Uri getDefaultBrowserUri() {
498 String url = getString(R.string.default_browser_url);
499 if (url.indexOf("{CID}") != -1) {
500 url = url.replace("{CID}", "android-google");
501 }
502 return Uri.parse(url);
503 }
504
505 // Load the Intent templates from arrays.xml to populate the hotseats. For
506 // each Intent, if it resolves to a single app, use that as the launch
507 // intent & use that app's label as the contentDescription. Otherwise,
508 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400509 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400510 if (mHotseatConfig == null) {
511 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
512 if (mHotseatConfig.length > 0) {
513 mHotseats = new Intent[mHotseatConfig.length];
514 mHotseatLabels = new CharSequence[mHotseatConfig.length];
515 mHotseatIcons = new Drawable[mHotseatConfig.length];
516 } else {
517 mHotseats = null;
518 mHotseatIcons = null;
519 mHotseatLabels = null;
520 }
521
522 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
523 for (int i=0; i<mHotseatConfig.length; i++) {
524 // load icon for this slot; currently unrelated to the actual activity
525 try {
526 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
527 } catch (ArrayIndexOutOfBoundsException ex) {
528 Log.w(TAG, "Missing hotseat_icons array item #" + i);
529 mHotseatIcons[i] = null;
530 }
531 }
532 hotseatIconDrawables.recycle();
533 }
534
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400535 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400536 for (int i=0; i<mHotseatConfig.length; i++) {
537 Intent intent = null;
538 if (mHotseatConfig[i].equals("*BROWSER*")) {
539 // magic value meaning "launch user's default web browser"
540 // replace it with a generic web request so we can see if there is indeed a default
541 String defaultUri = getString(R.string.default_browser_url);
542 intent = new Intent(
543 Intent.ACTION_VIEW,
544 ((defaultUri != null)
545 ? Uri.parse(defaultUri)
546 : getDefaultBrowserUri())
547 ).addCategory(Intent.CATEGORY_BROWSABLE);
548 // note: if the user launches this without a default set, she
549 // will always be taken to the default URL above; this is
550 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700551 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400552 // URL is unavoidably sent to the chosen app.
553 } else {
554 try {
555 intent = Intent.parseUri(mHotseatConfig[i], 0);
556 } catch (java.net.URISyntaxException ex) {
557 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
558 // bogus; leave intent=null
559 }
560 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700561
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 if (intent == null) {
563 mHotseats[i] = null;
564 mHotseatLabels[i] = getText(R.string.activity_not_found);
565 continue;
566 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400567
568 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700569 Log.d(TAG, "loadHotseats: hotseat " + i
570 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400571 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 + "]");
573 }
574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
576 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700577 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400578 Log.d(TAG, "Best match for intent: " + bestMatch);
579 Log.d(TAG, "All matches: ");
580 for (ResolveInfo ri : allMatches) {
581 Log.d(TAG, " --> " + ri);
582 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 // did this resolve to a single app, or the resolver?
585 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 // can't find any activity to handle this. let's leave the
587 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 // to launch.
589 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400590
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 // set accessibility text to "Not installed"
592 mHotseatLabels[i] = getText(R.string.activity_not_found);
593 } else {
594 boolean found = false;
595 for (ResolveInfo ri : allMatches) {
596 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
597 && bestMatch.activityInfo.applicationInfo.packageName
598 .equals(ri.activityInfo.applicationInfo.packageName)) {
599 found = true;
600 break;
601 }
602 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700603
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400604 if (!found) {
605 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
606 // the bestMatch is probably the ResolveActivity, meaning the
607 // user has not yet selected a default
608 // so: we'll keep the original intent for now
609 mHotseats[i] = intent;
610
611 // set the accessibility text to "Select shortcut"
612 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
613 } else {
614 // we have an app!
615 // now reconstruct the intent to launch it through the front
616 // door
617 ComponentName com = new ComponentName(
618 bestMatch.activityInfo.applicationInfo.packageName,
619 bestMatch.activityInfo.name);
620 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
621
622 // load the app label for accessibility
623 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
624 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625 }
626
627 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700628 Log.d(TAG, "loadHotseats: hotseat " + i
629 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400630 + ((mHotseats[i] == null)
631 ? "null"
632 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633 + "] label=[" + mHotseatLabels[i]
634 + "]"
635 );
636 }
637 }
638 }
639
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800640 private void completeAdd(PendingAddArguments args) {
641 switch (args.requestCode) {
642 case REQUEST_PICK_APPLICATION:
643 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
644 break;
645 case REQUEST_PICK_SHORTCUT:
646 processShortcut(args.intent);
647 break;
648 case REQUEST_CREATE_SHORTCUT:
649 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
650 break;
651 case REQUEST_PICK_LIVE_FOLDER:
652 addLiveFolder(args.intent);
653 break;
654 case REQUEST_CREATE_LIVE_FOLDER:
655 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
656 break;
657 case REQUEST_PICK_APPWIDGET:
658 addAppWidgetFromPick(args.intent);
659 break;
660 case REQUEST_CREATE_APPWIDGET:
661 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
662 completeAddAppWidget(appWidgetId, args.screen);
663 break;
664 case REQUEST_PICK_WALLPAPER:
665 // We just wanted the activity result here so we can clear mWaitingForResult
666 break;
667 }
668 }
669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700672 mWaitingForResult = false;
673
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 // The pattern used here is that a user PICKs a specific application,
675 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
678 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700679
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800681 final PendingAddArguments args = new PendingAddArguments();
682 args.requestCode = requestCode;
683 args.intent = data;
684 args.screen = mAddScreen;
685 args.cellX = mAddIntersectCellX;
686 args.cellY = mAddIntersectCellY;
687
688 // If the loader is still running, defer the add until it is done.
689 if (isWorkspaceLocked()) {
690 sPendingAddList.add(args);
691 } else {
692 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
Romain Guy18042c82009-11-06 11:44:55 -0800694 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
695 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
696 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 // Clean up the appWidgetId if we canceled
698 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
699 if (appWidgetId != -1) {
700 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 }
704
705 @Override
706 protected void onResume() {
707 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800708 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700709 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 mWorkspaceLoading = true;
711 mModel.startLoader(this, true);
712 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700713 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800715 if (mWaitingForResume != null) {
716 mWaitingForResume.setStayPressed(false);
717 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700718 // When we resume Launcher, a different Activity might be responsible for the app
719 // market intent, so refresh the icon
720 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 }
722
723 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 protected void onPause() {
725 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 // Some launcher layouts don't have a previous and next view
727 if (mPreviousView != null) {
728 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700729 }
730 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700731 dismissPreview(mNextView);
732 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700733 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800734 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 }
Romain Guycbb89e42009-06-08 15:52:54 -0700736
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700737 @Override
738 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 // Flag the loader to stop early before switching
740 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800741 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800742 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700743 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800745 // We can't hide the IME if it was forced open. So don't bother
746 /*
747 @Override
748 public void onWindowFocusChanged(boolean hasFocus) {
749 super.onWindowFocusChanged(hasFocus);
750
751 if (hasFocus) {
752 final InputMethodManager inputManager = (InputMethodManager)
753 getSystemService(Context.INPUT_METHOD_SERVICE);
754 WindowManager.LayoutParams lp = getWindow().getAttributes();
755 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
756 android.os.Handler()) {
757 protected void onReceiveResult(int resultCode, Bundle resultData) {
758 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
759 }
760 });
761 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
762 }
763 }
764 */
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 private boolean acceptFilter() {
767 final InputMethodManager inputManager = (InputMethodManager)
768 getSystemService(Context.INPUT_METHOD_SERVICE);
769 return !inputManager.isFullscreenMode();
770 }
771
772 @Override
773 public boolean onKeyDown(int keyCode, KeyEvent event) {
774 boolean handled = super.onKeyDown(keyCode, event);
775 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
776 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);
963 final View divider = findViewById(R.id.divider);
964 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 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001012 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1013
1014 mAllAppsButton = findViewById(R.id.all_apps_button);
1015 mDivider = findViewById(R.id.divider);
1016 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001017
1018 // We had previously set these click handlers in XML, but the first time we launched
1019 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1020 // found the right handler. Setting the handlers directly here eliminates that cost.
1021 if (mConfigureButton != null) {
1022 mConfigureButton.setOnClickListener(new OnClickListener() {
1023 public void onClick(View v) {
1024 onClickConfigureButton(v);
1025 }
1026 });
1027 }
1028 if (mDivider != null) {
1029 mDivider.setOnClickListener(new OnClickListener() {
1030 public void onClick(View v) {
1031 onClickAllAppsButton(v);
1032 }
1033 });
1034 }
1035 if (mAllAppsButton != null) {
1036 mAllAppsButton.setOnClickListener(new OnClickListener() {
1037 public void onClick(View v) {
1038 onClickAllAppsButton(v);
1039 }
1040 });
1041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
Romain Guy8a73c512009-11-09 19:19:59 -08001044 @SuppressWarnings({"UnusedDeclaration"})
1045 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001046 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001047 mWorkspace.scrollLeft();
1048 }
Romain Guy8a73c512009-11-09 19:19:59 -08001049 }
1050
1051 @SuppressWarnings({"UnusedDeclaration"})
1052 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001053 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001054 mWorkspace.scrollRight();
1055 }
Romain Guy8a73c512009-11-09 19:19:59 -08001056 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001057
1058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001060 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001061
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001062 int index = -1;
1063 if (v.getId() == R.id.hotseat_left) {
1064 index = 0;
1065 } else if (v.getId() == R.id.hotseat_right) {
1066 index = 1;
1067 }
1068
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001069 // reload these every tap; you never know when they might change
1070 loadHotseats();
1071 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001072 startActivitySafely(
1073 mHotseats[index],
1074 "hotseat"
1075 );
1076 }
1077 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 /**
1080 * Creates a view representing a shortcut.
1081 *
1082 * @param info The data structure describing the shortcut.
1083 *
1084 * @return A View inflated from R.layout.application.
1085 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001086 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001088 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 }
1090
1091 /**
1092 * Creates a view representing a shortcut inflated from the specified resource.
1093 *
1094 * @param layoutResId The id of the XML layout used to create the shortcut.
1095 * @param parent The group the shortcut belongs to.
1096 * @param info The data structure describing the shortcut.
1097 *
1098 * @return A View inflated from layoutResId.
1099 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001101 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1102 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 return favorite;
1105 }
1106
1107 /**
1108 * Add an application shortcut to the workspace.
1109 *
1110 * @param data The intent describing the application.
1111 * @param cellInfo The position on screen where to create the shortcut.
1112 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001113 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 int intersectCellX, int intersectCellY) {
1115 final int[] cellXY = mTmpAddItemCellCoordinates;
1116 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1117
1118 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1119 showOutOfSpaceMessage();
1120 return;
1121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001123 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124
Romain Guy73b979d2009-06-09 12:57:21 -07001125 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001128 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001129 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1130 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001131 } else {
1132 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134 }
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 /**
1137 * Add a shortcut to the workspace.
1138 *
1139 * @param data The intent describing the shortcut.
1140 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001142 private void completeAddShortcut(Intent data, int screen,
1143 int intersectCellX, int intersectCellY) {
1144 final int[] cellXY = mTmpAddItemCellCoordinates;
1145 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001147 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001148 boolean foundCellSpan = false;
1149 if (touchXY != null) {
1150 // when dragging and dropping, just find the closest free spot
1151 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1152 int[] result = screenLayout.findNearestVacantArea(
1153 touchXY[0], touchXY[1], 1, 1, cellXY);
1154 foundCellSpan = (result != null);
1155 } else {
1156 foundCellSpan = layout.findCellForSpanThatIntersects(
1157 cellXY, 1, 1, intersectCellX, intersectCellY);
1158 }
1159
1160 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 showOutOfSpaceMessage();
1162 return;
1163 }
1164
1165 final ShortcutInfo info = mModel.addShortcut(
1166 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167
1168 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001170 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172 }
1173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001175 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001177 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001178 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 */
Winson Chungf7640c82011-02-28 13:47:29 -08001180 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001181 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001182
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1185 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001188 // We have saved the position to which the widget was dragged-- this really only matters
1189 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001190 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001191
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001192 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001193 boolean foundCellSpan = false;
1194 if (touchXY != null) {
1195 // when dragging and dropping, just find the closest free spot
1196 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1197 int[] result = screenLayout.findNearestVacantArea(
1198 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001199 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001201 // if we long pressed on an empty cell to bring up a menu,
1202 // make sure we intersect the empty cell
1203 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1204 // findCellForSpanThatIntersects will just ignore them
1205 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1206 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001207 }
1208
Michael Jurka0280c3b2010-09-17 15:00:07 -07001209 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001210 if (appWidgetId != -1) {
1211 // Deleting an app widget ID is a void call but writes to disk before returning
1212 // to the caller...
1213 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1214 new Thread("deleteAppWidgetId") {
1215 public void run() {
1216 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1217 }
1218 }.start();
1219 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001221 return;
1222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 // Build Launcher-specific widget info and save to database
1225 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 launcherInfo.spanX = spanXY[0];
1227 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 LauncherModel.addItemToDatabase(this, launcherInfo,
1230 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232
1233 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001237 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001238
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001241
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001244
1245 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 }
1247 }
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Adam Cohended9f8d2010-11-03 13:25:16 -07001249 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1250 @Override
1251 public void onReceive(Context context, Intent intent) {
1252 final String action = intent.getAction();
1253 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1254 mUserPresent = false;
1255 updateRunning();
1256 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1257 mUserPresent = true;
1258 updateRunning();
1259 }
1260 }
1261 };
1262
1263 @Override
1264 public void onAttachedToWindow() {
1265 super.onAttachedToWindow();
1266
1267 // Listen for broadcasts related to user-presence
1268 final IntentFilter filter = new IntentFilter();
1269 filter.addAction(Intent.ACTION_SCREEN_OFF);
1270 filter.addAction(Intent.ACTION_USER_PRESENT);
1271 registerReceiver(mReceiver, filter);
1272
Adam Cohend113e0c2010-11-11 10:48:05 -08001273 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001274 mVisible = true;
1275 }
1276
1277 @Override
1278 public void onDetachedFromWindow() {
1279 super.onDetachedFromWindow();
1280 mVisible = false;
1281
Adam Cohend113e0c2010-11-11 10:48:05 -08001282 if (mAttached) {
1283 unregisterReceiver(mReceiver);
1284 mAttached = false;
1285 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 updateRunning();
1287 }
1288
1289 public void onWindowVisibilityChanged(int visibility) {
1290 mVisible = visibility == View.VISIBLE;
1291 updateRunning();
1292 }
1293
1294 private void sendAdvanceMessage(long delay) {
1295 mHandler.removeMessages(ADVANCE_MSG);
1296 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1297 mHandler.sendMessageDelayed(msg, delay);
1298 mAutoAdvanceSentTime = System.currentTimeMillis();
1299 }
1300
1301 private void updateRunning() {
1302 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1303 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1304 mAutoAdvanceRunning = autoAdvanceRunning;
1305 if (autoAdvanceRunning) {
1306 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1307 sendAdvanceMessage(delay);
1308 } else {
1309 if (!mWidgetsToAdvance.isEmpty()) {
1310 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1311 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1312 }
1313 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001314 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001315 }
1316 }
1317 }
1318
1319 private final Handler mHandler = new Handler() {
1320 @Override
1321 public void handleMessage(Message msg) {
1322 if (msg.what == ADVANCE_MSG) {
1323 int i = 0;
1324 for (View key: mWidgetsToAdvance.keySet()) {
1325 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1326 final int delay = mAdvanceStagger * i;
1327 if (v instanceof Advanceable) {
1328 postDelayed(new Runnable() {
1329 public void run() {
1330 ((Advanceable) v).advance();
1331 }
1332 }, delay);
1333 }
1334 i++;
1335 }
1336 sendAdvanceMessage(mAdvanceInterval);
1337 }
1338 }
1339 };
1340
1341 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1342 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1343 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1344 if (v instanceof Advanceable) {
1345 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001346 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 updateRunning();
1348 }
1349 }
1350
1351 void removeWidgetToAutoAdvance(View hostView) {
1352 if (mWidgetsToAdvance.containsKey(hostView)) {
1353 mWidgetsToAdvance.remove(hostView);
1354 updateRunning();
1355 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356 }
1357
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1359 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001360 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 launcherInfo.hostView = null;
1362 }
1363
Adam Cohended9f8d2010-11-03 13:25:16 -07001364 void showOutOfSpaceMessage() {
1365 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1366 }
1367
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001368 public LauncherAppWidgetHost getAppWidgetHost() {
1369 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
Romain Guycbb89e42009-06-08 15:52:54 -07001371
Winson Chunga9abd0e2010-10-27 17:18:37 -07001372 public LauncherModel getModel() {
1373 return mModel;
1374 }
1375
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001376 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001377 getWindow().closeAllPanels();
1378
1379 try {
1380 dismissDialog(DIALOG_CREATE_SHORTCUT);
1381 // Unlock the workspace if the dialog was showing
1382 } catch (Exception e) {
1383 // An exception is thrown if the dialog is not visible, which is fine
1384 }
1385
1386 try {
1387 dismissDialog(DIALOG_RENAME_FOLDER);
1388 // Unlock the workspace if the dialog was showing
1389 } catch (Exception e) {
1390 // An exception is thrown if the dialog is not visible, which is fine
1391 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001392
1393 // Whatever we were doing is hereby canceled.
1394 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 @Override
1398 protected void onNewIntent(Intent intent) {
1399 super.onNewIntent(intent);
1400
1401 // Close the menu
1402 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001403 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001404 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001405
Joe Onorato14f122b2009-11-19 14:06:36 -08001406 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1407 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001408
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001409 // In all these cases, only animate if we're already on home
1410
Michael Jurka4cb37242010-08-09 21:05:32 -07001411 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001412 mWorkspace.unshrink(alreadyOnHome);
1413 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001414
Patrick Dubroy758a9232011-03-03 19:54:56 -08001415 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001416 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001417 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001418 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001419 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001420
Joe Onorato3a8820b2009-11-10 15:06:42 -08001421 final View v = getWindow().peekDecorView();
1422 if (v != null && v.getWindowToken() != null) {
1423 InputMethodManager imm = (InputMethodManager)getSystemService(
1424 INPUT_METHOD_SERVICE);
1425 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
1427 }
1428 }
1429
1430 @Override
1431 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1432 // Do not call super here
1433 mSavedInstanceState = savedInstanceState;
1434 }
1435
1436 @Override
1437 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001438 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439
1440 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1441 if (folders.size() > 0) {
1442 final int count = folders.size();
1443 long[] ids = new long[count];
1444 for (int i = 0; i < count; i++) {
1445 final FolderInfo info = folders.get(i).getInfo();
1446 ids[i] = info.id;
1447 }
1448 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1449 } else {
1450 super.onSaveInstanceState(outState);
1451 }
1452
Michael Jurka883f55b2010-10-21 15:47:14 -07001453 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454
Michael Jurka0280c3b2010-09-17 15:00:07 -07001455 if (mAddScreen > -1 && mWaitingForResult) {
1456 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1457 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1458 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460
1461 if (mFolderInfo != null && mWaitingForResult) {
1462 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1463 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1464 }
Michael Jurka946ad472010-07-09 18:05:18 -07001465
Winson Chung43b119d2010-12-06 16:45:05 -08001466 // Save the current AllApps drawer tab
1467 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1468 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1469 String currentTabTag = tabhost.getCurrentTabTag();
1470 if (currentTabTag != null) {
1471 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001472 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001473 }
1474 }
1475
1476 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001477 if (mHomeCustomizationDrawer != null) {
1478 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1479 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001480 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001481 }
1482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
1484
1485 @Override
1486 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001490 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001492 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001494 mAppWidgetHost = null;
1495
1496 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497
1498 TextKeyListener.getInstance().release();
1499
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501
Joe Onorato9c1289c2009-08-17 11:03:03 -04001502 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001503
1504 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001505
Patrick Dubroyab962b72010-07-26 12:10:10 -07001506 // Some launcher layouts don't have a previous and next view
1507 if (mPreviousView != null) {
1508 dismissPreview(mPreviousView);
1509 }
1510 if (mNextView != null) {
1511 dismissPreview(mNextView);
1512 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001513
1514 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001515
1516 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1517 mWorkspace.removeAllViews();
1518 mWorkspace = null;
1519 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001520
1521 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 }
1523
1524 @Override
1525 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001526 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 super.startActivityForResult(intent, requestCode);
1528 }
1529
1530 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001531 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001533
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001534 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 if (initialQuery == null) {
1537 // Use any text typed in the launcher as the initial query
1538 initialQuery = getTypedText();
1539 clearTypedText();
1540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 if (appSearchData == null) {
1542 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001543 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
Romain Guycbb89e42009-06-08 15:52:54 -07001545
Karl Rosaen138a0412009-04-23 19:00:21 -07001546 final SearchManager searchManager =
1547 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001548 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001549 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551
1552 @Override
1553 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001554 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 return false;
1556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557
1558 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1561 .setIcon(android.R.drawable.ic_menu_add)
1562 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001563 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1564 .setIcon(android.R.drawable.ic_menu_manage)
1565 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001566 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 .setIcon(android.R.drawable.ic_menu_gallery)
1568 .setAlphabeticShortcut('W');
1569 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1570 .setIcon(android.R.drawable.ic_search_category_default)
1571 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1572 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1573 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1574 .setAlphabeticShortcut('N');
1575
1576 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001577 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1578 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
1580 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1581 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1582 .setIntent(settings);
1583
1584 return true;
1585 }
1586
1587 @Override
1588 public boolean onPrepareOptionsMenu(Menu menu) {
1589 super.onPrepareOptionsMenu(menu);
1590
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001591 // If all apps is animating, don't show the menu, because we don't know
1592 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001593 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001594 return false;
1595 }
1596
1597 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001598 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001599 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1600 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1601
1602 // Disable add if the workspace is full.
1603 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001604 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1605 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607
1608 return true;
1609 }
1610
1611 @Override
1612 public boolean onOptionsItemSelected(MenuItem item) {
1613 switch (item.getItemId()) {
1614 case MENU_ADD:
1615 addItems();
1616 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001617 case MENU_MANAGE_APPS:
1618 manageApps();
1619 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 case MENU_WALLPAPER_SETTINGS:
1621 startWallpaper();
1622 return true;
1623 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001624 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 return true;
1626 case MENU_NOTIFICATIONS:
1627 showNotifications();
1628 return true;
1629 }
1630
1631 return super.onOptionsItemSelected(item);
1632 }
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Karl Rosaen138a0412009-04-23 19:00:21 -07001634 /**
1635 * Indicates that we want global search for this activity by setting the globalSearch
1636 * argument for {@link #startSearch} to true.
1637 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001639 @Override
1640 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001641 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001642 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001643 }
1644
Joe Onorato9c1289c2009-08-17 11:03:03 -04001645 public boolean isWorkspaceLocked() {
1646 return mWorkspaceLoading || mWaitingForResult;
1647 }
1648
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001649 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1650 private boolean isPreviewVisible() {
1651 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1652 (mNextView != null && mNextView.getTag() != null);
1653 }
1654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001656 if (LauncherApplication.isScreenXLarge()) {
1657 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001658 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001659 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001660 }
1661 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001662 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
1666
Michael Jurka0280c3b2010-09-17 15:00:07 -07001667 private void resetAddInfo() {
1668 mAddScreen = -1;
1669 mAddIntersectCellX = -1;
1670 mAddIntersectCellY = -1;
1671 mAddDropPosition = null;
1672 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001673
Winson Chung55cef262010-10-28 14:14:18 -07001674 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001675 resetAddInfo();
1676 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001677 mAddDropPosition = position;
1678
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001680 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1681 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001682 }
1683
Winson Chung7ad01412010-09-27 11:33:03 -07001684 private void manageApps() {
1685 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1686 }
1687
Michael Jurkaaf442092010-06-10 17:01:57 -07001688 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001689 // TODO: catch bad widget exception when sent
1690 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001691 // TODO: Is this log message meaningful?
1692 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001693 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001694 }
1695
Winson Chung55cef262010-10-28 14:14:18 -07001696 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001697 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698
Bjorn Bringert7984c942009-12-09 15:38:25 +00001699 if (appWidget.configure != null) {
1700 // Launch over to configure widget, if needed
1701 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1702 intent.setComponent(appWidget.configure);
1703 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001704 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001705 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1706 intent.putExtra(
1707 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1708 info.mimeType);
1709
1710 final String mimeType = info.mimeType;
1711 final ClipData clipData = (ClipData) info.configurationData;
1712 final ClipDescription clipDesc = clipData.getDescription();
1713 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1714 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001715 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001716 final CharSequence stringData = item.getText();
1717 final Uri uriData = item.getUri();
1718 final Intent intentData = item.getIntent();
1719 final String key =
1720 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1721 if (uriData != null) {
1722 intent.putExtra(key, uriData);
1723 } else if (intentData != null) {
1724 intent.putExtra(key, intentData);
1725 } else if (stringData != null) {
1726 intent.putExtra(key, stringData);
1727 }
1728 break;
1729 }
1730 }
1731 }
Winson Chung55cef262010-10-28 14:14:18 -07001732 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001733
Romain Guy8e633c52010-03-04 12:51:36 -08001734 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001736 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
1739 }
Romain Guycbb89e42009-06-08 15:52:54 -07001740
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1742 resetAddInfo();
1743 mAddScreen = screen;
1744 mAddDropPosition = position;
1745
1746 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1747 createShortcutIntent.setComponent(componentName);
1748 processShortcut(createShortcutIntent);
1749 }
1750
Joe Onoratodeb98af2010-02-19 14:59:39 -08001751 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 // Handle case where user selected "Applications"
1753 String applicationName = getResources().getString(R.string.group_applications);
1754 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 if (applicationName != null && applicationName.equals(shortcutName)) {
1757 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1758 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001759
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001760 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1761 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001762 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001763 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001764 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001765 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 }
1768
Winson Chung24ab2f12010-09-16 14:10:47 -07001769 void processWallpaper(Intent intent) {
1770 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1771 }
1772
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1774 resetAddInfo();
1775 mAddScreen = screen;
1776 mAddDropPosition = position;
1777
1778 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1779 createFolderIntent.setComponent(componentName);
1780
1781 addLiveFolder(createFolderIntent);
1782 }
1783
1784 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001785 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001786 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001788
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001790 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001792 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 UserFolderInfo folderInfo = new UserFolderInfo();
1798 folderInfo.title = getText(R.string.folder_name);
1799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1801 final int[] cellXY = mTmpAddItemCellCoordinates;
1802 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1803 showOutOfSpaceMessage();
1804 return;
1805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806
1807 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 LauncherModel.addItemToDatabase(this, folderInfo,
1809 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001811 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
1813 // Create the view
1814 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001815 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1816 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
Romain Guycbb89e42009-06-08 15:52:54 -07001819
Joe Onorato9c1289c2009-08-17 11:03:03 -04001820 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001821 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 }
1823
Michael Jurka28750fb2010-09-24 17:43:49 -07001824 private void completeAddLiveFolder(
1825 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001826 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1827 final int[] cellXY = mTmpAddItemCellCoordinates;
1828 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1829 showOutOfSpaceMessage();
1830 return;
1831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834
1835 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001837 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001838 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 }
1840 }
1841
1842 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844
1845 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1846 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1847
1848 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 Intent.ShortcutIconResource iconResource = null;
1850
1851 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1852 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1853 try {
1854 iconResource = (Intent.ShortcutIconResource) extra;
1855 final PackageManager packageManager = context.getPackageManager();
1856 Resources resources = packageManager.getResourcesForApplication(
1857 iconResource.packageName);
1858 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1859 icon = resources.getDrawable(id);
1860 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001861 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
1863 }
1864
1865 if (icon == null) {
1866 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1867 }
1868
1869 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001870 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 info.title = name;
1872 info.iconResource = iconResource;
1873 info.uri = data.getData();
1874 info.baseIntent = baseIntent;
1875 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1876 LiveFolders.DISPLAY_MODE_GRID);
1877
1878 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001879 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001880 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881
1882 return info;
1883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 private void showNotifications() {
1886 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1887 if (statusBar != null) {
1888 statusBar.expand();
1889 }
1890 }
1891
1892 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001893 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001895 Intent chooser = Intent.createChooser(pickWallpaper,
1896 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001897 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1898 // Removed in Eclair MR1
1899// WallpaperManager wm = (WallpaperManager)
1900// getSystemService(Context.WALLPAPER_SERVICE);
1901// WallpaperInfo wi = wm.getWallpaperInfo();
1902// if (wi != null && wi.getSettingsActivity() != null) {
1903// LabeledIntent li = new LabeledIntent(getPackageName(),
1904// R.string.configure_wallpaper, 0);
1905// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1906// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1907// }
Mike Clerona0618e42009-10-22 13:55:21 -07001908 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
1910
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 /**
1912 * Registers various content observers. The current implementation registers
1913 * only a favorites observer to keep track of the favorites applications.
1914 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001915 private void registerContentObservers() {
1916 ContentResolver resolver = getContentResolver();
1917 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1918 true, mWidgetObserver);
1919 }
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 @Override
1922 public boolean dispatchKeyEvent(KeyEvent event) {
1923 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1924 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001926 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001927 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001928 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001929 dumpState();
1930 return true;
1931 }
1932 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001933 }
1934 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1935 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001936 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 return true;
1938 }
1939 }
1940
1941 return super.dispatchKeyEvent(event);
1942 }
1943
Joe Onorato88ec0992009-11-19 13:16:06 -08001944 @Override
1945 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001946 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1947 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001948 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001949 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001950 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001951 dismissPreview(mPreviousView);
1952 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001953 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001954 mWorkspace.exitWidgetResizeMode();
1955
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001956 // Back button is a no-op here, but give at least some feedback for the button press
1957 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001958 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001959 }
1960
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 private void closeFolder() {
1962 Folder folder = mWorkspace.getOpenFolder();
1963 if (folder != null) {
1964 closeFolder(folder);
1965 }
1966 }
1967
1968 void closeFolder(Folder folder) {
1969 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001970 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001972 CellLayout cl = (CellLayout) parent;
1973 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001974 if (folder instanceof DropTarget) {
1975 // Live folders aren't DropTargets.
1976 mDragController.removeDropTarget((DropTarget)folder);
1977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979 folder.onClose();
1980 }
1981
1982 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001983 * Re-listen when widgets are reset.
1984 */
1985 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001986 if (mAppWidgetHost != null) {
1987 mAppWidgetHost.startListening();
1988 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001989 }
1990
1991 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001992 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1993 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001995 private void unbindDesktopItems() {
1996 for (ItemInfo item: mDesktopItems) {
1997 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001999 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 /**
2003 * Launches the intent referred by the clicked shortcut.
2004 *
2005 * @param v The view representing the clicked shortcut.
2006 */
2007 public void onClick(View v) {
2008 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002009 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002011 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002012 int[] pos = new int[2];
2013 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002014 intent.setSourceBounds(new Rect(pos[0], pos[1],
2015 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002016 boolean success = startActivitySafely(intent, tag);
2017
2018 if (success && v instanceof BubbleTextView) {
2019 mWaitingForResume = (BubbleTextView) v;
2020 mWaitingForResume.setStayPressed(true);
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 } else if (tag instanceof FolderInfo) {
2023 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002024 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002025 if (mState == State.ALL_APPS) {
2026 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002027 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002028 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 }
2031 }
2032
Michael Jurka0e260592010-06-30 17:07:39 -07002033 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002034 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002035 // clicking anywhere on the workspace causes the customization drawer to slide down
2036 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002037 return false;
2038 }
2039
Michael Jurkaaf442092010-06-10 17:01:57 -07002040 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002041 * Event handler for the search button
2042 *
2043 * @param v The view that was clicked.
2044 */
2045 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002046 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002047 // Use a custom animation for launching search
2048 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002049 }
2050
2051 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002052 * Event handler for the voice button
2053 *
2054 * @param v The view that was clicked.
2055 */
2056 public void onClickVoiceButton(View v) {
2057 startVoiceSearch();
2058 }
2059
2060 private void startVoiceSearch() {
2061 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2062 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2063 startActivity(intent);
2064 }
2065
2066 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002067 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002068 * enters home screen customization mode.
2069 *
2070 * @param v The view that was clicked.
2071 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002072 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002073 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002074 }
2075
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002076 /**
2077 * Event handler for the "grid" button that appears on the home screen, which
2078 * enters all apps mode.
2079 *
2080 * @param v The view that was clicked.
2081 */
2082 public void onClickAllAppsButton(View v) {
2083 showAllApps(true);
2084 }
2085
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002086 public void onClickAppMarketButton(View v) {
2087 if (mAppMarketIntent != null) {
2088 startActivitySafely(mAppMarketIntent, "app market");
2089 }
2090 }
2091
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002092 void startApplicationDetailsActivity(ComponentName componentName) {
2093 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002094 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2095 Uri.fromParts("package", packageName, null));
2096 startActivity(intent);
2097 }
2098
Patrick Dubroy5539af72010-09-07 15:22:01 -07002099 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2100 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2101 // System applications cannot be installed. For now, show a toast explaining that.
2102 // We may give them the option of disabling apps this way.
2103 int messageId = R.string.uninstall_system_app_text;
2104 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2105 } else {
2106 String packageName = appInfo.componentName.getPackageName();
2107 String className = appInfo.componentName.getClassName();
2108 Intent intent = new Intent(
2109 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2110 startActivity(intent);
2111 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002112 }
2113
Michael Jurkaddd62e92011-02-16 17:49:14 -08002114 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2116 try {
2117 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002118 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 } catch (ActivityNotFoundException e) {
2120 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002121 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 } catch (SecurityException e) {
2123 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002124 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002126 "or use the exported attribute for this activity. "
2127 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002129 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002131
Romain Guy8e633c52010-03-04 12:51:36 -08002132 void startActivityForResultSafely(Intent intent, int requestCode) {
2133 try {
2134 startActivityForResult(intent, requestCode);
2135 } catch (ActivityNotFoundException e) {
2136 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2137 } catch (SecurityException e) {
2138 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2139 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2140 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2141 "or use the exported attribute for this activity.", e);
2142 }
2143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144
2145 private void handleFolderClick(FolderInfo folderInfo) {
2146 if (!folderInfo.opened) {
2147 // Close any open folder
2148 closeFolder();
2149 // Open the requested folder
2150 openFolder(folderInfo);
2151 } else {
2152 // Find the open folder...
2153 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2154 int folderScreen;
2155 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002156 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 // .. and close it
2158 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002159 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 // Close any folder open on the current screen
2161 closeFolder();
2162 // Pull the folder onto this screen
2163 openFolder(folderInfo);
2164 }
2165 }
2166 }
2167 }
2168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002170 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 * is animated relative to the specified View. If the View is null, no animation
2172 * is played.
2173 *
2174 * @param folderInfo The FolderInfo describing the folder to open.
2175 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002176 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 Folder openFolder;
2178
2179 if (folderInfo instanceof UserFolderInfo) {
2180 openFolder = UserFolder.fromXml(this);
2181 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002182 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 } else {
2184 return;
2185 }
2186
Joe Onorato00acb122009-08-04 16:04:30 -04002187 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 openFolder.setLauncher(this);
2189
2190 openFolder.bind(folderInfo);
2191 folderInfo.opened = true;
2192
Winson Chungaafa03c2010-06-11 17:34:16 -07002193 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 openFolder.onOpen();
2196 }
2197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002199 if (mState != State.WORKSPACE) {
2200 return false;
2201 }
2202
Romain Guy1fbc1c82009-11-09 20:43:08 -08002203 switch (v.getId()) {
2204 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002205 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002206 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2207 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002208 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002209 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002210 return true;
2211 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002212 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002213 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2214 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002215 showPreviews(v);
2216 }
2217 return true;
2218 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002219 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002220 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2221 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2222 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002223 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002224 return true;
2225 }
2226
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 return false;
2229 }
2230
2231 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002232 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 }
2234
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 resetAddInfo();
2236 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 return true;
2240 }
2241
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 final View itemUnderLongClick = longClickCellInfo.cell;
2243
Winson Chung304dcde2011-01-07 11:17:23 -08002244 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 if (itemUnderLongClick == null) {
2246 // User long pressed on empty space
2247 mWorkspace.setAllowLongPress(false);
2248 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2249 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002250 if (LauncherApplication.isScreenXLarge()) {
2251 addItems();
2252 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 }
2255 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002258 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2259 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260 mAddIntersectCellX = longClickCellInfo.cellX;
2261 mAddIntersectCellY = longClickCellInfo.cellY;
2262 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 }
2264 }
2265 }
2266 return true;
2267 }
2268
Romain Guye6b8e2f2009-11-10 11:56:55 -08002269 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002270 private void dismissPreview(final View v) {
2271 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002272 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002273 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2274 public void onDismiss() {
2275 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2276 int count = group.getChildCount();
2277 for (int i = 0; i < count; i++) {
2278 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2279 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002280 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2281 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2282
2283 v.setTag(R.id.workspace, null);
2284 v.setTag(R.id.icon, null);
2285 window.setOnDismissListener(null);
2286 }
2287 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002288 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002289 }
2290 v.setTag(null);
2291 }
2292
Romain Guyf8e6a802009-12-07 17:48:02 -08002293 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002294 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002295 }
2296
Romain Guya6abce82009-11-10 02:54:41 -08002297 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002298 final Resources resources = getResources();
2299 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002300
Romain Guya6abce82009-11-10 02:54:41 -08002301 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002302
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002304
Romain Guyf8e6a802009-12-07 17:48:02 -08002305 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002306 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 int extraW = (int) ((r.left + r.right) * max);
2308 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002309
2310 int aW = cell.getWidth() - extraW;
2311 float w = aW / max;
2312
2313 int width = cell.getWidth();
2314 int height = cell.getHeight();
2315 int x = cell.getLeftPadding();
2316 int y = cell.getTopPadding();
2317 width -= (x + cell.getRightPadding());
2318 height -= (y + cell.getBottomPadding());
2319
2320 float scale = w / width;
2321
2322 int count = end - start;
2323
2324 final float sWidth = width * scale;
2325 float sHeight = height * scale;
2326
Romain Guye6b8e2f2009-11-10 11:56:55 -08002327 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002328
Romain Guye6b8e2f2009-11-10 11:56:55 -08002329 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2330 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002331
2332 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002333 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002334 cell = (CellLayout) workspace.getChildAt(i);
2335
Romain Guyf8e6a802009-12-07 17:48:02 -08002336 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002337 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002338
2339 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002340 c.scale(scale, scale);
2341 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002342 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002343
Romain Guy9d31e9f2009-11-11 18:54:28 -08002344 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002345 image.setImageBitmap(bitmap);
2346 image.setTag(i);
2347 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002348 image.setOnFocusChangeListener(handler);
2349 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002350 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002351
2352 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002353 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2354
Winson Chungaafa03c2010-06-11 17:34:16 -07002355 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002356 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002357
2358 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002359 p.setContentView(preview);
2360 p.setWidth((int) (sWidth * count + extraW));
2361 p.setHeight((int) (sHeight + extraH));
2362 p.setAnimationStyle(R.style.AnimationPreview);
2363 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002364 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002365 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002366 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002367
Romain Guye6b8e2f2009-11-10 11:56:55 -08002368 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2369 public void onDismiss() {
2370 dismissPreview(anchor);
2371 }
2372 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002373
2374 anchor.setTag(p);
2375 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002376 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002377 }
2378
Romain Guy9d31e9f2009-11-11 18:54:28 -08002379 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002380 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002381
Romain Guye6b8e2f2009-11-10 11:56:55 -08002382 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002383 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002384 }
2385
2386 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002387 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002388 v.post(this);
2389 }
2390
2391 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002392 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002393 }
2394
2395 public void onFocusChange(View v, boolean hasFocus) {
2396 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002397 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002398 }
Romain Guya6abce82009-11-10 02:54:41 -08002399 }
2400 }
2401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 Workspace getWorkspace() {
2403 return mWorkspace;
2404 }
2405
Michael Jurka3c38d562011-02-18 19:22:12 -08002406 TabHost getCustomizationDrawer() {
2407 return mHomeCustomizationDrawer;
2408 }
2409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 @Override
2411 protected Dialog onCreateDialog(int id) {
2412 switch (id) {
2413 case DIALOG_CREATE_SHORTCUT:
2414 return new CreateShortcut().createDialog();
2415 case DIALOG_RENAME_FOLDER:
2416 return new RenameFolder().createDialog();
2417 }
2418
2419 return super.onCreateDialog(id);
2420 }
2421
2422 @Override
2423 protected void onPrepareDialog(int id, Dialog dialog) {
2424 switch (id) {
2425 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 break;
2427 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002428 if (mFolderInfo != null) {
2429 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2430 final CharSequence text = mFolderInfo.title;
2431 input.setText(text);
2432 input.setSelection(0, text.length());
2433 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 break;
2435 }
2436 }
2437
2438 void showRenameDialog(FolderInfo info) {
2439 mFolderInfo = info;
2440 mWaitingForResult = true;
2441 showDialog(DIALOG_RENAME_FOLDER);
2442 }
2443
Michael Jurka0280c3b2010-09-17 15:00:07 -07002444 private void showAddDialog(int intersectX, int intersectY) {
2445 resetAddInfo();
2446 mAddIntersectCellX = intersectX;
2447 mAddIntersectCellY = intersectY;
2448 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 mWaitingForResult = true;
2450 showDialog(DIALOG_CREATE_SHORTCUT);
2451 }
2452
Joe Onoratodeb98af2010-02-19 14:59:39 -08002453 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002454 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002455 Bundle bundle = new Bundle();
2456
2457 ArrayList<String> shortcutNames = new ArrayList<String>();
2458 shortcutNames.add(getString(R.string.group_applications));
2459 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2460
2461 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2462 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2463 R.drawable.ic_launcher_application));
2464 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2465
2466 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2467 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002468 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002469 pickIntent.putExtras(bundle);
2470
Joe Onoratodeb98af2010-02-19 14:59:39 -08002471 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 private class RenameFolder {
2475 private EditText mInput;
2476
2477 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2479 mInput = (EditText) layout.findViewById(R.id.folder_name);
2480
2481 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2482 builder.setIcon(0);
2483 builder.setTitle(getString(R.string.rename_folder_title));
2484 builder.setCancelable(true);
2485 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2486 public void onCancel(DialogInterface dialog) {
2487 cleanup();
2488 }
2489 });
2490 builder.setNegativeButton(getString(R.string.cancel_action),
2491 new Dialog.OnClickListener() {
2492 public void onClick(DialogInterface dialog, int which) {
2493 cleanup();
2494 }
2495 }
2496 );
2497 builder.setPositiveButton(getString(R.string.rename_action),
2498 new Dialog.OnClickListener() {
2499 public void onClick(DialogInterface dialog, int which) {
2500 changeFolderName();
2501 }
2502 }
2503 );
2504 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002505
2506 final AlertDialog dialog = builder.create();
2507 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2508 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002509 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002510 mInput.requestFocus();
2511 InputMethodManager inputManager = (InputMethodManager)
2512 getSystemService(Context.INPUT_METHOD_SERVICE);
2513 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002514 }
2515 });
2516
2517 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 }
2519
2520 private void changeFolderName() {
2521 final String name = mInput.getText().toString();
2522 if (!TextUtils.isEmpty(name)) {
2523 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002524 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 mFolderInfo.title = name;
2526 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2527
Joe Onorato9c1289c2009-08-17 11:03:03 -04002528 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002529 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002530 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 } else {
2532 final FolderIcon folderIcon = (FolderIcon)
2533 mWorkspace.getViewForTag(mFolderInfo);
2534 if (folderIcon != null) {
2535 folderIcon.setText(name);
2536 getWorkspace().requestLayout();
2537 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002538 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002539 mWorkspaceLoading = true;
2540 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 }
2542 }
2543 }
2544 cleanup();
2545 }
2546
2547 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 dismissDialog(DIALOG_RENAME_FOLDER);
2549 mWaitingForResult = false;
2550 mFolderInfo = null;
2551 }
2552 }
2553
Daniel Sandler843e8602010-06-07 14:59:01 -04002554 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2555 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002556 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002557 }
2558
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002559 // AllAppsView.Watcher
2560 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002561 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2562 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002563 mWorkspace.setVisibility(View.GONE);
2564 }
2565 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002566
Michael Jurkaafca9532011-02-16 14:50:35 -08002567 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 button.setVisibility(View.VISIBLE);
2569 button.setFocusable(true);
2570 button.setClickable(true);
2571 }
2572
2573 private void hideToolbarButton(View button) {
2574 button.setAlpha(0.0f);
2575 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2576 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002577 }
2578
2579 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 button.setFocusable(false);
2581 button.setClickable(false);
2582 }
2583
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002584 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 *
2587 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2588 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002589 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002592 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 final boolean showing = show;
2594 final boolean hiding = !show;
2595
2596 final int duration = show ?
2597 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2598 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2599
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002600 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002601 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002602 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002603 anim.addUpdateListener(new AnimatorUpdateListener() {
2604 public void onAnimationUpdate(ValueAnimator animation) {
2605 view.setAlpha((Float) animation.getAnimatedValue());
2606 }
2607 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002608 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002609 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002610 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002611 if (showing) showAndEnableToolbarButton(view);
2612 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002614 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002615 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002616 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002618 });
2619 seq.play(anim);
2620 } else {
2621 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002622 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002623 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002624 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002625 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629 }
2630
2631 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002632 * Show/hide the appropriate toolbar buttons for newState.
2633 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 *
2635 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002636 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2637 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002639 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 switch (newState) {
2641 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002642 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2643 mDeleteZone.setOverlappingViews(
2644 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002645 mDeleteZone.setDragAndDropEnabled(true);
2646 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002647 break;
2648 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002649 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002650 mDeleteZone.setDragAndDropEnabled(false);
2651 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002652 break;
2653 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002654 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002655 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 break;
2657 }
2658 }
2659
2660 /**
2661 * Helper method for the cameraZoomIn/cameraZoomOut animations
2662 * @param view The view being animated
2663 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2664 * @param scaleFactor The scale factor used for the zoom
2665 */
2666 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2667 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002668
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002670 // Set pivotY so that at the starting zoom factor, the view is partially
2671 // visible. Modifying initialHeightFactor changes how much of the view is
2672 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002674 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002675 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002677 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002678 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 }
2680 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002681
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002682 /**
2683 * Zoom the camera out from the workspace to reveal 'toView'.
2684 * Assumes that the view to show is anchored at either the very top or very bottom
2685 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002687 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002688 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002689 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002690 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002691
2692 final int duration = toAllApps ?
2693 res.getInteger(R.integer.config_allAppsZoomInTime) :
2694 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002695 final int fadeDuration = toAllApps ?
2696 res.getInteger(R.integer.config_allAppsFadeInTime) :
2697 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002698
2699 final float scale = toAllApps ?
2700 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2701 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2702
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002703 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002704
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 setPivotsForZoom(toView, toState, scale);
2706
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002707 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002708 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002709 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002710 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002711 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712
2713 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002714 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002715 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002716 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2717 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002718 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002719 toView.setFastScaleX(a * scale + b * 1f);
2720 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002721 }
2722 });
Adam Cohen61033d32010-11-15 18:29:44 -08002723
Adam Cohenf16e5712011-01-13 13:31:45 -08002724 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002725 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002726 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002727 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002728 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2729 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002730 // don't need to invalidate because we do so above
2731 toView.setFastAlpha(a * 0f + b * 1f);
2732 }
2733 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002734 alphaAnim.start();
2735 }
2736
Michael Jurkaabded662011-03-04 12:06:57 -08002737 if (toView instanceof LauncherTransitionable) {
2738 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002739 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002740 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002741 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002742 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002743 // Prepare the position
2744 toView.setTranslationX(0.0f);
2745 toView.setTranslationY(0.0f);
2746 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002747 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002748 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002749 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002750 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002751 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002752 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002753 // If we don't set the final scale values here, if this animation is cancelled
2754 // it will have the wrong scale value and subsequent cameraPan animations will
2755 // not fix that
2756 toView.setScaleX(1.0f);
2757 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002758 if (toView instanceof LauncherTransitionable) {
2759 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002760 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002761 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002762 });
2763
Chet Haaseb1254a62010-09-07 13:35:00 -07002764 AnimatorSet toolbarHideAnim = new AnimatorSet();
2765 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002766 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2767
2768 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002769 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002770
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002771 if (mStateAnimation != null) mStateAnimation.cancel();
2772 mStateAnimation = new AnimatorSet();
2773 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2774 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002775
2776 // Show the new toolbar buttons just as the main animation is ending
2777 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002778 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2779 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002780 } else {
2781 toView.setTranslationX(0.0f);
2782 toView.setTranslationY(0.0f);
2783 toView.setScaleX(1.0f);
2784 toView.setScaleY(1.0f);
2785 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002786 if (toView instanceof LauncherTransitionable) {
2787 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2788 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2789 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 hideAndShowToolbarButtons(toState, null, null);
2791 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002792 }
2793
2794 /**
2795 * Zoom the camera back into the workspace, hiding 'fromView'.
2796 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002797 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2798 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002799 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002801 cameraZoomIn(fromState, animated, false);
2802 }
2803
2804 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002805 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002806 final boolean fromAllApps = (fromState == State.ALL_APPS);
2807
2808 int duration = fromAllApps ?
2809 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2810 res.getInteger(R.integer.config_customizeZoomOutTime);
2811
Michael Jurka742574b2011-02-02 23:51:01 -08002812 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002813 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2814 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2815
2816 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002817
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002818 mCustomizePagedView.endChoiceMode();
2819 mAllAppsPagedView.endChoiceMode();
2820
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002821 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002822
Michael Jurkad3ef3062010-11-23 16:23:58 -08002823 if (!springLoaded) {
2824 mWorkspace.unshrink(animated);
2825 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002827 if (mStateAnimation != null) mStateAnimation.cancel();
2828 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002829
Michael Jurka742574b2011-02-02 23:51:01 -08002830 final float oldScaleX = fromView.getScaleX();
2831 final float oldScaleY = fromView.getScaleY();
2832
2833 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2834 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002835 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2836 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002837 ((View)fromView.getParent()).fastInvalidate();
2838 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2839 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2840 }
2841 });
Michael Jurkaabded662011-03-04 12:06:57 -08002842 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002843 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002844 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002845 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2846 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002847 // don't need to invalidate because we do so above
2848 fromView.setFastAlpha(a * 1f + b * 0f);
2849 }
2850 });
Michael Jurkaabded662011-03-04 12:06:57 -08002851 if (fromView instanceof LauncherTransitionable) {
2852 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002853 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002854 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002855 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002856 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002857 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002858 if (fromView instanceof LauncherTransitionable) {
2859 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002860 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 }
2862 });
2863
Chet Haaseb1254a62010-09-07 13:35:00 -07002864 AnimatorSet toolbarHideAnim = new AnimatorSet();
2865 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002866 if (!springLoaded) {
2867 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2868 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869
Adam Cohen61033d32010-11-15 18:29:44 -08002870 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002871
2872 // Show the new toolbar buttons at the very end of the whole animation
2873 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2874 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002875 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2876 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002877 } else {
2878 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002879 if (fromView instanceof LauncherTransitionable) {
2880 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2881 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2882 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002883 if (!springLoaded) {
2884 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2885 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002886 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002887 }
2888
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002889 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002890 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002891 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002892 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002893
Michael Jurka79212d82010-07-30 16:36:20 -07002894 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002895 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002896 } else {
2897 mAllAppsGrid.zoom(1.0f, animated);
2898 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002899
Romain Guyc16fea72010-03-12 17:17:56 -08002900 ((View) mAllAppsGrid).setFocusable(true);
2901 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002902
Joe Onorato7c312c12009-08-13 21:36:53 -07002903 // TODO: fade these two too
2904 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002905
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002906 // Change the state *after* we've called all the transition code
2907 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002908
Winson Chung5fb63472011-02-02 17:03:37 -08002909 // Pause the auto-advance of widgets until we are out of AllApps
2910 mUserPresent = false;
2911 updateRunning();
2912
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002913 // send an accessibility event to announce the context change
2914 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2915 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002916
2917 void showWorkspace(boolean animated) {
2918 showWorkspace(animated, null);
2919 }
2920
2921 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002922 if (layout != null) {
2923 // always animated, but that's ok since we never specify a layout and
2924 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002925 mWorkspace.unshrink(layout);
2926 } else {
2927 mWorkspace.unshrink(animated);
2928 }
2929 if (mState == State.ALL_APPS) {
2930 closeAllApps(animated);
2931 } else if (mState == State.CUSTOMIZE) {
2932 hideCustomizationDrawer(animated);
2933 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002934
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935 // Change the state *after* we've called all the transition code
2936 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002937
Winson Chung5fb63472011-02-02 17:03:37 -08002938 // Resume the auto-advance of widgets
2939 mUserPresent = true;
2940 updateRunning();
2941
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002942 // send an accessibility event to announce the context change
2943 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002944 }
2945
Michael Jurkad3ef3062010-11-23 16:23:58 -08002946 void enterSpringLoadedDragMode(CellLayout layout) {
2947 mWorkspace.enterSpringLoadedDragMode(layout);
2948 if (mState == State.ALL_APPS) {
2949 cameraZoomIn(State.ALL_APPS, true, true);
2950 mState = State.ALL_APPS_SPRING_LOADED;
2951 } else if (mState == State.CUSTOMIZE) {
2952 cameraZoomIn(State.CUSTOMIZE, true, true);
2953 mState = State.CUSTOMIZE_SPRING_LOADED;
2954 }/* else {
2955 // we're already in spring loaded mode; don't do anything
2956 }*/
2957 }
2958
2959 void exitSpringLoadedDragMode() {
2960 if (mState == State.ALL_APPS_SPRING_LOADED) {
2961 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2962 cameraZoomOut(State.ALL_APPS, true);
2963 mState = State.ALL_APPS;
2964 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2965 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2966 cameraZoomOut(State.CUSTOMIZE, true);
2967 mState = State.CUSTOMIZE;
2968 }/* else {
2969 // we're not in spring loaded mode; don't do anything
2970 }*/
2971 }
2972
Joe Onoratob2061212009-11-24 16:13:54 -05002973 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002974 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002975 * - Home from workspace
2976 * - from center screen
2977 * - from other screens
2978 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002979 * - from center screen
2980 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002981 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002982 * - from center screen
2983 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002984 * - Launch app from workspace and quit
2985 * - with back
2986 * - with home
2987 * - Launch app from all apps and quit
2988 * - with back
2989 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002990 * - Go to a screen that's not the default, then all
2991 * apps, and launch and app, and go back
2992 * - with back
2993 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002994 * - On workspace, long press power and go back
2995 * - with back
2996 * - with home
2997 * - On all apps, long press power and go back
2998 * - with back
2999 * - with home
3000 * - On workspace, power off
3001 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003002 * - Launch an app and turn off the screen while in that app
3003 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003004 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003005 * - From all apps
3006 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003007 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3008 * - From all apps
3009 * - From the center workspace
3010 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003011 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003012 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003013 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003014 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003015 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003016 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003017 } else {
3018 mAllAppsGrid.zoom(0.0f, animated);
3019 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003020 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003021 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003022 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003023 }
3024
Joe Onorato7c312c12009-08-13 21:36:53 -07003025 void lockAllApps() {
3026 // TODO
3027 }
3028
3029 void unlockAllApps() {
3030 // TODO
3031 }
3032
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003033 // Show the customization drawer (only exists in x-large configuration)
3034 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003035 if (mState != State.WORKSPACE) {
3036 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003037 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003038
3039 cameraZoomOut(State.CUSTOMIZE, animated);
3040
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003041 // Change the state *after* we've called all the transition code
3042 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003043
3044 // Pause the auto-advance of widgets until we are out of Customization drawer
3045 mUserPresent = false;
3046 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003047 }
3048
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003049 // Hide the customization drawer (only exists in x-large configuration)
3050 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003051 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003052 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003053 }
3054 }
3055
Patrick Dubroy5f445422011-02-18 14:35:21 -08003056 /**
3057 * Add an item from all apps or customize onto the given workspace screen.
3058 * If layout is null, add to the current screen.
3059 */
3060 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003061 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3062 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003063 } else {
3064 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003065 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003066 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003067
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003068 void onWorkspaceClick(CellLayout layout) {
3069 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003070 }
3071
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003072 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003073 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003074 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003075 // Look for the toolbar icon specified in the activity meta-data
3076 Bundle metaData = packageManager.getActivityInfo(
3077 activityName, PackageManager.GET_META_DATA).metaData;
3078 if (metaData != null) {
3079 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3080 if (iconResId != 0) {
3081 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003082 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003083 }
3084 }
3085 } catch (NameNotFoundException e) {
3086 // Do nothing
3087 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003088 return null;
3089 }
3090
3091 // if successful in getting icon, return it; otherwise, set button to use default drawable
3092 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3093 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3094 TextView button = (TextView) findViewById(buttonId);
3095 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3096
3097 // If we were unable to find the icon via the meta-data, use a generic one
3098 if (toolbarIcon == null) {
3099 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3100 return null;
3101 } else {
3102 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3103 return toolbarIcon.getConstantState();
3104 }
3105 }
3106
3107 // if successful in getting icon, return it; otherwise, set button to use default drawable
3108 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3109 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3110 ImageView button = (ImageView) findViewById(buttonId);
3111 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3112
Michael Jurka0423dcf2010-10-05 14:56:18 -07003113 // If we were unable to find the icon via the meta-data, use a generic one
3114 if (toolbarIcon == null) {
3115 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003116 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003117 } else {
3118 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003119 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003120 }
3121 }
3122
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003123 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3124 TextView button = (TextView) findViewById(buttonId);
3125 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3126 }
3127
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003128 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003129 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003130 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003131 }
3132
Michael Jurka0423dcf2010-10-05 14:56:18 -07003133 private void updateGlobalSearchIcon() {
3134 if (LauncherApplication.isScreenXLarge()) {
3135 final SearchManager searchManager =
3136 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3137 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003138 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003139 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003140 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003141 } else {
3142 findViewById(R.id.search_button).setVisibility(View.GONE);
3143 }
3144 }
3145 }
3146
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003147 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003148 updateButtonWithDrawable(R.id.search_button, d);
3149 }
3150
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003151 private void updateVoiceSearchIcon() {
3152 if (LauncherApplication.isScreenXLarge()) {
3153 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3154 ComponentName activityName = intent.resolveActivity(getPackageManager());
3155 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003156 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003157 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3158 } else {
3159 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003160 }
3161 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003162 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003164 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003165 updateButtonWithDrawable(R.id.voice_button, d);
3166 }
3167
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003168 /**
3169 * Sets the app market icon (shown when all apps is visible on x-large screens)
3170 */
3171 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003172 if (LauncherApplication.isScreenXLarge()) {
3173 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3174 // Find the app market activity by resolving an intent.
3175 // (If multiple app markets are installed, it will return the ResolverActivity.)
3176 ComponentName activityName = intent.resolveActivity(getPackageManager());
3177 if (activityName != null) {
3178 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003179 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003180 R.id.market_button, activityName, R.drawable.app_market_generic);
3181 }
3182 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003183 }
3184
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003185 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003186 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003187 }
3188
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003189 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003190 * Displays the shortcut creation dialog and launches, if necessary, the
3191 * appropriate activity.
3192 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003193 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003194 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3195 DialogInterface.OnShowListener {
3196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3203 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003204 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 builder.setInverseBackgroundForced(true);
3207
3208 AlertDialog dialog = builder.create();
3209 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003210 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003211 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 return dialog;
3214 }
3215
3216 public void onCancel(DialogInterface dialog) {
3217 mWaitingForResult = false;
3218 cleanup();
3219 }
3220
Romain Guycbb89e42009-06-08 15:52:54 -07003221 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003222 }
3223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003225 try {
3226 dismissDialog(DIALOG_CREATE_SHORTCUT);
3227 } catch (Exception e) {
3228 // An exception is thrown if the dialog is not visible, which is fine
3229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 }
3231
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003232 /**
3233 * Handle the action clicked in the "Add to home" dialog.
3234 */
3235 public void onClick(DialogInterface dialog, int which) {
3236 Resources res = getResources();
3237 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003238
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003239 switch (which) {
3240 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003241 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003242 break;
3243 }
Romain Guycbb89e42009-06-08 15:52:54 -07003244
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003245 case AddAdapter.ITEM_APPWIDGET: {
3246 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003247
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003248 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3249 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003250 // start the pick activity
3251 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3252 break;
3253 }
Romain Guycbb89e42009-06-08 15:52:54 -07003254
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003255 case AddAdapter.ITEM_LIVE_FOLDER: {
3256 // Insert extra item to handle inserting folder
3257 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003258
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003259 ArrayList<String> shortcutNames = new ArrayList<String>();
3260 shortcutNames.add(res.getString(R.string.group_folder));
3261 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003262
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003263 ArrayList<ShortcutIconResource> shortcutIcons =
3264 new ArrayList<ShortcutIconResource>();
3265 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3266 R.drawable.ic_launcher_folder));
3267 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3268
3269 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3270 pickIntent.putExtra(Intent.EXTRA_INTENT,
3271 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3272 pickIntent.putExtra(Intent.EXTRA_TITLE,
3273 getText(R.string.title_select_live_folder));
3274 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003275
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003276 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3277 break;
3278 }
3279
3280 case AddAdapter.ITEM_WALLPAPER: {
3281 startWallpaper();
3282 break;
3283 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003284 }
3285 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003286
3287 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003288 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 }
3291
3292 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003293 * Receives notifications when applications are added/removed.
3294 */
3295 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3296 @Override
3297 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003298 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003299 String reason = intent.getStringExtra("reason");
3300 if (!"homekey".equals(reason)) {
3301 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003302 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003303 animate = false;
3304 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003305 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003306 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003307 }
3308 }
3309
3310 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003311 * Receives notifications whenever the appwidgets are reset.
3312 */
3313 private class AppWidgetResetObserver extends ContentObserver {
3314 public AppWidgetResetObserver() {
3315 super(new Handler());
3316 }
3317
3318 @Override
3319 public void onChange(boolean selfChange) {
3320 onAppWidgetReset();
3321 }
3322 }
3323
3324 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003325 * If the activity is currently paused, signal that we need to re-run the loader
3326 * in onResume.
3327 *
3328 * This needs to be called from incoming places where resources might have been loaded
3329 * while we are paused. That is becaues the Configuration might be wrong
3330 * when we're not running, and if it comes back to what it was when we
3331 * were paused, we are not restarted.
3332 *
3333 * Implementation of the method from LauncherModel.Callbacks.
3334 *
3335 * @return true if we are currently paused. The caller might be able to
3336 * skip some work in that case since we will come back again.
3337 */
3338 public boolean setLoadOnResume() {
3339 if (mPaused) {
3340 Log.i(TAG, "setLoadOnResume");
3341 mOnResumeNeedsLoad = true;
3342 return true;
3343 } else {
3344 return false;
3345 }
3346 }
3347
3348 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003350 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003351 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003352 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003353 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003354 } else {
3355 return SCREEN_COUNT / 2;
3356 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003358
Winson Chunga12a2502010-12-20 14:41:35 -08003359 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003360 mAllAppsPagedView = view;
3361 }
3362
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 /**
3364 * Refreshes the shortcuts shown on the workspace.
3365 *
3366 * Implementation of the method from LauncherModel.Callbacks.
3367 */
3368 public void startBinding() {
3369 final Workspace workspace = mWorkspace;
3370 int count = workspace.getChildCount();
3371 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003372 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003373 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3374 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003376
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 if (DEBUG_USER_INTERFACE) {
3378 android.widget.Button finishButton = new android.widget.Button(this);
3379 finishButton.setText("Finish");
3380 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3381
3382 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3383 public void onClick(View v) {
3384 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003385 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003387 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003388
3389 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3390 // mDesktopItems -> AppWidgetInfo -> hostView).
3391 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003392 }
3393
3394 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 * Bind the items start-end from the list.
3396 *
3397 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003398 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3400
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003401 setLoadOnResume();
3402
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 final Workspace workspace = mWorkspace;
3404
3405 for (int i=start; i<end; i++) {
3406 final ItemInfo item = shortcuts.get(i);
3407 mDesktopItems.add(item);
3408 switch (item.itemType) {
3409 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3410 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003411 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3413 false);
3414 break;
3415 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3416 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003417 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003418 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3420 false);
3421 break;
3422 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3423 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3424 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003425 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 (LiveFolderInfo) item);
3427 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3428 false);
3429 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003431 }
3432
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003434 }
3435
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 /**
3437 * Implementation of the method from LauncherModel.Callbacks.
3438 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003439 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003440 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003441 sFolders.clear();
3442 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003443 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444
3445 /**
3446 * Add the views for a widget to the workspace.
3447 *
3448 * Implementation of the method from LauncherModel.Callbacks.
3449 */
3450 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003451 setLoadOnResume();
3452
Daniel Sandler843e8602010-06-07 14:59:01 -04003453 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3454 if (DEBUG_WIDGETS) {
3455 Log.d(TAG, "bindAppWidget: " + item);
3456 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003457 final Workspace workspace = mWorkspace;
3458
3459 final int appWidgetId = item.appWidgetId;
3460 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003461 if (DEBUG_WIDGETS) {
3462 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3463 }
3464
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3466
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3468 item.hostView.setTag(item);
3469
3470 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3471 item.cellY, item.spanX, item.spanY, false);
3472
Adam Cohended9f8d2010-11-03 13:25:16 -07003473 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3474
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 workspace.requestLayout();
3476
3477 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003478
3479 if (DEBUG_WIDGETS) {
3480 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3481 + (SystemClock.uptimeMillis()-start) + "ms");
3482 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 }
3484
3485 /**
3486 * Callback saying that there aren't any more items to bind.
3487 *
3488 * Implementation of the method from LauncherModel.Callbacks.
3489 */
3490 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003491 setLoadOnResume();
3492
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 if (mSavedState != null) {
3494 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003495 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 }
3497
3498 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3499 if (userFolders != null) {
3500 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003501 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 if (info != null) {
3503 openFolder(info);
3504 }
3505 }
3506 final Folder openFolder = mWorkspace.getOpenFolder();
3507 if (openFolder != null) {
3508 openFolder.requestFocus();
3509 }
3510 }
3511
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 mSavedState = null;
3513 }
3514
3515 if (mSavedInstanceState != null) {
3516 super.onRestoreInstanceState(mSavedInstanceState);
3517 mSavedInstanceState = null;
3518 }
3519
Winson Chung32bb5e52011-01-31 14:51:56 -08003520 // Workaround a bug that occurs when rotating the device while the customization mode is
3521 // open, we trigger a new layout on all the CellLayout children.
3522 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3523 final int childCount = mWorkspace.getChildCount();
3524 for (int i = 0; i < childCount; ++i) {
3525 mWorkspace.getChildAt(i).requestLayout();
3526 }
3527 }
3528
Joe Onorato9c1289c2009-08-17 11:03:03 -04003529 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003530
3531 // If we received the result of any pending adds while the loader was running (e.g. the
3532 // widget configuration forced an orientation change), process them now.
3533 for (int i = 0; i < sPendingAddList.size(); i++) {
3534 completeAdd(sPendingAddList.get(i));
3535 }
3536 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 }
3538
3539 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003540 * Updates the icons on the launcher that are affected by changes to the package list
3541 * on the device.
3542 */
3543 private void updateIconsAffectedByPackageManagerChanges() {
3544 updateAppMarketIcon();
3545 updateGlobalSearchIcon();
3546 updateVoiceSearchIcon();
3547 }
3548
3549 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 * Add the icons for all apps.
3551 *
3552 * Implementation of the method from LauncherModel.Callbacks.
3553 */
3554 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003555 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003556 if (mCustomizePagedView != null) {
3557 mCustomizePagedView.setApps(apps);
3558 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003559 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 }
3561
3562 /**
3563 * A package was installed.
3564 *
3565 * Implementation of the method from LauncherModel.Callbacks.
3566 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003567 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003568 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003569 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003570 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003571 if (mCustomizePagedView != null) {
3572 mCustomizePagedView.addApps(apps);
3573 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003574 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 }
3576
3577 /**
3578 * A package was updated.
3579 *
3580 * Implementation of the method from LauncherModel.Callbacks.
3581 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003582 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003583 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003585 if (mWorkspace != null) {
3586 mWorkspace.updateShortcuts(apps);
3587 }
3588 if (mAllAppsGrid != null) {
3589 mAllAppsGrid.updateApps(apps);
3590 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003591 if (mCustomizePagedView != null) {
3592 mCustomizePagedView.updateApps(apps);
3593 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003594 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 }
3596
3597 /**
3598 * A package was uninstalled.
3599 *
3600 * Implementation of the method from LauncherModel.Callbacks.
3601 */
Joe Onorato36115782010-06-17 13:28:48 -04003602 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003604 if (permanent) {
3605 mWorkspace.removeItems(apps);
3606 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003607 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003608 if (mCustomizePagedView != null) {
3609 mCustomizePagedView.removeApps(apps);
3610 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003611 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 }
Joe Onoratobe386092009-11-17 17:32:16 -08003613
3614 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003615 * A number of packages were updated.
3616 */
3617 public void bindPackagesUpdated() {
3618 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003619 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003620 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003621 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003622 }
3623
Winson Chung400438b2011-01-16 17:53:48 -08003624 private int mapConfigurationOriActivityInfoOri(int configOri) {
3625 final Display d = getWindowManager().getDefaultDisplay();
3626 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3627 switch (d.getRotation()) {
3628 case Surface.ROTATION_0:
3629 case Surface.ROTATION_180:
3630 // We are currently in the same basic orientation as the natural orientation
3631 naturalOri = configOri;
3632 break;
3633 case Surface.ROTATION_90:
3634 case Surface.ROTATION_270:
3635 // We are currently in the other basic orientation to the natural orientation
3636 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3637 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3638 break;
3639 }
3640
3641 int[] oriMap = {
3642 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3643 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3644 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3645 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3646 };
3647 // Since the map starts at portrait, we need to offset if this device's natural orientation
3648 // is landscape.
3649 int indexOffset = 0;
3650 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3651 indexOffset = 1;
3652 }
3653 return oriMap[(d.getRotation() + indexOffset) % 4];
3654 }
3655 public void lockScreenOrientation() {
3656 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3657 .getConfiguration().orientation));
3658 }
3659 public void unlockScreenOrientation() {
3660 mHandler.postDelayed(new Runnable() {
3661 public void run() {
3662 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3663 }
3664 }, mRestoreScreenOrientationDelay);
3665 }
3666
Winson Chung80baf5a2010-08-09 16:03:15 -07003667 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003668 * Prints out out state for debugging.
3669 */
3670 public void dumpState() {
3671 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003672 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003673 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3674 Log.d(TAG, "mRestoring=" + mRestoring);
3675 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3676 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3677 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003678 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003679 mModel.dumpState();
3680 mAllAppsGrid.dumpState();
3681 Log.d(TAG, "END launcher2 dump state");
3682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003683}
Michael Jurka2763be32011-02-24 11:19:57 -08003684
Michael Jurkaabded662011-03-04 12:06:57 -08003685interface LauncherTransitionable {
3686 void onLauncherTransitionStart(Animator animation);
3687 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003688}