blob: a68bd9b7427bd387f288018c1f322c6bf8e328a0 [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();
Winson Chung337cd9d2011-03-30 10:39:30 -07001256
1257 // Reset AllApps to it's initial state
1258 if (mAllAppsGrid != null) {
1259 mAllAppsGrid.reset();
1260 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001261 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1262 mUserPresent = true;
1263 updateRunning();
1264 }
1265 }
1266 };
1267
1268 @Override
1269 public void onAttachedToWindow() {
1270 super.onAttachedToWindow();
1271
1272 // Listen for broadcasts related to user-presence
1273 final IntentFilter filter = new IntentFilter();
1274 filter.addAction(Intent.ACTION_SCREEN_OFF);
1275 filter.addAction(Intent.ACTION_USER_PRESENT);
1276 registerReceiver(mReceiver, filter);
1277
Adam Cohend113e0c2010-11-11 10:48:05 -08001278 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001279 mVisible = true;
1280 }
1281
1282 @Override
1283 public void onDetachedFromWindow() {
1284 super.onDetachedFromWindow();
1285 mVisible = false;
1286
Adam Cohend113e0c2010-11-11 10:48:05 -08001287 if (mAttached) {
1288 unregisterReceiver(mReceiver);
1289 mAttached = false;
1290 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001291 updateRunning();
1292 }
1293
1294 public void onWindowVisibilityChanged(int visibility) {
1295 mVisible = visibility == View.VISIBLE;
1296 updateRunning();
1297 }
1298
1299 private void sendAdvanceMessage(long delay) {
1300 mHandler.removeMessages(ADVANCE_MSG);
1301 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1302 mHandler.sendMessageDelayed(msg, delay);
1303 mAutoAdvanceSentTime = System.currentTimeMillis();
1304 }
1305
1306 private void updateRunning() {
1307 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1308 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1309 mAutoAdvanceRunning = autoAdvanceRunning;
1310 if (autoAdvanceRunning) {
1311 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1312 sendAdvanceMessage(delay);
1313 } else {
1314 if (!mWidgetsToAdvance.isEmpty()) {
1315 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1316 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1317 }
1318 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001319 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001320 }
1321 }
1322 }
1323
1324 private final Handler mHandler = new Handler() {
1325 @Override
1326 public void handleMessage(Message msg) {
1327 if (msg.what == ADVANCE_MSG) {
1328 int i = 0;
1329 for (View key: mWidgetsToAdvance.keySet()) {
1330 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1331 final int delay = mAdvanceStagger * i;
1332 if (v instanceof Advanceable) {
1333 postDelayed(new Runnable() {
1334 public void run() {
1335 ((Advanceable) v).advance();
1336 }
1337 }, delay);
1338 }
1339 i++;
1340 }
1341 sendAdvanceMessage(mAdvanceInterval);
1342 }
1343 }
1344 };
1345
1346 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1347 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1348 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1349 if (v instanceof Advanceable) {
1350 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001351 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001352 updateRunning();
1353 }
1354 }
1355
1356 void removeWidgetToAutoAdvance(View hostView) {
1357 if (mWidgetsToAdvance.containsKey(hostView)) {
1358 mWidgetsToAdvance.remove(hostView);
1359 updateRunning();
1360 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001361 }
1362
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1364 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 launcherInfo.hostView = null;
1367 }
1368
Adam Cohended9f8d2010-11-03 13:25:16 -07001369 void showOutOfSpaceMessage() {
1370 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1371 }
1372
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001373 public LauncherAppWidgetHost getAppWidgetHost() {
1374 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Romain Guycbb89e42009-06-08 15:52:54 -07001376
Winson Chunga9abd0e2010-10-27 17:18:37 -07001377 public LauncherModel getModel() {
1378 return mModel;
1379 }
1380
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001381 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001382 getWindow().closeAllPanels();
1383
1384 try {
1385 dismissDialog(DIALOG_CREATE_SHORTCUT);
1386 // Unlock the workspace if the dialog was showing
1387 } catch (Exception e) {
1388 // An exception is thrown if the dialog is not visible, which is fine
1389 }
1390
1391 try {
1392 dismissDialog(DIALOG_RENAME_FOLDER);
1393 // Unlock the workspace if the dialog was showing
1394 } catch (Exception e) {
1395 // An exception is thrown if the dialog is not visible, which is fine
1396 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001397
1398 // Whatever we were doing is hereby canceled.
1399 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001400 }
1401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 @Override
1403 protected void onNewIntent(Intent intent) {
1404 super.onNewIntent(intent);
1405
1406 // Close the menu
1407 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001408 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001409 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001410
Joe Onorato14f122b2009-11-19 14:06:36 -08001411 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1412 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001413
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001414 // In all these cases, only animate if we're already on home
1415
Michael Jurka4cb37242010-08-09 21:05:32 -07001416 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001417 mWorkspace.unshrink(alreadyOnHome);
1418 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001419
Patrick Dubroy758a9232011-03-03 19:54:56 -08001420 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001421 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001422 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001423 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001424 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001425
Joe Onorato3a8820b2009-11-10 15:06:42 -08001426 final View v = getWindow().peekDecorView();
1427 if (v != null && v.getWindowToken() != null) {
1428 InputMethodManager imm = (InputMethodManager)getSystemService(
1429 INPUT_METHOD_SERVICE);
1430 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001432
1433 // Reset AllApps to it's initial state
1434 if (mAllAppsGrid != null) {
1435 mAllAppsGrid.reset();
1436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 }
1438 }
1439
1440 @Override
1441 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1442 // Do not call super here
1443 mSavedInstanceState = savedInstanceState;
1444 }
1445
1446 @Override
1447 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001448 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449
1450 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1451 if (folders.size() > 0) {
1452 final int count = folders.size();
1453 long[] ids = new long[count];
1454 for (int i = 0; i < count; i++) {
1455 final FolderInfo info = folders.get(i).getInfo();
1456 ids[i] = info.id;
1457 }
1458 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1459 } else {
1460 super.onSaveInstanceState(outState);
1461 }
1462
Michael Jurka883f55b2010-10-21 15:47:14 -07001463 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464
Michael Jurka0280c3b2010-09-17 15:00:07 -07001465 if (mAddScreen > -1 && mWaitingForResult) {
1466 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1467 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1468 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
1471 if (mFolderInfo != null && mWaitingForResult) {
1472 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1473 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1474 }
Michael Jurka946ad472010-07-09 18:05:18 -07001475
Winson Chung43b119d2010-12-06 16:45:05 -08001476 // Save the current AllApps drawer tab
1477 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1478 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1479 String currentTabTag = tabhost.getCurrentTabTag();
1480 if (currentTabTag != null) {
1481 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001482 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001483 }
1484 }
1485
1486 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001487 if (mHomeCustomizationDrawer != null) {
1488 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1489 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001490 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001491 }
1492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
1495 @Override
1496 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001500 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001502 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001504 mAppWidgetHost = null;
1505
1506 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507
1508 TextKeyListener.getInstance().release();
1509
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001513
1514 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001515
Patrick Dubroyab962b72010-07-26 12:10:10 -07001516 // Some launcher layouts don't have a previous and next view
1517 if (mPreviousView != null) {
1518 dismissPreview(mPreviousView);
1519 }
1520 if (mNextView != null) {
1521 dismissPreview(mNextView);
1522 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001523
1524 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001525
1526 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1527 mWorkspace.removeAllViews();
1528 mWorkspace = null;
1529 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001530
1531 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 }
1533
1534 @Override
1535 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001536 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 super.startActivityForResult(intent, requestCode);
1538 }
1539
1540 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001541 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001543
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001544 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001545
Karl Rosaen138a0412009-04-23 19:00:21 -07001546 if (initialQuery == null) {
1547 // Use any text typed in the launcher as the initial query
1548 initialQuery = getTypedText();
1549 clearTypedText();
1550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 if (appSearchData == null) {
1552 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001553 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Karl Rosaen138a0412009-04-23 19:00:21 -07001556 final SearchManager searchManager =
1557 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001558 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001559 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561
1562 @Override
1563 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001564 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 return false;
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
1568 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001569
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1571 .setIcon(android.R.drawable.ic_menu_add)
1572 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001573 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1574 .setIcon(android.R.drawable.ic_menu_manage)
1575 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001576 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 .setIcon(android.R.drawable.ic_menu_gallery)
1578 .setAlphabeticShortcut('W');
1579 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1580 .setIcon(android.R.drawable.ic_search_category_default)
1581 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1582 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1583 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1584 .setAlphabeticShortcut('N');
1585
1586 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001587 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1588 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589
1590 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1591 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1592 .setIntent(settings);
1593
1594 return true;
1595 }
1596
1597 @Override
1598 public boolean onPrepareOptionsMenu(Menu menu) {
1599 super.onPrepareOptionsMenu(menu);
1600
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001601 // If all apps is animating, don't show the menu, because we don't know
1602 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001603 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001604 return false;
1605 }
1606
1607 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001608 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001609 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1610 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1611
1612 // Disable add if the workspace is full.
1613 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001614 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1615 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
1618 return true;
1619 }
1620
1621 @Override
1622 public boolean onOptionsItemSelected(MenuItem item) {
1623 switch (item.getItemId()) {
1624 case MENU_ADD:
1625 addItems();
1626 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001627 case MENU_MANAGE_APPS:
1628 manageApps();
1629 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 case MENU_WALLPAPER_SETTINGS:
1631 startWallpaper();
1632 return true;
1633 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001634 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 return true;
1636 case MENU_NOTIFICATIONS:
1637 showNotifications();
1638 return true;
1639 }
1640
1641 return super.onOptionsItemSelected(item);
1642 }
Romain Guycbb89e42009-06-08 15:52:54 -07001643
Karl Rosaen138a0412009-04-23 19:00:21 -07001644 /**
1645 * Indicates that we want global search for this activity by setting the globalSearch
1646 * argument for {@link #startSearch} to true.
1647 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001649 @Override
1650 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001651 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001652 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001653 }
1654
Joe Onorato9c1289c2009-08-17 11:03:03 -04001655 public boolean isWorkspaceLocked() {
1656 return mWorkspaceLoading || mWaitingForResult;
1657 }
1658
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001659 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1660 private boolean isPreviewVisible() {
1661 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1662 (mNextView != null && mNextView.getTag() != null);
1663 }
1664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001666 if (LauncherApplication.isScreenXLarge()) {
1667 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001668 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001669 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001670 }
1671 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001672 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001673 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
1676
Michael Jurka0280c3b2010-09-17 15:00:07 -07001677 private void resetAddInfo() {
1678 mAddScreen = -1;
1679 mAddIntersectCellX = -1;
1680 mAddIntersectCellY = -1;
1681 mAddDropPosition = null;
1682 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001683
Winson Chung55cef262010-10-28 14:14:18 -07001684 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001685 resetAddInfo();
1686 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001687 mAddDropPosition = position;
1688
Michael Jurkaaf442092010-06-10 17:01:57 -07001689 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001690 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1691 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001692 }
1693
Winson Chung7ad01412010-09-27 11:33:03 -07001694 private void manageApps() {
1695 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1696 }
1697
Michael Jurkaaf442092010-06-10 17:01:57 -07001698 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001699 // TODO: catch bad widget exception when sent
1700 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001701 // TODO: Is this log message meaningful?
1702 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001703 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001704 }
1705
Winson Chung55cef262010-10-28 14:14:18 -07001706 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001707 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708
Bjorn Bringert7984c942009-12-09 15:38:25 +00001709 if (appWidget.configure != null) {
1710 // Launch over to configure widget, if needed
1711 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1712 intent.setComponent(appWidget.configure);
1713 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001714 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001715 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1716 intent.putExtra(
1717 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1718 info.mimeType);
1719
1720 final String mimeType = info.mimeType;
1721 final ClipData clipData = (ClipData) info.configurationData;
1722 final ClipDescription clipDesc = clipData.getDescription();
1723 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1724 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001725 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001726 final CharSequence stringData = item.getText();
1727 final Uri uriData = item.getUri();
1728 final Intent intentData = item.getIntent();
1729 final String key =
1730 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1731 if (uriData != null) {
1732 intent.putExtra(key, uriData);
1733 } else if (intentData != null) {
1734 intent.putExtra(key, intentData);
1735 } else if (stringData != null) {
1736 intent.putExtra(key, stringData);
1737 }
1738 break;
1739 }
1740 }
1741 }
Winson Chung55cef262010-10-28 14:14:18 -07001742 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001743
Romain Guy8e633c52010-03-04 12:51:36 -08001744 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001746 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001747 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
1749 }
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1752 resetAddInfo();
1753 mAddScreen = screen;
1754 mAddDropPosition = position;
1755
1756 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1757 createShortcutIntent.setComponent(componentName);
1758 processShortcut(createShortcutIntent);
1759 }
1760
Joe Onoratodeb98af2010-02-19 14:59:39 -08001761 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001762 // Handle case where user selected "Applications"
1763 String applicationName = getResources().getString(R.string.group_applications);
1764 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001765
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001766 if (applicationName != null && applicationName.equals(shortcutName)) {
1767 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1768 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001769
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001770 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1771 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001772 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001773 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001774 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001775 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778
Winson Chung24ab2f12010-09-16 14:10:47 -07001779 void processWallpaper(Intent intent) {
1780 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1781 }
1782
Michael Jurka0280c3b2010-09-17 15:00:07 -07001783 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1784 resetAddInfo();
1785 mAddScreen = screen;
1786 mAddDropPosition = position;
1787
1788 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1789 createFolderIntent.setComponent(componentName);
1790
1791 addLiveFolder(createFolderIntent);
1792 }
1793
1794 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001795 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001796 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001797 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001798
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001799 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001801 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001802 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001803 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 UserFolderInfo folderInfo = new UserFolderInfo();
1808 folderInfo.title = getText(R.string.folder_name);
1809
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1811 final int[] cellXY = mTmpAddItemCellCoordinates;
1812 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1813 showOutOfSpaceMessage();
1814 return;
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816
1817 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 LauncherModel.addItemToDatabase(this, folderInfo,
1819 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001821 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822
1823 // Create the view
1824 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001825 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1826 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 }
Romain Guycbb89e42009-06-08 15:52:54 -07001829
Joe Onorato9c1289c2009-08-17 11:03:03 -04001830 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001831 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832 }
1833
Michael Jurka28750fb2010-09-24 17:43:49 -07001834 private void completeAddLiveFolder(
1835 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1837 final int[] cellXY = mTmpAddItemCellCoordinates;
1838 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1839 showOutOfSpaceMessage();
1840 return;
1841 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844
1845 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001847 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001848 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
1850 }
1851
1852 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854
1855 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1856 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1857
1858 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 Intent.ShortcutIconResource iconResource = null;
1860
1861 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1862 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1863 try {
1864 iconResource = (Intent.ShortcutIconResource) extra;
1865 final PackageManager packageManager = context.getPackageManager();
1866 Resources resources = packageManager.getResourcesForApplication(
1867 iconResource.packageName);
1868 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1869 icon = resources.getDrawable(id);
1870 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001871 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873 }
1874
1875 if (icon == null) {
1876 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1877 }
1878
1879 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001880 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 info.title = name;
1882 info.iconResource = iconResource;
1883 info.uri = data.getData();
1884 info.baseIntent = baseIntent;
1885 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1886 LiveFolders.DISPLAY_MODE_GRID);
1887
1888 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001890 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891
1892 return info;
1893 }
1894
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 private void showNotifications() {
1896 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1897 if (statusBar != null) {
1898 statusBar.expand();
1899 }
1900 }
1901
1902 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001903 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001905 Intent chooser = Intent.createChooser(pickWallpaper,
1906 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001907 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1908 // Removed in Eclair MR1
1909// WallpaperManager wm = (WallpaperManager)
1910// getSystemService(Context.WALLPAPER_SERVICE);
1911// WallpaperInfo wi = wm.getWallpaperInfo();
1912// if (wi != null && wi.getSettingsActivity() != null) {
1913// LabeledIntent li = new LabeledIntent(getPackageName(),
1914// R.string.configure_wallpaper, 0);
1915// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1916// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1917// }
Mike Clerona0618e42009-10-22 13:55:21 -07001918 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001921 /**
1922 * Registers various content observers. The current implementation registers
1923 * only a favorites observer to keep track of the favorites applications.
1924 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001925 private void registerContentObservers() {
1926 ContentResolver resolver = getContentResolver();
1927 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1928 true, mWidgetObserver);
1929 }
1930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 @Override
1932 public boolean dispatchKeyEvent(KeyEvent event) {
1933 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1934 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001936 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001937 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001938 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001939 dumpState();
1940 return true;
1941 }
1942 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001943 }
1944 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1945 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001946 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 return true;
1948 }
1949 }
1950
1951 return super.dispatchKeyEvent(event);
1952 }
1953
Joe Onorato88ec0992009-11-19 13:16:06 -08001954 @Override
1955 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001956 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1957 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001958 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001959 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001960 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001961 dismissPreview(mPreviousView);
1962 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001963 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001964 mWorkspace.exitWidgetResizeMode();
1965
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001966 // Back button is a no-op here, but give at least some feedback for the button press
1967 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001968 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001969 }
1970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 private void closeFolder() {
1972 Folder folder = mWorkspace.getOpenFolder();
1973 if (folder != null) {
1974 closeFolder(folder);
1975 }
1976 }
1977
1978 void closeFolder(Folder folder) {
1979 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001980 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001982 CellLayout cl = (CellLayout) parent;
1983 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001984 if (folder instanceof DropTarget) {
1985 // Live folders aren't DropTargets.
1986 mDragController.removeDropTarget((DropTarget)folder);
1987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989 folder.onClose();
1990 }
1991
1992 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001993 * Re-listen when widgets are reset.
1994 */
1995 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001996 if (mAppWidgetHost != null) {
1997 mAppWidgetHost.startListening();
1998 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001999 }
2000
2001 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002002 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2003 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002005 private void unbindDesktopItems() {
2006 for (ItemInfo item: mDesktopItems) {
2007 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002009 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 /**
2013 * Launches the intent referred by the clicked shortcut.
2014 *
2015 * @param v The view representing the clicked shortcut.
2016 */
2017 public void onClick(View v) {
2018 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002019 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002021 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002022 int[] pos = new int[2];
2023 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002024 intent.setSourceBounds(new Rect(pos[0], pos[1],
2025 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002026 boolean success = startActivitySafely(intent, tag);
2027
2028 if (success && v instanceof BubbleTextView) {
2029 mWaitingForResume = (BubbleTextView) v;
2030 mWaitingForResume.setStayPressed(true);
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 } else if (tag instanceof FolderInfo) {
2033 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002034 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002035 if (mState == State.ALL_APPS) {
2036 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002037 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002038 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041 }
2042
Michael Jurka0e260592010-06-30 17:07:39 -07002043 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002044 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002045 // clicking anywhere on the workspace causes the customization drawer to slide down
2046 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002047 return false;
2048 }
2049
Michael Jurkaaf442092010-06-10 17:01:57 -07002050 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002051 * Event handler for the search button
2052 *
2053 * @param v The view that was clicked.
2054 */
2055 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002056 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002057 // Use a custom animation for launching search
2058 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002059 }
2060
2061 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002062 * Event handler for the voice button
2063 *
2064 * @param v The view that was clicked.
2065 */
2066 public void onClickVoiceButton(View v) {
2067 startVoiceSearch();
2068 }
2069
2070 private void startVoiceSearch() {
2071 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2072 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2073 startActivity(intent);
2074 }
2075
2076 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002077 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002078 * enters home screen customization mode.
2079 *
2080 * @param v The view that was clicked.
2081 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002082 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002083 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002084 }
2085
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002086 /**
2087 * Event handler for the "grid" button that appears on the home screen, which
2088 * enters all apps mode.
2089 *
2090 * @param v The view that was clicked.
2091 */
2092 public void onClickAllAppsButton(View v) {
2093 showAllApps(true);
2094 }
2095
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002096 public void onClickAppMarketButton(View v) {
2097 if (mAppMarketIntent != null) {
2098 startActivitySafely(mAppMarketIntent, "app market");
2099 }
2100 }
2101
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002102 void startApplicationDetailsActivity(ComponentName componentName) {
2103 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002104 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2105 Uri.fromParts("package", packageName, null));
2106 startActivity(intent);
2107 }
2108
Patrick Dubroy5539af72010-09-07 15:22:01 -07002109 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2110 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2111 // System applications cannot be installed. For now, show a toast explaining that.
2112 // We may give them the option of disabling apps this way.
2113 int messageId = R.string.uninstall_system_app_text;
2114 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2115 } else {
2116 String packageName = appInfo.componentName.getPackageName();
2117 String className = appInfo.componentName.getClassName();
2118 Intent intent = new Intent(
2119 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2120 startActivity(intent);
2121 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002122 }
2123
Michael Jurkaddd62e92011-02-16 17:49:14 -08002124 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2126 try {
2127 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002128 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 } catch (ActivityNotFoundException e) {
2130 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002131 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 } catch (SecurityException e) {
2133 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002134 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002136 "or use the exported attribute for this activity. "
2137 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002139 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002141
Romain Guy8e633c52010-03-04 12:51:36 -08002142 void startActivityForResultSafely(Intent intent, int requestCode) {
2143 try {
2144 startActivityForResult(intent, requestCode);
2145 } catch (ActivityNotFoundException e) {
2146 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2147 } catch (SecurityException e) {
2148 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2149 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2150 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2151 "or use the exported attribute for this activity.", e);
2152 }
2153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154
2155 private void handleFolderClick(FolderInfo folderInfo) {
2156 if (!folderInfo.opened) {
2157 // Close any open folder
2158 closeFolder();
2159 // Open the requested folder
2160 openFolder(folderInfo);
2161 } else {
2162 // Find the open folder...
2163 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2164 int folderScreen;
2165 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002166 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 // .. and close it
2168 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002169 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 // Close any folder open on the current screen
2171 closeFolder();
2172 // Pull the folder onto this screen
2173 openFolder(folderInfo);
2174 }
2175 }
2176 }
2177 }
2178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002180 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 * is animated relative to the specified View. If the View is null, no animation
2182 * is played.
2183 *
2184 * @param folderInfo The FolderInfo describing the folder to open.
2185 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002186 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 Folder openFolder;
2188
2189 if (folderInfo instanceof UserFolderInfo) {
2190 openFolder = UserFolder.fromXml(this);
2191 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002192 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 } else {
2194 return;
2195 }
2196
Joe Onorato00acb122009-08-04 16:04:30 -04002197 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 openFolder.setLauncher(this);
2199
2200 openFolder.bind(folderInfo);
2201 folderInfo.opened = true;
2202
Winson Chungaafa03c2010-06-11 17:34:16 -07002203 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 openFolder.onOpen();
2206 }
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002209 if (mState != State.WORKSPACE) {
2210 return false;
2211 }
2212
Romain Guy1fbc1c82009-11-09 20:43:08 -08002213 switch (v.getId()) {
2214 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002215 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002216 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2217 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002218 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002219 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002220 return true;
2221 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002222 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002223 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2224 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002225 showPreviews(v);
2226 }
2227 return true;
2228 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002229 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002230 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2231 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2232 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002233 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002234 return true;
2235 }
2236
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 return false;
2239 }
2240
2241 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002242 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 }
2244
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 resetAddInfo();
2246 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 return true;
2250 }
2251
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 final View itemUnderLongClick = longClickCellInfo.cell;
2253
Winson Chung304dcde2011-01-07 11:17:23 -08002254 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 if (itemUnderLongClick == null) {
2256 // User long pressed on empty space
2257 mWorkspace.setAllowLongPress(false);
2258 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2259 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002260 if (LauncherApplication.isScreenXLarge()) {
2261 addItems();
2262 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 }
2265 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002266 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002268 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2269 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 mAddIntersectCellX = longClickCellInfo.cellX;
2271 mAddIntersectCellY = longClickCellInfo.cellY;
2272 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
2274 }
2275 }
2276 return true;
2277 }
2278
Romain Guye6b8e2f2009-11-10 11:56:55 -08002279 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002280 private void dismissPreview(final View v) {
2281 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002282 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002283 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2284 public void onDismiss() {
2285 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2286 int count = group.getChildCount();
2287 for (int i = 0; i < count; i++) {
2288 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2289 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002290 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2291 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2292
2293 v.setTag(R.id.workspace, null);
2294 v.setTag(R.id.icon, null);
2295 window.setOnDismissListener(null);
2296 }
2297 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002298 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002299 }
2300 v.setTag(null);
2301 }
2302
Romain Guyf8e6a802009-12-07 17:48:02 -08002303 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002304 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002305 }
2306
Romain Guya6abce82009-11-10 02:54:41 -08002307 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002308 final Resources resources = getResources();
2309 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002310
Romain Guya6abce82009-11-10 02:54:41 -08002311 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002312
Romain Guy9d31e9f2009-11-11 18:54:28 -08002313 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002314
Romain Guyf8e6a802009-12-07 17:48:02 -08002315 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002317 int extraW = (int) ((r.left + r.right) * max);
2318 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002319
2320 int aW = cell.getWidth() - extraW;
2321 float w = aW / max;
2322
2323 int width = cell.getWidth();
2324 int height = cell.getHeight();
2325 int x = cell.getLeftPadding();
2326 int y = cell.getTopPadding();
2327 width -= (x + cell.getRightPadding());
2328 height -= (y + cell.getBottomPadding());
2329
2330 float scale = w / width;
2331
2332 int count = end - start;
2333
2334 final float sWidth = width * scale;
2335 float sHeight = height * scale;
2336
Romain Guye6b8e2f2009-11-10 11:56:55 -08002337 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002338
Romain Guye6b8e2f2009-11-10 11:56:55 -08002339 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2340 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002341
2342 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002343 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002344 cell = (CellLayout) workspace.getChildAt(i);
2345
Romain Guyf8e6a802009-12-07 17:48:02 -08002346 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002347 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002348
2349 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002350 c.scale(scale, scale);
2351 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002352 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002353
Romain Guy9d31e9f2009-11-11 18:54:28 -08002354 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002355 image.setImageBitmap(bitmap);
2356 image.setTag(i);
2357 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002358 image.setOnFocusChangeListener(handler);
2359 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002360 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002361
2362 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002363 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2364
Winson Chungaafa03c2010-06-11 17:34:16 -07002365 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002366 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002367
2368 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002369 p.setContentView(preview);
2370 p.setWidth((int) (sWidth * count + extraW));
2371 p.setHeight((int) (sHeight + extraH));
2372 p.setAnimationStyle(R.style.AnimationPreview);
2373 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002374 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002375 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002377
Romain Guye6b8e2f2009-11-10 11:56:55 -08002378 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2379 public void onDismiss() {
2380 dismissPreview(anchor);
2381 }
2382 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002383
2384 anchor.setTag(p);
2385 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002386 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002387 }
2388
Romain Guy9d31e9f2009-11-11 18:54:28 -08002389 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002390 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002391
Romain Guye6b8e2f2009-11-10 11:56:55 -08002392 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002393 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002394 }
2395
2396 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002397 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002398 v.post(this);
2399 }
2400
2401 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002402 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002403 }
2404
2405 public void onFocusChange(View v, boolean hasFocus) {
2406 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002407 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002408 }
Romain Guya6abce82009-11-10 02:54:41 -08002409 }
2410 }
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 Workspace getWorkspace() {
2413 return mWorkspace;
2414 }
2415
Michael Jurka3c38d562011-02-18 19:22:12 -08002416 TabHost getCustomizationDrawer() {
2417 return mHomeCustomizationDrawer;
2418 }
2419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 @Override
2421 protected Dialog onCreateDialog(int id) {
2422 switch (id) {
2423 case DIALOG_CREATE_SHORTCUT:
2424 return new CreateShortcut().createDialog();
2425 case DIALOG_RENAME_FOLDER:
2426 return new RenameFolder().createDialog();
2427 }
2428
2429 return super.onCreateDialog(id);
2430 }
2431
2432 @Override
2433 protected void onPrepareDialog(int id, Dialog dialog) {
2434 switch (id) {
2435 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 break;
2437 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002438 if (mFolderInfo != null) {
2439 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2440 final CharSequence text = mFolderInfo.title;
2441 input.setText(text);
2442 input.setSelection(0, text.length());
2443 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 break;
2445 }
2446 }
2447
2448 void showRenameDialog(FolderInfo info) {
2449 mFolderInfo = info;
2450 mWaitingForResult = true;
2451 showDialog(DIALOG_RENAME_FOLDER);
2452 }
2453
Michael Jurka0280c3b2010-09-17 15:00:07 -07002454 private void showAddDialog(int intersectX, int intersectY) {
2455 resetAddInfo();
2456 mAddIntersectCellX = intersectX;
2457 mAddIntersectCellY = intersectY;
2458 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 mWaitingForResult = true;
2460 showDialog(DIALOG_CREATE_SHORTCUT);
2461 }
2462
Joe Onoratodeb98af2010-02-19 14:59:39 -08002463 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002464 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002465 Bundle bundle = new Bundle();
2466
2467 ArrayList<String> shortcutNames = new ArrayList<String>();
2468 shortcutNames.add(getString(R.string.group_applications));
2469 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2470
2471 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2472 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2473 R.drawable.ic_launcher_application));
2474 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2475
2476 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2477 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002478 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002479 pickIntent.putExtras(bundle);
2480
Joe Onoratodeb98af2010-02-19 14:59:39 -08002481 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 private class RenameFolder {
2485 private EditText mInput;
2486
2487 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2489 mInput = (EditText) layout.findViewById(R.id.folder_name);
2490
2491 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2492 builder.setIcon(0);
2493 builder.setTitle(getString(R.string.rename_folder_title));
2494 builder.setCancelable(true);
2495 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2496 public void onCancel(DialogInterface dialog) {
2497 cleanup();
2498 }
2499 });
2500 builder.setNegativeButton(getString(R.string.cancel_action),
2501 new Dialog.OnClickListener() {
2502 public void onClick(DialogInterface dialog, int which) {
2503 cleanup();
2504 }
2505 }
2506 );
2507 builder.setPositiveButton(getString(R.string.rename_action),
2508 new Dialog.OnClickListener() {
2509 public void onClick(DialogInterface dialog, int which) {
2510 changeFolderName();
2511 }
2512 }
2513 );
2514 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002515
2516 final AlertDialog dialog = builder.create();
2517 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2518 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002519 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002520 mInput.requestFocus();
2521 InputMethodManager inputManager = (InputMethodManager)
2522 getSystemService(Context.INPUT_METHOD_SERVICE);
2523 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002524 }
2525 });
2526
2527 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
2529
2530 private void changeFolderName() {
2531 final String name = mInput.getText().toString();
2532 if (!TextUtils.isEmpty(name)) {
2533 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002534 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 mFolderInfo.title = name;
2536 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2537
Joe Onorato9c1289c2009-08-17 11:03:03 -04002538 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002539 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 } else {
2542 final FolderIcon folderIcon = (FolderIcon)
2543 mWorkspace.getViewForTag(mFolderInfo);
2544 if (folderIcon != null) {
2545 folderIcon.setText(name);
2546 getWorkspace().requestLayout();
2547 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002548 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 mWorkspaceLoading = true;
2550 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552 }
2553 }
2554 cleanup();
2555 }
2556
2557 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 dismissDialog(DIALOG_RENAME_FOLDER);
2559 mWaitingForResult = false;
2560 mFolderInfo = null;
2561 }
2562 }
2563
Daniel Sandler843e8602010-06-07 14:59:01 -04002564 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2565 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002567 }
2568
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002569 // AllAppsView.Watcher
2570 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002571 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2572 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002573 mWorkspace.setVisibility(View.GONE);
2574 }
2575 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576
Michael Jurkaafca9532011-02-16 14:50:35 -08002577 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 button.setVisibility(View.VISIBLE);
2579 button.setFocusable(true);
2580 button.setClickable(true);
2581 }
2582
2583 private void hideToolbarButton(View button) {
2584 button.setAlpha(0.0f);
2585 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2586 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002587 }
2588
2589 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 button.setFocusable(false);
2591 button.setClickable(false);
2592 }
2593
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002594 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 *
2597 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2598 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002599 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002600 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002601 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002602 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 final boolean showing = show;
2604 final boolean hiding = !show;
2605
2606 final int duration = show ?
2607 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2608 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2609
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002610 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002611 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002612 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002613 anim.addUpdateListener(new AnimatorUpdateListener() {
2614 public void onAnimationUpdate(ValueAnimator animation) {
2615 view.setAlpha((Float) animation.getAnimatedValue());
2616 }
2617 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002618 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002619 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002620 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002621 if (showing) showAndEnableToolbarButton(view);
2622 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002624 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002625 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 });
2629 seq.play(anim);
2630 } else {
2631 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002632 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002633 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002634 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002635 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002638 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 }
2640
2641 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 * Show/hide the appropriate toolbar buttons for newState.
2643 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002644 *
2645 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002646 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2647 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002648 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002649 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002650 switch (newState) {
2651 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002652 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2653 mDeleteZone.setOverlappingViews(
2654 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002655 mDeleteZone.setDragAndDropEnabled(true);
2656 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 break;
2658 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002659 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002660 mDeleteZone.setDragAndDropEnabled(false);
2661 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 break;
2663 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002664 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002665 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 break;
2667 }
2668 }
2669
2670 /**
2671 * Helper method for the cameraZoomIn/cameraZoomOut animations
2672 * @param view The view being animated
2673 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2674 * @param scaleFactor The scale factor used for the zoom
2675 */
2676 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2677 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002678
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002680 // Set pivotY so that at the starting zoom factor, the view is partially
2681 // visible. Modifying initialHeightFactor changes how much of the view is
2682 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002684 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002685 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002687 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002688 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 }
2690 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002691
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002692 /**
2693 * Zoom the camera out from the workspace to reveal 'toView'.
2694 * Assumes that the view to show is anchored at either the very top or very bottom
2695 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002696 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002697 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002698 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002699 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002701
2702 final int duration = toAllApps ?
2703 res.getInteger(R.integer.config_allAppsZoomInTime) :
2704 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002705 final int fadeDuration = toAllApps ?
2706 res.getInteger(R.integer.config_allAppsFadeInTime) :
2707 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002708
2709 final float scale = toAllApps ?
2710 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2711 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2712
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002713 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002714
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002715 setPivotsForZoom(toView, toState, scale);
2716
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002717 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002718 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002719 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002720 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002721 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722
2723 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002724 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002725 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002726 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2727 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002728 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002729 toView.setFastScaleX(a * scale + b * 1f);
2730 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002731 }
2732 });
Adam Cohen61033d32010-11-15 18:29:44 -08002733
Adam Cohenf16e5712011-01-13 13:31:45 -08002734 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002735 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002736 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002737 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002738 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2739 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002740 // don't need to invalidate because we do so above
2741 toView.setFastAlpha(a * 0f + b * 1f);
2742 }
2743 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002744 alphaAnim.start();
2745 }
2746
Michael Jurkaabded662011-03-04 12:06:57 -08002747 if (toView instanceof LauncherTransitionable) {
2748 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002749 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002750 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002751 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002752 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002753 // Prepare the position
2754 toView.setTranslationX(0.0f);
2755 toView.setTranslationY(0.0f);
2756 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002757 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002758 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002759 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002760 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002761 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002762 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002763 // If we don't set the final scale values here, if this animation is cancelled
2764 // it will have the wrong scale value and subsequent cameraPan animations will
2765 // not fix that
2766 toView.setScaleX(1.0f);
2767 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002768 if (toView instanceof LauncherTransitionable) {
2769 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002770 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002771 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 });
2773
Chet Haaseb1254a62010-09-07 13:35:00 -07002774 AnimatorSet toolbarHideAnim = new AnimatorSet();
2775 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002776 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2777
2778 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002779 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002780
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002781 if (mStateAnimation != null) mStateAnimation.cancel();
2782 mStateAnimation = new AnimatorSet();
2783 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2784 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002785
2786 // Show the new toolbar buttons just as the main animation is ending
2787 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002788 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2789 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790 } else {
2791 toView.setTranslationX(0.0f);
2792 toView.setTranslationY(0.0f);
2793 toView.setScaleX(1.0f);
2794 toView.setScaleY(1.0f);
2795 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002796 if (toView instanceof LauncherTransitionable) {
2797 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2798 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2799 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 hideAndShowToolbarButtons(toState, null, null);
2801 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002802 }
2803
2804 /**
2805 * Zoom the camera back into the workspace, hiding 'fromView'.
2806 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002807 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2808 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002809 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002810 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002811 cameraZoomIn(fromState, animated, false);
2812 }
2813
2814 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002815 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002816 final boolean fromAllApps = (fromState == State.ALL_APPS);
2817
2818 int duration = fromAllApps ?
2819 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2820 res.getInteger(R.integer.config_customizeZoomOutTime);
2821
Michael Jurka742574b2011-02-02 23:51:01 -08002822 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002823 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2824 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2825
2826 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002828 mCustomizePagedView.endChoiceMode();
2829 mAllAppsPagedView.endChoiceMode();
2830
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002831 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002832
Michael Jurkad3ef3062010-11-23 16:23:58 -08002833 if (!springLoaded) {
2834 mWorkspace.unshrink(animated);
2835 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002837 if (mStateAnimation != null) mStateAnimation.cancel();
2838 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002839
Michael Jurka742574b2011-02-02 23:51:01 -08002840 final float oldScaleX = fromView.getScaleX();
2841 final float oldScaleY = fromView.getScaleY();
2842
2843 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2844 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002845 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2846 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002847 ((View)fromView.getParent()).fastInvalidate();
2848 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2849 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2850 }
2851 });
Michael Jurkaabded662011-03-04 12:06:57 -08002852 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002853 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002854 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002855 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2856 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002857 // don't need to invalidate because we do so above
2858 fromView.setFastAlpha(a * 1f + b * 0f);
2859 }
2860 });
Michael Jurkaabded662011-03-04 12:06:57 -08002861 if (fromView instanceof LauncherTransitionable) {
2862 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002863 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002864 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002865 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002866 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002868 if (fromView instanceof LauncherTransitionable) {
2869 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002870 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002871 }
2872 });
2873
Chet Haaseb1254a62010-09-07 13:35:00 -07002874 AnimatorSet toolbarHideAnim = new AnimatorSet();
2875 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002876 if (!springLoaded) {
2877 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2878 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002879
Adam Cohen61033d32010-11-15 18:29:44 -08002880 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002881
2882 // Show the new toolbar buttons at the very end of the whole animation
2883 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2884 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002885 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2886 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002887 } else {
2888 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002889 if (fromView instanceof LauncherTransitionable) {
2890 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2891 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2892 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002893 if (!springLoaded) {
2894 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2895 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002896 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002897 }
2898
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002899 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002900 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002901 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002902 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002903
Michael Jurka79212d82010-07-30 16:36:20 -07002904 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002905 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002906 } else {
2907 mAllAppsGrid.zoom(1.0f, animated);
2908 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002909
Romain Guyc16fea72010-03-12 17:17:56 -08002910 ((View) mAllAppsGrid).setFocusable(true);
2911 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002912
Joe Onorato7c312c12009-08-13 21:36:53 -07002913 // TODO: fade these two too
2914 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002915
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002916 // Change the state *after* we've called all the transition code
2917 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002918
Winson Chung5fb63472011-02-02 17:03:37 -08002919 // Pause the auto-advance of widgets until we are out of AllApps
2920 mUserPresent = false;
2921 updateRunning();
2922
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002923 // send an accessibility event to announce the context change
2924 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2925 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002926
2927 void showWorkspace(boolean animated) {
2928 showWorkspace(animated, null);
2929 }
2930
2931 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002932 if (layout != null) {
2933 // always animated, but that's ok since we never specify a layout and
2934 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935 mWorkspace.unshrink(layout);
2936 } else {
2937 mWorkspace.unshrink(animated);
2938 }
2939 if (mState == State.ALL_APPS) {
2940 closeAllApps(animated);
2941 } else if (mState == State.CUSTOMIZE) {
2942 hideCustomizationDrawer(animated);
2943 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002944
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002945 // Change the state *after* we've called all the transition code
2946 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002947
Winson Chung5fb63472011-02-02 17:03:37 -08002948 // Resume the auto-advance of widgets
2949 mUserPresent = true;
2950 updateRunning();
2951
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002952 // send an accessibility event to announce the context change
2953 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002954 }
2955
Michael Jurkad3ef3062010-11-23 16:23:58 -08002956 void enterSpringLoadedDragMode(CellLayout layout) {
2957 mWorkspace.enterSpringLoadedDragMode(layout);
2958 if (mState == State.ALL_APPS) {
2959 cameraZoomIn(State.ALL_APPS, true, true);
2960 mState = State.ALL_APPS_SPRING_LOADED;
2961 } else if (mState == State.CUSTOMIZE) {
2962 cameraZoomIn(State.CUSTOMIZE, true, true);
2963 mState = State.CUSTOMIZE_SPRING_LOADED;
2964 }/* else {
2965 // we're already in spring loaded mode; don't do anything
2966 }*/
2967 }
2968
2969 void exitSpringLoadedDragMode() {
2970 if (mState == State.ALL_APPS_SPRING_LOADED) {
2971 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2972 cameraZoomOut(State.ALL_APPS, true);
2973 mState = State.ALL_APPS;
2974 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2975 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2976 cameraZoomOut(State.CUSTOMIZE, true);
2977 mState = State.CUSTOMIZE;
2978 }/* else {
2979 // we're not in spring loaded mode; don't do anything
2980 }*/
2981 }
2982
Joe Onoratob2061212009-11-24 16:13:54 -05002983 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002984 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002985 * - Home from workspace
2986 * - from center screen
2987 * - from other screens
2988 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002989 * - from center screen
2990 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002991 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002992 * - from center screen
2993 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002994 * - Launch app from workspace and quit
2995 * - with back
2996 * - with home
2997 * - Launch app from all apps and quit
2998 * - with back
2999 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003000 * - Go to a screen that's not the default, then all
3001 * apps, and launch and app, and go back
3002 * - with back
3003 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003004 * - On workspace, long press power and go back
3005 * - with back
3006 * - with home
3007 * - On all apps, long press power and go back
3008 * - with back
3009 * - with home
3010 * - On workspace, power off
3011 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003012 * - Launch an app and turn off the screen while in that app
3013 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003014 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003015 * - From all apps
3016 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003017 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3018 * - From all apps
3019 * - From the center workspace
3020 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003021 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003022 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003023 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003024 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003025 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003026 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003027 } else {
3028 mAllAppsGrid.zoom(0.0f, animated);
3029 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003030 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003031 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003032 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003033 }
3034
Joe Onorato7c312c12009-08-13 21:36:53 -07003035 void lockAllApps() {
3036 // TODO
3037 }
3038
3039 void unlockAllApps() {
3040 // TODO
3041 }
3042
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003043 // Show the customization drawer (only exists in x-large configuration)
3044 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003045 if (mState != State.WORKSPACE) {
3046 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003047 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003048
3049 cameraZoomOut(State.CUSTOMIZE, animated);
3050
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003051 // Change the state *after* we've called all the transition code
3052 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003053
3054 // Pause the auto-advance of widgets until we are out of Customization drawer
3055 mUserPresent = false;
3056 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003057 }
3058
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003059 // Hide the customization drawer (only exists in x-large configuration)
3060 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003061 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003062 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003063 }
3064 }
3065
Patrick Dubroy5f445422011-02-18 14:35:21 -08003066 /**
3067 * Add an item from all apps or customize onto the given workspace screen.
3068 * If layout is null, add to the current screen.
3069 */
3070 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003071 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3072 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003073 } else {
3074 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003075 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003076 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003077
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003078 void onWorkspaceClick(CellLayout layout) {
3079 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003080 }
3081
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003082 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003083 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003084 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003085 // Look for the toolbar icon specified in the activity meta-data
3086 Bundle metaData = packageManager.getActivityInfo(
3087 activityName, PackageManager.GET_META_DATA).metaData;
3088 if (metaData != null) {
3089 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3090 if (iconResId != 0) {
3091 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003092 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 }
3094 }
3095 } catch (NameNotFoundException e) {
3096 // Do nothing
3097 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003098 return null;
3099 }
3100
3101 // if successful in getting icon, return it; otherwise, set button to use default drawable
3102 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3103 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3104 TextView button = (TextView) findViewById(buttonId);
3105 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3106
3107 // If we were unable to find the icon via the meta-data, use a generic one
3108 if (toolbarIcon == null) {
3109 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3110 return null;
3111 } else {
3112 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3113 return toolbarIcon.getConstantState();
3114 }
3115 }
3116
3117 // if successful in getting icon, return it; otherwise, set button to use default drawable
3118 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3119 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3120 ImageView button = (ImageView) findViewById(buttonId);
3121 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3122
Michael Jurka0423dcf2010-10-05 14:56:18 -07003123 // If we were unable to find the icon via the meta-data, use a generic one
3124 if (toolbarIcon == null) {
3125 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003126 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003127 } else {
3128 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003129 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003130 }
3131 }
3132
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003133 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3134 TextView button = (TextView) findViewById(buttonId);
3135 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3136 }
3137
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003138 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003139 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003140 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 }
3142
Michael Jurka0423dcf2010-10-05 14:56:18 -07003143 private void updateGlobalSearchIcon() {
3144 if (LauncherApplication.isScreenXLarge()) {
3145 final SearchManager searchManager =
3146 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3147 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003148 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003149 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003150 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003151 } else {
3152 findViewById(R.id.search_button).setVisibility(View.GONE);
3153 }
3154 }
3155 }
3156
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003157 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003158 updateButtonWithDrawable(R.id.search_button, d);
3159 }
3160
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003161 private void updateVoiceSearchIcon() {
3162 if (LauncherApplication.isScreenXLarge()) {
3163 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3164 ComponentName activityName = intent.resolveActivity(getPackageManager());
3165 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003166 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003167 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3168 } else {
3169 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003170 }
3171 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003172 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003173
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003174 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003175 updateButtonWithDrawable(R.id.voice_button, d);
3176 }
3177
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003178 /**
3179 * Sets the app market icon (shown when all apps is visible on x-large screens)
3180 */
3181 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003182 if (LauncherApplication.isScreenXLarge()) {
3183 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3184 // Find the app market activity by resolving an intent.
3185 // (If multiple app markets are installed, it will return the ResolverActivity.)
3186 ComponentName activityName = intent.resolveActivity(getPackageManager());
3187 if (activityName != null) {
3188 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003189 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003190 R.id.market_button, activityName, R.drawable.app_market_generic);
3191 }
3192 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003193 }
3194
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003195 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003196 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003197 }
3198
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003199 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 * Displays the shortcut creation dialog and launches, if necessary, the
3201 * appropriate activity.
3202 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003203 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003204 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3205 DialogInterface.OnShowListener {
3206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003208
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3213 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003214 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 builder.setInverseBackgroundForced(true);
3217
3218 AlertDialog dialog = builder.create();
3219 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003220 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003221 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 return dialog;
3224 }
3225
3226 public void onCancel(DialogInterface dialog) {
3227 mWaitingForResult = false;
3228 cleanup();
3229 }
3230
Romain Guycbb89e42009-06-08 15:52:54 -07003231 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003232 }
3233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003234 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003235 try {
3236 dismissDialog(DIALOG_CREATE_SHORTCUT);
3237 } catch (Exception e) {
3238 // An exception is thrown if the dialog is not visible, which is fine
3239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003240 }
3241
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003242 /**
3243 * Handle the action clicked in the "Add to home" dialog.
3244 */
3245 public void onClick(DialogInterface dialog, int which) {
3246 Resources res = getResources();
3247 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003248
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003249 switch (which) {
3250 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003251 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003252 break;
3253 }
Romain Guycbb89e42009-06-08 15:52:54 -07003254
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003255 case AddAdapter.ITEM_APPWIDGET: {
3256 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003257
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003258 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3259 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003260 // start the pick activity
3261 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3262 break;
3263 }
Romain Guycbb89e42009-06-08 15:52:54 -07003264
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003265 case AddAdapter.ITEM_LIVE_FOLDER: {
3266 // Insert extra item to handle inserting folder
3267 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003268
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003269 ArrayList<String> shortcutNames = new ArrayList<String>();
3270 shortcutNames.add(res.getString(R.string.group_folder));
3271 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003272
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003273 ArrayList<ShortcutIconResource> shortcutIcons =
3274 new ArrayList<ShortcutIconResource>();
3275 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3276 R.drawable.ic_launcher_folder));
3277 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3278
3279 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3280 pickIntent.putExtra(Intent.EXTRA_INTENT,
3281 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3282 pickIntent.putExtra(Intent.EXTRA_TITLE,
3283 getText(R.string.title_select_live_folder));
3284 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003285
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003286 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3287 break;
3288 }
3289
3290 case AddAdapter.ITEM_WALLPAPER: {
3291 startWallpaper();
3292 break;
3293 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 }
3295 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003296
3297 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003298 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300 }
3301
3302 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003303 * Receives notifications when applications are added/removed.
3304 */
3305 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3306 @Override
3307 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003308 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003309 String reason = intent.getStringExtra("reason");
3310 if (!"homekey".equals(reason)) {
3311 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003312 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003313 animate = false;
3314 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003315 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003316 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003317 }
3318 }
3319
3320 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003321 * Receives notifications whenever the appwidgets are reset.
3322 */
3323 private class AppWidgetResetObserver extends ContentObserver {
3324 public AppWidgetResetObserver() {
3325 super(new Handler());
3326 }
3327
3328 @Override
3329 public void onChange(boolean selfChange) {
3330 onAppWidgetReset();
3331 }
3332 }
3333
3334 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003335 * If the activity is currently paused, signal that we need to re-run the loader
3336 * in onResume.
3337 *
3338 * This needs to be called from incoming places where resources might have been loaded
3339 * while we are paused. That is becaues the Configuration might be wrong
3340 * when we're not running, and if it comes back to what it was when we
3341 * were paused, we are not restarted.
3342 *
3343 * Implementation of the method from LauncherModel.Callbacks.
3344 *
3345 * @return true if we are currently paused. The caller might be able to
3346 * skip some work in that case since we will come back again.
3347 */
3348 public boolean setLoadOnResume() {
3349 if (mPaused) {
3350 Log.i(TAG, "setLoadOnResume");
3351 mOnResumeNeedsLoad = true;
3352 return true;
3353 } else {
3354 return false;
3355 }
3356 }
3357
3358 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003360 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003362 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003363 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003364 } else {
3365 return SCREEN_COUNT / 2;
3366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003368
Winson Chunga12a2502010-12-20 14:41:35 -08003369 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003370 mAllAppsPagedView = view;
3371 }
3372
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 /**
3374 * Refreshes the shortcuts shown on the workspace.
3375 *
3376 * Implementation of the method from LauncherModel.Callbacks.
3377 */
3378 public void startBinding() {
3379 final Workspace workspace = mWorkspace;
3380 int count = workspace.getChildCount();
3381 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003382 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003383 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3384 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003386
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 if (DEBUG_USER_INTERFACE) {
3388 android.widget.Button finishButton = new android.widget.Button(this);
3389 finishButton.setText("Finish");
3390 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3391
3392 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3393 public void onClick(View v) {
3394 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003397 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003398
3399 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3400 // mDesktopItems -> AppWidgetInfo -> hostView).
3401 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003402 }
3403
3404 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 * Bind the items start-end from the list.
3406 *
3407 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003408 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3410
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003411 setLoadOnResume();
3412
Joe Onorato9c1289c2009-08-17 11:03:03 -04003413 final Workspace workspace = mWorkspace;
3414
3415 for (int i=start; i<end; i++) {
3416 final ItemInfo item = shortcuts.get(i);
3417 mDesktopItems.add(item);
3418 switch (item.itemType) {
3419 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3420 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003421 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3423 false);
3424 break;
3425 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3426 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003427 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003428 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3430 false);
3431 break;
3432 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3433 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3434 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003435 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 (LiveFolderInfo) item);
3437 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3438 false);
3439 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003441 }
3442
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003444 }
3445
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 /**
3447 * Implementation of the method from LauncherModel.Callbacks.
3448 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003449 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003450 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003451 sFolders.clear();
3452 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454
3455 /**
3456 * Add the views for a widget to the workspace.
3457 *
3458 * Implementation of the method from LauncherModel.Callbacks.
3459 */
3460 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003461 setLoadOnResume();
3462
Daniel Sandler843e8602010-06-07 14:59:01 -04003463 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3464 if (DEBUG_WIDGETS) {
3465 Log.d(TAG, "bindAppWidget: " + item);
3466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 final Workspace workspace = mWorkspace;
3468
3469 final int appWidgetId = item.appWidgetId;
3470 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003471 if (DEBUG_WIDGETS) {
3472 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3473 }
3474
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3476
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3478 item.hostView.setTag(item);
3479
3480 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3481 item.cellY, item.spanX, item.spanY, false);
3482
Adam Cohended9f8d2010-11-03 13:25:16 -07003483 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3484
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 workspace.requestLayout();
3486
3487 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003488
3489 if (DEBUG_WIDGETS) {
3490 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3491 + (SystemClock.uptimeMillis()-start) + "ms");
3492 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 }
3494
3495 /**
3496 * Callback saying that there aren't any more items to bind.
3497 *
3498 * Implementation of the method from LauncherModel.Callbacks.
3499 */
3500 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003501 setLoadOnResume();
3502
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 if (mSavedState != null) {
3504 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003505 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 }
3507
3508 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3509 if (userFolders != null) {
3510 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003511 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 if (info != null) {
3513 openFolder(info);
3514 }
3515 }
3516 final Folder openFolder = mWorkspace.getOpenFolder();
3517 if (openFolder != null) {
3518 openFolder.requestFocus();
3519 }
3520 }
3521
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 mSavedState = null;
3523 }
3524
3525 if (mSavedInstanceState != null) {
3526 super.onRestoreInstanceState(mSavedInstanceState);
3527 mSavedInstanceState = null;
3528 }
3529
Winson Chung32bb5e52011-01-31 14:51:56 -08003530 // Workaround a bug that occurs when rotating the device while the customization mode is
3531 // open, we trigger a new layout on all the CellLayout children.
3532 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3533 final int childCount = mWorkspace.getChildCount();
3534 for (int i = 0; i < childCount; ++i) {
3535 mWorkspace.getChildAt(i).requestLayout();
3536 }
3537 }
3538
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003540
3541 // If we received the result of any pending adds while the loader was running (e.g. the
3542 // widget configuration forced an orientation change), process them now.
3543 for (int i = 0; i < sPendingAddList.size(); i++) {
3544 completeAdd(sPendingAddList.get(i));
3545 }
3546 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 }
3548
3549 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003550 * Updates the icons on the launcher that are affected by changes to the package list
3551 * on the device.
3552 */
3553 private void updateIconsAffectedByPackageManagerChanges() {
3554 updateAppMarketIcon();
3555 updateGlobalSearchIcon();
3556 updateVoiceSearchIcon();
3557 }
3558
3559 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 * Add the icons for all apps.
3561 *
3562 * Implementation of the method from LauncherModel.Callbacks.
3563 */
3564 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003565 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003566 if (mCustomizePagedView != null) {
3567 mCustomizePagedView.setApps(apps);
3568 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003569 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 }
3571
3572 /**
3573 * A package was installed.
3574 *
3575 * Implementation of the method from LauncherModel.Callbacks.
3576 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003577 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003578 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003580 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003581 if (mCustomizePagedView != null) {
3582 mCustomizePagedView.addApps(apps);
3583 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003584 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585 }
3586
3587 /**
3588 * A package was updated.
3589 *
3590 * Implementation of the method from LauncherModel.Callbacks.
3591 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003592 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003593 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003595 if (mWorkspace != null) {
3596 mWorkspace.updateShortcuts(apps);
3597 }
3598 if (mAllAppsGrid != null) {
3599 mAllAppsGrid.updateApps(apps);
3600 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003601 if (mCustomizePagedView != null) {
3602 mCustomizePagedView.updateApps(apps);
3603 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003604 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 }
3606
3607 /**
3608 * A package was uninstalled.
3609 *
3610 * Implementation of the method from LauncherModel.Callbacks.
3611 */
Joe Onorato36115782010-06-17 13:28:48 -04003612 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003614 if (permanent) {
3615 mWorkspace.removeItems(apps);
3616 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003617 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003618 if (mCustomizePagedView != null) {
3619 mCustomizePagedView.removeApps(apps);
3620 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003621 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 }
Joe Onoratobe386092009-11-17 17:32:16 -08003623
3624 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003625 * A number of packages were updated.
3626 */
3627 public void bindPackagesUpdated() {
3628 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003629 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003630 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003631 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003632 }
3633
Winson Chung400438b2011-01-16 17:53:48 -08003634 private int mapConfigurationOriActivityInfoOri(int configOri) {
3635 final Display d = getWindowManager().getDefaultDisplay();
3636 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3637 switch (d.getRotation()) {
3638 case Surface.ROTATION_0:
3639 case Surface.ROTATION_180:
3640 // We are currently in the same basic orientation as the natural orientation
3641 naturalOri = configOri;
3642 break;
3643 case Surface.ROTATION_90:
3644 case Surface.ROTATION_270:
3645 // We are currently in the other basic orientation to the natural orientation
3646 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3647 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3648 break;
3649 }
3650
3651 int[] oriMap = {
3652 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3653 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3654 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3655 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3656 };
3657 // Since the map starts at portrait, we need to offset if this device's natural orientation
3658 // is landscape.
3659 int indexOffset = 0;
3660 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3661 indexOffset = 1;
3662 }
3663 return oriMap[(d.getRotation() + indexOffset) % 4];
3664 }
3665 public void lockScreenOrientation() {
3666 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3667 .getConfiguration().orientation));
3668 }
3669 public void unlockScreenOrientation() {
3670 mHandler.postDelayed(new Runnable() {
3671 public void run() {
3672 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3673 }
3674 }, mRestoreScreenOrientationDelay);
3675 }
3676
Winson Chung80baf5a2010-08-09 16:03:15 -07003677 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003678 * Prints out out state for debugging.
3679 */
3680 public void dumpState() {
3681 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003682 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003683 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3684 Log.d(TAG, "mRestoring=" + mRestoring);
3685 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3686 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3687 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003688 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003689 mModel.dumpState();
3690 mAllAppsGrid.dumpState();
3691 Log.d(TAG, "END launcher2 dump state");
3692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003693}
Michael Jurka2763be32011-02-24 11:19:57 -08003694
Michael Jurkaabded662011-03-04 12:06:57 -08003695interface LauncherTransitionable {
3696 void onLauncherTransitionStart(Animator animation);
3697 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003698}